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.

222 lines
6.6 KiB

20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 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 include/sync0sync.ic
  21. Mutex, the basic synchronization primitive
  22. Created 9/5/1995 Heikki Tuuri
  23. *******************************************************/
  24. /******************************************************************//**
  25. Sets the waiters field in a mutex. */
  26. UNIV_INTERN
  27. void
  28. mutex_set_waiters(
  29. /*==============*/
  30. mutex_t* mutex, /*!< in: mutex */
  31. ulint n); /*!< in: value to set */
  32. /******************************************************************//**
  33. Reserves a mutex for the current thread. If the mutex is reserved, the
  34. function spins a preset time (controlled by SYNC_SPIN_ROUNDS) waiting
  35. for the mutex before suspending the thread. */
  36. UNIV_INTERN
  37. void
  38. mutex_spin_wait(
  39. /*============*/
  40. mutex_t* mutex, /*!< in: pointer to mutex */
  41. const char* file_name, /*!< in: file name where mutex
  42. requested */
  43. ulint line); /*!< in: line where requested */
  44. #ifdef UNIV_SYNC_DEBUG
  45. /******************************************************************//**
  46. Sets the debug information for a reserved mutex. */
  47. UNIV_INTERN
  48. void
  49. mutex_set_debug_info(
  50. /*=================*/
  51. mutex_t* mutex, /*!< in: mutex */
  52. const char* file_name, /*!< in: file where requested */
  53. ulint line); /*!< in: line where requested */
  54. #endif /* UNIV_SYNC_DEBUG */
  55. /******************************************************************//**
  56. Releases the threads waiting in the primary wait array for this mutex. */
  57. UNIV_INTERN
  58. void
  59. mutex_signal_object(
  60. /*================*/
  61. mutex_t* mutex); /*!< in: mutex */
  62. /******************************************************************//**
  63. Performs an atomic test-and-set instruction to the lock_word field of a
  64. mutex.
  65. @return the previous value of lock_word: 0 or 1 */
  66. UNIV_INLINE
  67. byte
  68. mutex_test_and_set(
  69. /*===============*/
  70. mutex_t* mutex) /*!< in: mutex */
  71. {
  72. #if defined(HAVE_ATOMIC_BUILTINS)
  73. return(os_atomic_test_and_set_byte(&mutex->lock_word, 1));
  74. #else
  75. ibool ret;
  76. ret = os_fast_mutex_trylock(&(mutex->os_fast_mutex));
  77. if (ret == 0) {
  78. /* We check that os_fast_mutex_trylock does not leak
  79. and allow race conditions */
  80. ut_a(mutex->lock_word == 0);
  81. mutex->lock_word = 1;
  82. }
  83. return((byte)ret);
  84. #endif
  85. }
  86. /******************************************************************//**
  87. Performs a reset instruction to the lock_word field of a mutex. This
  88. instruction also serializes memory operations to the program order. */
  89. UNIV_INLINE
  90. void
  91. mutex_reset_lock_word(
  92. /*==================*/
  93. mutex_t* mutex) /*!< in: mutex */
  94. {
  95. #if defined(HAVE_ATOMIC_BUILTINS)
  96. /* In theory __sync_lock_release should be used to release the lock.
  97. Unfortunately, it does not work properly alone. The workaround is
  98. that more conservative __sync_lock_test_and_set is used instead. */
  99. os_atomic_test_and_set_byte(&mutex->lock_word, 0);
  100. #else
  101. mutex->lock_word = 0;
  102. os_fast_mutex_unlock(&(mutex->os_fast_mutex));
  103. #endif
  104. }
  105. /******************************************************************//**
  106. Gets the value of the lock word. */
  107. UNIV_INLINE
  108. lock_word_t
  109. mutex_get_lock_word(
  110. /*================*/
  111. const mutex_t* mutex) /*!< in: mutex */
  112. {
  113. ut_ad(mutex);
  114. return(mutex->lock_word);
  115. }
  116. /******************************************************************//**
  117. Gets the waiters field in a mutex.
  118. @return value to set */
  119. UNIV_INLINE
  120. ulint
  121. mutex_get_waiters(
  122. /*==============*/
  123. const mutex_t* mutex) /*!< in: mutex */
  124. {
  125. const volatile ulint* ptr; /*!< declared volatile to ensure that
  126. the value is read from memory */
  127. ut_ad(mutex);
  128. ptr = &(mutex->waiters);
  129. return(*ptr); /* Here we assume that the read of a single
  130. word from memory is atomic */
  131. }
  132. /******************************************************************//**
  133. Unlocks a mutex owned by the current thread. */
  134. UNIV_INLINE
  135. void
  136. mutex_exit(
  137. /*=======*/
  138. mutex_t* mutex) /*!< in: pointer to mutex */
  139. {
  140. ut_ad(mutex_own(mutex));
  141. ut_d(mutex->thread_id = (os_thread_id_t) ULINT_UNDEFINED);
  142. #ifdef UNIV_SYNC_DEBUG
  143. sync_thread_reset_level(mutex);
  144. #endif
  145. mutex_reset_lock_word(mutex);
  146. /* A problem: we assume that mutex_reset_lock word
  147. is a memory barrier, that is when we read the waiters
  148. field next, the read must be serialized in memory
  149. after the reset. A speculative processor might
  150. perform the read first, which could leave a waiting
  151. thread hanging indefinitely.
  152. Our current solution call every second
  153. sync_arr_wake_threads_if_sema_free()
  154. to wake up possible hanging threads if
  155. they are missed in mutex_signal_object. */
  156. if (mutex_get_waiters(mutex) != 0) {
  157. mutex_signal_object(mutex);
  158. }
  159. #ifdef UNIV_SYNC_PERF_STAT
  160. mutex_exit_count++;
  161. #endif
  162. }
  163. /******************************************************************//**
  164. Locks a mutex for the current thread. If the mutex is reserved, the function
  165. spins a preset time (controlled by SYNC_SPIN_ROUNDS), waiting for the mutex
  166. before suspending the thread. */
  167. UNIV_INLINE
  168. void
  169. mutex_enter_func(
  170. /*=============*/
  171. mutex_t* mutex, /*!< in: pointer to mutex */
  172. const char* file_name, /*!< in: file name where locked */
  173. ulint line) /*!< in: line where locked */
  174. {
  175. ut_ad(mutex_validate(mutex));
  176. ut_ad(!mutex_own(mutex));
  177. /* Note that we do not peek at the value of lock_word before trying
  178. the atomic test_and_set; we could peek, and possibly save time. */
  179. ut_d(mutex->count_using++);
  180. if (!mutex_test_and_set(mutex)) {
  181. ut_d(mutex->thread_id = os_thread_get_curr_id());
  182. #ifdef UNIV_SYNC_DEBUG
  183. mutex_set_debug_info(mutex, file_name, line);
  184. #endif
  185. return; /* Succeeded! */
  186. }
  187. mutex_spin_wait(mutex, file_name, line);
  188. }