00001 /****************************************************** 00002 Mutex, the basic synchronization primitive 00003 00004 (c) 1995 Innobase Oy 00005 00006 Created 9/5/1995 Heikki Tuuri 00007 *******************************************************/ 00008 00009 #ifndef sync0sync_h 00010 #define sync0sync_h 00011 00012 #include "univ.i" 00013 #include "sync0types.h" 00014 #include "ut0lst.h" 00015 #include "ut0mem.h" 00016 #include "os0thread.h" 00017 #include "os0sync.h" 00018 #include "sync0arr.h" 00019 00020 #ifndef UNIV_HOTBACKUP 00021 extern my_bool timed_mutexes; 00022 #endif /* UNIV_HOTBACKUP */ 00023 00024 /********************************************************************** 00025 Initializes the synchronization data structures. */ 00026 00027 void 00028 sync_init(void); 00029 /*===========*/ 00030 /********************************************************************** 00031 Frees the resources in synchronization data structures. */ 00032 00033 void 00034 sync_close(void); 00035 /*===========*/ 00036 /********************************************************************** 00037 Creates, or rather, initializes a mutex object to a specified memory 00038 location (which must be appropriately aligned). The mutex is initialized 00039 in the reset state. Explicit freeing of the mutex with mutex_free is 00040 necessary only if the memory block containing it is freed. */ 00041 00042 #define mutex_create(M, level) mutex_create_func((M), (level), __FILE__, __LINE__, #M) 00043 /*===================*/ 00044 /********************************************************************** 00045 Creates, or rather, initializes a mutex object in a specified memory 00046 location (which must be appropriately aligned). The mutex is initialized 00047 in the reset state. Explicit freeing of the mutex with mutex_free is 00048 necessary only if the memory block containing it is freed. */ 00049 00050 void 00051 mutex_create_func( 00052 /*==============*/ 00053 mutex_t* mutex, /* in: pointer to memory */ 00054 ulint level, /* in: level */ 00055 const char* cfile_name, /* in: file name where created */ 00056 ulint cline, /* in: file line where created */ 00057 const char* cmutex_name); /* in: mutex name */ 00058 /********************************************************************** 00059 Calling this function is obligatory only if the memory buffer containing 00060 the mutex is freed. Removes a mutex object from the mutex list. The mutex 00061 is checked to be in the reset state. */ 00062 00063 #undef mutex_free /* Fix for MacOS X */ 00064 void 00065 mutex_free( 00066 /*=======*/ 00067 mutex_t* mutex); /* in: mutex */ 00068 /****************************************************************** 00069 NOTE! The following macro should be used in mutex locking, not the 00070 corresponding function. */ 00071 00072 #define mutex_enter(M) mutex_enter_func((M), __FILE__, __LINE__) 00073 /********************************************************************** 00074 A noninlined function that reserves a mutex. In ha_innodb.cc we have disabled 00075 inlining of InnoDB functions, and no inlined functions should be called from 00076 there. That is why we need to duplicate the inlined function here. */ 00077 00078 void 00079 mutex_enter_noninline( 00080 /*==================*/ 00081 mutex_t* mutex); /* in: mutex */ 00082 /****************************************************************** 00083 NOTE! The following macro should be used in mutex locking, not the 00084 corresponding function. */ 00085 00086 /* NOTE! currently same as mutex_enter! */ 00087 00088 #define mutex_enter_fast(M) mutex_enter_func((M), __FILE__, __LINE__) 00089 #define mutex_enter_fast_func mutex_enter_func; 00090 /********************************************************************** 00091 NOTE! Use the corresponding macro in the header file, not this function 00092 directly. Locks a mutex for the current thread. If the mutex is reserved 00093 the function spins a preset time (controlled by SYNC_SPIN_ROUNDS) waiting 00094 for the mutex before suspending the thread. */ 00095 UNIV_INLINE 00096 void 00097 mutex_enter_func( 00098 /*=============*/ 00099 mutex_t* mutex, /* in: pointer to mutex */ 00100 const char* file_name, /* in: file name where locked */ 00101 ulint line); /* in: line where locked */ 00102 /************************************************************************ 00103 Tries to lock the mutex for the current thread. If the lock is not acquired 00104 immediately, returns with return value 1. */ 00105 00106 ulint 00107 mutex_enter_nowait( 00108 /*===============*/ 00109 /* out: 0 if succeed, 1 if not */ 00110 mutex_t* mutex, /* in: pointer to mutex */ 00111 const char* file_name, /* in: file name where mutex 00112 requested */ 00113 ulint line); /* in: line where requested */ 00114 /********************************************************************** 00115 Unlocks a mutex owned by the current thread. */ 00116 UNIV_INLINE 00117 void 00118 mutex_exit( 00119 /*=======*/ 00120 mutex_t* mutex); /* in: pointer to mutex */ 00121 /********************************************************************** 00122 Releases a mutex. */ 00123 00124 void 00125 mutex_exit_noninline( 00126 /*=================*/ 00127 mutex_t* mutex); /* in: mutex */ 00128 /********************************************************************** 00129 Returns TRUE if no mutex or rw-lock is currently locked. 00130 Works only in the debug version. */ 00131 00132 ibool 00133 sync_all_freed(void); 00134 /*================*/ 00135 /*##################################################################### 00136 FUNCTION PROTOTYPES FOR DEBUGGING */ 00137 /*********************************************************************** 00138 Prints wait info of the sync system. */ 00139 00140 void 00141 sync_print_wait_info( 00142 /*=================*/ 00143 FILE* file); /* in: file where to print */ 00144 /*********************************************************************** 00145 Prints info of the sync system. */ 00146 00147 void 00148 sync_print( 00149 /*=======*/ 00150 FILE* file); /* in: file where to print */ 00151 /********************************************************************** 00152 Checks that the mutex has been initialized. */ 00153 00154 ibool 00155 mutex_validate( 00156 /*===========*/ 00157 mutex_t* mutex); 00158 /********************************************************************** 00159 Adds a latch and its level in the thread level array. Allocates the memory 00160 for the array if called first time for this OS thread. Makes the checks 00161 against other latch levels stored in the array for this thread. */ 00162 00163 void 00164 sync_thread_add_level( 00165 /*==================*/ 00166 void* latch, /* in: pointer to a mutex or an rw-lock */ 00167 ulint level); /* in: level in the latching order; if 00168 SYNC_LEVEL_VARYING, nothing is done */ 00169 /********************************************************************** 00170 Removes a latch from the thread level array if it is found there. */ 00171 00172 ibool 00173 sync_thread_reset_level( 00174 /*====================*/ 00175 /* out: TRUE if found from the array; it is no error 00176 if the latch is not found, as we presently are not 00177 able to determine the level for every latch 00178 reservation the program does */ 00179 void* latch); /* in: pointer to a mutex or an rw-lock */ 00180 /********************************************************************** 00181 Checks that the level array for the current thread is empty. */ 00182 00183 ibool 00184 sync_thread_levels_empty(void); 00185 /*==========================*/ 00186 /* out: TRUE if empty */ 00187 /********************************************************************** 00188 Checks that the level array for the current thread is empty. */ 00189 00190 ibool 00191 sync_thread_levels_empty_gen( 00192 /*=========================*/ 00193 /* out: TRUE if empty except the 00194 exceptions specified below */ 00195 ibool dict_mutex_allowed); /* in: TRUE if dictionary mutex is 00196 allowed to be owned by the thread, 00197 also purge_is_running mutex is 00198 allowed */ 00199 #ifdef UNIV_SYNC_DEBUG 00200 /********************************************************************** 00201 Checks that the current thread owns the mutex. Works only 00202 in the debug version. */ 00203 00204 ibool 00205 mutex_own( 00206 /*======*/ 00207 /* out: TRUE if owns */ 00208 mutex_t* mutex); /* in: mutex */ 00209 /********************************************************************** 00210 Gets the debug information for a reserved mutex. */ 00211 00212 void 00213 mutex_get_debug_info( 00214 /*=================*/ 00215 mutex_t* mutex, /* in: mutex */ 00216 const char** file_name, /* out: file where requested */ 00217 ulint* line, /* out: line where requested */ 00218 os_thread_id_t* thread_id); /* out: id of the thread which owns 00219 the mutex */ 00220 /********************************************************************** 00221 Counts currently reserved mutexes. Works only in the debug version. */ 00222 00223 ulint 00224 mutex_n_reserved(void); 00225 /*==================*/ 00226 /********************************************************************** 00227 Prints debug info of currently reserved mutexes. */ 00228 00229 void 00230 mutex_list_print_info(void); 00231 /*========================*/ 00232 #endif /* UNIV_SYNC_DEBUG */ 00233 /********************************************************************** 00234 NOT to be used outside this module except in debugging! Gets the value 00235 of the lock word. */ 00236 UNIV_INLINE 00237 ulint 00238 mutex_get_lock_word( 00239 /*================*/ 00240 mutex_t* mutex); /* in: mutex */ 00241 #ifdef UNIV_SYNC_DEBUG 00242 /********************************************************************** 00243 NOT to be used outside this module except in debugging! Gets the waiters 00244 field in a mutex. */ 00245 UNIV_INLINE 00246 ulint 00247 mutex_get_waiters( 00248 /*==============*/ 00249 /* out: value to set */ 00250 mutex_t* mutex); /* in: mutex */ 00251 #endif /* UNIV_SYNC_DEBUG */ 00252 00253 /* 00254 LATCHING ORDER WITHIN THE DATABASE 00255 ================================== 00256 00257 The mutex or latch in the central memory object, for instance, a rollback 00258 segment object, must be acquired before acquiring the latch or latches to 00259 the corresponding file data structure. In the latching order below, these 00260 file page object latches are placed immediately below the corresponding 00261 central memory object latch or mutex. 00262 00263 Synchronization object Notes 00264 ---------------------- ----- 00265 00266 Dictionary mutex If we have a pointer to a dictionary 00267 | object, e.g., a table, it can be 00268 | accessed without reserving the 00269 | dictionary mutex. We must have a 00270 | reservation, a memoryfix, to the 00271 | appropriate table object in this case, 00272 | and the table must be explicitly 00273 | released later. 00274 V 00275 Dictionary header 00276 | 00277 V 00278 Secondary index tree latch The tree latch protects also all 00279 | the B-tree non-leaf pages. These 00280 V can be read with the page only 00281 Secondary index non-leaf bufferfixed to save CPU time, 00282 | no s-latch is needed on the page. 00283 | Modification of a page requires an 00284 | x-latch on the page, however. If a 00285 | thread owns an x-latch to the tree, 00286 | it is allowed to latch non-leaf pages 00287 | even after it has acquired the fsp 00288 | latch. 00289 V 00290 Secondary index leaf The latch on the secondary index leaf 00291 | can be kept while accessing the 00292 | clustered index, to save CPU time. 00293 V 00294 Clustered index tree latch To increase concurrency, the tree 00295 | latch is usually released when the 00296 | leaf page latch has been acquired. 00297 V 00298 Clustered index non-leaf 00299 | 00300 V 00301 Clustered index leaf 00302 | 00303 V 00304 Transaction system header 00305 | 00306 V 00307 Transaction undo mutex The undo log entry must be written 00308 | before any index page is modified. 00309 | Transaction undo mutex is for the undo 00310 | logs the analogue of the tree latch 00311 | for a B-tree. If a thread has the 00312 | trx undo mutex reserved, it is allowed 00313 | to latch the undo log pages in any 00314 | order, and also after it has acquired 00315 | the fsp latch. 00316 V 00317 Rollback segment mutex The rollback segment mutex must be 00318 | reserved, if, e.g., a new page must 00319 | be added to an undo log. The rollback 00320 | segment and the undo logs in its 00321 | history list can be seen as an 00322 | analogue of a B-tree, and the latches 00323 | reserved similarly, using a version of 00324 | lock-coupling. If an undo log must be 00325 | extended by a page when inserting an 00326 | undo log record, this corresponds to 00327 | a pessimistic insert in a B-tree. 00328 V 00329 Rollback segment header 00330 | 00331 V 00332 Purge system latch 00333 | 00334 V 00335 Undo log pages If a thread owns the trx undo mutex, 00336 | or for a log in the history list, the 00337 | rseg mutex, it is allowed to latch 00338 | undo log pages in any order, and even 00339 | after it has acquired the fsp latch. 00340 | If a thread does not have the 00341 | appropriate mutex, it is allowed to 00342 | latch only a single undo log page in 00343 | a mini-transaction. 00344 V 00345 File space management latch If a mini-transaction must allocate 00346 | several file pages, it can do that, 00347 | because it keeps the x-latch to the 00348 | file space management in its memo. 00349 V 00350 File system pages 00351 | 00352 V 00353 Kernel mutex If a kernel operation needs a file 00354 | page allocation, it must reserve the 00355 | fsp x-latch before acquiring the kernel 00356 | mutex. 00357 V 00358 Search system mutex 00359 | 00360 V 00361 Buffer pool mutex 00362 | 00363 V 00364 Log mutex 00365 | 00366 Any other latch 00367 | 00368 V 00369 Memory pool mutex */ 00370 00371 /* Latching order levels */ 00372 00373 /* User transaction locks are higher than any of the latch levels below: 00374 no latches are allowed when a thread goes to wait for a normal table 00375 or row lock! */ 00376 #define SYNC_USER_TRX_LOCK 9999 00377 #define SYNC_NO_ORDER_CHECK 3000 /* this can be used to suppress 00378 latching order checking */ 00379 #define SYNC_LEVEL_VARYING 2000 /* Level is varying. Only used with 00380 buffer pool page locks, which do not 00381 have a fixed level, but instead have 00382 their level set after the page is 00383 locked; see e.g. 00384 ibuf_bitmap_get_map_page(). */ 00385 #define SYNC_DICT_OPERATION 1001 /* table create, drop, etc. reserve 00386 this in X-mode, implicit or backround 00387 operations purge, rollback, foreign 00388 key checks reserve this in S-mode */ 00389 #define SYNC_DICT 1000 00390 #define SYNC_DICT_AUTOINC_MUTEX 999 00391 #define SYNC_DICT_HEADER 995 00392 #define SYNC_IBUF_HEADER 914 00393 #define SYNC_IBUF_PESS_INSERT_MUTEX 912 00394 #define SYNC_IBUF_MUTEX 910 /* ibuf mutex is really below 00395 SYNC_FSP_PAGE: we assign a value this 00396 high only to make the program to pass 00397 the debug checks */ 00398 /*-------------------------------*/ 00399 #define SYNC_INDEX_TREE 900 00400 #define SYNC_TREE_NODE_NEW 892 00401 #define SYNC_TREE_NODE_FROM_HASH 891 00402 #define SYNC_TREE_NODE 890 00403 #define SYNC_PURGE_SYS 810 00404 #define SYNC_PURGE_LATCH 800 00405 #define SYNC_TRX_UNDO 700 00406 #define SYNC_RSEG 600 00407 #define SYNC_RSEG_HEADER_NEW 591 00408 #define SYNC_RSEG_HEADER 590 00409 #define SYNC_TRX_UNDO_PAGE 570 00410 #define SYNC_EXTERN_STORAGE 500 00411 #define SYNC_FSP 400 00412 #define SYNC_FSP_PAGE 395 00413 /*------------------------------------- Insert buffer headers */ 00414 /*------------------------------------- ibuf_mutex */ 00415 /*------------------------------------- Insert buffer tree */ 00416 #define SYNC_IBUF_BITMAP_MUTEX 351 00417 #define SYNC_IBUF_BITMAP 350 00418 /*------------------------------------- MySQL query cache mutex */ 00419 /*------------------------------------- MySQL binlog mutex */ 00420 /*-------------------------------*/ 00421 #define SYNC_KERNEL 300 00422 #define SYNC_REC_LOCK 299 00423 #define SYNC_TRX_LOCK_HEAP 298 00424 #define SYNC_TRX_SYS_HEADER 290 00425 #define SYNC_LOG 170 00426 #define SYNC_RECV 168 00427 #define SYNC_WORK_QUEUE 161 00428 #define SYNC_SEARCH_SYS 160 /* NOTE that if we have a memory 00429 heap that can be extended to the 00430 buffer pool, its logical level is 00431 SYNC_SEARCH_SYS, as memory allocation 00432 can call routines there! Otherwise 00433 the level is SYNC_MEM_HASH. */ 00434 #define SYNC_BUF_POOL 150 00435 #define SYNC_BUF_BLOCK 149 00436 #define SYNC_DOUBLEWRITE 140 00437 #define SYNC_ANY_LATCH 135 00438 #define SYNC_THR_LOCAL 133 00439 #define SYNC_MEM_HASH 131 00440 #define SYNC_MEM_POOL 130 00441 00442 /* Codes used to designate lock operations */ 00443 #define RW_LOCK_NOT_LOCKED 350 00444 #define RW_LOCK_EX 351 00445 #define RW_LOCK_EXCLUSIVE 351 00446 #define RW_LOCK_SHARED 352 00447 #define RW_LOCK_WAIT_EX 353 00448 #define SYNC_MUTEX 354 00449 00450 /* NOTE! The structure appears here only for the compiler to know its size. 00451 Do not use its fields directly! The structure used in the spin lock 00452 implementation of a mutual exclusion semaphore. */ 00453 00454 struct mutex_struct { 00455 ulint lock_word; /* This ulint is the target of the atomic 00456 test-and-set instruction in Win32 */ 00457 #if !defined(_WIN32) || !defined(UNIV_CAN_USE_X86_ASSEMBLER) 00458 os_fast_mutex_t 00459 os_fast_mutex; /* In other systems we use this OS mutex 00460 in place of lock_word */ 00461 #endif 00462 ulint waiters; /* This ulint is set to 1 if there are (or 00463 may be) threads waiting in the global wait 00464 array for this mutex to be released. 00465 Otherwise, this is 0. */ 00466 UT_LIST_NODE_T(mutex_t) list; /* All allocated mutexes are put into 00467 a list. Pointers to the next and prev. */ 00468 #ifdef UNIV_SYNC_DEBUG 00469 const char* file_name; /* File where the mutex was locked */ 00470 ulint line; /* Line where the mutex was locked */ 00471 os_thread_id_t thread_id; /* Debug version: The thread id of the 00472 thread which locked the mutex. */ 00473 #endif /* UNIV_SYNC_DEBUG */ 00474 ulint level; /* Level in the global latching order */ 00475 const char* cfile_name;/* File name where mutex created */ 00476 ulint cline; /* Line where created */ 00477 ulint magic_n; 00478 #ifndef UNIV_HOTBACKUP 00479 ulong count_using; /* count of times mutex used */ 00480 ulong count_spin_loop; /* count of spin loops */ 00481 ulong count_spin_rounds; /* count of spin rounds */ 00482 ulong count_os_wait; /* count of os_wait */ 00483 ulong count_os_yield; /* count of os_wait */ 00484 ulonglong lspent_time; /* mutex os_wait timer msec */ 00485 ulonglong lmax_spent_time; /* mutex os_wait timer msec */ 00486 const char* cmutex_name;/* mutex name */ 00487 ulint mutex_type;/* 0 - usual mutex 1 - rw_lock mutex */ 00488 #endif /* !UNIV_HOTBACKUP */ 00489 }; 00490 00491 #define MUTEX_MAGIC_N (ulint)979585 00492 00493 /* The global array of wait cells for implementation of the databases own 00494 mutexes and read-write locks. Appears here for debugging purposes only! */ 00495 00496 extern sync_array_t* sync_primary_wait_array; 00497 00498 /* Constant determining how long spin wait is continued before suspending 00499 the thread. A value 600 rounds on a 1995 100 MHz Pentium seems to correspond 00500 to 20 microseconds. */ 00501 00502 #define SYNC_SPIN_ROUNDS srv_n_spin_wait_rounds 00503 00504 #define SYNC_INFINITE_TIME ((ulint)(-1)) 00505 00506 /* Means that a timeout elapsed when waiting */ 00507 00508 #define SYNC_TIME_EXCEEDED (ulint)1 00509 00510 /* The number of system calls made in this module. Intended for performance 00511 monitoring. */ 00512 00513 extern ulint mutex_system_call_count; 00514 extern ulint mutex_exit_count; 00515 00516 /* Latching order checks start when this is set TRUE */ 00517 extern ibool sync_order_checks_on; 00518 00519 /* This variable is set to TRUE when sync_init is called */ 00520 extern ibool sync_initialized; 00521 00522 /* Global list of database mutexes (not OS mutexes) created. */ 00523 typedef UT_LIST_BASE_NODE_T(mutex_t) ut_list_base_node_t; 00524 extern ut_list_base_node_t mutex_list; 00525 00526 /* Mutex protecting the mutex_list variable */ 00527 extern mutex_t mutex_list_mutex; 00528 00529 00530 #ifndef UNIV_NONINL 00531 #include "sync0sync.ic" 00532 #endif 00533 00534 #endif
1.4.7

