sync0sync.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:32k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /******************************************************
  2. Mutex, the basic synchronization primitive
  3. (c) 1995 Innobase Oy
  4. Created 9/5/1995 Heikki Tuuri
  5. *******************************************************/
  6. #include "sync0sync.h"
  7. #ifdef UNIV_NONINL
  8. #include "sync0sync.ic"
  9. #endif
  10. #include "sync0rw.h"
  11. #include "buf0buf.h"
  12. #include "srv0srv.h"
  13. #include "buf0types.h"
  14. /*
  15. REASONS FOR IMPLEMENTING THE SPIN LOCK MUTEX
  16. ============================================
  17. Semaphore operations in operating systems are slow: Solaris on a 1993 Sparc
  18. takes 3 microseconds (us) for a lock-unlock pair and Windows NT on a 1995
  19. Pentium takes 20 microseconds for a lock-unlock pair. Therefore, we have to
  20. implement our own efficient spin lock mutex. Future operating systems may
  21. provide efficient spin locks, but we cannot count on that.
  22. Another reason for implementing a spin lock is that on multiprocessor systems
  23. it can be more efficient for a processor to run a loop waiting for the 
  24. semaphore to be released than to switch to a different thread. A thread switch
  25. takes 25 us on both platforms mentioned above. See Gray and Reuter's book
  26. Transaction processing for background.
  27. How long should the spin loop last before suspending the thread? On a
  28. uniprocessor, spinning does not help at all, because if the thread owning the
  29. mutex is not executing, it cannot be released. Spinning actually wastes
  30. resources. 
  31. On a multiprocessor, we do not know if the thread owning the mutex is
  32. executing or not. Thus it would make sense to spin as long as the operation
  33. guarded by the mutex would typically last assuming that the thread is
  34. executing. If the mutex is not released by that time, we may assume that the
  35. thread owning the mutex is not executing and suspend the waiting thread.
  36. A typical operation (where no i/o involved) guarded by a mutex or a read-write
  37. lock may last 1 - 20 us on the current Pentium platform. The longest
  38. operations are the binary searches on an index node.
  39. We conclude that the best choice is to set the spin time at 20 us. Then the
  40. system should work well on a multiprocessor. On a uniprocessor we have to
  41. make sure that thread swithches due to mutex collisions are not frequent,
  42. i.e., they do not happen every 100 us or so, because that wastes too much
  43. resources. If the thread switches are not frequent, the 20 us wasted in spin
  44. loop is not too much. 
  45. Empirical studies on the effect of spin time should be done for different
  46. platforms.
  47. IMPLEMENTATION OF THE MUTEX
  48. ===========================
  49. For background, see Curt Schimmel's book on Unix implementation on modern
  50. architectures. The key points in the implementation are atomicity and
  51. serialization of memory accesses. The test-and-set instruction (XCHG in
  52. Pentium) must be atomic. As new processors may have weak memory models, also
  53. serialization of memory references may be necessary. The successor of Pentium,
  54. P6, has at least one mode where the memory model is weak. As far as we know,
  55. in Pentium all memory accesses are serialized in the program order and we do
  56. not have to worry about the memory model. On other processors there are
  57. special machine instructions called a fence, memory barrier, or storage
  58. barrier (STBAR in Sparc), which can be used to serialize the memory accesses
  59. to happen in program order relative to the fence instruction.
  60. Leslie Lamport has devised a "bakery algorithm" to implement a mutex without
  61. the atomic test-and-set, but his algorithm should be modified for weak memory
  62. models. We do not use Lamport's algorithm, because we guess it is slower than
  63. the atomic test-and-set.
  64. Our mutex implementation works as follows: After that we perform the atomic
  65. test-and-set instruction on the memory word. If the test returns zero, we
  66. know we got the lock first. If the test returns not zero, some other thread
  67. was quicker and got the lock: then we spin in a loop reading the memory word,
  68. waiting it to become zero. It is wise to just read the word in the loop, not
  69. perform numerous test-and-set instructions, because they generate memory
  70. traffic between the cache and the main memory. The read loop can just access
  71. the cache, saving bus bandwidth.
  72. If we cannot acquire the mutex lock in the specified time, we reserve a cell
  73. in the wait array, set the waiters byte in the mutex to 1. To avoid a race
  74. condition, after setting the waiters byte and before suspending the waiting
  75. thread, we still have to check that the mutex is reserved, because it may
  76. have happened that the thread which was holding the mutex has just released
  77. it and did not see the waiters byte set to 1, a case which would lead the
  78. other thread to an infinite wait.
  79. LEMMA 1: After a thread resets the event of the cell it reserves for waiting
  80. ========
  81. for a mutex, some thread will eventually call sync_array_signal_object with
  82. the mutex as an argument. Thus no infinite wait is possible.
  83. Proof: After making the reservation the thread sets the waiters field in the
  84. mutex to 1. Then it checks that the mutex is still reserved by some thread,
  85. or it reserves the mutex for itself. In any case, some thread (which may be
  86. also some earlier thread, not necessarily the one currently holding the mutex)
  87. will set the waiters field to 0 in mutex_exit, and then call
  88. sync_array_signal_object with the mutex as an argument. 
  89. Q.E.D. */
  90. ulint sync_dummy = 0;
  91. /* The number of system calls made in this module. Intended for performance
  92. monitoring. */
  93. ulint mutex_system_call_count = 0;
  94. /* Number of spin waits on mutexes: for performance monitoring */
  95. ulint mutex_spin_round_count = 0;
  96. ulint mutex_spin_wait_count = 0;
  97. ulint mutex_exit_count = 0;
  98. /* The global array of wait cells for implementation of the database's own
  99. mutexes and read-write locks */
  100. sync_array_t* sync_primary_wait_array;
  101. /* This variable is set to TRUE when sync_init is called */
  102. ibool sync_initialized = FALSE;
  103. /* Global list of database mutexes (not OS mutexes) created. */
  104. UT_LIST_BASE_NODE_T(mutex_t) mutex_list;
  105. /* Mutex protecting the mutex_list variable */
  106. mutex_t mutex_list_mutex;
  107. typedef struct sync_level_struct sync_level_t;
  108. typedef struct sync_thread_struct sync_thread_t;
  109. /* The latch levels currently owned by threads are stored in this data
  110. structure; the size of this array is OS_THREAD_MAX_N */
  111. sync_thread_t* sync_thread_level_arrays;
  112. /* Mutex protecting sync_thread_level_arrays */
  113. mutex_t sync_thread_mutex;
  114. /* Latching order checks start when this is set TRUE */
  115. ibool sync_order_checks_on = FALSE;
  116. /* Dummy mutex used to implement mutex_fence */
  117. mutex_t dummy_mutex_for_fence;
  118. struct sync_thread_struct{
  119. os_thread_id_t id; /* OS thread id */
  120. sync_level_t* levels; /* level array for this thread; if this is NULL
  121. this slot is unused */
  122. };
  123. /* Number of slots reserved for each OS thread in the sync level array */
  124. #define SYNC_THREAD_N_LEVELS 256
  125. struct sync_level_struct{
  126. void* latch; /* pointer to a mutex or an rw-lock; NULL means that
  127. the slot is empty */
  128. ulint level; /* level of the latch in the latching order */
  129. };
  130. /**********************************************************************
  131. Creates, or rather, initializes a mutex object in a specified memory
  132. location (which must be appropriately aligned). The mutex is initialized
  133. in the reset state. Explicit freeing of the mutex with mutex_free is
  134. necessary only if the memory block containing it is freed. */
  135. void
  136. mutex_create_func(
  137. /*==============*/
  138. mutex_t* mutex, /* in: pointer to memory */
  139. char* cfile_name, /* in: file name where created */
  140. ulint cline) /* in: file line where created */
  141. {
  142. #ifdef _WIN32
  143. mutex_reset_lock_word(mutex);
  144. #else
  145. os_fast_mutex_init(&(mutex->os_fast_mutex));
  146. mutex->lock_word = 0;
  147. #endif
  148. mutex_set_waiters(mutex, 0);
  149. mutex->magic_n = MUTEX_MAGIC_N;
  150. mutex->line = 0;
  151. mutex->file_name = "FILE NOT KNOWN";
  152. mutex->thread_id = ULINT_UNDEFINED;
  153. mutex->level = SYNC_LEVEL_NONE;
  154. ut_memcpy(&(mutex->cfile_name), cfile_name,
  155. ut_min(MUTEX_CNAME_LEN - 1, ut_strlen(cfile_name)));
  156. mutex->cfile_name[MUTEX_CNAME_LEN - 1] = '';
  157. mutex->cline = cline;
  158. /* Check that lock_word is aligned; this is important on Intel */
  159. ut_a(((ulint)(&(mutex->lock_word))) % 4 == 0);
  160. /* NOTE! The very first mutexes are not put to the mutex list */
  161. if ((mutex == &mutex_list_mutex) || (mutex == &sync_thread_mutex)) {
  162.      return;
  163. }
  164. mutex_enter(&mutex_list_mutex);
  165. UT_LIST_ADD_FIRST(list, mutex_list, mutex);
  166. mutex_exit(&mutex_list_mutex);
  167. }
  168. /**********************************************************************
  169. Calling this function is obligatory only if the memory buffer containing
  170. the mutex is freed. Removes a mutex object from the mutex list. The mutex
  171. is checked to be in the reset state. */
  172. void
  173. mutex_free(
  174. /*=======*/
  175. mutex_t* mutex) /* in: mutex */
  176. {
  177. ut_ad(mutex_validate(mutex));
  178. ut_a(mutex_get_lock_word(mutex) == 0);
  179. ut_a(mutex_get_waiters(mutex) == 0);
  180. mutex_enter(&mutex_list_mutex);
  181. UT_LIST_REMOVE(list, mutex_list, mutex);
  182. mutex_exit(&mutex_list_mutex);
  183. #ifndef _WIN32
  184. os_fast_mutex_free(&(mutex->os_fast_mutex));
  185. #endif
  186. /* If we free the mutex protecting the mutex list (freeing is
  187. not necessary), we have to reset the magic number AFTER removing
  188. it from the list. */
  189. mutex->magic_n = 0;
  190. }
  191. /************************************************************************
  192. Tries to lock the mutex for the current thread. If the lock is not acquired
  193. immediately, returns with return value 1. */
  194. ulint
  195. mutex_enter_nowait(
  196. /*===============*/
  197. /* out: 0 if succeed, 1 if not */
  198. mutex_t* mutex) /* in: pointer to mutex */
  199. {
  200. ut_ad(mutex_validate(mutex));
  201. if (!mutex_test_and_set(mutex)) {
  202. #ifdef UNIV_SYNC_DEBUG
  203. mutex_set_debug_info(mutex, IB__FILE__, __LINE__);
  204. #endif
  205. return(0); /* Succeeded! */
  206. }
  207. return(1);
  208. }
  209. /**********************************************************************
  210. Checks that the mutex has been initialized. */
  211. ibool
  212. mutex_validate(
  213. /*===========*/
  214. mutex_t* mutex)
  215. {
  216. ut_a(mutex);
  217. ut_a(mutex->magic_n == MUTEX_MAGIC_N);
  218. return(TRUE);
  219. }
  220. /**********************************************************************
  221. Sets the waiters field in a mutex. */
  222. void
  223. mutex_set_waiters(
  224. /*==============*/
  225. mutex_t* mutex, /* in: mutex */
  226. ulint n) /* in: value to set */
  227. {
  228. volatile ulint* ptr; /* declared volatile to ensure that
  229. the value is stored to memory */
  230. ut_ad(mutex);
  231. ptr = &(mutex->waiters);
  232. *ptr = n; /* Here we assume that the write of a single
  233. word in memory is atomic */
  234. }
  235. /**********************************************************************
  236. Reserves a mutex for the current thread. If the mutex is reserved, the
  237. function spins a preset time (controlled by SYNC_SPIN_ROUNDS), waiting
  238. for the mutex before suspending the thread. */
  239. void
  240. mutex_spin_wait(
  241. /*============*/
  242.         mutex_t*   mutex      /* in: pointer to mutex */
  243. #ifdef UNIV_SYNC_DEBUG
  244. ,char*    file_name, /* in: file name where mutex requested */
  245. ulint    line       /* in: line where requested */
  246. #endif
  247. )
  248. {
  249.         ulint    index; /* index of the reserved wait cell */
  250.         ulint    i;    /* spin round count */
  251.         
  252.         ut_ad(mutex);
  253. mutex_loop:
  254.         i = 0;
  255.         /* Spin waiting for the lock word to become zero. Note that we do not
  256. have to assume that the read access to the lock word is atomic, as the
  257. actual locking is always committed with atomic test-and-set. In
  258. reality, however, all processors probably have an atomic read of a
  259. memory word. */
  260.         
  261. spin_loop:
  262. mutex_spin_wait_count++;
  263.         while (mutex_get_lock_word(mutex) != 0 && i < SYNC_SPIN_ROUNDS) {
  264.          if (srv_spin_wait_delay) {
  265.          ut_delay(ut_rnd_interval(0, srv_spin_wait_delay));
  266.          }
  267.         
  268.               i++;
  269.         }
  270. if (i == SYNC_SPIN_ROUNDS) {
  271. os_thread_yield();
  272. }
  273. if (srv_print_latch_waits) {
  274. printf(
  275. "Thread %lu spin wait mutex at %lx cfile %s cline %lu rnds %lun",
  276. os_thread_get_curr_id(), (ulint)mutex, &(mutex->cfile_name),
  277. mutex->cline, i);
  278. }
  279. mutex_spin_round_count += i;
  280.         if (mutex_test_and_set(mutex) == 0) {
  281. /* Succeeded! */
  282. #ifdef UNIV_SYNC_DEBUG
  283. mutex_set_debug_info(mutex, file_name, line);
  284. #endif
  285.                 return;
  286.     }
  287.         
  288. if (i < SYNC_SPIN_ROUNDS) {
  289. goto spin_loop;
  290. }
  291.         sync_array_reserve_cell(sync_primary_wait_array, mutex,
  292.          SYNC_MUTEX,
  293. #ifdef UNIV_SYNC_DEBUG
  294. file_name, line,
  295. #endif
  296. &index);
  297. mutex_system_call_count++;
  298. /* The memory order of the array reservation and the change in the
  299. waiters field is important: when we suspend a thread, we first
  300. reserve the cell and then set waiters field to 1. When threads are
  301. released in mutex_exit, the waiters field is first set to zero and
  302. then the event is set to the signaled state. */
  303.         
  304. mutex_set_waiters(mutex, 1);
  305.         if (mutex_test_and_set(mutex) == 0) {
  306.                 /* Succeeded! Free the reserved wait cell */
  307.                 sync_array_free_cell(sync_primary_wait_array, index);
  308.                 
  309. #ifdef UNIV_SYNC_DEBUG
  310. mutex_set_debug_info(mutex, file_name, line);
  311. #endif
  312. if (srv_print_latch_waits) {
  313. printf(
  314. "Thread %lu spin wait succeeds at 2: mutex at %lxn",
  315. os_thread_get_curr_id(), (ulint)mutex);
  316. }
  317.                 return;
  318.                 /* Note that in this case we leave the waiters field
  319.                 set to 1. We cannot reset it to zero, as we do not know
  320.                 if there are other waiters. */
  321.         }
  322.         /* Now we know that there has been some thread holding the mutex
  323.         after the change in the wait array and the waiters field was made.
  324. Now there is no risk of infinite wait on the event. */
  325. if (srv_print_latch_waits) {
  326. printf(
  327. "Thread %lu OS wait mutex at %lx cfile %s cline %lu rnds %lun",
  328. os_thread_get_curr_id(), (ulint)mutex, &(mutex->cfile_name),
  329. mutex->cline, i);
  330. }
  331. mutex_system_call_count++;
  332.         sync_array_wait_event(sync_primary_wait_array, index);
  333.         goto mutex_loop;        
  334. }
  335. /**********************************************************************
  336. Releases the threads waiting in the primary wait array for this mutex. */
  337. void
  338. mutex_signal_object(
  339. /*================*/
  340. mutex_t* mutex) /* in: mutex */
  341. {
  342. mutex_set_waiters(mutex, 0);
  343. /* The memory order of resetting the waiters field and
  344. signaling the object is important. See LEMMA 1 above. */
  345. sync_array_signal_object(sync_primary_wait_array, mutex);
  346. }
  347. /**********************************************************************
  348. Sets the debug information for a reserved mutex. */
  349. void
  350. mutex_set_debug_info(
  351. /*=================*/
  352. mutex_t* mutex, /* in: mutex */
  353. char* file_name, /* in: file where requested */
  354. ulint line) /* in: line where requested */
  355. {
  356. ut_ad(mutex);
  357. ut_ad(file_name);
  358. sync_thread_add_level(mutex, mutex->level);
  359. mutex->file_name = file_name;
  360. mutex->line   = line;
  361. mutex->thread_id = os_thread_get_curr_id();
  362. }
  363. /**********************************************************************
  364. Gets the debug information for a reserved mutex. */
  365. void
  366. mutex_get_debug_info(
  367. /*=================*/
  368. mutex_t* mutex, /* in: mutex */
  369. char** file_name, /* out: file where requested */
  370. ulint* line, /* out: line where requested */
  371. os_thread_id_t* thread_id) /* out: id of the thread which owns
  372. the mutex */
  373. {
  374. ut_ad(mutex);
  375. *file_name = mutex->file_name;
  376. *line    = mutex->line;
  377. *thread_id = mutex->thread_id;
  378. }
  379. /**********************************************************************
  380. Sets the mutex latching level field. */
  381. void
  382. mutex_set_level(
  383. /*============*/
  384. mutex_t* mutex, /* in: mutex */
  385. ulint level) /* in: level */
  386. {
  387. mutex->level = level;
  388. }
  389. /**********************************************************************
  390. Checks that the current thread owns the mutex. Works only in the debug
  391. version. */
  392. ibool
  393. mutex_own(
  394. /*======*/
  395. /* out: TRUE if owns */
  396. mutex_t* mutex) /* in: mutex */
  397. {
  398. ut_a(mutex_validate(mutex));
  399. if (mutex_get_lock_word(mutex) != 1) {
  400. return(FALSE);
  401. }
  402. if (mutex->thread_id != os_thread_get_curr_id()) {
  403. return(FALSE);
  404. }
  405. return(TRUE);
  406. }
  407. /**********************************************************************
  408. Prints debug info of currently reserved mutexes. */
  409. void
  410. mutex_list_print_info(void)
  411. /*=======================*/
  412. {
  413. #ifndef UNIV_SYNC_DEBUG
  414. printf("Sorry, cannot give mutex list info in non-debug version!n");
  415. #else
  416. mutex_t* mutex;
  417. char* file_name;
  418. ulint line;
  419. os_thread_id_t thread_id;
  420. ulint count = 0;
  421. printf("-----------------------------------------------n");
  422. printf("MUTEX INFOn");
  423. mutex_enter(&mutex_list_mutex);
  424. mutex = UT_LIST_GET_FIRST(mutex_list);
  425. while (mutex != NULL) {
  426. count++;
  427. if (mutex_get_lock_word(mutex) != 0) {
  428.     mutex_get_debug_info(mutex, &file_name, &line, &thread_id);
  429.  printf("Locked mutex: addr %lx thread %ld file %s line %ldn",
  430.      (ulint)mutex, thread_id, file_name, line);
  431. }
  432. mutex = UT_LIST_GET_NEXT(list, mutex);
  433. }
  434. printf("Total number of mutexes %ldn", count);
  435. mutex_exit(&mutex_list_mutex);
  436. #endif
  437. }
  438. /**********************************************************************
  439. Counts currently reserved mutexes. Works only in the debug version. */
  440. ulint
  441. mutex_n_reserved(void)
  442. /*==================*/
  443. {
  444. #ifndef UNIV_SYNC_DEBUG
  445. printf("Sorry, cannot give mutex info in non-debug version!n");
  446. ut_error;
  447. return(0);
  448. #else
  449. mutex_t* mutex;
  450. ulint count = 0;
  451. mutex_enter(&mutex_list_mutex);
  452. mutex = UT_LIST_GET_FIRST(mutex_list);
  453. while (mutex != NULL) {
  454. if (mutex_get_lock_word(mutex) != 0) {
  455. count++;
  456. }
  457. mutex = UT_LIST_GET_NEXT(list, mutex);
  458. }
  459. mutex_exit(&mutex_list_mutex);
  460. ut_a(count >= 1);
  461. return(count - 1); /* Subtract one, because this function itself
  462.    was holding one mutex (mutex_list_mutex) */
  463. #endif
  464. }
  465. /**********************************************************************
  466. Returns TRUE if no mutex or rw-lock is currently locked. Works only in
  467. the debug version. */
  468. ibool
  469. sync_all_freed(void)
  470. /*================*/
  471. {
  472. #ifdef UNIV_SYNC_DEBUG
  473. if (mutex_n_reserved() + rw_lock_n_locked() == 0) {
  474. return(TRUE);
  475. } else {
  476. return(FALSE);
  477. }
  478. #else
  479. ut_error;
  480. return(FALSE);
  481. #endif
  482. }
  483. /**********************************************************************
  484. Gets the value in the nth slot in the thread level arrays. */
  485. static
  486. sync_thread_t*
  487. sync_thread_level_arrays_get_nth(
  488. /*=============================*/
  489. /* out: pointer to thread slot */
  490. ulint n) /* in: slot number */
  491. {
  492. ut_ad(n < OS_THREAD_MAX_N);
  493. return(sync_thread_level_arrays + n);
  494. }
  495. /**********************************************************************
  496. Looks for the thread slot for the calling thread. */
  497. static
  498. sync_thread_t*
  499. sync_thread_level_arrays_find_slot(void)
  500. /*====================================*/
  501. /* out: pointer to thread slot, NULL if not found */
  502. {
  503. sync_thread_t* slot;
  504. os_thread_id_t id;
  505. ulint i;
  506. id = os_thread_get_curr_id();
  507. for (i = 0; i < OS_THREAD_MAX_N; i++) {
  508. slot = sync_thread_level_arrays_get_nth(i);
  509. if (slot->levels && (slot->id == id)) {
  510. return(slot);
  511. }
  512. }
  513. return(NULL);
  514. }
  515. /**********************************************************************
  516. Looks for an unused thread slot. */
  517. static
  518. sync_thread_t*
  519. sync_thread_level_arrays_find_free(void)
  520. /*====================================*/
  521. /* out: pointer to thread slot */
  522. {
  523. sync_thread_t* slot;
  524. ulint i;
  525. for (i = 0; i < OS_THREAD_MAX_N; i++) {
  526. slot = sync_thread_level_arrays_get_nth(i);
  527. if (slot->levels == NULL) {
  528. return(slot);
  529. }
  530. }
  531. return(NULL);
  532. }
  533. /**********************************************************************
  534. Gets the value in the nth slot in the thread level array. */
  535. static
  536. sync_level_t*
  537. sync_thread_levels_get_nth(
  538. /*=======================*/
  539. /* out: pointer to level slot */
  540. sync_level_t* arr, /* in: pointer to level array for an OS
  541. thread */
  542. ulint n) /* in: slot number */
  543. {
  544. ut_ad(n < SYNC_THREAD_N_LEVELS);
  545. return(arr + n);
  546. }
  547. /**********************************************************************
  548. Checks if all the level values stored in the level array are greater than
  549. the given limit. */
  550. static
  551. ibool
  552. sync_thread_levels_g(
  553. /*=================*/
  554. /* out: TRUE if all greater */
  555. sync_level_t* arr, /* in: pointer to level array for an OS
  556. thread */
  557. ulint limit) /* in: level limit */
  558. {
  559. sync_level_t* slot;
  560. rw_lock_t* lock;
  561. mutex_t* mutex;
  562. ulint i;
  563. for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {
  564. slot = sync_thread_levels_get_nth(arr, i);
  565. if (slot->latch != NULL) {
  566. if (slot->level <= limit) {
  567. lock = slot->latch;
  568. mutex = slot->latch;
  569. ut_error;
  570. return(FALSE);
  571. }
  572. }
  573. }
  574. return(TRUE);
  575. }
  576. /**********************************************************************
  577. Checks if the level value is stored in the level array. */
  578. static
  579. ibool
  580. sync_thread_levels_contain(
  581. /*=======================*/
  582. /* out: TRUE if stored */
  583. sync_level_t* arr, /* in: pointer to level array for an OS
  584. thread */
  585. ulint level) /* in: level */
  586. {
  587. sync_level_t* slot;
  588. ulint i;
  589. for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {
  590. slot = sync_thread_levels_get_nth(arr, i);
  591. if (slot->latch != NULL) {
  592. if (slot->level == level) {
  593. return(TRUE);
  594. }
  595. }
  596. }
  597. return(FALSE);
  598. }
  599. /**********************************************************************
  600. Checks that the level array for the current thread is empty. */
  601. ibool
  602. sync_thread_levels_empty_gen(
  603. /*=========================*/
  604. /* out: TRUE if empty except the
  605. exceptions specified below */
  606. ibool dict_mutex_allowed) /* in: TRUE if dictionary mutex is
  607. allowed to be owned by the thread,
  608. also purge_is_running mutex is
  609. allowed */
  610. {
  611. sync_level_t* arr;
  612. sync_thread_t* thread_slot;
  613. sync_level_t* slot;
  614. rw_lock_t* lock;
  615. mutex_t* mutex;
  616. ulint i;
  617. if (!sync_order_checks_on) {
  618. return(TRUE);
  619. }
  620. mutex_enter(&sync_thread_mutex);
  621. thread_slot = sync_thread_level_arrays_find_slot();
  622. if (thread_slot == NULL) {
  623. mutex_exit(&sync_thread_mutex);
  624. return(TRUE);
  625. }
  626. arr = thread_slot->levels;
  627. for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {
  628. slot = sync_thread_levels_get_nth(arr, i);
  629. if (slot->latch != NULL && (!dict_mutex_allowed ||
  630. (slot->level != SYNC_DICT
  631. && slot->level != SYNC_PURGE_IS_RUNNING))) {
  632. lock = slot->latch;
  633. mutex = slot->latch;
  634. mutex_exit(&sync_thread_mutex);
  635. sync_print();
  636. ut_error;
  637. return(FALSE);
  638. }
  639. }
  640. mutex_exit(&sync_thread_mutex);
  641. return(TRUE);
  642. }
  643. /**********************************************************************
  644. Checks that the level array for the current thread is empty. */
  645. ibool
  646. sync_thread_levels_empty(void)
  647. /*==========================*/
  648. /* out: TRUE if empty */
  649. {
  650. return(sync_thread_levels_empty_gen(FALSE));
  651. }
  652. /**********************************************************************
  653. Adds a latch and its level in the thread level array. Allocates the memory
  654. for the array if called first time for this OS thread. Makes the checks
  655. against other latch levels stored in the array for this thread. */
  656. void
  657. sync_thread_add_level(
  658. /*==================*/
  659. void* latch, /* in: pointer to a mutex or an rw-lock */
  660. ulint level) /* in: level in the latching order; if SYNC_LEVEL_NONE,
  661. nothing is done */
  662. {
  663. sync_level_t* array;
  664. sync_level_t* slot;
  665. sync_thread_t* thread_slot;
  666. ulint i;
  667. if (!sync_order_checks_on) {
  668. return;
  669. }
  670. if ((latch == (void*)&sync_thread_mutex)
  671.     || (latch == (void*)&mutex_list_mutex)
  672.     || (latch == (void*)&rw_lock_debug_mutex)
  673.     || (latch == (void*)&rw_lock_list_mutex)) {
  674. return;
  675. }
  676. if (level == SYNC_LEVEL_NONE) {
  677. return;
  678. }
  679. mutex_enter(&sync_thread_mutex);
  680. thread_slot = sync_thread_level_arrays_find_slot();
  681. if (thread_slot == NULL) {
  682. /* We have to allocate the level array for a new thread */
  683. array = ut_malloc(sizeof(sync_level_t) * SYNC_THREAD_N_LEVELS);
  684. thread_slot = sync_thread_level_arrays_find_free();
  685.   thread_slot->id = os_thread_get_curr_id();
  686. thread_slot->levels = array;
  687. for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {
  688. slot = sync_thread_levels_get_nth(array, i);
  689. slot->latch = NULL;
  690. }
  691. }
  692. array = thread_slot->levels;
  693.  
  694. /* NOTE that there is a problem with _NODE and _LEAF levels: if the
  695. B-tree height changes, then a leaf can change to an internal node
  696. or the other way around. We do not know at present if this can cause
  697. unnecessary assertion failures below. */
  698. if (level == SYNC_NO_ORDER_CHECK) {
  699. /* Do no order checking */
  700. } else if (level == SYNC_MEM_POOL) {
  701. ut_a(sync_thread_levels_g(array, SYNC_MEM_POOL));
  702. } else if (level == SYNC_MEM_HASH) {
  703. ut_a(sync_thread_levels_g(array, SYNC_MEM_HASH));
  704. } else if (level == SYNC_RECV) {
  705. ut_a(sync_thread_levels_g(array, SYNC_RECV));
  706. } else if (level == SYNC_LOG) {
  707. ut_a(sync_thread_levels_g(array, SYNC_LOG));
  708. } else if (level == SYNC_ANY_LATCH) {
  709. ut_a(sync_thread_levels_g(array, SYNC_ANY_LATCH));
  710. } else if (level == SYNC_TRX_SYS_HEADER) {
  711. ut_a(sync_thread_levels_contain(array, SYNC_KERNEL));
  712. } else if (level == SYNC_BUF_BLOCK) {
  713. ut_a((sync_thread_levels_contain(array, SYNC_BUF_POOL)
  714. && sync_thread_levels_g(array, SYNC_BUF_BLOCK - 1))
  715.      || sync_thread_levels_g(array, SYNC_BUF_BLOCK));
  716. } else if (level == SYNC_BUF_POOL) {
  717. ut_a(sync_thread_levels_g(array, SYNC_BUF_POOL));
  718. } else if (level == SYNC_SEARCH_SYS) {
  719. ut_a(sync_thread_levels_g(array, SYNC_SEARCH_SYS));
  720. } else if (level == SYNC_TRX_LOCK_HEAP) {
  721. ut_a(sync_thread_levels_g(array, SYNC_TRX_LOCK_HEAP));
  722. } else if (level == SYNC_REC_LOCK) {
  723. ut_a((sync_thread_levels_contain(array, SYNC_KERNEL)
  724. && sync_thread_levels_g(array, SYNC_REC_LOCK - 1))
  725.      || sync_thread_levels_g(array, SYNC_REC_LOCK));
  726. } else if (level == SYNC_KERNEL) {
  727. ut_a(sync_thread_levels_g(array, SYNC_KERNEL));
  728. } else if (level == SYNC_IBUF_BITMAP) {
  729. ut_a((sync_thread_levels_contain(array, SYNC_IBUF_BITMAP_MUTEX)
  730.          && sync_thread_levels_g(array, SYNC_IBUF_BITMAP - 1))
  731.      || sync_thread_levels_g(array, SYNC_IBUF_BITMAP));
  732. } else if (level == SYNC_IBUF_BITMAP_MUTEX) {
  733. ut_a(sync_thread_levels_g(array, SYNC_IBUF_BITMAP_MUTEX));
  734. } else if (level == SYNC_FSP_PAGE) {
  735. ut_a(sync_thread_levels_contain(array, SYNC_FSP));
  736. } else if (level == SYNC_FSP) {
  737. ut_a(sync_thread_levels_contain(array, SYNC_FSP)
  738.      || sync_thread_levels_g(array, SYNC_FSP));
  739. } else if (level == SYNC_TRX_UNDO_PAGE) {
  740. ut_a(sync_thread_levels_contain(array, SYNC_TRX_UNDO)
  741.      || sync_thread_levels_contain(array, SYNC_RSEG)
  742.      || sync_thread_levels_contain(array, SYNC_PURGE_SYS)
  743.      || sync_thread_levels_g(array, SYNC_TRX_UNDO_PAGE));
  744. } else if (level == SYNC_RSEG_HEADER) {
  745. ut_a(sync_thread_levels_contain(array, SYNC_RSEG));
  746. } else if (level == SYNC_RSEG_HEADER_NEW) {
  747. ut_a(sync_thread_levels_contain(array, SYNC_KERNEL)
  748.      && sync_thread_levels_contain(array, SYNC_FSP_PAGE));
  749. } else if (level == SYNC_RSEG) {
  750. ut_a(sync_thread_levels_g(array, SYNC_RSEG));
  751. } else if (level == SYNC_TRX_UNDO) {
  752. ut_a(sync_thread_levels_g(array, SYNC_TRX_UNDO));
  753. } else if (level == SYNC_PURGE_LATCH) {
  754. ut_a(sync_thread_levels_g(array, SYNC_PURGE_LATCH));
  755. } else if (level == SYNC_PURGE_SYS) {
  756. ut_a(sync_thread_levels_g(array, SYNC_PURGE_SYS));
  757. } else if (level == SYNC_TREE_NODE) {
  758. ut_a(sync_thread_levels_contain(array, SYNC_INDEX_TREE)
  759.      || sync_thread_levels_g(array, SYNC_TREE_NODE - 1));
  760. } else if (level == SYNC_TREE_NODE_FROM_HASH) {
  761. ut_a(1);
  762. } else if (level == SYNC_TREE_NODE_NEW) {
  763. ut_a(sync_thread_levels_contain(array, SYNC_FSP_PAGE)
  764.      || sync_thread_levels_contain(array, SYNC_IBUF_MUTEX));
  765. } else if (level == SYNC_INDEX_TREE) {
  766. ut_a((sync_thread_levels_contain(array, SYNC_IBUF_MUTEX)
  767.       && sync_thread_levels_contain(array, SYNC_FSP)
  768.       && sync_thread_levels_g(array, SYNC_FSP_PAGE - 1))
  769.      || sync_thread_levels_g(array, SYNC_TREE_NODE - 1));
  770. } else if (level == SYNC_IBUF_MUTEX) {
  771. ut_a(sync_thread_levels_g(array, SYNC_FSP_PAGE - 1));
  772. } else if (level == SYNC_IBUF_PESS_INSERT_MUTEX) {
  773. ut_a(sync_thread_levels_g(array, SYNC_FSP - 1)
  774.      && !sync_thread_levels_contain(array, SYNC_IBUF_MUTEX));
  775. } else if (level == SYNC_IBUF_HEADER) {
  776. ut_a(sync_thread_levels_g(array, SYNC_FSP - 1)
  777.      && !sync_thread_levels_contain(array, SYNC_IBUF_MUTEX)
  778.      && !sync_thread_levels_contain(array,
  779. SYNC_IBUF_PESS_INSERT_MUTEX));
  780. } else if (level == SYNC_DICT_HEADER) {
  781. ut_a(sync_thread_levels_g(array, SYNC_DICT_HEADER));
  782. } else if (level == SYNC_PURGE_IS_RUNNING) {
  783. ut_a(sync_thread_levels_g(array, SYNC_PURGE_IS_RUNNING));
  784. } else if (level == SYNC_DICT) {
  785. ut_a(buf_debug_prints
  786.      || sync_thread_levels_g(array, SYNC_DICT));
  787. } else {
  788. ut_error;
  789. }
  790. for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {
  791. slot = sync_thread_levels_get_nth(array, i);
  792. if (slot->latch == NULL) {
  793. slot->latch = latch;
  794. slot->level = level;
  795. break;
  796. }
  797. }
  798. ut_a(i < SYNC_THREAD_N_LEVELS);
  799. mutex_exit(&sync_thread_mutex);
  800. }
  801. /**********************************************************************
  802. Removes a latch from the thread level array if it is found there. */
  803. ibool
  804. sync_thread_reset_level(
  805. /*====================*/
  806. /* out: TRUE if found from the array; it is an error
  807. if the latch is not found */
  808. void* latch) /* in: pointer to a mutex or an rw-lock */
  809. {
  810. sync_level_t* array;
  811. sync_level_t* slot;
  812. sync_thread_t* thread_slot;
  813. ulint i;
  814. if (!sync_order_checks_on) {
  815. return(FALSE);
  816. }
  817. if ((latch == (void*)&sync_thread_mutex)
  818.     || (latch == (void*)&mutex_list_mutex)
  819.     || (latch == (void*)&rw_lock_debug_mutex)
  820.     || (latch == (void*)&rw_lock_list_mutex)) {
  821. return(FALSE);
  822. }
  823. mutex_enter(&sync_thread_mutex);
  824. thread_slot = sync_thread_level_arrays_find_slot();
  825. if (thread_slot == NULL) {
  826. ut_error;
  827. mutex_exit(&sync_thread_mutex);
  828. return(FALSE);
  829. }
  830. array = thread_slot->levels;
  831. for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {
  832. slot = sync_thread_levels_get_nth(array, i);
  833. if (slot->latch == latch) {
  834. slot->latch = NULL;
  835. mutex_exit(&sync_thread_mutex);
  836. return(TRUE);
  837. }
  838. }
  839. ut_error;
  840. mutex_exit(&sync_thread_mutex);
  841. return(FALSE);
  842. }
  843. /**********************************************************************
  844. Initializes the synchronization data structures. */
  845. void
  846. sync_init(void)
  847. /*===========*/
  848. {
  849. sync_thread_t* thread_slot;
  850. ulint i;
  851. ut_a(sync_initialized == FALSE);
  852. sync_initialized = TRUE;
  853. /* Create the primary system wait array which is protected by an OS
  854. mutex */
  855. sync_primary_wait_array = sync_array_create(OS_THREAD_MAX_N,
  856.     SYNC_ARRAY_OS_MUTEX);
  857. /* Create the thread latch level array where the latch levels
  858. are stored for each OS thread */
  859. sync_thread_level_arrays = ut_malloc(OS_THREAD_MAX_N
  860. * sizeof(sync_thread_t));
  861. for (i = 0; i < OS_THREAD_MAX_N; i++) {
  862. thread_slot = sync_thread_level_arrays_get_nth(i);
  863. thread_slot->levels = NULL;
  864. }
  865.         /* Init the mutex list and create the mutex to protect it. */
  866. UT_LIST_INIT(mutex_list);
  867.         mutex_create(&mutex_list_mutex);
  868.         mutex_set_level(&mutex_list_mutex, SYNC_NO_ORDER_CHECK);
  869.         mutex_create(&sync_thread_mutex);
  870.         mutex_set_level(&sync_thread_mutex, SYNC_NO_ORDER_CHECK);
  871.         
  872. /* Init the rw-lock list and create the mutex to protect it. */
  873. UT_LIST_INIT(rw_lock_list);
  874.         mutex_create(&rw_lock_list_mutex);
  875.         mutex_set_level(&rw_lock_list_mutex, SYNC_NO_ORDER_CHECK);
  876.         mutex_create(&rw_lock_debug_mutex);
  877.         mutex_set_level(&rw_lock_debug_mutex, SYNC_NO_ORDER_CHECK);
  878. rw_lock_debug_event = os_event_create(NULL);
  879. rw_lock_debug_waiters = FALSE;
  880. }
  881. /**********************************************************************
  882. Frees the resources in synchronization data structures. */
  883. void
  884. sync_close(void)
  885. /*===========*/
  886. {
  887. sync_array_free(sync_primary_wait_array);
  888. }
  889. /***********************************************************************
  890. Prints wait info of the sync system. */
  891. void
  892. sync_print_wait_info(void)
  893. /*======================*/
  894. {
  895. printf(
  896. "Mut ex %lu sp %lu r %lu sys %lu; rws %lu %lu %lu; rwx %lu %lu %lun",
  897. mutex_exit_count,
  898. mutex_spin_wait_count, mutex_spin_round_count,
  899. mutex_system_call_count,
  900. rw_s_exit_count,
  901. rw_s_spin_wait_count, rw_s_system_call_count,
  902. rw_x_exit_count,
  903. rw_x_spin_wait_count, rw_x_system_call_count);
  904. }
  905. /***********************************************************************
  906. Prints info of the sync system. */
  907. void
  908. sync_print(void)
  909. /*============*/
  910. {
  911. printf("SYNC INFO:------------------------------------------n");
  912. mutex_list_print_info();
  913. rw_lock_list_print_info();
  914. sync_array_print_info(sync_primary_wait_array);
  915. sync_print_wait_info();
  916. printf("----------------------------------------------------n");
  917. }