MySQL  8.0.20
Source Code Documentation
ut0mutex.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 2012, 2018, Oracle and/or its affiliates. All Rights Reserved.
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 also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 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 
38 extern ulong srv_spin_wait_delay;
39 extern ulong srv_n_spin_wait_rounds;
40 extern 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 and necessary to suppress compiler warnings. */
55 #define mutex_own(M) ((M) || false)
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 
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 
83 
86 
88 UT_MUTEX_TYPE(TTASEventMutex, BlockMutexPolicy, BlockSyncArrayMutex)
89 
90 #ifndef UNIV_HOTBACKUP
91 #ifdef MUTEX_FUTEX
92 /** The default mutex type. */
93 typedef FutexMutex ib_mutex_t;
94 typedef BlockFutexMutex ib_bpmutex_t;
95 #define MUTEX_TYPE "Uses futexes"
96 #elif defined(MUTEX_SYS)
97 typedef SysMutex ib_mutex_t;
98 typedef BlockSysMutex ib_bpmutex_t;
99 #define MUTEX_TYPE "Uses system mutexes"
100 #elif defined(MUTEX_EVENT)
101 typedef SyncArrayMutex ib_mutex_t;
102 typedef BlockSyncArrayMutex ib_bpmutex_t;
103 #define MUTEX_TYPE "Uses event mutexes"
104 #else
105 #error "ib_mutex_t type is unknown"
106 #endif /* MUTEX_FUTEX */
107 
108 #include "ut0mutex.ic"
109 
110 extern ulong srv_spin_wait_delay;
112 
113 #define mutex_create(I, M) mutex_init((M), (I), __FILE__, __LINE__)
114 
115 #define mutex_enter(M) \
116  (M)->enter(srv_n_spin_wait_rounds, srv_spin_wait_delay, __FILE__, __LINE__)
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  explicit IB_mutex_guard(ib_mutex_t *in_mutex) : m_mutex(in_mutex) {
131  mutex_enter(in_mutex);
132  }
133 
134  /** Destructor to release mutex */
136 
137  /** Disable copy construction */
138  IB_mutex_guard(IB_mutex_guard const &) = delete;
139 
140  /** Disable assignment */
141  IB_mutex_guard &operator=(IB_mutex_guard const &) = delete;
142 
143  private:
144  /** Current mutex for RAII */
145  ib_mutex_t *m_mutex;
146 };
147 
148 #ifdef UNIV_DEBUG
149 /**
150 Checks that the mutex has been initialized. */
151 #define mutex_validate(M) (M)->validate()
152 
153 /**
154 Checks that the current thread owns the mutex. Works only
155 in the debug version. */
156 #define mutex_own(M) (M)->is_owned()
157 #else
158 #define mutex_own(M) /* No op */
159 #define mutex_validate(M) /* No op */
160 #endif /* UNIV_DEBUG */
161 #else /* !UNIV_HOTBACKUP */
162 #include "../meb/mutex.h"
163 typedef meb::Mutex ib_mutex_t;
164 typedef meb::Mutex ib_bpmutex_t;
165 #endif /* !UNIV_HOTBACKUP */
166 
167 /** Iterate over the mutex meta data */
169  public:
170  /** Constructor */
172 
173  /** Destructor */
175 
176  /** Enable the mutex monitoring */
177  void enable();
178 
179  /** Disable the mutex monitoring */
180  void disable();
181 
182  /** Reset the mutex monitoring values */
183  void reset();
184 
185  /** Invoke the callback for each active mutex collection
186  @param[in,out] callback Functor to call
187  @return false if callback returned false */
188  template <typename Callback>
189  bool iterate(Callback &callback) const UNIV_NOTHROW {
190  LatchMetaData::iterator end = latch_meta.end();
191 
192  for (LatchMetaData::iterator it = latch_meta.begin(); it != end; ++it) {
193  /* Some of the slots will be null in non-debug mode */
194 
195  if (*it == NULL) {
196  continue;
197  }
198 
199  latch_meta_t *latch_meta = *it;
200 
201  bool ret = callback(*latch_meta);
202 
203  if (!ret) {
204  return (ret);
205  }
206  }
207 
208  return (true);
209  }
210 };
211 
212 /** Defined in sync0sync.cc */
214 
215 #ifndef UNIV_HOTBACKUP
216 /**
217 Creates, or rather, initializes a mutex object in a specified memory
218 location (which must be appropriately aligned). The mutex is initialized
219 in the reset state. Explicit freeing of the mutex with mutex_free is
220 necessary only if the memory block containing it is freed.
221 Add the mutex instance to the global mutex list.
222 @param[in,out] mutex mutex to initialise
223 @param[in] id The mutex ID (Latch ID)
224 @param[in] file_name Filename from where it was called
225 @param[in] line Line number in filename from where called */
226 template <typename Mutex>
227 void mutex_init(Mutex *mutex, latch_id_t id, const char *file_name,
228  uint32_t line) {
229  new (mutex) Mutex();
230 
231  mutex->init(id, file_name, line);
232 }
233 
234 /**
235 Removes a mutex instance from the mutex list. The mutex is checked to
236 be in the reset state.
237 @param[in,out] mutex mutex instance to destroy */
238 template <typename Mutex>
239 void mutex_destroy(Mutex *mutex) {
240  mutex->destroy();
241 }
242 #endif /* UNIV_LIBRARY */
243 #endif /* !UNIV_HOTBACKUP */
244 
245 #endif /* ut0mutex_h */
LatchMetaData latch_meta
Note: This is accessed without any mutex protection.
Definition: sync0debug.cc:1221
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:227
Policy based mutexes.
Policies for mutexes.
MutexMonitor()
Constructor.
Definition: ut0mutex.h:171
ib_mutex_t * m_mutex
Current mutex for RAII.
Definition: ut0mutex.h:145
Collect the metrics per mutex instance, no aggregation.
Definition: sync0policy.h:214
Class that stores callback function reference as well as the result of the callback function call (in...
Definition: keyring_service.cc:42
latch_id_t
Each latch has an ID.
Definition: sync0types.h:344
IB_mutex_guard & operator=(IB_mutex_guard const &)=delete
Disable assignment.
Iterate over the mutex meta data.
Definition: ut0mutex.h:168
Macros for using atomics.
Some integer typedefs for easier portability.
Definition: ut0mutex.h:127
pthread_mutex_t mutex
Definition: memcached.c:384
OS mutex for tracking lock/unlock for debugging.
Definition: ib0mutex.h:46
Definition: ib0mutex.h:492
ulong srv_spin_wait_delay
Definition: ut0mutex.h:110
~IB_mutex_guard()
Destructor to release mutex.
Definition: ut0mutex.h:135
OS mutex, without any policy.
Definition: sync0types.h:467
ulong srv_n_spin_wait_rounds
Definition: ut0mutex.h:111
OSMutex EventMutex
Definition: ut0mutex.h:74
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:188
MutexMonitor * mutex_monitor
Defined in sync0sync.cc.
Definition: sync0sync.cc:176
IB_mutex_guard(ib_mutex_t *in_mutex)
Constructor to acquire mutex.
Definition: ut0mutex.h:130
#define NULL
Definition: types.h:55
Definition: ib0mutex.h:355
std::string HARNESS_EXPORT reset()
get &#39;reset attributes&#39; ESC sequence.
Definition: vt100.cc:36
void mutex_destroy(Mutex *mutex)
Removes a mutex instance from the mutex list.
Definition: ut0mutex.h:239
Latch meta data.
Definition: sync0types.h:767
Track agregate metrics policy, used by the page mutex.
Definition: sync0policy.h:331
#define mutex_enter(M)
Definition: ut0mutex.h:115
#define mutex_exit(M)
Definition: ut0mutex.h:122
#define UT_MUTEX_TYPE(M, P, T)
Create a typedef using the MutexType<PolicyType>
Definition: ut0mutex.h:72
~MutexMonitor()
Destructor.
Definition: ut0mutex.h:174
ulong srv_force_recovery_crash
Inject a crash at different steps of the recovery process.
Definition: srv0srv.cc:498
bool iterate(Callback &callback) const 1
Invoke the callback for each active mutex collection.
Definition: ut0mutex.h:189