| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2008, Google Inc. |
| 5 |
|
| 6 |
Portions of this file contain modifications contributed and copyrighted by |
| 7 |
Google, Inc. Those modifications are gratefully acknowledged and are described |
| 8 |
briefly in the InnoDB documentation. The contributions by Google are |
| 9 |
incorporated with their permission, and subject to the conditions contained in |
| 10 |
the file COPYING.Google. |
| 11 |
|
| 12 |
This program is free software; you can redistribute it and/or modify it under |
| 13 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 14 |
Free Software Foundation. |
| 15 |
|
| 16 |
This program is designed to work with certain software (including |
| 17 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 18 |
as designated in a particular file or component or in included license |
| 19 |
documentation. The authors of MySQL hereby grant you an additional |
| 20 |
permission to link the program and your derivative works with the |
| 21 |
separately licensed software that they have either included with |
| 22 |
the program or referenced in the documentation. |
| 23 |
|
| 24 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 25 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 26 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 27 |
for more details. |
| 28 |
|
| 29 |
You should have received a copy of the GNU General Public License along with |
| 30 |
this program; if not, write to the Free Software Foundation, Inc., |
| 31 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 32 |
|
| 33 |
*****************************************************************************/ |
| 34 |
|
| 35 |
/** @file include/sync0rw.h |
| 36 |
The read-write lock (for threads, not for database transactions) |
| 37 |
|
| 38 |
Created 9/11/1995 Heikki Tuuri |
| 39 |
*******************************************************/ |
| 40 |
|
| 41 |
#ifndef sync0rw_h |
| 42 |
#define sync0rw_h |
| 43 |
|
| 44 |
#include "univ.i" |
| 45 |
#ifndef UNIV_HOTBACKUP |
| 46 |
#include "os0event.h" |
| 47 |
#include "ut0counter.h" |
| 48 |
#endif /* !UNIV_HOTBACKUP */ |
| 49 |
#include <atomic> |
| 50 |
#include "ut0mutex.h" |
| 51 |
|
| 52 |
struct rw_lock_t; |
| 53 |
|
| 54 |
#ifndef UNIV_HOTBACKUP |
| 55 |
|
| 56 |
#ifdef UNIV_LIBRARY |
| 57 |
|
| 58 |
#ifdef UNIV_DEBUG |
| 59 |
|
| 60 |
/** |
| 61 |
Pass-through version of rw_lock_own(), which normally checks that the |
| 62 |
thread has locked the rw-lock in the specified mode. |
| 63 |
@param[in] lock pointer to rw-lock |
| 64 |
@param[in] lock_type lock type: RW_LOCK_S, RW_LOCK_X |
| 65 |
@return true if success */ |
| 66 |
static inline bool rw_lock_own(rw_lock_t *lock, ulint lock_type) { |
| 67 |
return (lock != nullptr); |
| 68 |
} |
| 69 |
|
| 70 |
#define sync_check_iterate(A) true |
| 71 |
#endif /* UNIV_DEBUG */ |
| 72 |
|
| 73 |
#define rw_lock_s_lock(L, Loc) ((void)0) |
| 74 |
#define rw_lock_s_lock_nowait(L, Loc) true |
| 75 |
#define rw_lock_s_unlock(L) ((void)0) |
| 76 |
#define rw_lock_x_lock(L, Loc) ((void)0) |
| 77 |
#define rw_lock_x_lock_nowait(L, Loc) true |
| 78 |
#define rw_lock_x_unlock(L) ((void)0) |
| 79 |
#define rw_lock_sx_lock(L, Loc) ((void)0) |
| 80 |
#define rw_lock_sx_unlock(L) ((void)0) |
| 81 |
#define rw_lock_s_lock_gen(M, P, L) ((void)0) |
| 82 |
#define rw_lock_x_lock_gen(M, P, L) ((void)0) |
| 83 |
#define rw_lock_sx_lock_gen(M, P, L) ((void)0) |
| 84 |
#define sync_check_lock(A, B) ((void)0) |
| 85 |
#define rw_lock_own_flagged(A, B) true |
| 86 |
#endif /* UNIV_LIBRARY */ |
| 87 |
|
| 88 |
#endif /* !UNIV_HOTBACKUP */ |
| 89 |
|
| 90 |
/* Latch types; these are used also in btr0btr.h and mtr0mtr.h: keep the |
| 91 |
numerical values smaller than 30 (smaller than BTR_MODIFY_TREE and |
| 92 |
MTR_MEMO_MODIFY) and the order of the numerical values like below! and they |
| 93 |
should be 2pow value to be used also as ORed combination of flag. */ |
| 94 |
enum rw_lock_type_t { |
| 95 |
RW_S_LATCH = 1, |
| 96 |
RW_X_LATCH = 2, |
| 97 |
RW_SX_LATCH = 4, |
| 98 |
RW_NO_LATCH = 8 |
| 99 |
}; |
| 100 |
|
| 101 |
/* We decrement lock_word by X_LOCK_DECR for each x_lock. It is also the |
| 102 |
start value for the lock_word, meaning that it limits the maximum number |
| 103 |
of concurrent read locks before the rw_lock breaks. */ |
| 104 |
/* We decrement lock_word by X_LOCK_HALF_DECR for sx_lock. */ |
| 105 |
constexpr int32_t X_LOCK_DECR = 0x20000000; |
| 106 |
constexpr int32_t X_LOCK_HALF_DECR = 0x10000000; |
| 107 |
|
| 108 |
#ifdef UNIV_DEBUG |
| 109 |
struct rw_lock_debug_t; |
| 110 |
#endif /* UNIV_DEBUG */ |
| 111 |
|
| 112 |
extern ib_mutex_t rw_lock_list_mutex; |
| 113 |
|
| 114 |
#ifndef UNIV_LIBRARY |
| 115 |
#ifndef UNIV_HOTBACKUP |
| 116 |
|
| 117 |
/** Creates, or rather, initializes an rw-lock object in a specified memory |
| 118 |
location (which must be appropriately aligned). The rw-lock is initialized |
| 119 |
to the non-locked state. Explicit freeing of the rw-lock with rw_lock_free |
| 120 |
is necessary only if the memory block containing it is freed. |
| 121 |
@param[in] lock pointer to memory |
| 122 |
@param[in] id latch_id |
| 123 |
@param[in] clocation location where created */ |
| 124 |
void rw_lock_create_func(rw_lock_t *lock, |
| 125 |
IF_DEBUG(latch_id_t id, ) ut::Location clocation); |
| 126 |
/** Calling this function is obligatory only if the memory buffer containing |
| 127 |
the rw-lock is freed. Removes an rw-lock object from the global list. The |
| 128 |
rw-lock is checked to be in the non-locked state. */ |
| 129 |
void rw_lock_free_func(rw_lock_t *lock); /*!< in/out: rw-lock */ |
| 130 |
#ifdef UNIV_DEBUG |
| 131 |
/** Checks that the rw-lock has been initialized and that there are no |
| 132 |
simultaneous shared and exclusive locks. |
| 133 |
@return true */ |
| 134 |
[[nodiscard]] bool rw_lock_validate(const rw_lock_t *lock); /*!< in: rw-lock */ |
| 135 |
#endif /* UNIV_DEBUG */ |
| 136 |
|
| 137 |
/** Low-level function which tries to lock an rw-lock in s-mode. Performs no |
| 138 |
spinning. |
| 139 |
@param[in] lock pointer to rw-lock |
| 140 |
@param[in] pass pass value; != 0, if the lock will be passed |
| 141 |
to another thread to unlock |
| 142 |
@param[in] location location where requested |
| 143 |
@return true if success */ |
| 144 |
[[nodiscard]] static inline bool rw_lock_s_lock_low(rw_lock_t *lock, |
| 145 |
ulint pass [[maybe_unused]], |
| 146 |
ut::Location location); |
| 147 |
|
| 148 |
/** NOTE! Use the corresponding macro, not directly this function, except if |
| 149 |
you supply the file name and line number. Lock an rw-lock in shared mode for |
| 150 |
the current thread. If the rw-lock is locked in exclusive mode, or there is an |
| 151 |
exclusive lock request waiting, the function spins a preset time (controlled |
| 152 |
by srv_n_spin_wait_rounds), waiting for the lock, before suspending the thread. |
| 153 |
@param[in] lock pointer to rw-lock |
| 154 |
@param[in] pass pass value; != 0, if the lock will be passed to |
| 155 |
another thread to unlock |
| 156 |
@param[in] location location where requested */ |
| 157 |
static inline void rw_lock_s_lock_func(rw_lock_t *lock, ulint pass, |
| 158 |
ut::Location location); |
| 159 |
|
| 160 |
/** NOTE! Use the corresponding macro, not directly this function! Lock an |
| 161 |
rw-lock in exclusive mode for the current thread if the lock can be obtained |
| 162 |
immediately. |
| 163 |
@param[in] lock pointer to rw-lock |
| 164 |
@param[in] location location where requested |
| 165 |
@return true if success */ |
| 166 |
[[nodiscard]] static inline bool rw_lock_x_lock_func_nowait( |
| 167 |
rw_lock_t *lock, ut::Location location); |
| 168 |
|
| 169 |
/** Releases a shared mode lock. |
| 170 |
@param[in] pass pass value; != 0, if the lock will be passed to another |
| 171 |
thread to unlock |
| 172 |
@param[in,out] lock rw-lock */ |
| 173 |
static inline void rw_lock_s_unlock_func(IF_DEBUG(ulint pass, ) |
| 174 |
rw_lock_t *lock); |
| 175 |
|
| 176 |
/** NOTE! Use the corresponding macro, not directly this function! Lock an |
| 177 |
rw-lock in exclusive mode for the current thread. If the rw-lock is locked |
| 178 |
in shared or exclusive mode, or there is an exclusive lock request waiting, |
| 179 |
the function spins a preset time (controlled by srv_n_spin_wait_rounds), |
| 180 |
waiting for the lock, before suspending the thread. If the same thread has an |
| 181 |
x-lock on the rw-lock, locking succeed, with the following exception: if pass |
| 182 |
!= 0, only a single x-lock may be taken on the lock. NOTE: If the same thread |
| 183 |
has an s-lock, locking does not succeed! |
| 184 |
@param[in] lock pointer to rw-lock |
| 185 |
@param[in] pass pass value; != 0, if the lock will be passed to |
| 186 |
another thread to unlock |
| 187 |
@param[in] location location where requested */ |
| 188 |
void rw_lock_x_lock_func(rw_lock_t *lock, ulint pass, ut::Location location); |
| 189 |
|
| 190 |
/** Low-level function for acquiring an sx lock. |
| 191 |
@param[in] lock pointer to rw-lock |
| 192 |
@param[in] pass pass value; != 0, if the lock will be passed to |
| 193 |
another thread to unlock |
| 194 |
@param[in] location location where requested |
| 195 |
@return false if did not succeed, true if success. */ |
| 196 |
[[nodiscard]] bool rw_lock_sx_lock_low(rw_lock_t *lock, ulint pass, |
| 197 |
ut::Location location); |
| 198 |
/** NOTE! Use the corresponding macro, not directly this function! Lock an |
| 199 |
rw-lock in SX mode for the current thread. If the rw-lock is locked |
| 200 |
in exclusive mode, or there is an exclusive lock request waiting, |
| 201 |
the function spins a preset time (controlled by SYNC_SPIN_ROUNDS), waiting |
| 202 |
for the lock, before suspending the thread. If the same thread has an x-lock |
| 203 |
on the rw-lock, locking succeed, with the following exception: if pass != 0, |
| 204 |
only a single sx-lock may be taken on the lock. NOTE: If the same thread has |
| 205 |
an s-lock, locking does not succeed! |
| 206 |
@param[in] lock pointer to rw-lock |
| 207 |
@param[in] pass pass value; != 0, if the lock will be passed to |
| 208 |
another thread to unlock |
| 209 |
@param[in] location location where requested */ |
| 210 |
void rw_lock_sx_lock_func(rw_lock_t *lock, ulint pass, ut::Location location); |
| 211 |
|
| 212 |
/** Releases an exclusive mode lock. |
| 213 |
@param[in] pass pass value; != 0, if the lock will be passed to another |
| 214 |
thread to unlock |
| 215 |
@param[in,out] lock rw-lock */ |
| 216 |
static inline void rw_lock_x_unlock_func(IF_DEBUG(ulint pass, ) |
| 217 |
rw_lock_t *lock); |
| 218 |
|
| 219 |
/** Releases an sx mode lock. |
| 220 |
@param[in] pass pass value; != 0, if the lock will be passed to another |
| 221 |
thread to unlock |
| 222 |
@param[in,out] lock rw-lock */ |
| 223 |
static inline void rw_lock_sx_unlock_func(IF_DEBUG(ulint pass, ) |
| 224 |
rw_lock_t *lock); |
| 225 |
|
| 226 |
/** This function is used in the insert buffer to move the ownership of an |
| 227 |
x-latch on a buffer frame to the current thread. The x-latch was set by |
| 228 |
the buffer read operation and it protected the buffer frame while the |
| 229 |
read was done. The ownership is moved because we want that the current |
| 230 |
thread is able to acquire a second x-latch which is stored in an mtr. |
| 231 |
This, in turn, is needed to pass the debug checks of index page |
| 232 |
operations. |
| 233 |
@param[in] lock lock which was x-locked in the buffer read. */ |
| 234 |
void rw_lock_x_lock_move_ownership(rw_lock_t *lock); |
| 235 |
/** Returns the value of writer_count for the lock. Does not reserve the lock |
| 236 |
mutex, so the caller must be sure it is not changed during the call. |
| 237 |
@return value of writer_count |
| 238 |
@param[in] lock rw-lock */ |
| 239 |
static inline ulint rw_lock_get_x_lock_count(const rw_lock_t *lock); |
| 240 |
/** Returns the number of sx-lock for the lock. Does not reserve the lock |
| 241 |
mutex, so the caller must be sure it is not changed during the call. |
| 242 |
@param[in] lock rw-lock |
| 243 |
@return value of writer_count */ |
| 244 |
static inline ulint rw_lock_get_sx_lock_count(const rw_lock_t *lock); |
| 245 |
/** Check if there are threads waiting for the rw-lock. |
| 246 |
@param[in] lock rw-lock |
| 247 |
@return true if waiters, false otherwise */ |
| 248 |
[[nodiscard]] static inline bool rw_lock_get_waiters(const rw_lock_t *lock); |
| 249 |
/** Returns the write-status of the lock - this function made more sense |
| 250 |
with the old rw_lock implementation. |
| 251 |
@param[in] lock rw-lock |
| 252 |
@return RW_LOCK_NOT_LOCKED, RW_LOCK_X, RW_LOCK_X_WAIT, RW_LOCK_SX */ |
| 253 |
static inline ulint rw_lock_get_writer(const rw_lock_t *lock); |
| 254 |
/** Returns the number of readers (s-locks). |
| 255 |
@param[in] lock rw-lock |
| 256 |
@return number of readers */ |
| 257 |
static inline ulint rw_lock_get_reader_count(const rw_lock_t *lock); |
| 258 |
|
| 259 |
/** Decrements lock_word the specified amount if it is greater than 0. |
| 260 |
This is used by both s_lock and x_lock operations. |
| 261 |
@param[in,out] lock rw-lock |
| 262 |
@param[in] amount amount to decrement |
| 263 |
@param[in] threshold threshold of judgement |
| 264 |
@return true if decr occurs */ |
| 265 |
[[nodiscard]] static inline bool rw_lock_lock_word_decr(rw_lock_t *lock, |
| 266 |
ulint amount, |
| 267 |
lint threshold); |
| 268 |
|
| 269 |
/** Increments lock_word the specified amount and returns new value. |
| 270 |
@param[in,out] lock rw-lock |
| 271 |
@param[in] amount amount to decrement |
| 272 |
@return lock->lock_word after increment */ |
| 273 |
static inline lint rw_lock_lock_word_incr(rw_lock_t *lock, ulint amount); |
| 274 |
|
| 275 |
/** This function sets the lock->writer_thread and lock->recursive fields. Sets |
| 276 |
lock->recursive field using atomic release after setting lock->writer thread to |
| 277 |
ensure proper memory ordering of the two. |
| 278 |
Note that it is assumed that the caller of this function effectively owns |
| 279 |
the lock i.e.: nobody else is allowed to modify lock->writer_thread at this |
| 280 |
point in time. The protocol is that lock->writer_thread MUST be updated BEFORE |
| 281 |
the lock->recursive flag is set. |
| 282 |
@param[in,out] lock lock to work on |
| 283 |
@param[in] recursive true if recursion allowed */ |
| 284 |
static inline void rw_lock_set_writer_id_and_recursion_flag(rw_lock_t *lock, |
| 285 |
bool recursive); |
| 286 |
|
| 287 |
#ifdef UNIV_DEBUG |
| 288 |
/** Checks if the thread has locked the rw-lock in the specified mode, with |
| 289 |
the pass value == 0. Note that the mode is checked exactly, so if the thread |
| 290 |
owns RW_LOCK_X only, the rw_lock_own(..,RW_LOCK_S) will return false. |
| 291 |
@param[in] lock the rw-lock |
| 292 |
@param[in] lock_type The exact lock type to check: |
| 293 |
RW_LOCK_S, RW_LOCK_SX or RW_LOCK_X |
| 294 |
*/ |
| 295 |
[[nodiscard]] bool rw_lock_own(const rw_lock_t *lock, ulint lock_type); |
| 296 |
|
| 297 |
/** Checks if the thread has locked the rw-lock in the specified mode, with the |
| 298 |
pass value == 0. |
| 299 |
@param[in] lock rw-lock |
| 300 |
@param[in] flags specify lock types with OR of the rw_lock_flag_t values |
| 301 |
@return true if locked */ |
| 302 |
[[nodiscard]] bool rw_lock_own_flagged(const rw_lock_t *lock, |
| 303 |
rw_lock_flags_t flags); |
| 304 |
#endif /* UNIV_DEBUG */ |
| 305 |
#endif /* !UNIV_HOTBACKUP */ |
| 306 |
/** Checks if somebody has locked the rw-lock in the specified mode. |
| 307 |
@param[in] lock the rw-lock |
| 308 |
@param[in] lock_type The exact lock type to check: |
| 309 |
RW_LOCK_S, RW_LOCK_SX or RW_LOCK_X |
| 310 |
@return true if locked */ |
| 311 |
[[nodiscard]] bool rw_lock_is_locked(rw_lock_t *lock, ulint lock_type); |
| 312 |
#ifdef UNIV_DEBUG |
| 313 |
/** Prints debug info of currently locked rw-locks. |
| 314 |
@param[in] file file where to print */ |
| 315 |
void rw_lock_list_print_info(FILE *file); |
| 316 |
|
| 317 |
/*#####################################################################*/ |
| 318 |
|
| 319 |
/** Prints info of a debug struct. |
| 320 |
@param[in] f Output stream |
| 321 |
@param[in] info Debug struct */ |
| 322 |
void rw_lock_debug_print(FILE *f, const rw_lock_debug_t *info); |
| 323 |
|
| 324 |
#endif /* UNIV_DEBUG */ |
| 325 |
|
| 326 |
#endif /* !UNIV_LIBRARY */ |
| 327 |
|
| 328 |
#ifdef UNIV_DEBUG |
| 329 |
/** The structure for storing debug info of an rw-lock. All access to this |
| 330 |
structure must be protected by rw_lock_debug_mutex_enter(). */ |
| 331 |
struct rw_lock_debug_t { |
| 332 |
/** The thread id of the thread which locked the rw-lock. */ |
| 333 |
std::thread::id thread_id; |
| 334 |
/** Pass value given in the lock operation. */ |
| 335 |
ulint pass; |
| 336 |
/** Type of the lock: RW_LOCK_X, RW_LOCK_S, RW_LOCK_X_WAIT. */ |
| 337 |
ulint lock_type; |
| 338 |
/** Location where the rw-lock was locked. */ |
| 339 |
ut::Location location; |
| 340 |
/** Debug structs are linked in a two-way list. */ |
| 341 |
UT_LIST_NODE_T(rw_lock_debug_t) list; |
| 342 |
}; |
| 343 |
#endif /* UNIV_DEBUG */ |
| 344 |
|
| 345 |
/* NOTE! The structure appears here only for the compiler to know its size. |
| 346 |
Do not use its fields directly! */ |
| 347 |
|
| 348 |
/** The structure used in the spin lock implementation of a read-write |
| 349 |
lock. Several threads may have a shared lock simultaneously in this |
| 350 |
lock, but only one writer may have an exclusive lock, in which case no |
| 351 |
shared locks are allowed. To prevent starving of a writer blocked by |
| 352 |
readers, a writer may queue for x-lock by decrementing lock_word: no |
| 353 |
new readers will be let in while the thread waits for readers to |
| 354 |
exit. */ |
| 355 |
|
| 356 |
struct rw_lock_t |
| 357 |
#ifdef UNIV_DEBUG |
| 358 |
: public latch_t |
| 359 |
#endif /* UNIV_DEBUG */ |
| 360 |
{ |
| 361 |
rw_lock_t() = default; |
| 362 |
/** rw_lock_t is not a copyable object, the reasoning |
| 363 |
behind this is the same as the reasoning behind why |
| 364 |
std::mutex is not copyable. It is supposed to represent |
| 365 |
a synchronization primitive for which copying semantics |
| 366 |
do not make sense. */ |
| 367 |
rw_lock_t(const rw_lock_t &) = delete; |
| 368 |
rw_lock_t &operator=(const rw_lock_t &) = delete; |
| 369 |
|
| 370 |
/** Holds the state of the lock. */ |
| 371 |
std::atomic<int32_t> lock_word; |
| 372 |
|
| 373 |
/** 1: there are waiters */ |
| 374 |
std::atomic<bool> waiters; |
| 375 |
|
| 376 |
/** Default value false which means the lock is non-recursive. |
| 377 |
The value is typically set to true making normal rw_locks recursive. |
| 378 |
In case of asynchronous IO, when a non-zero value of 'pass' is |
| 379 |
passed then we keep the lock non-recursive. |
| 380 |
|
| 381 |
This flag also tells us about the state of writer_thread field. |
| 382 |
If this flag is set then writer_thread MUST contain the thread |
| 383 |
id of the current x-holder or wait-x thread. This flag must be |
| 384 |
reset in x_unlock functions before incrementing the lock_word */ |
| 385 |
std::atomic<bool> recursive; |
| 386 |
|
| 387 |
/** number of granted SX locks. */ |
| 388 |
volatile ulint sx_recursive; |
| 389 |
|
| 390 |
/** Thread id of writer thread. Is only guaranteed to have non-stale value if |
| 391 |
recursive flag is set, otherwise it may contain native thread ID of a |
| 392 |
thread which already released or passed the lock. */ |
| 393 |
std::atomic<std::thread::id> writer_thread; |
| 394 |
|
| 395 |
/** XOR of reader threads' IDs. If there is exactly one reader it should allow |
| 396 |
to retrieve the thread ID of that reader. */ |
| 397 |
Atomic_xor_of_thread_id reader_thread; |
| 398 |
|
| 399 |
/** Used by sync0arr.cc for thread queueing */ |
| 400 |
os_event_t event; |
| 401 |
|
| 402 |
/** Event for next-writer to wait on. A thread must decrement |
| 403 |
lock_word before waiting. */ |
| 404 |
os_event_t wait_ex_event; |
| 405 |
|
| 406 |
/** Location where lock created */ |
| 407 |
ut::Location clocation; |
| 408 |
|
| 409 |
/** last s-lock file/line is not guaranteed to be correct */ |
| 410 |
const char *last_s_file_name; |
| 411 |
|
| 412 |
/** File name where last x-locked */ |
| 413 |
const char *last_x_file_name; |
| 414 |
|
| 415 |
/** If 1 then the rw-lock is a block lock */ |
| 416 |
bool is_block_lock; |
| 417 |
|
| 418 |
/** Line number where last time s-locked */ |
| 419 |
uint16_t last_s_line; |
| 420 |
|
| 421 |
/** Line number where last time x-locked */ |
| 422 |
uint16_t last_x_line; |
| 423 |
|
| 424 |
/** Count of os_waits. May not be accurate */ |
| 425 |
uint32_t count_os_wait; |
| 426 |
|
| 427 |
/** All allocated rw locks are put into a list */ |
| 428 |
UT_LIST_NODE_T(rw_lock_t) list; |
| 429 |
|
| 430 |
#ifdef UNIV_PFS_RWLOCK |
| 431 |
/** The instrumentation hook */ |
| 432 |
struct PSI_rwlock *pfs_psi; |
| 433 |
#endif /* UNIV_PFS_RWLOCK */ |
| 434 |
|
| 435 |
#ifndef UNIV_DEBUG |
| 436 |
/** Destructor */ |
| 437 |
~rw_lock_t(); |
| 438 |
#else |
| 439 |
/** Destructor */ |
| 440 |
~rw_lock_t() override; |
| 441 |
|
| 442 |
virtual std::string to_string() const override; |
| 443 |
virtual std::string locked_from() const override; |
| 444 |
|
| 445 |
/** For checking memory corruption. */ |
| 446 |
static const uint32_t MAGIC_N = 22643; |
| 447 |
uint32_t magic_n = {MAGIC_N}; |
| 448 |
|
| 449 |
/** In the debug version: pointer to the debug info list of the lock */ |
| 450 |
UT_LIST_BASE_NODE_T(rw_lock_debug_t, list) debug_list{}; |
| 451 |
|
| 452 |
#endif /* UNIV_DEBUG */ |
| 453 |
|
| 454 |
/** Checks if there is a thread requesting an x-latch waiting for threads to |
| 455 |
release their s-latches. |
| 456 |
@return true iff there is an x-latcher blocked by s-latchers. */ |
| 457 |
bool is_x_blocked_by_s() { |
| 458 |
const auto snapshot = lock_word.load(); |
| 459 |
return snapshot < 0 && -X_LOCK_DECR < snapshot && |
| 460 |
snapshot != -X_LOCK_HALF_DECR; |
| 461 |
} |
| 462 |
}; |
| 463 |
|
| 464 |
#ifndef UNIV_LIBRARY |
| 465 |
#ifndef UNIV_HOTBACKUP |
| 466 |
/* For performance schema instrumentation, a new set of rwlock |
| 467 |
wrap functions are created if "UNIV_PFS_RWLOCK" is defined. |
| 468 |
The instrumentations are not planted directly into original |
| 469 |
functions, so that we keep the underlying function as they |
| 470 |
are. And in case, user wants to "take out" some rwlock from |
| 471 |
instrumentation even if performance schema (UNIV_PFS_RWLOCK) |
| 472 |
is defined, they can do so by reinstating APIs directly link to |
| 473 |
original underlying functions. |
| 474 |
The instrumented function names have prefix of "pfs_rw_lock_" vs. |
| 475 |
original name prefix of "rw_lock_". Following are list of functions |
| 476 |
that have been instrumented: |
| 477 |
|
| 478 |
rw_lock_create() |
| 479 |
rw_lock_x_lock() |
| 480 |
rw_lock_x_lock_gen() |
| 481 |
rw_lock_x_lock_nowait() |
| 482 |
rw_lock_x_unlock_gen() |
| 483 |
rw_lock_s_lock() |
| 484 |
rw_lock_s_lock_gen() |
| 485 |
rw_lock_s_lock_nowait() |
| 486 |
rw_lock_s_unlock_gen() |
| 487 |
rw_lock_sx_lock() |
| 488 |
rw_lock_sx_unlock_gen() |
| 489 |
rw_lock_free() |
| 490 |
*/ |
| 491 |
|
| 492 |
#ifdef UNIV_PFS_RWLOCK |
| 493 |
/** Performance schema instrumented wrap function for rw_lock_create_func() |
| 494 |
NOTE! Please use the corresponding macro rw_lock_create(), not directly this |
| 495 |
function! |
| 496 |
@param[in] key key registered with performance schema |
| 497 |
@param[in] lock rw lock |
| 498 |
@param[in] id latch_id |
| 499 |
@param[in] clocation location where created */ |
| 500 |
static inline void pfs_rw_lock_create_func(mysql_pfs_key_t key, rw_lock_t *lock, |
| 501 |
IF_DEBUG(latch_id_t id, ) |
| 502 |
ut::Location clocation); |
| 503 |
|
| 504 |
/** Performance schema instrumented wrap function for rw_lock_x_lock_func() |
| 505 |
NOTE! Please use the corresponding macro rw_lock_x_lock(), not directly this |
| 506 |
function! |
| 507 |
@param[in] lock pointer to rw-lock |
| 508 |
@param[in] pass pass value; != 0, if the lock will be passed |
| 509 |
to another thread to unlock |
| 510 |
@param[in] location location where requested */ |
| 511 |
static inline void pfs_rw_lock_x_lock_func(rw_lock_t *lock, ulint pass, |
| 512 |
ut::Location location); |
| 513 |
|
| 514 |
/** Performance schema instrumented wrap function for |
| 515 |
rw_lock_x_lock_func_nowait() |
| 516 |
NOTE! Please use the corresponding macro, not directly this function! |
| 517 |
@param[in] lock pointer to rw-lock |
| 518 |
@param[in] location location where requested |
| 519 |
@return true if success */ |
| 520 |
[[nodiscard]] static inline bool pfs_rw_lock_x_lock_func_nowait( |
| 521 |
rw_lock_t *lock, ut::Location location); |
| 522 |
|
| 523 |
/** Performance schema instrumented wrap function for rw_lock_s_lock_func() |
| 524 |
NOTE! Please use the corresponding macro rw_lock_s_lock(), not directly this |
| 525 |
function! |
| 526 |
@param[in] lock pointer to rw-lock |
| 527 |
@param[in] pass pass value; != 0, if the lock will be passed |
| 528 |
to another thread to unlock |
| 529 |
@param[in] location location where requested */ |
| 530 |
static inline void pfs_rw_lock_s_lock_func(rw_lock_t *lock, ulint pass, |
| 531 |
ut::Location location); |
| 532 |
|
| 533 |
/** Performance schema instrumented wrap function for rw_lock_s_lock_func() |
| 534 |
NOTE! Please use the corresponding macro rw_lock_s_lock(), not directly this |
| 535 |
function! |
| 536 |
@param[in] lock pointer to rw-lock |
| 537 |
@param[in] pass pass value; != 0, if the lock will be passed |
| 538 |
to another thread to unlock |
| 539 |
@param[in] location location where requested |
| 540 |
@return true if success */ |
| 541 |
[[nodiscard]] static inline bool pfs_rw_lock_s_lock_low(rw_lock_t *lock, |
| 542 |
ulint pass, |
| 543 |
ut::Location location); |
| 544 |
|
| 545 |
/** Performance schema instrumented wrap function for rw_lock_x_lock_func() |
| 546 |
NOTE! Please use the corresponding macro rw_lock_x_lock(), not directly this |
| 547 |
function! |
| 548 |
@param[in] lock pointer to rw-lock |
| 549 |
@param[in] pass pass value; != 0, if the lock will be passed |
| 550 |
to another thread to unlock |
| 551 |
@param[in] location location where requested */ |
| 552 |
static inline void pfs_rw_lock_x_lock_func(rw_lock_t *lock, ulint pass, |
| 553 |
ut::Location location); |
| 554 |
|
| 555 |
/** Performance schema instrumented wrap function for rw_lock_s_unlock_func() |
| 556 |
NOTE! Please use the corresponding macro rw_lock_s_unlock(), not directly this |
| 557 |
function! |
| 558 |
@param[in] pass pass value; != 0, if the lock may have been passed to |
| 559 |
another thread to unlock |
| 560 |
@param[in,out] lock rw-lock */ |
| 561 |
static inline void pfs_rw_lock_s_unlock_func(IF_DEBUG(ulint pass, ) |
| 562 |
rw_lock_t *lock); |
| 563 |
|
| 564 |
/** Performance schema instrumented wrap function for rw_lock_x_unlock_func() |
| 565 |
NOTE! Please use the corresponding macro rw_lock_x_unlock(), not directly this |
| 566 |
function! |
| 567 |
@param[in] pass pass value; != 0, if the lock may have been passed to |
| 568 |
another thread to unlock |
| 569 |
@param[in,out] lock rw-lock */ |
| 570 |
static inline void pfs_rw_lock_x_unlock_func(IF_DEBUG(ulint pass, ) |
| 571 |
rw_lock_t *lock); |
| 572 |
|
| 573 |
/** Performance schema instrumented wrap function for rw_lock_sx_lock_func() |
| 574 |
NOTE! Please use the corresponding macro rw_lock_sx_lock(), not directly this |
| 575 |
function! |
| 576 |
@param[in] lock pointer to rw-lock |
| 577 |
@param[in] pass pass value; != 0, if the lock will be passed |
| 578 |
to another thread to unlock |
| 579 |
@param[in] location location where requested */ |
| 580 |
static inline void pfs_rw_lock_sx_lock_func(rw_lock_t *lock, ulint pass, |
| 581 |
ut::Location location); |
| 582 |
|
| 583 |
/** Performance schema instrumented wrap function for rw_lock_sx_lock_nowait() |
| 584 |
NOTE! Please use the corresponding macro, not directly this function! |
| 585 |
@param[in] lock pointer to rw-lock |
| 586 |
@param[in] pass pass value; != 0, if the lock will be passed |
| 587 |
to another thread to unlock |
| 588 |
@param[in] location location where requested */ |
| 589 |
[[nodiscard]] static inline bool pfs_rw_lock_sx_lock_low(rw_lock_t *lock, |
| 590 |
ulint pass, |
| 591 |
ut::Location location); |
| 592 |
|
| 593 |
/** Performance schema instrumented wrap function for rw_lock_sx_unlock_func() |
| 594 |
NOTE! Please use the corresponding macro rw_lock_sx_unlock(), not directly this |
| 595 |
function! |
| 596 |
@param[in] pass pass value; != 0, if the lock will be passed to |
| 597 |
another thread to unlock |
| 598 |
@param[in,out] lock pointer to rw-lock */ |
| 599 |
static inline void pfs_rw_lock_sx_unlock_func(IF_DEBUG(ulint pass, ) |
| 600 |
rw_lock_t *lock); |
| 601 |
|
| 602 |
/** Performance schema instrumented wrap function for rw_lock_free_func() |
| 603 |
NOTE! Please use the corresponding macro rw_lock_free(), not directly |
| 604 |
this function! */ |
| 605 |
static inline void pfs_rw_lock_free_func(rw_lock_t *lock); /*!< in: rw-lock */ |
| 606 |
#endif /* UNIV_PFS_RWLOCK */ |
| 607 |
|
| 608 |
#ifndef UNIV_PFS_RWLOCK |
| 609 |
/** Creates, or rather, initializes an rw-lock object in a specified memory |
| 610 |
location (which must be appropriately aligned). The rw-lock is initialized |
| 611 |
to the non-locked state. Explicit freeing of the rw-lock with rw_lock_free |
| 612 |
is necessary only if the memory block containing it is freed. |
| 613 |
if MySQL performance schema is enabled and "UNIV_PFS_RWLOCK" is |
| 614 |
defined, the rwlock are instrumented with performance schema probes. */ |
| 615 |
#ifdef UNIV_DEBUG |
| 616 |
#define rw_lock_create(K, L, ID) \ |
| 617 |
rw_lock_create_func((L), (ID), UT_LOCATION_HERE) |
| 618 |
#else /* UNIV_DEBUG */ |
| 619 |
#define rw_lock_create(K, L, ID) rw_lock_create_func((L), UT_LOCATION_HERE) |
| 620 |
#endif /* UNIV_DEBUG */ |
| 621 |
|
| 622 |
/** NOTE! The following macros should be used in rw locking and |
| 623 |
unlocking, not the corresponding function. */ |
| 624 |
|
| 625 |
static inline void rw_lock_s_lock(rw_lock_t *M, ut::Location L) { |
| 626 |
rw_lock_s_lock_func(M, 0, L); |
| 627 |
} |
| 628 |
|
| 629 |
static inline void rw_lock_s_lock_gen(rw_lock_t *M, ulint P, ut::Location L) { |
| 630 |
rw_lock_s_lock_func(M, P, L); |
| 631 |
} |
| 632 |
|
| 633 |
static inline bool rw_lock_s_lock_nowait(rw_lock_t *M, ut::Location L) { |
| 634 |
return rw_lock_s_lock_low(M, 0, L); |
| 635 |
} |
| 636 |
|
| 637 |
#ifdef UNIV_DEBUG |
| 638 |
static inline void rw_lock_s_unlock_gen(rw_lock_t *L, ulint P) { |
| 639 |
rw_lock_s_unlock_func(P, L); |
| 640 |
} |
| 641 |
#else |
| 642 |
static inline void rw_lock_s_unlock_gen(rw_lock_t *L, ulint P) { |
| 643 |
rw_lock_s_unlock_func(L); |
| 644 |
} |
| 645 |
#endif /* UNIV_DEBUG */ |
| 646 |
|
| 647 |
static inline void rw_lock_sx_lock(rw_lock_t *L, ut::Location Loc) { |
| 648 |
rw_lock_sx_lock_func(L, 0, Loc); |
| 649 |
} |
| 650 |
|
| 651 |
static inline void rw_lock_sx_lock_gen(rw_lock_t *M, ulint P, ut::Location L) { |
| 652 |
rw_lock_sx_lock_func(M, P, L); |
| 653 |
} |
| 654 |
|
| 655 |
static inline bool rw_lock_sx_lock_nowait(rw_lock_t *M, ulint P, |
| 656 |
ut::Location L) { |
| 657 |
return rw_lock_sx_lock_low(M, P, L); |
| 658 |
} |
| 659 |
|
| 660 |
#ifdef UNIV_DEBUG |
| 661 |
static inline void rw_lock_sx_unlock(rw_lock_t *L) { |
| 662 |
rw_lock_sx_unlock_func(0, L); |
| 663 |
} |
| 664 |
static inline void rw_lock_sx_unlock_gen(rw_lock_t *L, ulint P) { |
| 665 |
rw_lock_sx_unlock_func(P, L); |
| 666 |
} |
| 667 |
#else /* UNIV_DEBUG */ |
| 668 |
static inline void rw_lock_sx_unlock(rw_lock_t *L) { |
| 669 |
rw_lock_sx_unlock_func(L); |
| 670 |
} |
| 671 |
static inline void rw_lock_sx_unlock_gen(rw_lock_t *L, ulint P) { |
| 672 |
rw_lock_sx_unlock_func(L); |
| 673 |
} |
| 674 |
#endif /* UNIV_DEBUG */ |
| 675 |
|
| 676 |
static inline void rw_lock_x_lock(rw_lock_t *M, ut::Location L) { |
| 677 |
rw_lock_x_lock_func(M, 0, L); |
| 678 |
} |
| 679 |
|
| 680 |
static inline void rw_lock_x_lock_gen(rw_lock_t *M, ulint P, ut::Location L) { |
| 681 |
rw_lock_x_lock_func(M, P, L); |
| 682 |
} |
| 683 |
|
| 684 |
static inline bool rw_lock_x_lock_nowait(rw_lock_t *M, ut::Location L) { |
| 685 |
return rw_lock_x_lock_func_nowait(M, L); |
| 686 |
} |
| 687 |
|
| 688 |
#ifdef UNIV_DEBUG |
| 689 |
static inline void rw_lock_x_unlock_gen(rw_lock_t *L, ulint P) { |
| 690 |
rw_lock_x_unlock_func(P, L); |
| 691 |
} |
| 692 |
#else |
| 693 |
static inline void rw_lock_x_unlock_gen(rw_lock_t *L, ulint P) { |
| 694 |
rw_lock_x_unlock_func(L); |
| 695 |
} |
| 696 |
#endif |
| 697 |
|
| 698 |
#define rw_lock_free(M) rw_lock_free_func(M) |
| 699 |
|
| 700 |
#else /* !UNIV_PFS_RWLOCK */ |
| 701 |
|
| 702 |
/* Following macros point to Performance Schema instrumented functions. */ |
| 703 |
#ifdef UNIV_DEBUG |
| 704 |
#define rw_lock_create(K, L, ID) \ |
| 705 |
pfs_rw_lock_create_func((K), (L), (ID), UT_LOCATION_HERE) |
| 706 |
#else /* UNIV_DEBUG */ |
| 707 |
#define rw_lock_create(K, L, ID) \ |
| 708 |
pfs_rw_lock_create_func((K), (L), UT_LOCATION_HERE) |
| 709 |
#endif /* UNIV_DEBUG */ |
| 710 |
|
| 711 |
/****************************************************************** |
| 712 |
NOTE! The following macros should be used in rw locking and |
| 713 |
unlocking, not the corresponding function. */ |
| 714 |
|
| 715 |
static inline void rw_lock_s_lock(rw_lock_t *M, ut::Location L) { |
| 716 |
pfs_rw_lock_s_lock_func(M, 0, L); |
| 717 |
} |
| 718 |
|
| 719 |
static inline void rw_lock_s_lock_gen(rw_lock_t *M, ulint P, ut::Location L) { |
| 720 |
pfs_rw_lock_s_lock_func(M, P, L); |
| 721 |
} |
| 722 |
|
| 723 |
static inline bool rw_lock_s_lock_nowait(rw_lock_t *M, ut::Location L) { |
| 724 |
return pfs_rw_lock_s_lock_low(M, 0, L); |
| 725 |
} |
| 726 |
|
| 727 |
#ifdef UNIV_DEBUG |
| 728 |
static inline void rw_lock_s_unlock_gen(rw_lock_t *L, ulint P) { |
| 729 |
pfs_rw_lock_s_unlock_func(P, L); |
| 730 |
} |
| 731 |
#else |
| 732 |
static inline void rw_lock_s_unlock_gen(rw_lock_t *L, ulint P) { |
| 733 |
pfs_rw_lock_s_unlock_func(L); |
| 734 |
} |
| 735 |
#endif |
| 736 |
|
| 737 |
static inline void rw_lock_sx_lock(rw_lock_t *M, ut::Location L) { |
| 738 |
pfs_rw_lock_sx_lock_func(M, 0, L); |
| 739 |
} |
| 740 |
|
| 741 |
static inline void rw_lock_sx_lock_gen(rw_lock_t *M, ulint P, ut::Location L) { |
| 742 |
pfs_rw_lock_sx_lock_func(M, P, L); |
| 743 |
} |
| 744 |
|
| 745 |
static inline bool rw_lock_sx_lock_nowait(rw_lock_t *M, ulint P, |
| 746 |
ut::Location L) { |
| 747 |
return pfs_rw_lock_sx_lock_low(M, P, L); |
| 748 |
} |
| 749 |
|
| 750 |
#ifdef UNIV_DEBUG |
| 751 |
static inline void rw_lock_sx_unlock(rw_lock_t *L) { |
| 752 |
pfs_rw_lock_sx_unlock_func(0, L); |
| 753 |
} |
| 754 |
static inline void rw_lock_sx_unlock_gen(rw_lock_t *L, ulint P) { |
| 755 |
pfs_rw_lock_sx_unlock_func(P, L); |
| 756 |
} |
| 757 |
#else |
| 758 |
static inline void rw_lock_sx_unlock(rw_lock_t *L) { |
| 759 |
pfs_rw_lock_sx_unlock_func(L); |
| 760 |
} |
| 761 |
static inline void rw_lock_sx_unlock_gen(rw_lock_t *L, ulint P) { |
| 762 |
pfs_rw_lock_sx_unlock_func(L); |
| 763 |
} |
| 764 |
#endif |
| 765 |
|
| 766 |
static inline void rw_lock_x_lock(rw_lock_t *M, ut::Location L) { |
| 767 |
pfs_rw_lock_x_lock_func(M, 0, L); |
| 768 |
} |
| 769 |
|
| 770 |
static inline void rw_lock_x_lock_gen(rw_lock_t *M, ulint P, ut::Location L) { |
| 771 |
pfs_rw_lock_x_lock_func(M, P, L); |
| 772 |
} |
| 773 |
|
| 774 |
static inline bool rw_lock_x_lock_nowait(rw_lock_t *M, ut::Location L) { |
| 775 |
return pfs_rw_lock_x_lock_func_nowait(M, L); |
| 776 |
} |
| 777 |
|
| 778 |
#ifdef UNIV_DEBUG |
| 779 |
static inline void rw_lock_x_unlock_gen(rw_lock_t *L, ulint P) { |
| 780 |
pfs_rw_lock_x_unlock_func(P, L); |
| 781 |
} |
| 782 |
#else |
| 783 |
static inline void rw_lock_x_unlock_gen(rw_lock_t *L, ulint P) { |
| 784 |
pfs_rw_lock_x_unlock_func(L); |
| 785 |
} |
| 786 |
#endif |
| 787 |
|
| 788 |
static inline void rw_lock_free(rw_lock_t *M) { pfs_rw_lock_free_func(M); } |
| 789 |
|
| 790 |
#endif /* !UNIV_PFS_RWLOCK */ |
| 791 |
|
| 792 |
static inline void rw_lock_s_unlock(rw_lock_t *L) { |
| 793 |
rw_lock_s_unlock_gen(L, 0); |
| 794 |
} |
| 795 |
static inline void rw_lock_x_unlock(rw_lock_t *L) { |
| 796 |
rw_lock_x_unlock_gen(L, 0); |
| 797 |
} |
| 798 |
|
| 799 |
#include "sync0rw.ic" |
| 800 |
|
| 801 |
#endif /* !UNIV_HOTBACKUP */ |
| 802 |
|
| 803 |
#endif /* !UNIV_LIBRARY */ |
| 804 |
typedef UT_LIST_BASE_NODE_T(rw_lock_t, list) rw_lock_list_t; |
| 805 |
|
| 806 |
extern rw_lock_list_t rw_lock_list; |
| 807 |
|
| 808 |
#endif /* sync0rw.h */ |