| 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.ic |
| 36 |
The read-write lock (for threads) |
| 37 |
|
| 38 |
Created 9/11/1995 Heikki Tuuri |
| 39 |
*******************************************************/ |
| 40 |
|
| 41 |
#include "os0event.h" |
| 42 |
|
| 43 |
/** Lock an rw-lock in shared mode for the current thread. If the rw-lock is |
| 44 |
locked in exclusive mode, or there is an exclusive lock request waiting, |
| 45 |
the function spins a preset time (controlled by srv_n_spin_wait_rounds), |
| 46 |
waiting for the lock before suspending the thread. |
| 47 |
@param[in] lock pointer to rw-lock |
| 48 |
@param[in] pass pass value; !=0, if the lock will be passed to another thread |
| 49 |
to unlock |
| 50 |
@param[in] location location where requested */ |
| 51 |
void rw_lock_s_lock_spin(rw_lock_t *lock, ulint pass, ut::Location location); |
| 52 |
#ifdef UNIV_DEBUG |
| 53 |
/** Inserts the debug information for an rw-lock. |
| 54 |
@param[in] lock rw-lock |
| 55 |
@param[in] pass pass value |
| 56 |
@param[in] lock_type lock type |
| 57 |
@param[in] location location where requested */ |
| 58 |
void rw_lock_add_debug_info(rw_lock_t *lock, ulint pass, ulint lock_type, |
| 59 |
ut::Location location); |
| 60 |
/** Removes a debug information struct for an rw-lock. */ |
| 61 |
void rw_lock_remove_debug_info(rw_lock_t *lock, /*!< in: rw-lock */ |
| 62 |
ulint pass, /*!< in: pass value */ |
| 63 |
ulint lock_type); /*!< in: lock type */ |
| 64 |
#endif /* UNIV_DEBUG */ |
| 65 |
|
| 66 |
/** Check if there are threads waiting for the rw-lock. |
| 67 |
@return true if waiters, false otherwise */ |
| 68 |
static inline bool rw_lock_get_waiters( |
| 69 |
const rw_lock_t *lock) /*!< in: rw-lock */ |
| 70 |
{ |
| 71 |
return lock->waiters.load(); |
| 72 |
} |
| 73 |
|
| 74 |
/** Sets lock->waiters to true. It is not an error if lock->waiters is already |
| 75 |
true. */ |
| 76 |
static inline void rw_lock_set_waiter_flag( |
| 77 |
rw_lock_t *lock) /*!< in/out: rw-lock */ |
| 78 |
{ |
| 79 |
bool zero = false; |
| 80 |
lock->waiters.compare_exchange_strong(zero, true); |
| 81 |
} |
| 82 |
|
| 83 |
/** Resets lock->waiters to false. It is not an error if lock->waiters is |
| 84 |
already false. */ |
| 85 |
static inline void rw_lock_reset_waiter_flag( |
| 86 |
rw_lock_t *lock) /*!< in/out: rw-lock */ |
| 87 |
{ |
| 88 |
bool one = true; |
| 89 |
lock->waiters.compare_exchange_strong(one, false); |
| 90 |
} |
| 91 |
|
| 92 |
/** Returns the write-status of the lock - this function made more sense |
| 93 |
with the old rw_lock implementation. |
| 94 |
@return RW_LOCK_NOT_LOCKED, RW_LOCK_X, RW_LOCK_X_WAIT, RW_LOCK_SX */ |
| 95 |
static inline ulint rw_lock_get_writer( |
| 96 |
const rw_lock_t *lock) /*!< in: rw-lock */ |
| 97 |
{ |
| 98 |
lint lock_word = lock->lock_word; |
| 99 |
|
| 100 |
ut_ad(lock_word <= X_LOCK_DECR); |
| 101 |
if (lock_word > X_LOCK_HALF_DECR) { |
| 102 |
/* return NOT_LOCKED in s-lock state, like the writer |
| 103 |
member of the old lock implementation. */ |
| 104 |
return (RW_LOCK_NOT_LOCKED); |
| 105 |
} else if (lock_word > 0) { |
| 106 |
/* sx-locked, no x-locks */ |
| 107 |
return (RW_LOCK_SX); |
| 108 |
} else if (lock_word == 0 || lock_word == -X_LOCK_HALF_DECR || |
| 109 |
lock_word <= -X_LOCK_DECR) { |
| 110 |
/* x-lock with sx-lock is also treated as RW_LOCK_EX */ |
| 111 |
return (RW_LOCK_X); |
| 112 |
} else { |
| 113 |
/* x-waiter with sx-lock is also treated as RW_LOCK_WAIT_EX |
| 114 |
e.g. -X_LOCK_HALF_DECR < lock_word < 0 : without sx |
| 115 |
-X_LOCK_DECR < lock_word < -X_LOCK_HALF_DECR : with sx */ |
| 116 |
return (RW_LOCK_X_WAIT); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** Returns the number of readers (s-locks). |
| 121 |
@return number of readers */ |
| 122 |
static inline ulint rw_lock_get_reader_count( |
| 123 |
const rw_lock_t *lock) /*!< in: rw-lock */ |
| 124 |
{ |
| 125 |
lint lock_word = lock->lock_word; |
| 126 |
ut_ad(lock_word <= X_LOCK_DECR); |
| 127 |
|
| 128 |
if (lock_word > X_LOCK_HALF_DECR) { |
| 129 |
/* s-locked, no x-waiter */ |
| 130 |
return (X_LOCK_DECR - lock_word); |
| 131 |
} else if (lock_word > 0) { |
| 132 |
/* s-locked, with sx-locks only */ |
| 133 |
return (X_LOCK_HALF_DECR - lock_word); |
| 134 |
} else if (lock_word == 0) { |
| 135 |
/* x-locked */ |
| 136 |
return (0); |
| 137 |
} else if (lock_word > -X_LOCK_HALF_DECR) { |
| 138 |
/* s-locked, with x-waiter */ |
| 139 |
return ((ulint)(-lock_word)); |
| 140 |
} else if (lock_word == -X_LOCK_HALF_DECR) { |
| 141 |
/* x-locked with sx-locks */ |
| 142 |
return (0); |
| 143 |
} else if (lock_word > -X_LOCK_DECR) { |
| 144 |
/* s-locked, with x-waiter and sx-lock */ |
| 145 |
return ((ulint)(-(lock_word + X_LOCK_HALF_DECR))); |
| 146 |
} |
| 147 |
/* no s-locks */ |
| 148 |
return (0); |
| 149 |
} |
| 150 |
|
| 151 |
/** Returns the value of writer_count for the lock. Does not reserve the lock |
| 152 |
mutex, so the caller must be sure it is not changed during the call. |
| 153 |
@return value of writer_count */ |
| 154 |
static inline ulint rw_lock_get_x_lock_count( |
| 155 |
const rw_lock_t *lock) /*!< in: rw-lock */ |
| 156 |
{ |
| 157 |
lint lock_copy = lock->lock_word; |
| 158 |
ut_ad(lock_copy <= X_LOCK_DECR); |
| 159 |
|
| 160 |
if (lock_copy == 0 || lock_copy == -X_LOCK_HALF_DECR) { |
| 161 |
/* "1 x-lock" or "1 x-lock + sx-locks" */ |
| 162 |
return (1); |
| 163 |
} else if (lock_copy > -X_LOCK_DECR) { |
| 164 |
/* s-locks, one or more sx-locks if > 0, or x-waiter if < 0 */ |
| 165 |
return (0); |
| 166 |
} else if (lock_copy > -(X_LOCK_DECR + X_LOCK_HALF_DECR)) { |
| 167 |
/* no s-lock, no sx-lock, 2 or more x-locks. |
| 168 |
First 2 x-locks are set with -X_LOCK_DECR, |
| 169 |
all other recursive x-locks are set with -1 */ |
| 170 |
return (2 - (lock_copy + X_LOCK_DECR)); |
| 171 |
} else { |
| 172 |
/* no s-lock, 1 or more sx-lock, 2 or more x-locks. |
| 173 |
First 2 x-locks are set with -(X_LOCK_DECR + X_LOCK_HALF_DECR), |
| 174 |
all other recursive x-locks are set with -1 */ |
| 175 |
return (2 - (lock_copy + X_LOCK_DECR + X_LOCK_HALF_DECR)); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/** Returns the number of sx-lock for the lock. Does not reserve the lock |
| 180 |
mutex, so the caller must be sure it is not changed during the call. |
| 181 |
@return value of sx-lock count */ |
| 182 |
static inline ulint rw_lock_get_sx_lock_count( |
| 183 |
const rw_lock_t *lock) /*!< in: rw-lock */ |
| 184 |
{ |
| 185 |
#ifdef UNIV_DEBUG |
| 186 |
lint lock_copy = lock->lock_word; |
| 187 |
|
| 188 |
ut_ad(lock_copy <= X_LOCK_DECR); |
| 189 |
|
| 190 |
while (lock_copy < 0) { |
| 191 |
lock_copy += X_LOCK_DECR; |
| 192 |
} |
| 193 |
|
| 194 |
if (lock_copy > 0 && lock_copy <= X_LOCK_HALF_DECR) { |
| 195 |
return (lock->sx_recursive); |
| 196 |
} |
| 197 |
|
| 198 |
return (0); |
| 199 |
#else /* UNIV_DEBUG */ |
| 200 |
return (lock->sx_recursive); |
| 201 |
#endif /* UNIV_DEBUG */ |
| 202 |
} |
| 203 |
|
| 204 |
/** Two different implementations for decrementing the lock_word of a rw_lock: |
| 205 |
one for systems supporting atomic operations, one for others. This does |
| 206 |
does not support recursive x-locks: they should be handled by the caller and |
| 207 |
need not be atomic since they are performed by the current lock holder. |
| 208 |
Returns true if the decrement was made, false if not. |
| 209 |
@return true if decr occurs */ |
| 210 |
ALWAYS_INLINE |
| 211 |
bool rw_lock_lock_word_decr(rw_lock_t *lock, /*!< in/out: rw-lock */ |
| 212 |
ulint amount, /*!< in: amount to decrement */ |
| 213 |
lint threshold) /*!< in: threshold of judgement */ |
| 214 |
{ |
| 215 |
int32_t local_lock_word; |
| 216 |
|
| 217 |
os_rmb; |
| 218 |
local_lock_word = lock->lock_word; |
| 219 |
while (local_lock_word > threshold) { |
| 220 |
if (lock->lock_word.compare_exchange_strong(local_lock_word, |
| 221 |
local_lock_word - amount)) { |
| 222 |
return (true); |
| 223 |
} |
| 224 |
} |
| 225 |
return (false); |
| 226 |
} |
| 227 |
|
| 228 |
/** Increments lock_word the specified amount and returns new value. |
| 229 |
@return lock->lock_word after increment */ |
| 230 |
static inline lint rw_lock_lock_word_incr( |
| 231 |
rw_lock_t *lock, /*!< in/out: rw-lock */ |
| 232 |
ulint amount) /*!< in: amount of increment */ |
| 233 |
{ |
| 234 |
return (lock->lock_word.fetch_add(amount) + amount); |
| 235 |
} |
| 236 |
|
| 237 |
static inline void rw_lock_set_writer_id_and_recursion_flag(rw_lock_t *lock, |
| 238 |
bool recursive) { |
| 239 |
lock->writer_thread.store(std::this_thread::get_id(), |
| 240 |
std::memory_order_relaxed); |
| 241 |
lock->recursive.store(recursive, std::memory_order_release); |
| 242 |
} |
| 243 |
|
| 244 |
ALWAYS_INLINE |
| 245 |
bool rw_lock_s_lock_low(rw_lock_t *lock, ulint pass [[maybe_unused]], |
| 246 |
ut::Location location) { |
| 247 |
if (!rw_lock_lock_word_decr(lock, 1, 0)) { |
| 248 |
/* Locking did not succeed */ |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
ut_d(rw_lock_add_debug_info(lock, pass, RW_LOCK_S, location)); |
| 253 |
|
| 254 |
/* These debugging values are not set safely: they may be incorrect |
| 255 |
or even refer to a line that is invalid for the file name. */ |
| 256 |
lock->last_s_file_name = location.filename; |
| 257 |
/* last_s_line is uint16_t */ |
| 258 |
ut_ad(location.line <= |
| 259 |
std::numeric_limits<decltype(lock->last_s_line)>::max()); |
| 260 |
lock->last_s_line = location.line; |
| 261 |
lock->reader_thread.xor_thing(std::this_thread::get_id()); |
| 262 |
|
| 263 |
return true; /* locking succeeded */ |
| 264 |
} |
| 265 |
|
| 266 |
/** NOTE! Use the corresponding macro, not directly this function! Lock an |
| 267 |
rw-lock in shared mode for the current thread. If the rw-lock is locked |
| 268 |
in exclusive mode, or there is an exclusive lock request waiting, the |
| 269 |
function spins a preset time (controlled by srv_n_spin_wait_rounds), waiting |
| 270 |
for the lock, before suspending the thread. |
| 271 |
@param[in] lock pointer to rw-lock |
| 272 |
@param[in] pass pass value; != 0, if the lock will be passed to another thread |
| 273 |
to unlock |
| 274 |
@param[in] location location where requested*/ |
| 275 |
static inline void rw_lock_s_lock_func(rw_lock_t *lock, ulint pass, |
| 276 |
ut::Location location) { |
| 277 |
/* NOTE: As we do not know the thread ids for threads which have |
| 278 |
s-locked a latch, and s-lockers will be served only after waiting |
| 279 |
x-lock requests have been fulfilled, then if this thread already |
| 280 |
owns an s-lock here, it may end up in a deadlock with another thread |
| 281 |
which requests an x-lock here. Therefore, we will forbid recursive |
| 282 |
s-locking of a latch: the following assert will warn the programmer |
| 283 |
of the possibility of this kind of a deadlock. If we want to implement |
| 284 |
safe recursive s-locking, we should keep in a list the thread ids of |
| 285 |
the threads which have s-locked a latch. This would use some CPU |
| 286 |
time. */ |
| 287 |
|
| 288 |
ut_ad(!rw_lock_own(lock, RW_LOCK_S)); /* see NOTE above */ |
| 289 |
ut_ad(!rw_lock_own(lock, RW_LOCK_X)); |
| 290 |
|
| 291 |
if (!rw_lock_s_lock_low(lock, pass, location)) { |
| 292 |
/* Did not succeed, try spin wait */ |
| 293 |
|
| 294 |
rw_lock_s_lock_spin(lock, pass, location); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
/** NOTE! Use the corresponding macro, not directly this function! Lock an |
| 299 |
rw-lock in exclusive mode for the current thread if the lock can be |
| 300 |
obtained immediately. |
| 301 |
@param[in] lock pointer to rw-lock |
| 302 |
@param[in] location location where requested |
| 303 |
@return true if success */ |
| 304 |
static inline bool rw_lock_x_lock_func_nowait(rw_lock_t *lock, |
| 305 |
ut::Location location) { |
| 306 |
int32_t x_lock_decr = X_LOCK_DECR; |
| 307 |
|
| 308 |
if (lock->lock_word.compare_exchange_strong(x_lock_decr, 0)) { |
| 309 |
rw_lock_set_writer_id_and_recursion_flag(lock, true); |
| 310 |
} else if (lock->recursive.load(std::memory_order_acquire) && |
| 311 |
lock->writer_thread.load(std::memory_order_relaxed) == |
| 312 |
std::this_thread::get_id()) { |
| 313 |
/* Relock: this lock_word modification is safe since no other |
| 314 |
threads can modify (lock, unlock, or reserve) lock_word while |
| 315 |
there is an exclusive writer and this is the writer thread. */ |
| 316 |
if (lock->lock_word == 0 || lock->lock_word == -X_LOCK_HALF_DECR) { |
| 317 |
/* There are 1 x-locks */ |
| 318 |
lock->lock_word -= X_LOCK_DECR; |
| 319 |
} else if (lock->lock_word <= -X_LOCK_DECR) { |
| 320 |
/* There are 2 or more x-locks */ |
| 321 |
lock->lock_word--; |
| 322 |
} else { |
| 323 |
/* Failure */ |
| 324 |
return false; |
| 325 |
} |
| 326 |
|
| 327 |
/* Watch for too many recursive locks */ |
| 328 |
ut_ad(lock->lock_word < 0); |
| 329 |
} else { |
| 330 |
/* Failure */ |
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
ut_d(rw_lock_add_debug_info(lock, 0, RW_LOCK_X, location)); |
| 335 |
|
| 336 |
lock->last_x_file_name = location.filename; |
| 337 |
/* last_x_line is uint16_t */ |
| 338 |
ut_ad(location.line <= |
| 339 |
std::numeric_limits<decltype(lock->last_x_line)>::max()); |
| 340 |
lock->last_x_line = location.line; |
| 341 |
|
| 342 |
ut_ad(rw_lock_validate(lock)); |
| 343 |
|
| 344 |
return true; |
| 345 |
} |
| 346 |
|
| 347 |
/** Releases a shared mode lock. |
| 348 |
@param[in] pass pass value; != 0, if the lock may have been passed to another |
| 349 |
thread to unlock |
| 350 |
@param[in,out] lock rw-lock */ |
| 351 |
static inline void rw_lock_s_unlock_func(IF_DEBUG(ulint pass, ) |
| 352 |
rw_lock_t *lock) { |
| 353 |
ut_ad(lock->lock_word > -X_LOCK_DECR); |
| 354 |
ut_ad(lock->lock_word != 0); |
| 355 |
ut_ad(lock->lock_word < X_LOCK_DECR); |
| 356 |
lock->reader_thread.xor_thing(std::this_thread::get_id()); |
| 357 |
|
| 358 |
ut_d(rw_lock_remove_debug_info(lock, pass, RW_LOCK_S)); |
| 359 |
|
| 360 |
/* Increment lock_word to indicate 1 less reader */ |
| 361 |
lint lock_word = rw_lock_lock_word_incr(lock, 1); |
| 362 |
if (lock_word == 0 || lock_word == -X_LOCK_HALF_DECR) { |
| 363 |
/* wait_ex waiter exists. It may not be asleep, but we signal |
| 364 |
anyway. We do not wake other waiters, because they can't |
| 365 |
exist without wait_ex waiter and wait_ex waiter goes first.*/ |
| 366 |
os_event_set(lock->wait_ex_event); |
| 367 |
sync_array_object_signalled(); |
| 368 |
} |
| 369 |
|
| 370 |
ut_ad(rw_lock_validate(lock)); |
| 371 |
} |
| 372 |
|
| 373 |
/** Releases an exclusive mode lock. |
| 374 |
@param[in] pass pass value; != 0, if the lock may have been passed to |
| 375 |
another thread to unlock |
| 376 |
@param[in,out] lock lock rw-lock */ |
| 377 |
static inline void rw_lock_x_unlock_func(IF_DEBUG(ulint pass, ) |
| 378 |
rw_lock_t *lock) { |
| 379 |
ut_ad(lock->lock_word == 0 || lock->lock_word == -X_LOCK_HALF_DECR || |
| 380 |
lock->lock_word <= -X_LOCK_DECR); |
| 381 |
|
| 382 |
/* lock->recursive == true implies that the lock->writer_thread is the |
| 383 |
current writer. If we are the last of the recursive callers then we must unset |
| 384 |
lock->recursive flag to indicate that the lock->writer_thread is now |
| 385 |
stale. Otherwise if our thread tried to reacquire the lock it would wrongly |
| 386 |
believe it already has it. |
| 387 |
Note that since we still hold the x-lock we can safely read the lock_word. */ |
| 388 |
if (lock->lock_word == 0) { |
| 389 |
/* Last caller in a possible recursive chain. */ |
| 390 |
lock->recursive.store(false, std::memory_order_relaxed); |
| 391 |
} |
| 392 |
|
| 393 |
ut_d(rw_lock_remove_debug_info(lock, pass, RW_LOCK_X)); |
| 394 |
|
| 395 |
if (lock->lock_word == 0 || lock->lock_word == -X_LOCK_HALF_DECR) { |
| 396 |
/* There is 1 x-lock */ |
| 397 |
/* atomic increment is needed, because it is last */ |
| 398 |
if (rw_lock_lock_word_incr(lock, X_LOCK_DECR) <= 0) { |
| 399 |
ut_error; |
| 400 |
} |
| 401 |
|
| 402 |
/* This no longer has an X-lock but it may still have |
| 403 |
an SX-lock. So it is now free for S-locks by other threads. |
| 404 |
We need to signal read/write waiters. |
| 405 |
We do not need to signal wait_ex waiters, since they cannot |
| 406 |
exist when there is a writer. */ |
| 407 |
if (lock->waiters) { |
| 408 |
rw_lock_reset_waiter_flag(lock); |
| 409 |
os_event_set(lock->event); |
| 410 |
sync_array_object_signalled(); |
| 411 |
} |
| 412 |
} else if (lock->lock_word == -X_LOCK_DECR || |
| 413 |
lock->lock_word == -(X_LOCK_DECR + X_LOCK_HALF_DECR)) { |
| 414 |
/* There are 2 x-locks */ |
| 415 |
lock->lock_word += X_LOCK_DECR; |
| 416 |
} else { |
| 417 |
/* There are more than 2 x-locks. */ |
| 418 |
ut_ad(lock->lock_word < -X_LOCK_DECR); |
| 419 |
lock->lock_word += 1; |
| 420 |
} |
| 421 |
|
| 422 |
ut_ad(rw_lock_validate(lock)); |
| 423 |
} |
| 424 |
|
| 425 |
/** Releases a sx mode lock. |
| 426 |
@param[in] pass pass value; != 0, if the lock may have been passed to another |
| 427 |
thread to unlock |
| 428 |
@param[in,out] lock rw-lock */ |
| 429 |
static inline void rw_lock_sx_unlock_func(IF_DEBUG(ulint pass, ) |
| 430 |
rw_lock_t *lock) { |
| 431 |
ut_ad(rw_lock_get_sx_lock_count(lock)); |
| 432 |
ut_ad(lock->sx_recursive > 0); |
| 433 |
|
| 434 |
--lock->sx_recursive; |
| 435 |
|
| 436 |
ut_d(rw_lock_remove_debug_info(lock, pass, RW_LOCK_SX)); |
| 437 |
|
| 438 |
if (lock->sx_recursive == 0) { |
| 439 |
/* Last caller in a possible recursive chain. */ |
| 440 |
if (lock->lock_word > 0) { |
| 441 |
lock->recursive.store(false, std::memory_order_relaxed); |
| 442 |
|
| 443 |
if (rw_lock_lock_word_incr(lock, X_LOCK_HALF_DECR) <= X_LOCK_HALF_DECR) { |
| 444 |
ut_error; |
| 445 |
} |
| 446 |
/* Lock is now free. May have to signal read/write |
| 447 |
waiters. We do not need to signal wait_ex waiters, |
| 448 |
since they cannot exist when there is an sx-lock |
| 449 |
holder. */ |
| 450 |
if (lock->waiters) { |
| 451 |
rw_lock_reset_waiter_flag(lock); |
| 452 |
os_event_set(lock->event); |
| 453 |
sync_array_object_signalled(); |
| 454 |
} |
| 455 |
} else { |
| 456 |
/* still has x-lock */ |
| 457 |
ut_ad(lock->lock_word == -X_LOCK_HALF_DECR || |
| 458 |
lock->lock_word <= -(X_LOCK_DECR + X_LOCK_HALF_DECR)); |
| 459 |
lock->lock_word += X_LOCK_HALF_DECR; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
ut_ad(rw_lock_validate(lock)); |
| 464 |
} |
| 465 |
|
| 466 |
#ifdef UNIV_PFS_RWLOCK |
| 467 |
|
| 468 |
static inline void pfs_rw_lock_create_func(mysql_pfs_key_t key, rw_lock_t *lock, |
| 469 |
IF_DEBUG(latch_id_t id, ) |
| 470 |
ut::Location clocation) { |
| 471 |
new (lock) rw_lock_t{}; |
| 472 |
|
| 473 |
/* Initialize the rwlock for performance schema */ |
| 474 |
lock->pfs_psi = PSI_RWLOCK_CALL(init_rwlock)(key.m_value, lock); |
| 475 |
|
| 476 |
/* The actual function to initialize an rwlock */ |
| 477 |
rw_lock_create_func(lock, IF_DEBUG(id, ) clocation); |
| 478 |
} |
| 479 |
/** Performance schema instrumented wrap function for rw_lock_x_lock_func() |
| 480 |
NOTE! Please use the corresponding macro rw_lock_x_lock(), not directly |
| 481 |
this function! |
| 482 |
@param[in] lock pointer to rw-lock |
| 483 |
@param[in] pass pass value; != 0, if the lock will be passed to another thread |
| 484 |
to unlock |
| 485 |
@param[in] location location where requested */ |
| 486 |
static inline void pfs_rw_lock_x_lock_func(rw_lock_t *lock, ulint pass, |
| 487 |
ut::Location location) { |
| 488 |
if (lock->pfs_psi != nullptr) { |
| 489 |
if (lock->pfs_psi->m_enabled) { |
| 490 |
PSI_rwlock_locker *locker; |
| 491 |
PSI_rwlock_locker_state state; |
| 492 |
|
| 493 |
/* Record the acquisition of a read-write lock in exclusive |
| 494 |
mode in performance schema */ |
| 495 |
|
| 496 |
locker = PSI_RWLOCK_CALL(start_rwlock_wrwait)( |
| 497 |
&state, lock->pfs_psi, PSI_RWLOCK_EXCLUSIVELOCK, location.filename, |
| 498 |
static_cast<uint>(location.line)); |
| 499 |
|
| 500 |
rw_lock_x_lock_func(lock, pass, location); |
| 501 |
|
| 502 |
if (locker != nullptr) { |
| 503 |
PSI_RWLOCK_CALL(end_rwlock_wrwait)(locker, 0); |
| 504 |
} |
| 505 |
return; |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
rw_lock_x_lock_func(lock, pass, location); |
| 510 |
} |
| 511 |
/** Performance schema instrumented wrap function for |
| 512 |
rw_lock_x_lock_func_nowait() |
| 513 |
NOTE! Please use the corresponding macro rw_lock_x_lock_func(), |
| 514 |
not directly this function! |
| 515 |
@return true if success */ |
| 516 |
[[nodiscard]] static inline bool pfs_rw_lock_x_lock_func_nowait( |
| 517 |
rw_lock_t *lock, /*!< in: pointer to rw-lock */ |
| 518 |
ut::Location location) /*!< in: line where requested */ |
| 519 |
{ |
| 520 |
bool ret; |
| 521 |
|
| 522 |
if (lock->pfs_psi != nullptr) { |
| 523 |
if (lock->pfs_psi->m_enabled) { |
| 524 |
PSI_rwlock_locker *locker; |
| 525 |
PSI_rwlock_locker_state state; |
| 526 |
|
| 527 |
/* Record the acquisition of a read-write trylock in exclusive |
| 528 |
mode in performance schema */ |
| 529 |
|
| 530 |
locker = PSI_RWLOCK_CALL(start_rwlock_wrwait)( |
| 531 |
&state, lock->pfs_psi, PSI_RWLOCK_TRYEXCLUSIVELOCK, location.filename, |
| 532 |
static_cast<uint>(location.line)); |
| 533 |
|
| 534 |
ret = rw_lock_x_lock_func_nowait(lock, location); |
| 535 |
|
| 536 |
if (locker != nullptr) { |
| 537 |
const int rc = (ret ? 0 : 1); |
| 538 |
PSI_RWLOCK_CALL(end_rwlock_wrwait)(locker, rc); |
| 539 |
} |
| 540 |
|
| 541 |
return ret; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
ret = rw_lock_x_lock_func_nowait(lock, location); |
| 546 |
|
| 547 |
return ret; |
| 548 |
} |
| 549 |
/** Performance schema instrumented wrap function for rw_lock_free_func() |
| 550 |
NOTE! Please use the corresponding macro rw_lock_free(), not directly |
| 551 |
this function! */ |
| 552 |
static inline void pfs_rw_lock_free_func( |
| 553 |
rw_lock_t *lock) /*!< in: pointer to rw-lock */ |
| 554 |
{ |
| 555 |
if (lock->pfs_psi != nullptr) { |
| 556 |
PSI_RWLOCK_CALL(destroy_rwlock)(lock->pfs_psi); |
| 557 |
lock->pfs_psi = nullptr; |
| 558 |
} |
| 559 |
|
| 560 |
rw_lock_free_func(lock); |
| 561 |
} |
| 562 |
/** Performance schema instrumented wrap function for rw_lock_s_lock_func() |
| 563 |
NOTE! Please use the corresponding macro rw_lock_s_lock(), not |
| 564 |
directly this function! |
| 565 |
@param[in] lock pointer to rw-lock |
| 566 |
@param[in] pass pass value; != 0, if the lock will be passed to another thread |
| 567 |
to unlock |
| 568 |
@param[in] location location where requested */ |
| 569 |
ALWAYS_INLINE |
| 570 |
void pfs_rw_lock_s_lock_func(rw_lock_t *lock, ulint pass, |
| 571 |
ut::Location location) { |
| 572 |
if (lock->pfs_psi != nullptr) { |
| 573 |
if (lock->pfs_psi->m_enabled) { |
| 574 |
PSI_rwlock_locker *locker; |
| 575 |
PSI_rwlock_locker_state state; |
| 576 |
|
| 577 |
/* Instrumented to inform we are acquiring a shared rwlock */ |
| 578 |
locker = PSI_RWLOCK_CALL(start_rwlock_rdwait)( |
| 579 |
&state, lock->pfs_psi, PSI_RWLOCK_SHAREDLOCK, location.filename, |
| 580 |
static_cast<uint>(location.line)); |
| 581 |
|
| 582 |
rw_lock_s_lock_func(lock, pass, location); |
| 583 |
|
| 584 |
if (locker != nullptr) { |
| 585 |
PSI_RWLOCK_CALL(end_rwlock_rdwait)(locker, 0); |
| 586 |
} |
| 587 |
|
| 588 |
return; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
rw_lock_s_lock_func(lock, pass, location); |
| 593 |
} |
| 594 |
/** Performance schema instrumented wrap function for rw_lock_sx_lock_func() |
| 595 |
NOTE! Please use the corresponding macro rw_lock_sx_lock(), not |
| 596 |
directly this function! |
| 597 |
@param[in] lock pointer to rw-lock |
| 598 |
@param[in] pass pass value; != 0, if the lock will be passed to another thread |
| 599 |
to unlock |
| 600 |
@param[in] location location where requested */ |
| 601 |
static inline void pfs_rw_lock_sx_lock_func(rw_lock_t *lock, ulint pass, |
| 602 |
ut::Location location) { |
| 603 |
if (lock->pfs_psi != nullptr) { |
| 604 |
if (lock->pfs_psi->m_enabled) { |
| 605 |
PSI_rwlock_locker *locker; |
| 606 |
PSI_rwlock_locker_state state; |
| 607 |
|
| 608 |
/* Instrumented to inform we are acquiring a shared rwlock */ |
| 609 |
locker = PSI_RWLOCK_CALL(start_rwlock_wrwait)( |
| 610 |
&state, lock->pfs_psi, PSI_RWLOCK_SHAREDEXCLUSIVELOCK, |
| 611 |
location.filename, static_cast<uint>(location.line)); |
| 612 |
|
| 613 |
rw_lock_sx_lock_func(lock, pass, location); |
| 614 |
|
| 615 |
if (locker != nullptr) { |
| 616 |
PSI_RWLOCK_CALL(end_rwlock_wrwait)(locker, 0); |
| 617 |
} |
| 618 |
|
| 619 |
return; |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
rw_lock_sx_lock_func(lock, pass, location); |
| 624 |
} |
| 625 |
[[nodiscard]] static inline bool pfs_rw_lock_s_lock_low(rw_lock_t *lock, |
| 626 |
ulint pass, |
| 627 |
ut::Location location) { |
| 628 |
bool ret; |
| 629 |
|
| 630 |
if (lock->pfs_psi != nullptr) { |
| 631 |
if (lock->pfs_psi->m_enabled) { |
| 632 |
PSI_rwlock_locker *locker; |
| 633 |
PSI_rwlock_locker_state state; |
| 634 |
|
| 635 |
/* Instrumented to inform we are acquiring a shared rwlock */ |
| 636 |
locker = PSI_RWLOCK_CALL(start_rwlock_rdwait)( |
| 637 |
&state, lock->pfs_psi, PSI_RWLOCK_TRYSHAREDLOCK, location.filename, |
| 638 |
static_cast<uint>(location.line)); |
| 639 |
|
| 640 |
ret = rw_lock_s_lock_low(lock, pass, location); |
| 641 |
|
| 642 |
if (locker != nullptr) { |
| 643 |
const int rc = (ret ? 0 : 1); |
| 644 |
PSI_RWLOCK_CALL(end_rwlock_rdwait)(locker, rc); |
| 645 |
} |
| 646 |
|
| 647 |
return ret; |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
ret = rw_lock_s_lock_low(lock, pass, location); |
| 652 |
|
| 653 |
return ret; |
| 654 |
} |
| 655 |
/** Performance schema instrumented wrap function for rw_lock_sx_lock_nowait() |
| 656 |
NOTE! Please use the corresponding macro, not |
| 657 |
directly this function! |
| 658 |
@param[in] lock pointer to rw-loxk |
| 659 |
@param[in] pass pass value; != 0 if the lock will be passed to another thread |
| 660 |
to unlock |
| 661 |
@param[in] location location where requested |
| 662 |
@return true if success */ |
| 663 |
[[nodiscard]] static inline bool pfs_rw_lock_sx_lock_low( |
| 664 |
rw_lock_t *lock, ulint pass, ut::Location location) { |
| 665 |
bool ret; |
| 666 |
|
| 667 |
if (lock->pfs_psi != nullptr) { |
| 668 |
if (lock->pfs_psi->m_enabled) { |
| 669 |
PSI_rwlock_locker *locker; |
| 670 |
PSI_rwlock_locker_state state; |
| 671 |
|
| 672 |
/* Instrumented to inform we are acquiring a shared |
| 673 |
exclusive rwlock */ |
| 674 |
locker = PSI_RWLOCK_CALL(start_rwlock_rdwait)( |
| 675 |
&state, lock->pfs_psi, PSI_RWLOCK_TRYSHAREDEXCLUSIVELOCK, |
| 676 |
location.filename, static_cast<uint>(location.line)); |
| 677 |
|
| 678 |
ret = rw_lock_sx_lock_low(lock, pass, location); |
| 679 |
|
| 680 |
if (locker != nullptr) { |
| 681 |
const int rc = (ret ? 0 : 1); |
| 682 |
PSI_RWLOCK_CALL(end_rwlock_rdwait)(locker, rc); |
| 683 |
} |
| 684 |
|
| 685 |
return (ret); |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
ret = rw_lock_sx_lock_low(lock, pass, location); |
| 690 |
|
| 691 |
return ret; |
| 692 |
} |
| 693 |
/** Performance schema instrumented wrap function for rw_lock_x_unlock_func() |
| 694 |
NOTE! Please use the corresponding macro rw_lock_x_unlock(), not directly |
| 695 |
this function! */ |
| 696 |
static inline void pfs_rw_lock_x_unlock_func(IF_DEBUG(ulint pass, ) |
| 697 |
rw_lock_t *lock) { |
| 698 |
/* Inform performance schema we are unlocking the lock */ |
| 699 |
if (lock->pfs_psi != nullptr) { |
| 700 |
if (lock->pfs_psi->m_enabled) { |
| 701 |
PSI_RWLOCK_CALL(unlock_rwlock)(lock->pfs_psi, PSI_RWLOCK_EXCLUSIVEUNLOCK); |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
rw_lock_x_unlock_func(IF_DEBUG(pass, ) lock); |
| 706 |
} |
| 707 |
|
| 708 |
static inline void pfs_rw_lock_sx_unlock_func(IF_DEBUG(ulint pass, ) |
| 709 |
rw_lock_t *lock) { |
| 710 |
/* Inform performance schema we are unlocking the lock */ |
| 711 |
if (lock->pfs_psi != nullptr) { |
| 712 |
if (lock->pfs_psi->m_enabled) { |
| 713 |
PSI_RWLOCK_CALL(unlock_rwlock) |
| 714 |
(lock->pfs_psi, PSI_RWLOCK_SHAREDEXCLUSIVEUNLOCK); |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
rw_lock_sx_unlock_func(IF_DEBUG(pass, ) lock); |
| 719 |
} |
| 720 |
|
| 721 |
ALWAYS_INLINE |
| 722 |
void pfs_rw_lock_s_unlock_func(IF_DEBUG(ulint pass, ) rw_lock_t *lock) { |
| 723 |
/* Inform performance schema we are unlocking the lock */ |
| 724 |
if (lock->pfs_psi != nullptr) { |
| 725 |
if (lock->pfs_psi->m_enabled) { |
| 726 |
PSI_RWLOCK_CALL(unlock_rwlock)(lock->pfs_psi, PSI_RWLOCK_SHAREDUNLOCK); |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
rw_lock_s_unlock_func(IF_DEBUG(pass, ) lock); |
| 731 |
} |
| 732 |
#endif /* UNIV_PFS_RWLOCK */ |