You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1456 lines
40 KiB

17 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
16 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
  3. Copyright (c) 2008, Google Inc.
  4. Portions of this file contain modifications contributed and copyrighted by
  5. Google, Inc. Those modifications are gratefully acknowledged and are described
  6. briefly in the InnoDB documentation. The contributions by Google are
  7. incorporated with their permission, and subject to the conditions contained in
  8. the file COPYING.Google.
  9. This program is free software; you can redistribute it and/or modify it under
  10. the terms of the GNU General Public License as published by the Free Software
  11. Foundation; version 2 of the License.
  12. This program is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License along with
  16. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  17. Place, Suite 330, Boston, MA 02111-1307 USA
  18. *****************************************************************************/
  19. /**************************************************//**
  20. @file sync/sync0sync.c
  21. Mutex, the basic synchronization primitive
  22. Created 9/5/1995 Heikki Tuuri
  23. *******************************************************/
  24. #include "sync0sync.h"
  25. #ifdef UNIV_NONINL
  26. #include "sync0sync.ic"
  27. #endif
  28. #include "sync0rw.h"
  29. #include "buf0buf.h"
  30. #include "srv0srv.h"
  31. #include "buf0types.h"
  32. #include "os0sync.h" /* for HAVE_ATOMIC_BUILTINS */
  33. /*
  34. REASONS FOR IMPLEMENTING THE SPIN LOCK MUTEX
  35. ============================================
  36. Semaphore operations in operating systems are slow: Solaris on a 1993 Sparc
  37. takes 3 microseconds (us) for a lock-unlock pair and Windows NT on a 1995
  38. Pentium takes 20 microseconds for a lock-unlock pair. Therefore, we have to
  39. implement our own efficient spin lock mutex. Future operating systems may
  40. provide efficient spin locks, but we cannot count on that.
  41. Another reason for implementing a spin lock is that on multiprocessor systems
  42. it can be more efficient for a processor to run a loop waiting for the
  43. semaphore to be released than to switch to a different thread. A thread switch
  44. takes 25 us on both platforms mentioned above. See Gray and Reuter's book
  45. Transaction processing for background.
  46. How long should the spin loop last before suspending the thread? On a
  47. uniprocessor, spinning does not help at all, because if the thread owning the
  48. mutex is not executing, it cannot be released. Spinning actually wastes
  49. resources.
  50. On a multiprocessor, we do not know if the thread owning the mutex is
  51. executing or not. Thus it would make sense to spin as long as the operation
  52. guarded by the mutex would typically last assuming that the thread is
  53. executing. If the mutex is not released by that time, we may assume that the
  54. thread owning the mutex is not executing and suspend the waiting thread.
  55. A typical operation (where no i/o involved) guarded by a mutex or a read-write
  56. lock may last 1 - 20 us on the current Pentium platform. The longest
  57. operations are the binary searches on an index node.
  58. We conclude that the best choice is to set the spin time at 20 us. Then the
  59. system should work well on a multiprocessor. On a uniprocessor we have to
  60. make sure that thread swithches due to mutex collisions are not frequent,
  61. i.e., they do not happen every 100 us or so, because that wastes too much
  62. resources. If the thread switches are not frequent, the 20 us wasted in spin
  63. loop is not too much.
  64. Empirical studies on the effect of spin time should be done for different
  65. platforms.
  66. IMPLEMENTATION OF THE MUTEX
  67. ===========================
  68. For background, see Curt Schimmel's book on Unix implementation on modern
  69. architectures. The key points in the implementation are atomicity and
  70. serialization of memory accesses. The test-and-set instruction (XCHG in
  71. Pentium) must be atomic. As new processors may have weak memory models, also
  72. serialization of memory references may be necessary. The successor of Pentium,
  73. P6, has at least one mode where the memory model is weak. As far as we know,
  74. in Pentium all memory accesses are serialized in the program order and we do
  75. not have to worry about the memory model. On other processors there are
  76. special machine instructions called a fence, memory barrier, or storage
  77. barrier (STBAR in Sparc), which can be used to serialize the memory accesses
  78. to happen in program order relative to the fence instruction.
  79. Leslie Lamport has devised a "bakery algorithm" to implement a mutex without
  80. the atomic test-and-set, but his algorithm should be modified for weak memory
  81. models. We do not use Lamport's algorithm, because we guess it is slower than
  82. the atomic test-and-set.
  83. Our mutex implementation works as follows: After that we perform the atomic
  84. test-and-set instruction on the memory word. If the test returns zero, we
  85. know we got the lock first. If the test returns not zero, some other thread
  86. was quicker and got the lock: then we spin in a loop reading the memory word,
  87. waiting it to become zero. It is wise to just read the word in the loop, not
  88. perform numerous test-and-set instructions, because they generate memory
  89. traffic between the cache and the main memory. The read loop can just access
  90. the cache, saving bus bandwidth.
  91. If we cannot acquire the mutex lock in the specified time, we reserve a cell
  92. in the wait array, set the waiters byte in the mutex to 1. To avoid a race
  93. condition, after setting the waiters byte and before suspending the waiting
  94. thread, we still have to check that the mutex is reserved, because it may
  95. have happened that the thread which was holding the mutex has just released
  96. it and did not see the waiters byte set to 1, a case which would lead the
  97. other thread to an infinite wait.
  98. LEMMA 1: After a thread resets the event of a mutex (or rw_lock), some
  99. =======
  100. thread will eventually call os_event_set() on that particular event.
  101. Thus no infinite wait is possible in this case.
  102. Proof: After making the reservation the thread sets the waiters field in the
  103. mutex to 1. Then it checks that the mutex is still reserved by some thread,
  104. or it reserves the mutex for itself. In any case, some thread (which may be
  105. also some earlier thread, not necessarily the one currently holding the mutex)
  106. will set the waiters field to 0 in mutex_exit, and then call
  107. os_event_set() with the mutex as an argument.
  108. Q.E.D.
  109. LEMMA 2: If an os_event_set() call is made after some thread has called
  110. =======
  111. the os_event_reset() and before it starts wait on that event, the call
  112. will not be lost to the second thread. This is true even if there is an
  113. intervening call to os_event_reset() by another thread.
  114. Thus no infinite wait is possible in this case.
  115. Proof (non-windows platforms): os_event_reset() returns a monotonically
  116. increasing value of signal_count. This value is increased at every
  117. call of os_event_set() If thread A has called os_event_reset() followed
  118. by thread B calling os_event_set() and then some other thread C calling
  119. os_event_reset(), the is_set flag of the event will be set to FALSE;
  120. but now if thread A calls os_event_wait_low() with the signal_count
  121. value returned from the earlier call of os_event_reset(), it will
  122. return immediately without waiting.
  123. Q.E.D.
  124. Proof (windows): If there is a writer thread which is forced to wait for
  125. the lock, it may be able to set the state of rw_lock to RW_LOCK_WAIT_EX
  126. The design of rw_lock ensures that there is one and only one thread
  127. that is able to change the state to RW_LOCK_WAIT_EX and this thread is
  128. guaranteed to acquire the lock after it is released by the current
  129. holders and before any other waiter gets the lock.
  130. On windows this thread waits on a separate event i.e.: wait_ex_event.
  131. Since only one thread can wait on this event there is no chance
  132. of this event getting reset before the writer starts wait on it.
  133. Therefore, this thread is guaranteed to catch the os_set_event()
  134. signalled unconditionally at the release of the lock.
  135. Q.E.D. */
  136. /* Number of spin waits on mutexes: for performance monitoring */
  137. /** The number of iterations in the mutex_spin_wait() spin loop.
  138. Intended for performance monitoring. */
  139. static ib_int64_t mutex_spin_round_count = 0;
  140. /** The number of mutex_spin_wait() calls. Intended for
  141. performance monitoring. */
  142. static ib_int64_t mutex_spin_wait_count = 0;
  143. /** The number of OS waits in mutex_spin_wait(). Intended for
  144. performance monitoring. */
  145. static ib_int64_t mutex_os_wait_count = 0;
  146. /** The number of mutex_exit() calls. Intended for performance
  147. monitoring. */
  148. UNIV_INTERN ib_int64_t mutex_exit_count = 0;
  149. /** The global array of wait cells for implementation of the database's own
  150. mutexes and read-write locks */
  151. UNIV_INTERN sync_array_t* sync_primary_wait_array;
  152. /** This variable is set to TRUE when sync_init is called */
  153. UNIV_INTERN ibool sync_initialized = FALSE;
  154. /** An acquired mutex or rw-lock and its level in the latching order */
  155. typedef struct sync_level_struct sync_level_t;
  156. /** Mutexes or rw-locks held by a thread */
  157. typedef struct sync_thread_struct sync_thread_t;
  158. #ifdef UNIV_SYNC_DEBUG
  159. /** The latch levels currently owned by threads are stored in this data
  160. structure; the size of this array is OS_THREAD_MAX_N */
  161. UNIV_INTERN sync_thread_t* sync_thread_level_arrays;
  162. /** Mutex protecting sync_thread_level_arrays */
  163. UNIV_INTERN mutex_t sync_thread_mutex;
  164. #endif /* UNIV_SYNC_DEBUG */
  165. /** Global list of database mutexes (not OS mutexes) created. */
  166. UNIV_INTERN ut_list_base_node_t mutex_list;
  167. /** Mutex protecting the mutex_list variable */
  168. UNIV_INTERN mutex_t mutex_list_mutex;
  169. #ifdef UNIV_SYNC_DEBUG
  170. /** Latching order checks start when this is set TRUE */
  171. UNIV_INTERN ibool sync_order_checks_on = FALSE;
  172. #endif /* UNIV_SYNC_DEBUG */
  173. /** Mutexes or rw-locks held by a thread */
  174. struct sync_thread_struct{
  175. os_thread_id_t id; /*!< OS thread id */
  176. sync_level_t* levels; /*!< level array for this thread; if
  177. this is NULL this slot is unused */
  178. };
  179. /** Number of slots reserved for each OS thread in the sync level array */
  180. #define SYNC_THREAD_N_LEVELS 10000
  181. /** An acquired mutex or rw-lock and its level in the latching order */
  182. struct sync_level_struct{
  183. void* latch; /*!< pointer to a mutex or an rw-lock; NULL means that
  184. the slot is empty */
  185. ulint level; /*!< level of the latch in the latching order */
  186. };
  187. /******************************************************************//**
  188. Creates, or rather, initializes a mutex object in a specified memory
  189. location (which must be appropriately aligned). The mutex is initialized
  190. in the reset state. Explicit freeing of the mutex with mutex_free is
  191. necessary only if the memory block containing it is freed. */
  192. UNIV_INTERN
  193. void
  194. mutex_create_func(
  195. /*==============*/
  196. mutex_t* mutex, /*!< in: pointer to memory */
  197. const char* cmutex_name, /*!< in: mutex name */
  198. #ifdef UNIV_DEBUG
  199. # ifdef UNIV_SYNC_DEBUG
  200. ulint level, /*!< in: level */
  201. # endif /* UNIV_SYNC_DEBUG */
  202. #endif /* UNIV_DEBUG */
  203. const char* cfile_name, /*!< in: file name where created */
  204. ulint cline) /*!< in: file line where created */
  205. {
  206. #if defined(HAVE_ATOMIC_BUILTINS)
  207. mutex_reset_lock_word(mutex);
  208. #else
  209. os_fast_mutex_init(&(mutex->os_fast_mutex));
  210. mutex->lock_word = 0;
  211. #endif
  212. mutex->event = os_event_create(NULL);
  213. mutex->waiters = 0;
  214. #ifdef UNIV_DEBUG
  215. mutex->magic_n = MUTEX_MAGIC_N;
  216. #endif /* UNIV_DEBUG */
  217. #ifdef UNIV_SYNC_DEBUG
  218. mutex->line = 0;
  219. mutex->file_name = "not yet reserved";
  220. mutex->level = level;
  221. #endif /* UNIV_SYNC_DEBUG */
  222. #ifdef UNIV_DEBUG
  223. mutex->cfile_name = cfile_name;
  224. mutex->cline = cline;
  225. #endif /* UNIV_DEBUG */
  226. mutex->count_os_wait = 0;
  227. mutex->cmutex_name= cmutex_name;
  228. #ifdef UNIV_DEBUG
  229. mutex->count_using= 0;
  230. mutex->mutex_type= 0;
  231. mutex->lspent_time= 0;
  232. mutex->lmax_spent_time= 0;
  233. mutex->count_spin_loop= 0;
  234. mutex->count_spin_rounds= 0;
  235. mutex->count_os_yield= 0;
  236. #endif /* UNIV_DEBUG */
  237. /* Check that lock_word is aligned; this is important on Intel */
  238. ut_ad(((ulint)(&(mutex->lock_word))) % 4 == 0);
  239. /* NOTE! The very first mutexes are not put to the mutex list */
  240. if ((mutex == &mutex_list_mutex)
  241. #ifdef UNIV_SYNC_DEBUG
  242. || (mutex == &sync_thread_mutex)
  243. #endif /* UNIV_SYNC_DEBUG */
  244. ) {
  245. return;
  246. }
  247. mutex_enter(&mutex_list_mutex);
  248. ut_ad(UT_LIST_GET_LEN(mutex_list) == 0
  249. || UT_LIST_GET_FIRST(mutex_list)->magic_n == MUTEX_MAGIC_N);
  250. UT_LIST_ADD_FIRST(list, mutex_list, mutex);
  251. mutex_exit(&mutex_list_mutex);
  252. }
  253. /******************************************************************//**
  254. Calling this function is obligatory only if the memory buffer containing
  255. the mutex is freed. Removes a mutex object from the mutex list. The mutex
  256. is checked to be in the reset state. */
  257. UNIV_INTERN
  258. void
  259. mutex_free(
  260. /*=======*/
  261. mutex_t* mutex) /*!< in: mutex */
  262. {
  263. ut_ad(mutex_validate(mutex));
  264. ut_a(mutex_get_lock_word(mutex) == 0);
  265. ut_a(mutex_get_waiters(mutex) == 0);
  266. if (mutex != &mutex_list_mutex
  267. #ifdef UNIV_SYNC_DEBUG
  268. && mutex != &sync_thread_mutex
  269. #endif /* UNIV_SYNC_DEBUG */
  270. ) {
  271. mutex_enter(&mutex_list_mutex);
  272. ut_ad(!UT_LIST_GET_PREV(list, mutex)
  273. || UT_LIST_GET_PREV(list, mutex)->magic_n
  274. == MUTEX_MAGIC_N);
  275. ut_ad(!UT_LIST_GET_NEXT(list, mutex)
  276. || UT_LIST_GET_NEXT(list, mutex)->magic_n
  277. == MUTEX_MAGIC_N);
  278. UT_LIST_REMOVE(list, mutex_list, mutex);
  279. mutex_exit(&mutex_list_mutex);
  280. }
  281. os_event_free(mutex->event);
  282. #if !defined(HAVE_ATOMIC_BUILTINS)
  283. os_fast_mutex_free(&(mutex->os_fast_mutex));
  284. #endif
  285. /* If we free the mutex protecting the mutex list (freeing is
  286. not necessary), we have to reset the magic number AFTER removing
  287. it from the list. */
  288. #ifdef UNIV_DEBUG
  289. mutex->magic_n = 0;
  290. #endif /* UNIV_DEBUG */
  291. }
  292. /********************************************************************//**
  293. NOTE! Use the corresponding macro in the header file, not this function
  294. directly. Tries to lock the mutex for the current thread. If the lock is not
  295. acquired immediately, returns with return value 1.
  296. @return 0 if succeed, 1 if not */
  297. UNIV_INTERN
  298. ulint
  299. mutex_enter_nowait_func(
  300. /*====================*/
  301. mutex_t* mutex, /*!< in: pointer to mutex */
  302. const char* file_name __attribute__((unused)),
  303. /*!< in: file name where mutex
  304. requested */
  305. ulint line __attribute__((unused)))
  306. /*!< in: line where requested */
  307. {
  308. ut_ad(mutex_validate(mutex));
  309. if (!mutex_test_and_set(mutex)) {
  310. ut_d(mutex->thread_id = os_thread_get_curr_id());
  311. #ifdef UNIV_SYNC_DEBUG
  312. mutex_set_debug_info(mutex, file_name, line);
  313. #endif
  314. return(0); /* Succeeded! */
  315. }
  316. return(1);
  317. }
  318. #ifdef UNIV_DEBUG
  319. /******************************************************************//**
  320. Checks that the mutex has been initialized.
  321. @return TRUE */
  322. UNIV_INTERN
  323. ibool
  324. mutex_validate(
  325. /*===========*/
  326. const mutex_t* mutex) /*!< in: mutex */
  327. {
  328. ut_a(mutex);
  329. ut_a(mutex->magic_n == MUTEX_MAGIC_N);
  330. return(TRUE);
  331. }
  332. /******************************************************************//**
  333. Checks that the current thread owns the mutex. Works only in the debug
  334. version.
  335. @return TRUE if owns */
  336. UNIV_INTERN
  337. ibool
  338. mutex_own(
  339. /*======*/
  340. const mutex_t* mutex) /*!< in: mutex */
  341. {
  342. ut_ad(mutex_validate(mutex));
  343. return(mutex_get_lock_word(mutex) == 1
  344. && os_thread_eq(mutex->thread_id, os_thread_get_curr_id()));
  345. }
  346. #endif /* UNIV_DEBUG */
  347. /******************************************************************//**
  348. Sets the waiters field in a mutex. */
  349. UNIV_INTERN
  350. void
  351. mutex_set_waiters(
  352. /*==============*/
  353. mutex_t* mutex, /*!< in: mutex */
  354. ulint n) /*!< in: value to set */
  355. {
  356. volatile ulint* ptr; /* declared volatile to ensure that
  357. the value is stored to memory */
  358. ut_ad(mutex);
  359. #ifdef INNODB_RW_LOCKS_USE_ATOMICS
  360. if (n) {
  361. os_compare_and_swap_ulint(&mutex->waiters, 0, 1);
  362. } else {
  363. os_compare_and_swap_ulint(&mutex->waiters, 1, 0);
  364. }
  365. #else
  366. ptr = &(mutex->waiters);
  367. *ptr = n; /* Here we assume that the write of a single
  368. word in memory is atomic */
  369. #endif
  370. }
  371. /******************************************************************//**
  372. Reserves a mutex for the current thread. If the mutex is reserved, the
  373. function spins a preset time (controlled by SYNC_SPIN_ROUNDS), waiting
  374. for the mutex before suspending the thread. */
  375. UNIV_INTERN
  376. void
  377. mutex_spin_wait(
  378. /*============*/
  379. mutex_t* mutex, /*!< in: pointer to mutex */
  380. const char* file_name, /*!< in: file name where mutex
  381. requested */
  382. ulint line) /*!< in: line where requested */
  383. {
  384. ulint index; /* index of the reserved wait cell */
  385. ulint i; /* spin round count */
  386. #ifdef UNIV_DEBUG
  387. ib_int64_t lstart_time = 0, lfinish_time; /* for timing os_wait */
  388. ulint ltime_diff;
  389. ulint sec;
  390. ulint ms;
  391. uint timer_started = 0;
  392. #endif /* UNIV_DEBUG */
  393. ut_ad(mutex);
  394. /* This update is not thread safe, but we don't mind if the count
  395. isn't exact. Moved out of ifdef that follows because we are willing
  396. to sacrifice the cost of counting this as the data is valuable.
  397. Count the number of calls to mutex_spin_wait. */
  398. mutex_spin_wait_count++;
  399. mutex_loop:
  400. i = 0;
  401. /* Spin waiting for the lock word to become zero. Note that we do
  402. not have to assume that the read access to the lock word is atomic,
  403. as the actual locking is always committed with atomic test-and-set.
  404. In reality, however, all processors probably have an atomic read of
  405. a memory word. */
  406. spin_loop:
  407. ut_d(mutex->count_spin_loop++);
  408. while (mutex_get_lock_word(mutex) != 0 && i < SYNC_SPIN_ROUNDS) {
  409. if (srv_spin_wait_delay) {
  410. ut_delay(ut_rnd_interval(0, srv_spin_wait_delay));
  411. }
  412. i++;
  413. }
  414. if (i == SYNC_SPIN_ROUNDS) {
  415. #ifdef UNIV_DEBUG
  416. mutex->count_os_yield++;
  417. #ifndef UNIV_HOTBACKUP
  418. if (timed_mutexes && timer_started == 0) {
  419. ut_usectime(&sec, &ms);
  420. lstart_time= (ib_int64_t)sec * 1000000 + ms;
  421. timer_started = 1;
  422. }
  423. #endif /* UNIV_HOTBACKUP */
  424. #endif /* UNIV_DEBUG */
  425. os_thread_yield();
  426. }
  427. #ifdef UNIV_SRV_PRINT_LATCH_WAITS
  428. fprintf(stderr,
  429. "Thread %lu spin wait mutex at %p"
  430. " '%s' rnds %lu\n",
  431. (ulong) os_thread_pf(os_thread_get_curr_id()), (void*) mutex,
  432. mutex->cmutex_name, (ulong) i);
  433. #endif
  434. mutex_spin_round_count += i;
  435. ut_d(mutex->count_spin_rounds += i);
  436. if (mutex_test_and_set(mutex) == 0) {
  437. /* Succeeded! */
  438. ut_d(mutex->thread_id = os_thread_get_curr_id());
  439. #ifdef UNIV_SYNC_DEBUG
  440. mutex_set_debug_info(mutex, file_name, line);
  441. #endif
  442. goto finish_timing;
  443. }
  444. /* We may end up with a situation where lock_word is 0 but the OS
  445. fast mutex is still reserved. On FreeBSD the OS does not seem to
  446. schedule a thread which is constantly calling pthread_mutex_trylock
  447. (in mutex_test_and_set implementation). Then we could end up
  448. spinning here indefinitely. The following 'i++' stops this infinite
  449. spin. */
  450. i++;
  451. if (i < SYNC_SPIN_ROUNDS) {
  452. goto spin_loop;
  453. }
  454. sync_array_reserve_cell(sync_primary_wait_array, mutex,
  455. SYNC_MUTEX, file_name, line, &index);
  456. /* The memory order of the array reservation and the change in the
  457. waiters field is important: when we suspend a thread, we first
  458. reserve the cell and then set waiters field to 1. When threads are
  459. released in mutex_exit, the waiters field is first set to zero and
  460. then the event is set to the signaled state. */
  461. mutex_set_waiters(mutex, 1);
  462. /* Try to reserve still a few times */
  463. for (i = 0; i < 4; i++) {
  464. if (mutex_test_and_set(mutex) == 0) {
  465. /* Succeeded! Free the reserved wait cell */
  466. sync_array_free_cell(sync_primary_wait_array, index);
  467. ut_d(mutex->thread_id = os_thread_get_curr_id());
  468. #ifdef UNIV_SYNC_DEBUG
  469. mutex_set_debug_info(mutex, file_name, line);
  470. #endif
  471. #ifdef UNIV_SRV_PRINT_LATCH_WAITS
  472. fprintf(stderr, "Thread %lu spin wait succeeds at 2:"
  473. " mutex at %p\n",
  474. (ulong) os_thread_pf(os_thread_get_curr_id()),
  475. (void*) mutex);
  476. #endif
  477. goto finish_timing;
  478. /* Note that in this case we leave the waiters field
  479. set to 1. We cannot reset it to zero, as we do not
  480. know if there are other waiters. */
  481. }
  482. }
  483. /* Now we know that there has been some thread holding the mutex
  484. after the change in the wait array and the waiters field was made.
  485. Now there is no risk of infinite wait on the event. */
  486. #ifdef UNIV_SRV_PRINT_LATCH_WAITS
  487. fprintf(stderr,
  488. "Thread %lu OS wait mutex at %p '%s' rnds %lu\n",
  489. (ulong) os_thread_pf(os_thread_get_curr_id()), (void*) mutex,
  490. mutex->cmutex_name, (ulong) i);
  491. #endif
  492. mutex_os_wait_count++;
  493. mutex->count_os_wait++;
  494. #ifdef UNIV_DEBUG
  495. /* !!!!! Sometimes os_wait can be called without os_thread_yield */
  496. #ifndef UNIV_HOTBACKUP
  497. if (timed_mutexes == 1 && timer_started == 0) {
  498. ut_usectime(&sec, &ms);
  499. lstart_time= (ib_int64_t)sec * 1000000 + ms;
  500. timer_started = 1;
  501. }
  502. #endif /* UNIV_HOTBACKUP */
  503. #endif /* UNIV_DEBUG */
  504. sync_array_wait_event(sync_primary_wait_array, index);
  505. goto mutex_loop;
  506. finish_timing:
  507. #ifdef UNIV_DEBUG
  508. if (timed_mutexes == 1 && timer_started==1) {
  509. ut_usectime(&sec, &ms);
  510. lfinish_time= (ib_int64_t)sec * 1000000 + ms;
  511. ltime_diff= (ulint) (lfinish_time - lstart_time);
  512. mutex->lspent_time += ltime_diff;
  513. if (mutex->lmax_spent_time < ltime_diff) {
  514. mutex->lmax_spent_time= ltime_diff;
  515. }
  516. }
  517. #endif /* UNIV_DEBUG */
  518. return;
  519. }
  520. /******************************************************************//**
  521. Releases the threads waiting in the primary wait array for this mutex. */
  522. UNIV_INTERN
  523. void
  524. mutex_signal_object(
  525. /*================*/
  526. mutex_t* mutex) /*!< in: mutex */
  527. {
  528. mutex_set_waiters(mutex, 0);
  529. /* The memory order of resetting the waiters field and
  530. signaling the object is important. See LEMMA 1 above. */
  531. os_event_set(mutex->event);
  532. sync_array_object_signalled(sync_primary_wait_array);
  533. }
  534. #ifdef UNIV_SYNC_DEBUG
  535. /******************************************************************//**
  536. Sets the debug information for a reserved mutex. */
  537. UNIV_INTERN
  538. void
  539. mutex_set_debug_info(
  540. /*=================*/
  541. mutex_t* mutex, /*!< in: mutex */
  542. const char* file_name, /*!< in: file where requested */
  543. ulint line) /*!< in: line where requested */
  544. {
  545. ut_ad(mutex);
  546. ut_ad(file_name);
  547. sync_thread_add_level(mutex, mutex->level);
  548. mutex->file_name = file_name;
  549. mutex->line = line;
  550. }
  551. /******************************************************************//**
  552. Gets the debug information for a reserved mutex. */
  553. UNIV_INTERN
  554. void
  555. mutex_get_debug_info(
  556. /*=================*/
  557. mutex_t* mutex, /*!< in: mutex */
  558. const char** file_name, /*!< out: file where requested */
  559. ulint* line, /*!< out: line where requested */
  560. os_thread_id_t* thread_id) /*!< out: id of the thread which owns
  561. the mutex */
  562. {
  563. ut_ad(mutex);
  564. *file_name = mutex->file_name;
  565. *line = mutex->line;
  566. *thread_id = mutex->thread_id;
  567. }
  568. /******************************************************************//**
  569. Prints debug info of currently reserved mutexes. */
  570. static
  571. void
  572. mutex_list_print_info(
  573. /*==================*/
  574. FILE* file) /*!< in: file where to print */
  575. {
  576. mutex_t* mutex;
  577. const char* file_name;
  578. ulint line;
  579. os_thread_id_t thread_id;
  580. ulint count = 0;
  581. fputs("----------\n"
  582. "MUTEX INFO\n"
  583. "----------\n", file);
  584. mutex_enter(&mutex_list_mutex);
  585. mutex = UT_LIST_GET_FIRST(mutex_list);
  586. while (mutex != NULL) {
  587. count++;
  588. if (mutex_get_lock_word(mutex) != 0) {
  589. mutex_get_debug_info(mutex, &file_name, &line,
  590. &thread_id);
  591. fprintf(file,
  592. "Locked mutex: addr %p thread %ld"
  593. " file %s line %ld\n",
  594. (void*) mutex, os_thread_pf(thread_id),
  595. file_name, line);
  596. }
  597. mutex = UT_LIST_GET_NEXT(list, mutex);
  598. }
  599. fprintf(file, "Total number of mutexes %ld\n", count);
  600. mutex_exit(&mutex_list_mutex);
  601. }
  602. /******************************************************************//**
  603. Counts currently reserved mutexes. Works only in the debug version.
  604. @return number of reserved mutexes */
  605. UNIV_INTERN
  606. ulint
  607. mutex_n_reserved(void)
  608. /*==================*/
  609. {
  610. mutex_t* mutex;
  611. ulint count = 0;
  612. mutex_enter(&mutex_list_mutex);
  613. mutex = UT_LIST_GET_FIRST(mutex_list);
  614. while (mutex != NULL) {
  615. if (mutex_get_lock_word(mutex) != 0) {
  616. count++;
  617. }
  618. mutex = UT_LIST_GET_NEXT(list, mutex);
  619. }
  620. mutex_exit(&mutex_list_mutex);
  621. ut_a(count >= 1);
  622. return(count - 1); /* Subtract one, because this function itself
  623. was holding one mutex (mutex_list_mutex) */
  624. }
  625. /******************************************************************//**
  626. Returns TRUE if no mutex or rw-lock is currently locked. Works only in
  627. the debug version.
  628. @return TRUE if no mutexes and rw-locks reserved */
  629. UNIV_INTERN
  630. ibool
  631. sync_all_freed(void)
  632. /*================*/
  633. {
  634. return(mutex_n_reserved() + rw_lock_n_locked() == 0);
  635. }
  636. /******************************************************************//**
  637. Gets the value in the nth slot in the thread level arrays.
  638. @return pointer to thread slot */
  639. static
  640. sync_thread_t*
  641. sync_thread_level_arrays_get_nth(
  642. /*=============================*/
  643. ulint n) /*!< in: slot number */
  644. {
  645. ut_ad(n < OS_THREAD_MAX_N);
  646. return(sync_thread_level_arrays + n);
  647. }
  648. /******************************************************************//**
  649. Looks for the thread slot for the calling thread.
  650. @return pointer to thread slot, NULL if not found */
  651. static
  652. sync_thread_t*
  653. sync_thread_level_arrays_find_slot(void)
  654. /*====================================*/
  655. {
  656. sync_thread_t* slot;
  657. os_thread_id_t id;
  658. ulint i;
  659. id = os_thread_get_curr_id();
  660. for (i = 0; i < OS_THREAD_MAX_N; i++) {
  661. slot = sync_thread_level_arrays_get_nth(i);
  662. if (slot->levels && os_thread_eq(slot->id, id)) {
  663. return(slot);
  664. }
  665. }
  666. return(NULL);
  667. }
  668. /******************************************************************//**
  669. Looks for an unused thread slot.
  670. @return pointer to thread slot */
  671. static
  672. sync_thread_t*
  673. sync_thread_level_arrays_find_free(void)
  674. /*====================================*/
  675. {
  676. sync_thread_t* slot;
  677. ulint i;
  678. for (i = 0; i < OS_THREAD_MAX_N; i++) {
  679. slot = sync_thread_level_arrays_get_nth(i);
  680. if (slot->levels == NULL) {
  681. return(slot);
  682. }
  683. }
  684. return(NULL);
  685. }
  686. /******************************************************************//**
  687. Gets the value in the nth slot in the thread level array.
  688. @return pointer to level slot */
  689. static
  690. sync_level_t*
  691. sync_thread_levels_get_nth(
  692. /*=======================*/
  693. sync_level_t* arr, /*!< in: pointer to level array for an OS
  694. thread */
  695. ulint n) /*!< in: slot number */
  696. {
  697. ut_ad(n < SYNC_THREAD_N_LEVELS);
  698. return(arr + n);
  699. }
  700. /******************************************************************//**
  701. Checks if all the level values stored in the level array are greater than
  702. the given limit.
  703. @return TRUE if all greater */
  704. static
  705. ibool
  706. sync_thread_levels_g(
  707. /*=================*/
  708. sync_level_t* arr, /*!< in: pointer to level array for an OS
  709. thread */
  710. ulint limit, /*!< in: level limit */
  711. ulint warn) /*!< in: TRUE=display a diagnostic message */
  712. {
  713. sync_level_t* slot;
  714. rw_lock_t* lock;
  715. mutex_t* mutex;
  716. ulint i;
  717. for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {
  718. slot = sync_thread_levels_get_nth(arr, i);
  719. if (slot->latch != NULL) {
  720. if (slot->level <= limit) {
  721. if (!warn) {
  722. return(FALSE);
  723. }
  724. lock = slot->latch;
  725. mutex = slot->latch;
  726. fprintf(stderr,
  727. "InnoDB: sync levels should be"
  728. " > %lu but a level is %lu\n",
  729. (ulong) limit, (ulong) slot->level);
  730. if (mutex->magic_n == MUTEX_MAGIC_N) {
  731. fprintf(stderr,
  732. "Mutex '%s'\n",
  733. mutex->cmutex_name);
  734. if (mutex_get_lock_word(mutex) != 0) {
  735. const char* file_name;
  736. ulint line;
  737. os_thread_id_t thread_id;
  738. mutex_get_debug_info(
  739. mutex, &file_name,
  740. &line, &thread_id);
  741. fprintf(stderr,
  742. "InnoDB: Locked mutex:"
  743. " addr %p thread %ld"
  744. " file %s line %ld\n",
  745. (void*) mutex,
  746. os_thread_pf(
  747. thread_id),
  748. file_name,
  749. (ulong) line);
  750. } else {
  751. fputs("Not locked\n", stderr);
  752. }
  753. } else {
  754. rw_lock_print(lock);
  755. }
  756. return(FALSE);
  757. }
  758. }
  759. }
  760. return(TRUE);
  761. }
  762. /******************************************************************//**
  763. Checks if the level value is stored in the level array.
  764. @return TRUE if stored */
  765. static
  766. ibool
  767. sync_thread_levels_contain(
  768. /*=======================*/
  769. sync_level_t* arr, /*!< in: pointer to level array for an OS
  770. thread */
  771. ulint level) /*!< in: level */
  772. {
  773. sync_level_t* slot;
  774. ulint i;
  775. for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {
  776. slot = sync_thread_levels_get_nth(arr, i);
  777. if (slot->latch != NULL) {
  778. if (slot->level == level) {
  779. return(TRUE);
  780. }
  781. }
  782. }
  783. return(FALSE);
  784. }
  785. /******************************************************************//**
  786. Checks that the level array for the current thread is empty.
  787. @return TRUE if empty except the exceptions specified below */
  788. UNIV_INTERN
  789. ibool
  790. sync_thread_levels_empty_gen(
  791. /*=========================*/
  792. ibool dict_mutex_allowed) /*!< in: TRUE if dictionary mutex is
  793. allowed to be owned by the thread,
  794. also purge_is_running mutex is
  795. allowed */
  796. {
  797. sync_level_t* arr;
  798. sync_thread_t* thread_slot;
  799. sync_level_t* slot;
  800. ulint i;
  801. if (!sync_order_checks_on) {
  802. return(TRUE);
  803. }
  804. mutex_enter(&sync_thread_mutex);
  805. thread_slot = sync_thread_level_arrays_find_slot();
  806. if (thread_slot == NULL) {
  807. mutex_exit(&sync_thread_mutex);
  808. return(TRUE);
  809. }
  810. arr = thread_slot->levels;
  811. for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {
  812. slot = sync_thread_levels_get_nth(arr, i);
  813. if (slot->latch != NULL
  814. && (!dict_mutex_allowed
  815. || (slot->level != SYNC_DICT
  816. && slot->level != SYNC_DICT_OPERATION))) {
  817. mutex_exit(&sync_thread_mutex);
  818. ut_error;
  819. return(FALSE);
  820. }
  821. }
  822. mutex_exit(&sync_thread_mutex);
  823. return(TRUE);
  824. }
  825. /******************************************************************//**
  826. Checks that the level array for the current thread is empty.
  827. @return TRUE if empty */
  828. UNIV_INTERN
  829. ibool
  830. sync_thread_levels_empty(void)
  831. /*==========================*/
  832. {
  833. return(sync_thread_levels_empty_gen(FALSE));
  834. }
  835. /******************************************************************//**
  836. Adds a latch and its level in the thread level array. Allocates the memory
  837. for the array if called first time for this OS thread. Makes the checks
  838. against other latch levels stored in the array for this thread. */
  839. UNIV_INTERN
  840. void
  841. sync_thread_add_level(
  842. /*==================*/
  843. void* latch, /*!< in: pointer to a mutex or an rw-lock */
  844. ulint level) /*!< in: level in the latching order; if
  845. SYNC_LEVEL_VARYING, nothing is done */
  846. {
  847. sync_level_t* array;
  848. sync_level_t* slot;
  849. sync_thread_t* thread_slot;
  850. ulint i;
  851. if (!sync_order_checks_on) {
  852. return;
  853. }
  854. if ((latch == (void*)&sync_thread_mutex)
  855. || (latch == (void*)&mutex_list_mutex)
  856. || (latch == (void*)&rw_lock_debug_mutex)
  857. || (latch == (void*)&rw_lock_list_mutex)) {
  858. return;
  859. }
  860. if (level == SYNC_LEVEL_VARYING) {
  861. return;
  862. }
  863. mutex_enter(&sync_thread_mutex);
  864. thread_slot = sync_thread_level_arrays_find_slot();
  865. if (thread_slot == NULL) {
  866. /* We have to allocate the level array for a new thread */
  867. array = ut_malloc(sizeof(sync_level_t) * SYNC_THREAD_N_LEVELS);
  868. thread_slot = sync_thread_level_arrays_find_free();
  869. thread_slot->id = os_thread_get_curr_id();
  870. thread_slot->levels = array;
  871. for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {
  872. slot = sync_thread_levels_get_nth(array, i);
  873. slot->latch = NULL;
  874. }
  875. }
  876. array = thread_slot->levels;
  877. /* NOTE that there is a problem with _NODE and _LEAF levels: if the
  878. B-tree height changes, then a leaf can change to an internal node
  879. or the other way around. We do not know at present if this can cause
  880. unnecessary assertion failures below. */
  881. switch (level) {
  882. case SYNC_NO_ORDER_CHECK:
  883. case SYNC_EXTERN_STORAGE:
  884. case SYNC_TREE_NODE_FROM_HASH:
  885. /* Do no order checking */
  886. break;
  887. case SYNC_MEM_POOL:
  888. case SYNC_MEM_HASH:
  889. case SYNC_RECV:
  890. case SYNC_WORK_QUEUE:
  891. case SYNC_LOG:
  892. case SYNC_THR_LOCAL:
  893. case SYNC_ANY_LATCH:
  894. case SYNC_TRX_SYS_HEADER:
  895. case SYNC_FILE_FORMAT_TAG:
  896. case SYNC_DOUBLEWRITE:
  897. case SYNC_BUF_LRU_LIST:
  898. case SYNC_BUF_FLUSH_LIST:
  899. case SYNC_BUF_PAGE_HASH:
  900. case SYNC_BUF_FREE_LIST:
  901. case SYNC_BUF_ZIP_FREE:
  902. case SYNC_BUF_ZIP_HASH:
  903. case SYNC_BUF_POOL:
  904. case SYNC_SEARCH_SYS:
  905. case SYNC_SEARCH_SYS_CONF:
  906. case SYNC_TRX_LOCK_HEAP:
  907. case SYNC_KERNEL:
  908. case SYNC_IBUF_BITMAP_MUTEX:
  909. case SYNC_RSEG:
  910. case SYNC_TRX_UNDO:
  911. case SYNC_PURGE_LATCH:
  912. case SYNC_PURGE_SYS:
  913. case SYNC_DICT_AUTOINC_MUTEX:
  914. case SYNC_DICT_OPERATION:
  915. case SYNC_DICT_HEADER:
  916. case SYNC_TRX_I_S_RWLOCK:
  917. case SYNC_TRX_I_S_LAST_READ:
  918. if (!sync_thread_levels_g(array, level, TRUE)) {
  919. fprintf(stderr,
  920. "InnoDB: sync_thread_levels_g(array, %lu)"
  921. " does not hold!\n", level);
  922. ut_error;
  923. }
  924. break;
  925. case SYNC_BUF_BLOCK:
  926. /* Either the thread must own the buffer pool mutex
  927. (buf_pool_mutex), or it is allowed to latch only ONE
  928. buffer block (block->mutex or buf_pool_zip_mutex). */
  929. if (!sync_thread_levels_g(array, level, FALSE)) {
  930. ut_a(sync_thread_levels_g(array, level - 1, TRUE));
  931. ut_a(sync_thread_levels_contain(array, SYNC_BUF_LRU_LIST));
  932. }
  933. break;
  934. case SYNC_REC_LOCK:
  935. if (sync_thread_levels_contain(array, SYNC_KERNEL)) {
  936. ut_a(sync_thread_levels_g(array, SYNC_REC_LOCK - 1,
  937. TRUE));
  938. } else {
  939. ut_a(sync_thread_levels_g(array, SYNC_REC_LOCK, TRUE));
  940. }
  941. break;
  942. case SYNC_IBUF_BITMAP:
  943. /* Either the thread must own the master mutex to all
  944. the bitmap pages, or it is allowed to latch only ONE
  945. bitmap page. */
  946. if (sync_thread_levels_contain(array,
  947. SYNC_IBUF_BITMAP_MUTEX)) {
  948. ut_a(sync_thread_levels_g(array, SYNC_IBUF_BITMAP - 1,
  949. TRUE));
  950. } else {
  951. ut_a(sync_thread_levels_g(array, SYNC_IBUF_BITMAP,
  952. TRUE));
  953. }
  954. break;
  955. case SYNC_FSP_PAGE:
  956. ut_a(sync_thread_levels_contain(array, SYNC_FSP));
  957. break;
  958. case SYNC_FSP:
  959. ut_a(sync_thread_levels_contain(array, SYNC_FSP)
  960. || sync_thread_levels_g(array, SYNC_FSP, TRUE));
  961. break;
  962. case SYNC_TRX_UNDO_PAGE:
  963. ut_a(sync_thread_levels_contain(array, SYNC_TRX_UNDO)
  964. || sync_thread_levels_contain(array, SYNC_RSEG)
  965. || sync_thread_levels_contain(array, SYNC_PURGE_SYS)
  966. || sync_thread_levels_g(array, SYNC_TRX_UNDO_PAGE, TRUE));
  967. break;
  968. case SYNC_RSEG_HEADER:
  969. ut_a(sync_thread_levels_contain(array, SYNC_RSEG));
  970. break;
  971. case SYNC_RSEG_HEADER_NEW:
  972. ut_a(sync_thread_levels_contain(array, SYNC_KERNEL)
  973. && sync_thread_levels_contain(array, SYNC_FSP_PAGE));
  974. break;
  975. case SYNC_TREE_NODE:
  976. ut_a(sync_thread_levels_contain(array, SYNC_INDEX_TREE)
  977. || sync_thread_levels_contain(array, SYNC_DICT_OPERATION)
  978. || sync_thread_levels_g(array, SYNC_TREE_NODE - 1, TRUE));
  979. break;
  980. case SYNC_TREE_NODE_NEW:
  981. ut_a(sync_thread_levels_contain(array, SYNC_FSP_PAGE)
  982. || sync_thread_levels_contain(array, SYNC_IBUF_MUTEX));
  983. break;
  984. case SYNC_INDEX_TREE:
  985. if (sync_thread_levels_contain(array, SYNC_IBUF_MUTEX)
  986. && sync_thread_levels_contain(array, SYNC_FSP)) {
  987. ut_a(sync_thread_levels_g(array, SYNC_FSP_PAGE - 1,
  988. TRUE));
  989. } else {
  990. ut_a(sync_thread_levels_g(array, SYNC_TREE_NODE - 1,
  991. TRUE));
  992. }
  993. break;
  994. case SYNC_IBUF_MUTEX:
  995. ut_a(sync_thread_levels_g(array, SYNC_FSP_PAGE - 1, TRUE));
  996. break;
  997. case SYNC_IBUF_PESS_INSERT_MUTEX:
  998. ut_a(sync_thread_levels_g(array, SYNC_FSP - 1, TRUE));
  999. ut_a(!sync_thread_levels_contain(array, SYNC_IBUF_MUTEX));
  1000. break;
  1001. case SYNC_IBUF_HEADER:
  1002. ut_a(sync_thread_levels_g(array, SYNC_FSP - 1, TRUE));
  1003. ut_a(!sync_thread_levels_contain(array, SYNC_IBUF_MUTEX));
  1004. ut_a(!sync_thread_levels_contain(array,
  1005. SYNC_IBUF_PESS_INSERT_MUTEX));
  1006. break;
  1007. case SYNC_DICT:
  1008. #ifdef UNIV_DEBUG
  1009. ut_a(buf_debug_prints
  1010. || sync_thread_levels_g(array, SYNC_DICT, TRUE));
  1011. #else /* UNIV_DEBUG */
  1012. ut_a(sync_thread_levels_g(array, SYNC_DICT, TRUE));
  1013. #endif /* UNIV_DEBUG */
  1014. break;
  1015. default:
  1016. ut_error;
  1017. }
  1018. for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {
  1019. slot = sync_thread_levels_get_nth(array, i);
  1020. if (slot->latch == NULL) {
  1021. slot->latch = latch;
  1022. slot->level = level;
  1023. break;
  1024. }
  1025. }
  1026. ut_a(i < SYNC_THREAD_N_LEVELS);
  1027. mutex_exit(&sync_thread_mutex);
  1028. }
  1029. /******************************************************************//**
  1030. Removes a latch from the thread level array if it is found there.
  1031. @return TRUE if found in the array; it is no error if the latch is
  1032. not found, as we presently are not able to determine the level for
  1033. every latch reservation the program does */
  1034. UNIV_INTERN
  1035. ibool
  1036. sync_thread_reset_level(
  1037. /*====================*/
  1038. void* latch) /*!< in: pointer to a mutex or an rw-lock */
  1039. {
  1040. sync_level_t* array;
  1041. sync_level_t* slot;
  1042. sync_thread_t* thread_slot;
  1043. ulint i;
  1044. if (!sync_order_checks_on) {
  1045. return(FALSE);
  1046. }
  1047. if ((latch == (void*)&sync_thread_mutex)
  1048. || (latch == (void*)&mutex_list_mutex)
  1049. || (latch == (void*)&rw_lock_debug_mutex)
  1050. || (latch == (void*)&rw_lock_list_mutex)) {
  1051. return(FALSE);
  1052. }
  1053. mutex_enter(&sync_thread_mutex);
  1054. thread_slot = sync_thread_level_arrays_find_slot();
  1055. if (thread_slot == NULL) {
  1056. ut_error;
  1057. mutex_exit(&sync_thread_mutex);
  1058. return(FALSE);
  1059. }
  1060. array = thread_slot->levels;
  1061. for (i = 0; i < SYNC_THREAD_N_LEVELS; i++) {
  1062. slot = sync_thread_levels_get_nth(array, i);
  1063. if (slot->latch == latch) {
  1064. slot->latch = NULL;
  1065. mutex_exit(&sync_thread_mutex);
  1066. return(TRUE);
  1067. }
  1068. }
  1069. if (((mutex_t*) latch)->magic_n != MUTEX_MAGIC_N) {
  1070. rw_lock_t* rw_lock;
  1071. rw_lock = (rw_lock_t*) latch;
  1072. if (rw_lock->level == SYNC_LEVEL_VARYING) {
  1073. mutex_exit(&sync_thread_mutex);
  1074. return(TRUE);
  1075. }
  1076. }
  1077. ut_error;
  1078. mutex_exit(&sync_thread_mutex);
  1079. return(FALSE);
  1080. }
  1081. #endif /* UNIV_SYNC_DEBUG */
  1082. /******************************************************************//**
  1083. Initializes the synchronization data structures. */
  1084. UNIV_INTERN
  1085. void
  1086. sync_init(void)
  1087. /*===========*/
  1088. {
  1089. #ifdef UNIV_SYNC_DEBUG
  1090. sync_thread_t* thread_slot;
  1091. ulint i;
  1092. #endif /* UNIV_SYNC_DEBUG */
  1093. ut_a(sync_initialized == FALSE);
  1094. sync_initialized = TRUE;
  1095. /* Create the primary system wait array which is protected by an OS
  1096. mutex */
  1097. sync_primary_wait_array = sync_array_create(OS_THREAD_MAX_N,
  1098. SYNC_ARRAY_OS_MUTEX);
  1099. #ifdef UNIV_SYNC_DEBUG
  1100. /* Create the thread latch level array where the latch levels
  1101. are stored for each OS thread */
  1102. sync_thread_level_arrays = ut_malloc(OS_THREAD_MAX_N
  1103. * sizeof(sync_thread_t));
  1104. for (i = 0; i < OS_THREAD_MAX_N; i++) {
  1105. thread_slot = sync_thread_level_arrays_get_nth(i);
  1106. thread_slot->levels = NULL;
  1107. }
  1108. #endif /* UNIV_SYNC_DEBUG */
  1109. /* Init the mutex list and create the mutex to protect it. */
  1110. UT_LIST_INIT(mutex_list);
  1111. mutex_create(&mutex_list_mutex, SYNC_NO_ORDER_CHECK);
  1112. #ifdef UNIV_SYNC_DEBUG
  1113. mutex_create(&sync_thread_mutex, SYNC_NO_ORDER_CHECK);
  1114. #endif /* UNIV_SYNC_DEBUG */
  1115. /* Init the rw-lock list and create the mutex to protect it. */
  1116. UT_LIST_INIT(rw_lock_list);
  1117. mutex_create(&rw_lock_list_mutex, SYNC_NO_ORDER_CHECK);
  1118. #ifdef UNIV_SYNC_DEBUG
  1119. mutex_create(&rw_lock_debug_mutex, SYNC_NO_ORDER_CHECK);
  1120. rw_lock_debug_event = os_event_create(NULL);
  1121. rw_lock_debug_waiters = FALSE;
  1122. #endif /* UNIV_SYNC_DEBUG */
  1123. }
  1124. /******************************************************************//**
  1125. Frees the resources in InnoDB's own synchronization data structures. Use
  1126. os_sync_free() after calling this. */
  1127. UNIV_INTERN
  1128. void
  1129. sync_close(void)
  1130. /*===========*/
  1131. {
  1132. mutex_t* mutex;
  1133. sync_array_free(sync_primary_wait_array);
  1134. mutex = UT_LIST_GET_FIRST(mutex_list);
  1135. while (mutex) {
  1136. mutex_free(mutex);
  1137. mutex = UT_LIST_GET_FIRST(mutex_list);
  1138. }
  1139. mutex_free(&mutex_list_mutex);
  1140. #ifdef UNIV_SYNC_DEBUG
  1141. mutex_free(&sync_thread_mutex);
  1142. /* Switch latching order checks on in sync0sync.c */
  1143. sync_order_checks_on = FALSE;
  1144. #endif /* UNIV_SYNC_DEBUG */
  1145. sync_initialized = FALSE;
  1146. }
  1147. /*******************************************************************//**
  1148. Prints wait info of the sync system. */
  1149. UNIV_INTERN
  1150. void
  1151. sync_print_wait_info(
  1152. /*=================*/
  1153. FILE* file) /*!< in: file where to print */
  1154. {
  1155. #ifdef UNIV_SYNC_DEBUG
  1156. fprintf(file, "Mutex exits %llu, rws exits %llu, rwx exits %llu\n",
  1157. mutex_exit_count, rw_s_exit_count, rw_x_exit_count);
  1158. #endif
  1159. fprintf(file,
  1160. "Mutex spin waits %llu, rounds %llu, OS waits %llu\n"
  1161. "RW-shared spins %llu, OS waits %llu;"
  1162. " RW-excl spins %llu, OS waits %llu\n",
  1163. mutex_spin_wait_count,
  1164. mutex_spin_round_count,
  1165. mutex_os_wait_count,
  1166. rw_s_spin_wait_count,
  1167. rw_s_os_wait_count,
  1168. rw_x_spin_wait_count,
  1169. rw_x_os_wait_count);
  1170. fprintf(file,
  1171. "Spin rounds per wait: %.2f mutex, %.2f RW-shared, "
  1172. "%.2f RW-excl\n",
  1173. (double) mutex_spin_round_count /
  1174. (mutex_spin_wait_count ? mutex_spin_wait_count : 1),
  1175. (double) rw_s_spin_round_count /
  1176. (rw_s_spin_wait_count ? rw_s_spin_wait_count : 1),
  1177. (double) rw_x_spin_round_count /
  1178. (rw_x_spin_wait_count ? rw_x_spin_wait_count : 1));
  1179. }
  1180. /*******************************************************************//**
  1181. Prints info of the sync system. */
  1182. UNIV_INTERN
  1183. void
  1184. sync_print(
  1185. /*=======*/
  1186. FILE* file) /*!< in: file where to print */
  1187. {
  1188. #ifdef UNIV_SYNC_DEBUG
  1189. mutex_list_print_info(file);
  1190. rw_lock_list_print_info(file);
  1191. #endif /* UNIV_SYNC_DEBUG */
  1192. sync_array_print_info(file, sync_primary_wait_array);
  1193. sync_print_wait_info(file);
  1194. }