MySQL 26.7.0
Source Code Documentation
ut0mutex.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2012, 2026, 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;
41
42#ifdef UNIV_LIBRARY
43/* Mutexes are disabled under UNIV_LIBRARY */
44#define mutex_create(I, M) (void)M
45#define mutex_enter(M) (void)M
46#define mutex_enter_nospin(M) (void)M
47#define mutex_enter_nowait(M) (void)M
48#define mutex_exit(M) (void)M
49#define mutex_free(M) (void)M
50
51#ifdef UNIV_DEBUG
52#define mutex_validate(M) (M)
53/* Since mutexes are disabled under UNIV_LIBRARY, the following is OK. */
54#define mutex_own(m) ((m) != nullptr)
55#endif /* UNIV_DEBUG */
56typedef OSMutex SysMutex;
57typedef OSMutex ib_mutex_t;
58typedef OSMutex ib_bpmutex_t;
59
60#else /* UNIV_LIBRARY */
61
62#include <set>
63#include "ib0mutex.h"
64#include "os0atomic.h"
65#include "sync0policy.h"
66
67/** Create a typedef using the MutexType<PolicyType>
68@param[in] M Mutex type
69@param[in] P Policy type
70@param[in] T The resulting typedef alias */
71#define UT_MUTEX_TYPE(M, P, T) typedef PolicyMutex<M<P>> T;
72
74
75#ifdef HAVE_IB_LINUX_FUTEX
76UT_MUTEX_TYPE(TTASFutexMutex, GenericPolicy, FutexMutex)
77UT_MUTEX_TYPE(TTASFutexMutex, BlockMutexPolicy, BlockFutexMutex)
78#endif /* HAVE_IB_LINUX_FUTEX */
79
82
85
86#ifndef UNIV_HOTBACKUP
87#ifdef MUTEX_FUTEX
88/** The default mutex type. */
89typedef FutexMutex ib_mutex_t;
90typedef BlockFutexMutex ib_bpmutex_t;
91#define MUTEX_TYPE "Uses futexes"
92#elif defined(MUTEX_SYS)
93typedef SysMutex ib_mutex_t;
94typedef BlockSysMutex ib_bpmutex_t;
95#define MUTEX_TYPE "Uses system mutexes"
96#elif defined(MUTEX_EVENT)
97typedef SyncArrayMutex ib_mutex_t;
98typedef BlockSyncArrayMutex ib_bpmutex_t;
99#define MUTEX_TYPE "Uses event mutexes"
100#else
101#error "ib_mutex_t type is unknown"
102#endif /* MUTEX_FUTEX */
103
104#include "ut0mutex.ic"
105
108
109#define mutex_create(I, M) mutex_init((M), (I), __FILE__, __LINE__)
110
111template <typename Mutex>
114}
115
116#define mutex_enter(M) mutex_enter_inline(M, UT_LOCATION_HERE)
117
118#define mutex_enter_nospin(M) (M)->enter(0, 0, __FILE__, __LINE__)
119
120#define mutex_enter_nowait(M) (M)->trylock(__FILE__, __LINE__)
121
122#define mutex_exit(M) (M)->exit()
123
124#define mutex_free(M) mutex_destroy(M)
125
126/* RAII guard for ib mutex */
128 /** Constructor to acquire mutex
129 @param[in] in_mutex input mutex
130 @param[in] location defines source file and line in code where the
131 constructor of IB_mutex_guard is called */
132 IB_mutex_guard(ib_mutex_t *in_mutex, const ut::Location &location)
133 : m_mutex(in_mutex) {
134 ut_ad(!in_mutex->is_owned());
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() {
152 ut_ad(m_mutex->is_owned());
154 m_mutex = nullptr;
155 }
156};
157
158#ifdef UNIV_DEBUG
159/**
160Checks that the mutex has been initialized. */
161#define mutex_validate(M) (M)->validate()
162
163/**
164Checks that the current thread owns the mutex. Works only
165in the debug version. */
166#define mutex_own(M) (M)->is_owned()
167#else
168#define mutex_own(M) /* No op */
169#define mutex_validate(M) /* No op */
170#endif /* UNIV_DEBUG */
171#else /* !UNIV_HOTBACKUP */
172#include "../meb/mutex.h"
173typedef meb::Mutex ib_mutex_t;
174typedef meb::Mutex ib_bpmutex_t;
175#endif /* !UNIV_HOTBACKUP */
176
177/** Iterate over the mutex meta data */
179 public:
180 /** Constructor */
181 MutexMonitor() = default;
182
183 /** Destructor */
184 ~MutexMonitor() = default;
185
186 /** Enable the mutex monitoring */
187 void enable();
188
189 /** Disable the mutex monitoring */
190 void disable();
191
192 /** Reset the mutex monitoring values */
193 void reset();
194
195 /** Invoke the callback for each active mutex collection
196 @param[in,out] callback Functor to call
197 @return false if callback returned false */
198 template <typename Callback>
199 bool iterate(Callback &callback) const UNIV_NOTHROW {
200 LatchMetaData::iterator end = latch_meta.end();
201
202 for (LatchMetaData::iterator it = latch_meta.begin(); it != end; ++it) {
203 /* Some of the slots will be null in non-debug mode */
204
205 if (*it == nullptr) {
206 continue;
207 }
208
210
211 bool ret = callback(*latch_meta);
212
213 if (!ret) {
214 return (ret);
215 }
216 }
217
218 return (true);
219 }
220};
221
222/** Defined in sync0sync.cc */
224
225#ifndef UNIV_HOTBACKUP
226/**
227Creates, or rather, initializes a mutex object in a specified memory
228location (which must be appropriately aligned). The mutex is initialized
229in the reset state. Explicit freeing of the mutex with mutex_free is
230necessary only if the memory block containing it is freed.
231Add the mutex instance to the global mutex list.
232@param[in,out] mutex mutex to initialise
233@param[in] id The mutex ID (Latch ID)
234@param[in] file_name Filename from where it was called
235@param[in] line Line number in filename from where called */
236template <typename Mutex>
237void mutex_init(Mutex *mutex, latch_id_t id, const char *file_name,
238 uint32_t line) {
239 new (mutex) Mutex();
240
241 mutex->init(id, file_name, line);
242}
243
244/**
245Removes a mutex instance from the mutex list. The mutex is checked to
246be in the reset state.
247@param[in,out] mutex mutex instance to destroy */
248template <typename Mutex>
249void mutex_destroy(Mutex *mutex) {
250 mutex->destroy();
251}
252
253class IB_mutex : public ib_mutex_t {
254 public:
255 explicit IB_mutex(latch_id_t latch_id) {
256 mutex_create(latch_id, static_cast<ib_mutex_t *>(this));
257 }
258 ~IB_mutex() { mutex_free(static_cast<ib_mutex_t *>(this)); }
259 void lock(const ut::Location &loc) {
260 mutex_enter_inline(static_cast<ib_mutex_t *>(this), loc);
261 }
262 void unlock() { exit(); }
263};
264
265#endif /* !UNIV_HOTBACKUP */
266#endif /* UNIV_LIBRARY */
267
268#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:44
Definition: ut0mutex.h:253
~IB_mutex()
Definition: ut0mutex.h:258
void unlock()
Definition: ut0mutex.h:262
IB_mutex(latch_id_t latch_id)
Definition: ut0mutex.h:255
void lock(const ut::Location &loc)
Definition: ut0mutex.h:259
Latch meta data.
Definition: sync0types.h:773
Iterate over the mutex meta data.
Definition: ut0mutex.h:178
MutexMonitor()=default
Constructor.
~MutexMonitor()=default
Destructor.
void reset()
Reset the mutex monitoring values.
Definition: sync0sync.cc:274
void disable()
Disable the mutex monitoring.
Definition: sync0sync.cc:260
void enable()
Enable the mutex monitoring.
Definition: sync0sync.cc:246
bool iterate(Callback &callback) const 1
Invoke the callback for each active mutex collection.
Definition: ut0mutex.h:199
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:45
Mutex_wrapper Mutex
Definition: mutex_srv.h:40
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:127
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:132
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:1195
latch_id_t
Each latch has an ID.
Definition: sync0types.h:342
#define UNIV_NOTHROW
Definition: univ.i:459
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:109
ulong srv_spin_wait_delay
Definition: ut0mutex.h:106
MutexMonitor * mutex_monitor
Defined in sync0sync.cc.
Definition: sync0sync.cc:186
OSMutex EventMutex
Definition: ut0mutex.h:73
ulong srv_n_spin_wait_rounds
Definition: ut0mutex.h:107
void mutex_destroy(Mutex *mutex)
Removes a mutex instance from the mutex list.
Definition: ut0mutex.h:249
#define UT_MUTEX_TYPE(M, P, T)
Create a typedef using the MutexType<PolicyType>
Definition: ut0mutex.h:71
#define mutex_exit(M)
Definition: ut0mutex.h:122
#define mutex_free(M)
Definition: ut0mutex.h:124
#define mutex_create(I, M)
Definition: ut0mutex.h:109
void mutex_enter_inline(Mutex *m, ut::Location loc)
Definition: ut0mutex.h:112
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:237
Mutex implementation include file.