MySQL 8.0.33
Source Code Documentation
ut0mutex.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2012, 2023, 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 also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/** @file include/ut0mutex.h
28 Policy based mutexes.
29
30 Created 2012-03-24 Sunny Bains.
31 ***********************************************************************/
32
33#ifndef ut0mutex_h
34#define ut0mutex_h
35
36#include "my_inttypes.h"
37
38extern ulong srv_spin_wait_delay;
39extern ulong srv_n_spin_wait_rounds;
40extern ulong srv_force_recovery_crash;
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>
112void mutex_enter_inline(Mutex *m, ut::Location loc) {
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 mutex_enter_inline(in_mutex, location);
135 }
136
137 /** Destructor to release mutex */
139
140 /** Disable copy construction */
142
143 /** Disable assignment */
145
146 private:
147 /** Current mutex for RAII */
148 ib_mutex_t *m_mutex;
149
150 void clear() {
152 m_mutex = nullptr;
153 }
154};
155
156#ifdef UNIV_DEBUG
157/**
158Checks that the mutex has been initialized. */
159#define mutex_validate(M) (M)->validate()
160
161/**
162Checks that the current thread owns the mutex. Works only
163in the debug version. */
164#define mutex_own(M) (M)->is_owned()
165#else
166#define mutex_own(M) /* No op */
167#define mutex_validate(M) /* No op */
168#endif /* UNIV_DEBUG */
169#else /* !UNIV_HOTBACKUP */
170#include "../meb/mutex.h"
171typedef meb::Mutex ib_mutex_t;
172typedef meb::Mutex ib_bpmutex_t;
173#endif /* !UNIV_HOTBACKUP */
174
175/** Iterate over the mutex meta data */
177 public:
178 /** Constructor */
179 MutexMonitor() = default;
180
181 /** Destructor */
182 ~MutexMonitor() = default;
183
184 /** Enable the mutex monitoring */
185 void enable();
186
187 /** Disable the mutex monitoring */
188 void disable();
189
190 /** Reset the mutex monitoring values */
191 void reset();
192
193 /** Invoke the callback for each active mutex collection
194 @param[in,out] callback Functor to call
195 @return false if callback returned false */
196 template <typename Callback>
197 bool iterate(Callback &callback) const UNIV_NOTHROW {
198 LatchMetaData::iterator end = latch_meta.end();
199
200 for (LatchMetaData::iterator it = latch_meta.begin(); it != end; ++it) {
201 /* Some of the slots will be null in non-debug mode */
202
203 if (*it == NULL) {
204 continue;
205 }
206
208
209 bool ret = callback(*latch_meta);
210
211 if (!ret) {
212 return (ret);
213 }
214 }
215
216 return (true);
217 }
218};
219
220/** Defined in sync0sync.cc */
222
223#ifndef UNIV_HOTBACKUP
224/**
225Creates, or rather, initializes a mutex object in a specified memory
226location (which must be appropriately aligned). The mutex is initialized
227in the reset state. Explicit freeing of the mutex with mutex_free is
228necessary only if the memory block containing it is freed.
229Add the mutex instance to the global mutex list.
230@param[in,out] mutex mutex to initialise
231@param[in] id The mutex ID (Latch ID)
232@param[in] file_name Filename from where it was called
233@param[in] line Line number in filename from where called */
234template <typename Mutex>
235void mutex_init(Mutex *mutex, latch_id_t id, const char *file_name,
236 uint32_t line) {
237 new (mutex) Mutex();
238
239 mutex->init(id, file_name, line);
240}
241
242/**
243Removes a mutex instance from the mutex list. The mutex is checked to
244be in the reset state.
245@param[in,out] mutex mutex instance to destroy */
246template <typename Mutex>
247void mutex_destroy(Mutex *mutex) {
248 mutex->destroy();
249}
250
251class IB_mutex : public ib_mutex_t {
252 public:
253 explicit IB_mutex(latch_id_t latch_id) {
254 mutex_create(latch_id, static_cast<ib_mutex_t *>(this));
255 }
256 ~IB_mutex() { mutex_free(static_cast<ib_mutex_t *>(this)); }
257 void lock(const ut::Location &loc) {
258 mutex_enter_inline(static_cast<ib_mutex_t *>(this), loc);
259 }
260 void unlock() { exit(); }
261};
262
263#endif /* !UNIV_HOTBACKUP */
264#endif /* UNIV_LIBRARY */
265
266#endif /* ut0mutex_h */
Track aggregate metrics policy, used by the page mutex.
Definition: sync0policy.h:335
Class that stores callback function reference as well as the result of the callback function call (in...
Definition: keyring_service.cc:42
Definition: ut0mutex.h:251
~IB_mutex()
Definition: ut0mutex.h:256
void unlock()
Definition: ut0mutex.h:260
IB_mutex(latch_id_t latch_id)
Definition: ut0mutex.h:253
void lock(const ut::Location &loc)
Definition: ut0mutex.h:257
Latch meta data.
Definition: sync0types.h:770
Iterate over the mutex meta data.
Definition: ut0mutex.h:176
MutexMonitor()=default
Constructor.
~MutexMonitor()=default
Destructor.
void reset()
Reset the mutex monitoring values.
Definition: sync0sync.cc:271
void disable()
Disable the mutex monitoring.
Definition: sync0sync.cc:257
void enable()
Enable the mutex monitoring.
Definition: sync0sync.cc:243
bool iterate(Callback &callback) const 1
Invoke the callback for each active mutex collection.
Definition: ut0mutex.h:197
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:93
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:191
Macros for using atomics.
Collect the metrics per mutex instance, no aggregation.
Definition: sync0policy.h:218
Definition: ut0mutex.h:127
void clear()
Definition: ut0mutex.h:150
~IB_mutex_guard()
Destructor to release mutex.
Definition: ut0mutex.h:138
IB_mutex_guard & operator=(IB_mutex_guard const &)=delete
Disable assignment.
ib_mutex_t * m_mutex
Current mutex for RAII.
Definition: ut0mutex.h:148
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:470
OS mutex for tracking lock/unlock for debugging.
Definition: ib0mutex.h:46
Definition: ib0mutex.h:374
Definition: ut0core.h:32
const char * filename
Definition: ut0core.h:33
size_t line
Definition: ut0core.h:34
Policies for mutexes.
LatchMetaData latch_meta
Note: This is accessed without any mutex protection.
Definition: sync0debug.cc:1190
latch_id_t
Each latch has an ID.
Definition: sync0types.h:341
#define NULL
Definition: types.h:54
#define UNIV_NOTHROW
Definition: univ.i:455
ulong srv_force_recovery_crash
Inject a crash at different steps of the recovery process.
Definition: srv0srv.cc:538
ulong srv_spin_wait_delay
Definition: ut0mutex.h:106
MutexMonitor * mutex_monitor
Defined in sync0sync.cc.
Definition: sync0sync.cc:183
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:247
#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:235
Mutex implementation include file.