Debian Codegraph @

storage/innobase/include/ut0mutex.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2012, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 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
39 extern ulong srv_spin_wait_delay;
40 extern ulong srv_n_spin_wait_rounds;
41 extern 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 */
57 typedef OSMutex SysMutex;
58 typedef OSMutex ib_mutex_t;
59 typedef 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
74 typedef OSMutex EventMutex;
75
76 #ifdef HAVE_IB_LINUX_FUTEX
77 UT_MUTEX_TYPE(TTASFutexMutex, GenericPolicy, FutexMutex)
78 UT_MUTEX_TYPE(TTASFutexMutex, BlockMutexPolicy, BlockFutexMutex)
79 #endif /* HAVE_IB_LINUX_FUTEX */
80
81 UT_MUTEX_TYPE(OSTrackMutex, GenericPolicy, SysMutex)
82 UT_MUTEX_TYPE(OSTrackMutex, BlockMutexPolicy, BlockSysMutex)
83
84 UT_MUTEX_TYPE(TTASEventMutex, GenericPolicy, SyncArrayMutex)
85 UT_MUTEX_TYPE(TTASEventMutex, BlockMutexPolicy, BlockSyncArrayMutex)
86
87 #ifndef UNIV_HOTBACKUP
88 #ifdef MUTEX_FUTEX
89 /** The default mutex type. */
90 typedef FutexMutex ib_mutex_t;
91 typedef BlockFutexMutex ib_bpmutex_t;
92 #define MUTEX_TYPE "Uses futexes"
93 #elif defined(MUTEX_SYS)
94 typedef SysMutex ib_mutex_t;
95 typedef BlockSysMutex ib_bpmutex_t;
96 #define MUTEX_TYPE "Uses system mutexes"
97 #elif defined(MUTEX_EVENT)
98 typedef SyncArrayMutex ib_mutex_t;
99 typedef 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
107 extern ulong srv_spin_wait_delay;
108 extern ulong srv_n_spin_wait_rounds;
109
110 #define mutex_create(I, M) mutex_init((M), (I), __FILE__, __LINE__)
111
112 template <typename Mutex>
113 void mutex_enter_inline(Mutex *m, ut::Location loc) {
114 m->enter(srv_n_spin_wait_rounds, srv_spin_wait_delay, loc.filename, loc.line);
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 */
128 struct IB_mutex_guard {
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 */
139 ~IB_mutex_guard() { clear(); }
140
141 /** Disable copy construction */
142 IB_mutex_guard(IB_mutex_guard const &) = delete;
143
144 /** Disable assignment */
145 IB_mutex_guard &operator=(IB_mutex_guard const &) = delete;
146
147 private:
148 /** Current mutex for RAII */
149 ib_mutex_t *m_mutex;
150
151 void clear() {
152 mutex_exit(m_mutex);
153 m_mutex = nullptr;
154 }
155 };
156
157 #ifdef UNIV_DEBUG
158 /**
159 Checks that the mutex has been initialized. */
160 #define mutex_validate(M) (M)->validate()
161
162 /**
163 Checks that the current thread owns the mutex. Works only
164 in 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"
172 typedef meb::Mutex ib_mutex_t;
173 typedef meb::Mutex ib_bpmutex_t;
174 #endif /* !UNIV_HOTBACKUP */
175
176 /** Iterate over the mutex meta data */
177 class MutexMonitor {
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 == NULL) {
205 continue;
206 }
207
208 latch_meta_t *latch_meta = *it;
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 */
222 extern MutexMonitor *mutex_monitor;
223
224 #ifndef UNIV_HOTBACKUP
225 /**
226 Creates, or rather, initializes a mutex object in a specified memory
227 location (which must be appropriately aligned). The mutex is initialized
228 in the reset state. Explicit freeing of the mutex with mutex_free is
229 necessary only if the memory block containing it is freed.
230 Add 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 */
235 template <typename Mutex>
236 void 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 /**
244 Removes a mutex instance from the mutex list. The mutex is checked to
245 be in the reset state.
246 @param[in,out] mutex mutex instance to destroy */
247 template <typename Mutex>
248 void mutex_destroy(Mutex *mutex) {
249 mutex->destroy();
250 }
251
252 class 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 */