MySQL 9.0.0
Source Code Documentation
ut0mutex.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2012, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/ut0mutex.h
29 Policy based mutexes.
30
31 Created 2012-03-24 Sunny Bains.
32 ***********************************************************************/
33
34#ifndef ut0mutex_h
35#define ut0mutex_h
36
37#include "my_inttypes.h"
38
39extern ulong srv_spin_wait_delay;
40extern ulong srv_n_spin_wait_rounds;
41extern ulong srv_force_recovery_crash;
42
43#ifdef UNIV_LIBRARY
44/* Mutexes are disabled under UNIV_LIBRARY */
45#define mutex_create(I, M) (void)M
46#define mutex_enter(M) (void)M
47#define mutex_enter_nospin(M) (void)M
48#define mutex_enter_nowait(M) (void)M
49#define mutex_exit(M) (void)M
50#define mutex_free(M) (void)M
51
52#ifdef UNIV_DEBUG
53#define mutex_validate(M) (M)
54/* Since mutexes are disabled under UNIV_LIBRARY, the following is OK. */
55#define mutex_own(m) ((m) != nullptr)
56#endif /* UNIV_DEBUG */
57typedef OSMutex SysMutex;
58typedef OSMutex ib_mutex_t;
59typedef OSMutex ib_bpmutex_t;
60
61#else /* UNIV_LIBRARY */
62
63#include <set>
64#include "ib0mutex.h"
65#include "os0atomic.h"
66#include "sync0policy.h"
67
68/** Create a typedef using the MutexType<PolicyType>
69@param[in] M Mutex type
70@param[in] P Policy type
71@param[in] T The resulting typedef alias */
72#define UT_MUTEX_TYPE(M, P, T) typedef PolicyMutex<M<P>> T;
73
75
76#ifdef HAVE_IB_LINUX_FUTEX
77UT_MUTEX_TYPE(TTASFutexMutex, GenericPolicy, FutexMutex)
78UT_MUTEX_TYPE(TTASFutexMutex, BlockMutexPolicy, BlockFutexMutex)
79#endif /* HAVE_IB_LINUX_FUTEX */
80
83
86
87#ifndef UNIV_HOTBACKUP
88#ifdef MUTEX_FUTEX
89/** The default mutex type. */
90typedef FutexMutex ib_mutex_t;
91typedef BlockFutexMutex ib_bpmutex_t;
92#define MUTEX_TYPE "Uses futexes"
93#elif defined(MUTEX_SYS)
94typedef SysMutex ib_mutex_t;
95typedef BlockSysMutex ib_bpmutex_t;
96#define MUTEX_TYPE "Uses system mutexes"
97#elif defined(MUTEX_EVENT)
98typedef SyncArrayMutex ib_mutex_t;
99typedef BlockSyncArrayMutex ib_bpmutex_t;
100#define MUTEX_TYPE "Uses event mutexes"
101#else
102#error "ib_mutex_t type is unknown"
103#endif /* MUTEX_FUTEX */
104
105#include "ut0mutex.ic"
106
109
110#define mutex_create(I, M) mutex_init((M), (I), __FILE__, __LINE__)
111
112template <typename Mutex>
113void mutex_enter_inline(Mutex *m, ut::Location loc) {
115}
116
117#define mutex_enter(M) mutex_enter_inline(M, UT_LOCATION_HERE)
118
119#define mutex_enter_nospin(M) (M)->enter(0, 0, __FILE__, __LINE__)
120
121#define mutex_enter_nowait(M) (M)->trylock(__FILE__, __LINE__)
122
123#define mutex_exit(M) (M)->exit()
124
125#define mutex_free(M) mutex_destroy(M)
126
127/* RAII guard for ib mutex */
129 /** Constructor to acquire mutex
130 @param[in] in_mutex input mutex
131 @param[in] location defines source file and line in code where the
132 constructor of IB_mutex_guard is called */
133 IB_mutex_guard(ib_mutex_t *in_mutex, const ut::Location &location)
134 : m_mutex(in_mutex) {
135 mutex_enter_inline(in_mutex, location);
136 }
137
138 /** Destructor to release mutex */
140
141 /** Disable copy construction */
143
144 /** Disable assignment */
146
147 private:
148 /** Current mutex for RAII */
149 ib_mutex_t *m_mutex;
150
151 void clear() {
153 m_mutex = nullptr;
154 }
155};
156
157#ifdef UNIV_DEBUG
158/**
159Checks that the mutex has been initialized. */
160#define mutex_validate(M) (M)->validate()
161
162/**
163Checks that the current thread owns the mutex. Works only
164in the debug version. */
165#define mutex_own(M) (M)->is_owned()
166#else
167#define mutex_own(M) /* No op */
168#define mutex_validate(M) /* No op */
169#endif /* UNIV_DEBUG */
170#else /* !UNIV_HOTBACKUP */
171#include "../meb/mutex.h"
172typedef meb::Mutex ib_mutex_t;
173typedef meb::Mutex ib_bpmutex_t;
174#endif /* !UNIV_HOTBACKUP */
175
176/** Iterate over the mutex meta data */
178 public:
179 /** Constructor */
180 MutexMonitor() = default;
181
182 /** Destructor */
183 ~MutexMonitor() = default;
184
185 /** Enable the mutex monitoring */
186 void enable();
187
188 /** Disable the mutex monitoring */
189 void disable();
190
191 /** Reset the mutex monitoring values */
192 void reset();
193
194 /** Invoke the callback for each active mutex collection
195 @param[in,out] callback Functor to call
196 @return false if callback returned false */
197 template <typename Callback>
198 bool iterate(Callback &callback) const UNIV_NOTHROW {
199 LatchMetaData::iterator end = latch_meta.end();
200
201 for (LatchMetaData::iterator it = latch_meta.begin(); it != end; ++it) {
202 /* Some of the slots will be null in non-debug mode */
203
204 if (*it == nullptr) {
205 continue;
206 }
207
209
210 bool ret = callback(*latch_meta);
211
212 if (!ret) {
213 return (ret);
214 }
215 }
216
217 return (true);
218 }
219};
220
221/** Defined in sync0sync.cc */
223
224#ifndef UNIV_HOTBACKUP
225/**
226Creates, or rather, initializes a mutex object in a specified memory
227location (which must be appropriately aligned). The mutex is initialized
228in the reset state. Explicit freeing of the mutex with mutex_free is
229necessary only if the memory block containing it is freed.
230Add the mutex instance to the global mutex list.
231@param[in,out] mutex mutex to initialise
232@param[in] id The mutex ID (Latch ID)
233@param[in] file_name Filename from where it was called
234@param[in] line Line number in filename from where called */
235template <typename Mutex>
236void mutex_init(Mutex *mutex, latch_id_t id, const char *file_name,
237 uint32_t line) {
238 new (mutex) Mutex();
239
240 mutex->init(id, file_name, line);
241}
242
243/**
244Removes a mutex instance from the mutex list. The mutex is checked to
245be in the reset state.
246@param[in,out] mutex mutex instance to destroy */
247template <typename Mutex>
248void mutex_destroy(Mutex *mutex) {
249 mutex->destroy();
250}
251
252class IB_mutex : public ib_mutex_t {
253 public:
254 explicit IB_mutex(latch_id_t latch_id) {
255 mutex_create(latch_id, static_cast<ib_mutex_t *>(this));
256 }
257 ~IB_mutex() { mutex_free(static_cast<ib_mutex_t *>(this)); }
258 void lock(const ut::Location &loc) {
259 mutex_enter_inline(static_cast<ib_mutex_t *>(this), loc);
260 }
261 void unlock() { exit(); }
262};
263
264#endif /* !UNIV_HOTBACKUP */
265#endif /* UNIV_LIBRARY */
266
267#endif /* ut0mutex_h */
Track aggregate metrics policy, used by the page mutex.
Definition: sync0policy.h:336
Class that stores callback function reference as well as the result of the callback function call (in...
Definition: keyring_service.cc:43
Definition: ut0mutex.h:252
~IB_mutex()
Definition: ut0mutex.h:257
void unlock()
Definition: ut0mutex.h:261
IB_mutex(latch_id_t latch_id)
Definition: ut0mutex.h:254
void lock(const ut::Location &loc)
Definition: ut0mutex.h:258
Latch meta data.
Definition: sync0types.h:773
Iterate over the mutex meta data.
Definition: ut0mutex.h:177
MutexMonitor()=default
Constructor.
~MutexMonitor()=default
Destructor.
void reset()
Reset the mutex monitoring values.
Definition: sync0sync.cc:273
void disable()
Disable the mutex monitoring.
Definition: sync0sync.cc:259
void enable()
Enable the mutex monitoring.
Definition: sync0sync.cc:245
bool iterate(Callback &callback) const 1
Invoke the callback for each active mutex collection.
Definition: ut0mutex.h:198
Policy based mutexes.
#define exit(A)
Definition: lexyy.cc:917
Some integer typedefs for easier portability.
std::string file_name(Log_file_id file_id)
Provides name of the log file with the given file id, e.g.
Definition: log0pre_8_0_30.cc:94
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
Macros for using atomics.
Collect the metrics per mutex instance, no aggregation.
Definition: sync0policy.h:219
Definition: ut0mutex.h:128
void clear()
Definition: ut0mutex.h:151
~IB_mutex_guard()
Destructor to release mutex.
Definition: ut0mutex.h:139
IB_mutex_guard & operator=(IB_mutex_guard const &)=delete
Disable assignment.
ib_mutex_t * m_mutex
Current mutex for RAII.
Definition: ut0mutex.h:149
IB_mutex_guard(IB_mutex_guard const &)=delete
Disable copy construction.
IB_mutex_guard(ib_mutex_t *in_mutex, const ut::Location &location)
Constructor to acquire mutex.
Definition: ut0mutex.h:133
OS mutex, without any policy.
Definition: sync0types.h:473
OS mutex for tracking lock/unlock for debugging.
Definition: ib0mutex.h:47
Definition: ib0mutex.h:375
Definition: ut0core.h:36
const char * filename
Definition: ut0core.h:37
size_t line
Definition: ut0core.h:38
Policies for mutexes.
LatchMetaData latch_meta
Note: This is accessed without any mutex protection.
Definition: sync0debug.cc:1193
latch_id_t
Each latch has an ID.
Definition: sync0types.h:343
#define UNIV_NOTHROW
Definition: univ.i:456
ulong srv_force_recovery_crash
Inject a crash at different steps of the recovery process.
Definition: srv0srv.cc:541
ulong srv_spin_wait_delay
Definition: ut0mutex.h:107
MutexMonitor * mutex_monitor
Defined in sync0sync.cc.
Definition: sync0sync.cc:185
OSMutex EventMutex
Definition: ut0mutex.h:74
ulong srv_n_spin_wait_rounds
Definition: ut0mutex.h:108
void mutex_destroy(Mutex *mutex)
Removes a mutex instance from the mutex list.
Definition: ut0mutex.h:248
#define UT_MUTEX_TYPE(M, P, T)
Create a typedef using the MutexType<PolicyType>
Definition: ut0mutex.h:72
#define mutex_exit(M)
Definition: ut0mutex.h:123
#define mutex_free(M)
Definition: ut0mutex.h:125
#define mutex_create(I, M)
Definition: ut0mutex.h:110
void mutex_enter_inline(Mutex *m, ut::Location loc)
Definition: ut0mutex.h:113
void mutex_init(Mutex *mutex, latch_id_t id, const char *file_name, uint32_t line)
Creates, or rather, initializes a mutex object in a specified memory location (which must be appropri...
Definition: ut0mutex.h:236
Mutex implementation include file.