1 /***************************************************************************** 2 3 Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved. 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 13 it under the terms of the GNU General Public License, version 2.0, 14 as published by the Free Software Foundation. 15 16 This program is also distributed 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 included with MySQL. 22 23 This program is distributed in the hope that it will be useful, 24 but WITHOUT ANY WARRANTY; without even the implied warranty of 25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26 GNU General Public License, version 2.0, for more details. 27 28 You should have received a copy of the GNU General Public License along with 29 this program; if not, write to the Free Software Foundation, Inc., 30 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA 31 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 "ut0lst.h" 47 #include "ut0counter.h" 48 #include "sync0sync.h" 49 #include "os0sync.h" 50 51 /* The following undef is to prevent a name conflict with a macro 52 in MySQL: */ 53 #undef rw_lock_t 54 #endif /* !UNIV_HOTBACKUP */ 55 56 /** Counters for RW locks. */ 57 struct rw_lock_stats_t { 58 typedef ib_counter_t<ib_int64_t, IB_N_SLOTS> ib_int64_counter_t; 59 60 /** number of spin waits on rw-latches, 61 resulted during shared (read) locks */ 62 ib_int64_counter_t rw_s_spin_wait_count; 63 64 /** number of spin loop rounds on rw-latches, 65 resulted during shared (read) locks */ 66 ib_int64_counter_t rw_s_spin_round_count; 67 68 /** number of OS waits on rw-latches, 69 resulted during shared (read) locks */ 70 ib_int64_counter_t rw_s_os_wait_count; 71 72 /** number of unlocks (that unlock shared locks), 73 set only when UNIV_SYNC_PERF_STAT is defined */ 74 ib_int64_counter_t rw_s_exit_count; 75 76 /** number of spin waits on rw-latches, 77 resulted during exclusive (write) locks */ 78 ib_int64_counter_t rw_x_spin_wait_count; 79 80 /** number of spin loop rounds on rw-latches, 81 resulted during exclusive (write) locks */ 82 ib_int64_counter_t rw_x_spin_round_count; 83 84 /** number of OS waits on rw-latches, 85 resulted during exclusive (write) locks */ 86 ib_int64_counter_t rw_x_os_wait_count; 87 88 /** number of unlocks (that unlock exclusive locks), 89 set only when UNIV_SYNC_PERF_STAT is defined */ 90 ib_int64_counter_t rw_x_exit_count; 91 }; 92 93 /* Latch types; these are used also in btr0btr.h: keep the numerical values 94 smaller than 30 and the order of the numerical values like below! */ 95 #define RW_S_LATCH 1 96 #define RW_X_LATCH 2 97 #define RW_NO_LATCH 3 98 99 #ifndef UNIV_HOTBACKUP 100 /* We decrement lock_word by this amount for each x_lock. It is also the 101 start value for the lock_word, meaning that it limits the maximum number 102 of concurrent read locks before the rw_lock breaks. The current value of 103 0x00100000 allows 1,048,575 concurrent readers and 2047 recursive writers.*/ 104 #define X_LOCK_DECR 0x00100000 105 106 struct rw_lock_t; 107 #ifdef UNIV_SYNC_DEBUG 108 struct rw_lock_debug_t; 109 #endif /* UNIV_SYNC_DEBUG */ 110 111 typedef UT_LIST_BASE_NODE_T(rw_lock_t) rw_lock_list_t; 112 113 extern rw_lock_list_t rw_lock_list; 114 extern ib_mutex_t rw_lock_list_mutex; 115 116 #ifdef UNIV_SYNC_DEBUG 117 /* The global mutex which protects debug info lists of all rw-locks. 118 To modify the debug info list of an rw-lock, this mutex has to be 119 120 acquired in addition to the mutex protecting the lock. */ 121 extern ib_mutex_t rw_lock_debug_mutex; 122 extern os_event_t rw_lock_debug_event; /*!< If deadlock detection does 123 not get immediately the mutex it 124 may wait for this event */ 125 extern ibool rw_lock_debug_waiters; /*!< This is set to TRUE, if 126 there may be waiters for the event */ 127 #endif /* UNIV_SYNC_DEBUG */ 128 129 /** Counters for RW locks. */ 130 extern rw_lock_stats_t rw_lock_stats; 131 132 #ifdef UNIV_PFS_RWLOCK 133 /* Following are rwlock keys used to register with MySQL 134 performance schema */ 135 # ifdef UNIV_LOG_ARCHIVE 136 extern mysql_pfs_key_t archive_lock_key; 137 # endif /* UNIV_LOG_ARCHIVE */ 138 extern mysql_pfs_key_t btr_search_latch_key; 139 extern mysql_pfs_key_t buf_block_lock_key; 140 # ifdef UNIV_SYNC_DEBUG 141 extern mysql_pfs_key_t buf_block_debug_latch_key; 142 # endif /* UNIV_SYNC_DEBUG */ 143 extern mysql_pfs_key_t dict_operation_lock_key; 144 extern mysql_pfs_key_t checkpoint_lock_key; 145 extern mysql_pfs_key_t fil_space_latch_key; 146 extern mysql_pfs_key_t fts_cache_rw_lock_key; 147 extern mysql_pfs_key_t fts_cache_init_rw_lock_key; 148 extern mysql_pfs_key_t trx_i_s_cache_lock_key; 149 extern mysql_pfs_key_t trx_purge_latch_key; 150 extern mysql_pfs_key_t index_tree_rw_lock_key; 151 extern mysql_pfs_key_t index_online_log_key; 152 extern mysql_pfs_key_t dict_table_stats_key; 153 extern mysql_pfs_key_t trx_sys_rw_lock_key; 154 extern mysql_pfs_key_t hash_table_rw_lock_key; 155 #endif /* UNIV_PFS_RWLOCK */ 156 157 158 #ifndef UNIV_PFS_RWLOCK 159 /******************************************************************//** 160 Creates, or rather, initializes an rw-lock object in a specified memory 161 location (which must be appropriately aligned). The rw-lock is initialized 162 to the non-locked state. Explicit freeing of the rw-lock with rw_lock_free 163 is necessary only if the memory block containing it is freed. 164 if MySQL performance schema is enabled and "UNIV_PFS_RWLOCK" is 165 defined, the rwlock are instrumented with performance schema probes. */ 166 # ifdef UNIV_DEBUG 167 # ifdef UNIV_SYNC_DEBUG 168 # define rw_lock_create(K, L, level) \ 169 rw_lock_create_func((L), (level), #L, __FILE__, __LINE__) 170 # else /* UNIV_SYNC_DEBUG */ 171 # define rw_lock_create(K, L, level) \ 172 rw_lock_create_func((L), #L, __FILE__, __LINE__) 173 # endif/* UNIV_SYNC_DEBUG */ 174 # else /* UNIV_DEBUG */ 175 # define rw_lock_create(K, L, level) \ 176 rw_lock_create_func((L), __FILE__, __LINE__) 177 # endif /* UNIV_DEBUG */ 178 179 /**************************************************************//** 180 NOTE! The following macros should be used in rw locking and 181 unlocking, not the corresponding function. */ 182 183 # define rw_lock_s_lock(M) \ 184 rw_lock_s_lock_func((M), 0, __FILE__, __LINE__) 185 186 # define rw_lock_s_lock_inline(M, P, F, L) \ 187 rw_lock_s_lock_func((M), (P), (F), (L)) 188 189 # define rw_lock_s_lock_gen(M, P) \ 190 rw_lock_s_lock_func((M), (P), __FILE__, __LINE__) 191 192 # define rw_lock_s_lock_gen_nowait(M, P) \ 193 rw_lock_s_lock_low((M), (P), __FILE__, __LINE__) 194 195 # define rw_lock_s_lock_nowait(M, F, L) \ 196 rw_lock_s_lock_low((M), 0, (F), (L)) 197 198 # ifdef UNIV_SYNC_DEBUG 199 # define rw_lock_s_unlock_gen(L, P) rw_lock_s_unlock_func(P, L) 200 # else 201 # define rw_lock_s_unlock_gen(L, P) rw_lock_s_unlock_func(L) 202 # endif 203 204 205 # define rw_lock_x_lock(M) \ 206 rw_lock_x_lock_func((M), 0, __FILE__, __LINE__) 207 208 # define rw_lock_x_lock_inline(M, P, F, L) \ 209 rw_lock_x_lock_func((M), (P), (F), (L)) 210 211 # define rw_lock_x_lock_gen(M, P) \ 212 rw_lock_x_lock_func((M), (P), __FILE__, __LINE__) 213 214 # define rw_lock_x_lock_nowait(M) \ 215 rw_lock_x_lock_func_nowait((M), __FILE__, __LINE__) 216 217 # define rw_lock_x_lock_func_nowait_inline(M, F, L) \ 218 rw_lock_x_lock_func_nowait((M), (F), (L)) 219 220 # ifdef UNIV_SYNC_DEBUG 221 # define rw_lock_x_unlock_gen(L, P) rw_lock_x_unlock_func(P, L) 222 # else 223 # define rw_lock_x_unlock_gen(L, P) rw_lock_x_unlock_func(L) 224 # endif 225 226 # define rw_lock_free(M) rw_lock_free_func(M) 227 228 #else /* !UNIV_PFS_RWLOCK */ 229 230 /* Following macros point to Performance Schema instrumented functions. */ 231 # ifdef UNIV_DEBUG 232 # ifdef UNIV_SYNC_DEBUG 233 # define rw_lock_create(K, L, level) \ 234 pfs_rw_lock_create_func((K), (L), (level), #L, __FILE__, __LINE__) 235 # else /* UNIV_SYNC_DEBUG */ 236 # define rw_lock_create(K, L, level) \ 237 pfs_rw_lock_create_func((K), (L), #L, __FILE__, __LINE__) 238 # endif/* UNIV_SYNC_DEBUG */ 239 # else /* UNIV_DEBUG */ 240 # define rw_lock_create(K, L, level) \ 241 pfs_rw_lock_create_func((K), (L), __FILE__, __LINE__) 242 # endif /* UNIV_DEBUG */ 243 244 /****************************************************************** 245 NOTE! The following macros should be used in rw locking and 246 unlocking, not the corresponding function. */ 247 248 # define rw_lock_s_lock(M) \ 249 pfs_rw_lock_s_lock_func((M), 0, __FILE__, __LINE__) 250 251 # define rw_lock_s_lock_inline(M, P, F, L) \ 252 pfs_rw_lock_s_lock_func((M), (P), (F), (L)) 253 254 # define rw_lock_s_lock_gen(M, P) \ 255 pfs_rw_lock_s_lock_func((M), (P), __FILE__, __LINE__) 256 257 # define rw_lock_s_lock_gen_nowait(M, P) \ 258 pfs_rw_lock_s_lock_low((M), (P), __FILE__, __LINE__) 259 260 # define rw_lock_s_lock_nowait(M, F, L) \ 261 pfs_rw_lock_s_lock_low((M), 0, (F), (L)) 262 263 # ifdef UNIV_SYNC_DEBUG 264 # define rw_lock_s_unlock_gen(L, P) pfs_rw_lock_s_unlock_func(P, L) 265 # else 266 # define rw_lock_s_unlock_gen(L, P) pfs_rw_lock_s_unlock_func(L) 267 # endif 268 269 # define rw_lock_x_lock(M) \ 270 pfs_rw_lock_x_lock_func((M), 0, __FILE__, __LINE__) 271 272 # define rw_lock_x_lock_inline(M, P, F, L) \ 273 pfs_rw_lock_x_lock_func((M), (P), (F), (L)) 274 275 # define rw_lock_x_lock_gen(M, P) \ 276 pfs_rw_lock_x_lock_func((M), (P), __FILE__, __LINE__) 277 278 # define rw_lock_x_lock_nowait(M) \ 279 pfs_rw_lock_x_lock_func_nowait((M), __FILE__, __LINE__) 280 281 # define rw_lock_x_lock_func_nowait_inline(M, F, L) \ 282 pfs_rw_lock_x_lock_func_nowait((M), (F), (L)) 283 284 # ifdef UNIV_SYNC_DEBUG 285 # define rw_lock_x_unlock_gen(L, P) pfs_rw_lock_x_unlock_func(P, L) 286 # else 287 # define rw_lock_x_unlock_gen(L, P) pfs_rw_lock_x_unlock_func(L) 288 # endif 289 290 # define rw_lock_free(M) pfs_rw_lock_free_func(M) 291 292 #endif /* UNIV_PFS_RWLOCK */ 293 294 #define rw_lock_s_unlock(L) rw_lock_s_unlock_gen(L, 0) 295 #define rw_lock_x_unlock(L) rw_lock_x_unlock_gen(L, 0) 296 297 /******************************************************************//** 298 Creates, or rather, initializes an rw-lock object in a specified memory 299 location (which must be appropriately aligned). The rw-lock is initialized 300 to the non-locked state. Explicit freeing of the rw-lock with rw_lock_free 301 is necessary only if the memory block containing it is freed. */ 302 UNIV_INTERN 303 void 304 rw_lock_create_func( 305 /*================*/ 306 rw_lock_t* lock, /*!< in: pointer to memory */ 307 #ifdef UNIV_DEBUG 308 # ifdef UNIV_SYNC_DEBUG 309 ulint level, /*!< in: level */ 310 # endif /* UNIV_SYNC_DEBUG */ 311 const char* cmutex_name, /*!< in: mutex name */ 312 #endif /* UNIV_DEBUG */ 313 const char* cfile_name, /*!< in: file name where created */ 314 ulint cline); /*!< in: file line where created */ 315 /******************************************************************//** 316 Calling this function is obligatory only if the memory buffer containing 317 the rw-lock is freed. Removes an rw-lock object from the global list. The 318 rw-lock is checked to be in the non-locked state. */ 319 UNIV_INTERN 320 void 321 rw_lock_free_func( 322 /*==============*/ 323 rw_lock_t* lock); /*!< in: rw-lock */ 324 #ifdef UNIV_DEBUG 325 /******************************************************************//** 326 Checks that the rw-lock has been initialized and that there are no 327 simultaneous shared and exclusive locks. 328 @return TRUE */ 329 UNIV_INTERN 330 ibool 331 rw_lock_validate( 332 /*=============*/ 333 rw_lock_t* lock); /*!< in: rw-lock */ 334 #endif /* UNIV_DEBUG */ 335 /******************************************************************//** 336 Low-level function which tries to lock an rw-lock in s-mode. Performs no 337 spinning. 338 @return TRUE if success */ 339 UNIV_INLINE 340 ibool 341 rw_lock_s_lock_low( 342 /*===============*/ 343 rw_lock_t* lock, /*!< in: pointer to rw-lock */ 344 ulint pass MY_ATTRIBUTE((unused)), 345 /*!< in: pass value; != 0, if the lock will be 346 passed to another thread to unlock */ 347 const char* file_name, /*!< in: file name where lock requested */ 348 ulint line); /*!< in: line where requested */ 349 /******************************************************************//** 350 NOTE! Use the corresponding macro, not directly this function, except if 351 you supply the file name and line number. Lock an rw-lock in shared mode 352 for the current thread. If the rw-lock is locked in exclusive mode, or 353 there is an exclusive lock request waiting, the function spins a preset 354 time (controlled by SYNC_SPIN_ROUNDS), waiting for the lock, before 355 suspending the thread. */ 356 UNIV_INLINE 357 void 358 rw_lock_s_lock_func( 359 /*================*/ 360 rw_lock_t* lock, /*!< in: pointer to rw-lock */ 361 ulint pass, /*!< in: pass value; != 0, if the lock will 362 be passed to another thread to unlock */ 363 const char* file_name,/*!< in: file name where lock requested */ 364 ulint line); /*!< in: line where requested */ 365 /******************************************************************//** 366 NOTE! Use the corresponding macro, not directly this function! Lock an 367 rw-lock in exclusive mode for the current thread if the lock can be 368 obtained immediately. 369 @return TRUE if success */ 370 UNIV_INLINE 371 ibool 372 rw_lock_x_lock_func_nowait( 373 /*=======================*/ 374 rw_lock_t* lock, /*!< in: pointer to rw-lock */ 375 const char* file_name,/*!< in: file name where lock requested */ 376 ulint line); /*!< in: line where requested */ 377 /******************************************************************//** 378 Releases a shared mode lock. */ 379 UNIV_INLINE 380 void 381 rw_lock_s_unlock_func( 382 /*==================*/ 383 #ifdef UNIV_SYNC_DEBUG 384 ulint pass, /*!< in: pass value; != 0, if the lock may have 385 been passed to another thread to unlock */ 386 #endif 387 rw_lock_t* lock); /*!< in/out: rw-lock */ 388 389 /******************************************************************//** 390 NOTE! Use the corresponding macro, not directly this function! Lock an 391 rw-lock in exclusive mode for the current thread. If the rw-lock is locked 392 in shared or exclusive mode, or there is an exclusive lock request waiting, 393 the function spins a preset time (controlled by SYNC_SPIN_ROUNDS), waiting 394 for the lock, before suspending the thread. If the same thread has an x-lock 395 on the rw-lock, locking succeed, with the following exception: if pass != 0, 396 only a single x-lock may be taken on the lock. NOTE: If the same thread has 397 an s-lock, locking does not succeed! */ 398 UNIV_INTERN 399 void 400 rw_lock_x_lock_func( 401 /*================*/ 402 rw_lock_t* lock, /*!< in: pointer to rw-lock */ 403 ulint pass, /*!< in: pass value; != 0, if the lock will 404 be passed to another thread to unlock */ 405 const char* file_name,/*!< in: file name where lock requested */ 406 ulint line); /*!< in: line where requested */ 407 /******************************************************************//** 408 Releases an exclusive mode lock. */ 409 UNIV_INLINE 410 void 411 rw_lock_x_unlock_func( 412 /*==================*/ 413 #ifdef UNIV_SYNC_DEBUG 414 ulint pass, /*!< in: pass value; != 0, if the lock may have 415 been passed to another thread to unlock */ 416 #endif 417 rw_lock_t* lock); /*!< in/out: rw-lock */ 418 /******************************************************************//** 419 This function is used in the insert buffer to move the ownership of an 420 x-latch on a buffer frame to the current thread. The x-latch was set by 421 the buffer read operation and it protected the buffer frame while the 422 read was done. The ownership is moved because we want that the current 423 thread is able to acquire a second x-latch which is stored in an mtr. 424 This, in turn, is needed to pass the debug checks of index page 425 operations. */ 426 UNIV_INTERN 427 void 428 rw_lock_x_lock_move_ownership( 429 /*==========================*/ 430 rw_lock_t* lock); /*!< in: lock which was x-locked in the 431 buffer read */ 432 /******************************************************************//** 433 Returns the value of writer_count for the lock. Does not reserve the lock 434 mutex, so the caller must be sure it is not changed during the call. 435 @return value of writer_count */ 436 UNIV_INLINE 437 ulint 438 rw_lock_get_x_lock_count( 439 /*=====================*/ 440 const rw_lock_t* lock); /*!< in: rw-lock */ 441 /********************************************************************//** 442 Check if there are threads waiting for the rw-lock. 443 @return 1 if waiters, 0 otherwise */ 444 UNIV_INLINE 445 ulint 446 rw_lock_get_waiters( 447 /*================*/ 448 const rw_lock_t* lock); /*!< in: rw-lock */ 449 /******************************************************************//** 450 Returns the write-status of the lock - this function made more sense 451 with the old rw_lock implementation. 452 @return RW_LOCK_NOT_LOCKED, RW_LOCK_EX, RW_LOCK_WAIT_EX */ 453 UNIV_INLINE 454 ulint 455 rw_lock_get_writer( 456 /*===============*/ 457 const rw_lock_t* lock); /*!< in: rw-lock */ 458 /******************************************************************//** 459 Returns the number of readers. 460 @return number of readers */ 461 UNIV_INLINE 462 ulint 463 rw_lock_get_reader_count( 464 /*=====================*/ 465 const rw_lock_t* lock); /*!< in: rw-lock */ 466 /******************************************************************//** 467 Decrements lock_word the specified amount if it is greater than 0. 468 This is used by both s_lock and x_lock operations. 469 @return TRUE if decr occurs */ 470 UNIV_INLINE 471 ibool 472 rw_lock_lock_word_decr( 473 /*===================*/ 474 rw_lock_t* lock, /*!< in/out: rw-lock */ 475 ulint amount); /*!< in: amount to decrement */ 476 /******************************************************************//** 477 Increments lock_word the specified amount and returns new value. 478 @return lock->lock_word after increment */ 479 UNIV_INLINE 480 lint 481 rw_lock_lock_word_incr( 482 /*===================*/ 483 rw_lock_t* lock, /*!< in/out: rw-lock */ 484 ulint amount); /*!< in: amount to increment */ 485 /******************************************************************//** 486 This function sets the lock->writer_thread and lock->recursive fields. 487 For platforms where we are using atomic builtins instead of lock->mutex 488 it sets the lock->writer_thread field using atomics to ensure memory 489 ordering. Note that it is assumed that the caller of this function 490 effectively owns the lock i.e.: nobody else is allowed to modify 491 lock->writer_thread at this point in time. 492 The protocol is that lock->writer_thread MUST be updated BEFORE the 493 lock->recursive flag is set. */ 494 UNIV_INLINE 495 void 496 rw_lock_set_writer_id_and_recursion_flag( 497 /*=====================================*/ 498 rw_lock_t* lock, /*!< in/out: lock to work on */ 499 ibool recursive); /*!< in: TRUE if recursion 500 allowed */ 501 #ifdef UNIV_SYNC_DEBUG 502 /******************************************************************//** 503 Checks if the thread has locked the rw-lock in the specified mode, with 504 the pass value == 0. */ 505 UNIV_INTERN 506 ibool 507 rw_lock_own( 508 /*========*/ 509 rw_lock_t* lock, /*!< in: rw-lock */ 510 ulint lock_type) /*!< in: lock type: RW_LOCK_SHARED, 511 RW_LOCK_EX */ 512 MY_ATTRIBUTE((warn_unused_result)); 513 #endif /* UNIV_SYNC_DEBUG */ 514 /******************************************************************//** 515 Checks if somebody has locked the rw-lock in the specified mode. */ 516 UNIV_INTERN 517 ibool 518 rw_lock_is_locked( 519 /*==============*/ 520 rw_lock_t* lock, /*!< in: rw-lock */ 521 ulint lock_type); /*!< in: lock type: RW_LOCK_SHARED, 522 RW_LOCK_EX */ 523 #ifdef UNIV_SYNC_DEBUG 524 /***************************************************************//** 525 Prints debug info of an rw-lock. */ 526 UNIV_INTERN 527 void 528 rw_lock_print( 529 /*==========*/ 530 rw_lock_t* lock); /*!< in: rw-lock */ 531 /***************************************************************//** 532 Prints debug info of currently locked rw-locks. */ 533 UNIV_INTERN 534 void 535 rw_lock_list_print_info( 536 /*====================*/ 537 FILE* file); /*!< in: file where to print */ 538 /***************************************************************//** 539 Returns the number of currently locked rw-locks. 540 Works only in the debug version. 541 @return number of locked rw-locks */ 542 UNIV_INTERN 543 ulint 544 rw_lock_n_locked(void); 545 /*==================*/ 546 547 /*#####################################################################*/ 548 549 /******************************************************************//** 550 Acquires the debug mutex. We cannot use the mutex defined in sync0sync, 551 because the debug mutex is also acquired in sync0arr while holding the OS 552 mutex protecting the sync array, and the ordinary mutex_enter might 553 recursively call routines in sync0arr, leading to a deadlock on the OS 554 mutex. */ 555 UNIV_INTERN 556 void 557 rw_lock_debug_mutex_enter(void); 558 /*===========================*/ 559 /******************************************************************//** 560 Releases the debug mutex. */ 561 UNIV_INTERN 562 void 563 rw_lock_debug_mutex_exit(void); 564 /*==========================*/ 565 /*********************************************************************//** 566 Prints info of a debug struct. */ 567 UNIV_INTERN 568 void 569 rw_lock_debug_print( 570 /*================*/ 571 FILE* f, /*!< in: output stream */ 572 rw_lock_debug_t* info); /*!< in: debug struct */ 573 #endif /* UNIV_SYNC_DEBUG */ 574 575 /* NOTE! The structure appears here only for the compiler to know its size. 576 Do not use its fields directly! */ 577 578 /** The structure used in the spin lock implementation of a read-write 579 lock. Several threads may have a shared lock simultaneously in this 580 lock, but only one writer may have an exclusive lock, in which case no 581 shared locks are allowed. To prevent starving of a writer blocked by 582 readers, a writer may queue for x-lock by decrementing lock_word: no 583 new readers will be let in while the thread waits for readers to 584 exit. */ 585 struct rw_lock_t { 586 volatile lint lock_word; 587 /*!< Holds the state of the lock. */ 588 volatile ulint waiters;/*!< 1: there are waiters */ 589 volatile ibool recursive;/*!< Default value FALSE which means the lock 590 is non-recursive. The value is typically set 591 to TRUE making normal rw_locks recursive. In 592 case of asynchronous IO, when a non-zero 593 value of 'pass' is passed then we keep the 594 lock non-recursive. 595 This flag also tells us about the state of 596 writer_thread field. If this flag is set 597 then writer_thread MUST contain the thread 598 id of the current x-holder or wait-x thread. 599 This flag must be reset in x_unlock 600 functions before incrementing the lock_word */ 601 volatile os_thread_id_t writer_thread; 602 /*!< Thread id of writer thread. Is only 603 guaranteed to have sane and non-stale 604 value iff recursive flag is set. */ 605 os_event_t event; /*!< Used by sync0arr.cc for thread queueing */ 606 os_event_t wait_ex_event; 607 /*!< Event for next-writer to wait on. A thread 608 must decrement lock_word before waiting. */ 609 #ifndef INNODB_RW_LOCKS_USE_ATOMICS 610 ib_mutex_t mutex; /*!< The mutex protecting rw_lock_t */ 611 #endif /* INNODB_RW_LOCKS_USE_ATOMICS */ 612 613 UT_LIST_NODE_T(rw_lock_t) list; 614 /*!< All allocated rw locks are put into a 615 list */ 616 #ifdef UNIV_SYNC_DEBUG 617 UT_LIST_BASE_NODE_T(rw_lock_debug_t) debug_list; 618 /*!< In the debug version: pointer to the debug 619 info list of the lock */ 620 ulint level; /*!< Level in the global latching order. */ 621 #endif /* UNIV_SYNC_DEBUG */ 622 #ifdef UNIV_PFS_RWLOCK 623 struct PSI_rwlock *pfs_psi;/*!< The instrumentation hook */ 624 #endif 625 ulint count_os_wait; /*!< Count of os_waits. May not be accurate */ 626 const char* cfile_name;/*!< File name where lock created */ 627 /* last s-lock file/line is not guaranteed to be correct */ 628 const char* last_s_file_name;/*!< File name where last s-locked */ 629 const char* last_x_file_name;/*!< File name where last x-locked */ 630 ibool writer_is_wait_ex; 631 /*!< This is TRUE if the writer field is 632 RW_LOCK_WAIT_EX; this field is located far 633 from the memory update hotspot fields which 634 are at the start of this struct, thus we can 635 peek this field without causing much memory 636 bus traffic */ 637 unsigned cline:14; /*!< Line where created */ 638 unsigned last_s_line:14; /*!< Line number where last time s-locked */ 639 unsigned last_x_line:14; /*!< Line number where last time x-locked */ 640 #ifdef UNIV_DEBUG 641 ulint magic_n; /*!< RW_LOCK_MAGIC_N */ 642 /** Value of rw_lock_t::magic_n */ 643 #define RW_LOCK_MAGIC_N 22643 644 #endif /* UNIV_DEBUG */ 645 }; 646 647 #ifdef UNIV_SYNC_DEBUG 648 /** The structure for storing debug info of an rw-lock. All access to this 649 structure must be protected by rw_lock_debug_mutex_enter(). */ 650 struct rw_lock_debug_t { 651 652 os_thread_id_t thread_id; /*!< The thread id of the thread which 653 locked the rw-lock */ 654 ulint pass; /*!< Pass value given in the lock operation */ 655 ulint lock_type; /*!< Type of the lock: RW_LOCK_EX, 656 RW_LOCK_SHARED, RW_LOCK_WAIT_EX */ 657 const char* file_name;/*!< File name where the lock was obtained */ 658 ulint line; /*!< Line where the rw-lock was locked */ 659 UT_LIST_NODE_T(rw_lock_debug_t) list; 660 /*!< Debug structs are linked in a two-way 661 list */ 662 }; 663 #endif /* UNIV_SYNC_DEBUG */ 664 665 /* For performance schema instrumentation, a new set of rwlock 666 wrap functions are created if "UNIV_PFS_RWLOCK" is defined. 667 The instrumentations are not planted directly into original 668 functions, so that we keep the underlying function as they 669 are. And in case, user wants to "take out" some rwlock from 670 instrumentation even if performance schema (UNIV_PFS_RWLOCK) 671 is defined, they can do so by reinstating APIs directly link to 672 original underlying functions. 673 The instrumented function names have prefix of "pfs_rw_lock_" vs. 674 original name prefix of "rw_lock_". Following are list of functions 675 that have been instrumented: 676 677 rw_lock_create() 678 rw_lock_x_lock() 679 rw_lock_x_lock_gen() 680 rw_lock_x_lock_nowait() 681 rw_lock_x_unlock_gen() 682 rw_lock_s_lock() 683 rw_lock_s_lock_gen() 684 rw_lock_s_lock_nowait() 685 rw_lock_s_unlock_gen() 686 rw_lock_free() 687 */ 688 689 #ifdef UNIV_PFS_RWLOCK 690 /******************************************************************//** 691 Performance schema instrumented wrap function for rw_lock_create_func() 692 NOTE! Please use the corresponding macro rw_lock_create(), not 693 directly this function! */ 694 UNIV_INLINE 695 void 696 pfs_rw_lock_create_func( 697 /*====================*/ 698 PSI_rwlock_key key, /*!< in: key registered with 699 performance schema */ 700 rw_lock_t* lock, /*!< in: rw lock */ 701 #ifdef UNIV_DEBUG 702 # ifdef UNIV_SYNC_DEBUG 703 ulint level, /*!< in: level */ 704 # endif /* UNIV_SYNC_DEBUG */ 705 const char* cmutex_name, /*!< in: mutex name */ 706 #endif /* UNIV_DEBUG */ 707 const char* cfile_name, /*!< in: file name where created */ 708 ulint cline); /*!< in: file line where created */ 709 710 /******************************************************************//** 711 Performance schema instrumented wrap function for rw_lock_x_lock_func() 712 NOTE! Please use the corresponding macro rw_lock_x_lock(), not 713 directly this function! */ 714 UNIV_INLINE 715 void 716 pfs_rw_lock_x_lock_func( 717 /*====================*/ 718 rw_lock_t* lock, /*!< in: pointer to rw-lock */ 719 ulint pass, /*!< in: pass value; != 0, if the lock will 720 be passed to another thread to unlock */ 721 const char* file_name,/*!< in: file name where lock requested */ 722 ulint line); /*!< in: line where requested */ 723 /******************************************************************//** 724 Performance schema instrumented wrap function for 725 rw_lock_x_lock_func_nowait() 726 NOTE! Please use the corresponding macro, not directly this function! 727 @return TRUE if success */ 728 UNIV_INLINE 729 ibool 730 pfs_rw_lock_x_lock_func_nowait( 731 /*===========================*/ 732 rw_lock_t* lock, /*!< in: pointer to rw-lock */ 733 const char* file_name,/*!< in: file name where lock requested */ 734 ulint line); /*!< in: line where requested */ 735 /******************************************************************//** 736 Performance schema instrumented wrap function for rw_lock_s_lock_func() 737 NOTE! Please use the corresponding macro rw_lock_s_lock(), not directly 738 this function! */ 739 UNIV_INLINE 740 void 741 pfs_rw_lock_s_lock_func( 742 /*====================*/ 743 rw_lock_t* lock, /*!< in: pointer to rw-lock */ 744 ulint pass, /*!< in: pass value; != 0, if the lock will 745 be passed to another thread to unlock */ 746 const char* file_name,/*!< in: file name where lock requested */ 747 ulint line); /*!< in: line where requested */ 748 /******************************************************************//** 749 Performance schema instrumented wrap function for rw_lock_s_lock_func() 750 NOTE! Please use the corresponding macro rw_lock_s_lock(), not directly 751 this function! 752 @return TRUE if success */ 753 UNIV_INLINE 754 ibool 755 pfs_rw_lock_s_lock_low( 756 /*===================*/ 757 rw_lock_t* lock, /*!< in: pointer to rw-lock */ 758 ulint pass, /*!< in: pass value; != 0, if the 759 lock will be passed to another 760 thread to unlock */ 761 const char* file_name, /*!< in: file name where lock requested */ 762 ulint line); /*!< in: line where requested */ 763 /******************************************************************//** 764 Performance schema instrumented wrap function for rw_lock_x_lock_func() 765 NOTE! Please use the corresponding macro rw_lock_x_lock(), not directly 766 this function! */ 767 UNIV_INLINE 768 void 769 pfs_rw_lock_x_lock_func( 770 /*====================*/ 771 rw_lock_t* lock, /*!< in: pointer to rw-lock */ 772 ulint pass, /*!< in: pass value; != 0, if the lock will 773 be passed to another thread to unlock */ 774 const char* file_name,/*!< in: file name where lock requested */ 775 ulint line); /*!< in: line where requested */ 776 /******************************************************************//** 777 Performance schema instrumented wrap function for rw_lock_s_unlock_func() 778 NOTE! Please use the corresponding macro rw_lock_s_unlock(), not directly 779 this function! */ 780 UNIV_INLINE 781 void 782 pfs_rw_lock_s_unlock_func( 783 /*======================*/ 784 #ifdef UNIV_SYNC_DEBUG 785 ulint pass, /*!< in: pass value; != 0, if the 786 lock may have been passed to another 787 thread to unlock */ 788 #endif 789 rw_lock_t* lock); /*!< in/out: rw-lock */ 790 /******************************************************************//** 791 Performance schema instrumented wrap function for rw_lock_s_unlock_func() 792 NOTE! Please use the corresponding macro rw_lock_x_unlock(), not directly 793 this function! */ 794 UNIV_INLINE 795 void 796 pfs_rw_lock_x_unlock_func( 797 /*======================*/ 798 #ifdef UNIV_SYNC_DEBUG 799 ulint pass, /*!< in: pass value; != 0, if the 800 lock may have been passed to another 801 thread to unlock */ 802 #endif 803 rw_lock_t* lock); /*!< in/out: rw-lock */ 804 /******************************************************************//** 805 Performance schema instrumented wrap function for rw_lock_free_func() 806 NOTE! Please use the corresponding macro rw_lock_free(), not directly 807 this function! */ 808 UNIV_INLINE 809 void 810 pfs_rw_lock_free_func( 811 /*==================*/ 812 rw_lock_t* lock); /*!< in: rw-lock */ 813 #endif /* UNIV_PFS_RWLOCK */ 814 815 816 #ifndef UNIV_NONINL 817 #include "sync0rw.ic" 818 #endif 819 #endif /* !UNIV_HOTBACKUP */ 820 821 #endif 822