thr_mutex.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #ifndef THR_MUTEX_INCLUDED
  2. #define THR_MUTEX_INCLUDED
  3. /* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License, version 2.0,
  6. as published by the Free Software Foundation.
  7. This program is also distributed with certain software (including
  8. but not limited to OpenSSL) that is licensed under separate terms,
  9. as designated in a particular file or component or in included license
  10. documentation. The authors of MySQL hereby grant you an additional
  11. permission to link the program and your derivative works with the
  12. separately licensed software that they have included with MySQL.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License, version 2.0, for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  20. /**
  21. MySQL mutex implementation.
  22. There are three "layers":
  23. 1) native_mutex_*()
  24. Functions that map directly down to OS primitives.
  25. Windows - CriticalSection
  26. Other OSes - pthread
  27. 2) my_mutex_*()
  28. Functions that implement SAFE_MUTEX (default for debug),
  29. Otherwise native_mutex_*() is used.
  30. 3) mysql_mutex_*()
  31. Functions that include Performance Schema instrumentation.
  32. See include/mysql/psi/mysql_thread.h
  33. */
  34. #include <my_global.h>
  35. #include "my_thread.h"
  36. C_MODE_START
  37. #ifdef _WIN32
  38. typedef CRITICAL_SECTION native_mutex_t;
  39. typedef int native_mutexattr_t;
  40. #else
  41. typedef pthread_mutex_t native_mutex_t;
  42. typedef pthread_mutexattr_t native_mutexattr_t;
  43. #endif
  44. /* Define mutex types, see my_thr_init.c */
  45. #define MY_MUTEX_INIT_SLOW NULL
  46. /* Can be set in /usr/include/pthread.h */
  47. #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
  48. extern native_mutexattr_t my_fast_mutexattr;
  49. #define MY_MUTEX_INIT_FAST &my_fast_mutexattr
  50. #else
  51. #define MY_MUTEX_INIT_FAST NULL
  52. #endif
  53. /* Can be set in /usr/include/pthread.h */
  54. #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
  55. extern native_mutexattr_t my_errorcheck_mutexattr;
  56. #define MY_MUTEX_INIT_ERRCHK &my_errorcheck_mutexattr
  57. #else
  58. #define MY_MUTEX_INIT_ERRCHK NULL
  59. #endif
  60. static inline int native_mutex_init(native_mutex_t *mutex,
  61. const native_mutexattr_t *attr)
  62. {
  63. #ifdef _WIN32
  64. InitializeCriticalSection(mutex);
  65. return 0;
  66. #else
  67. return pthread_mutex_init(mutex, attr);
  68. #endif
  69. }
  70. static inline int native_mutex_lock(native_mutex_t *mutex)
  71. {
  72. #ifdef _WIN32
  73. EnterCriticalSection(mutex);
  74. return 0;
  75. #else
  76. return pthread_mutex_lock(mutex);
  77. #endif
  78. }
  79. static inline int native_mutex_trylock(native_mutex_t *mutex)
  80. {
  81. #ifdef _WIN32
  82. if (TryEnterCriticalSection(mutex))
  83. {
  84. /* Don't allow recursive lock */
  85. if (mutex->RecursionCount > 1){
  86. LeaveCriticalSection(mutex);
  87. return EBUSY;
  88. }
  89. return 0;
  90. }
  91. return EBUSY;
  92. #else
  93. return pthread_mutex_trylock(mutex);
  94. #endif
  95. }
  96. static inline int native_mutex_unlock(native_mutex_t *mutex)
  97. {
  98. #ifdef _WIN32
  99. LeaveCriticalSection(mutex);
  100. return 0;
  101. #else
  102. return pthread_mutex_unlock(mutex);
  103. #endif
  104. }
  105. static inline int native_mutex_destroy(native_mutex_t *mutex)
  106. {
  107. #ifdef _WIN32
  108. DeleteCriticalSection(mutex);
  109. return 0;
  110. #else
  111. return pthread_mutex_destroy(mutex);
  112. #endif
  113. }
  114. #ifdef SAFE_MUTEX
  115. /* safe_mutex adds checking to mutex for easier debugging */
  116. typedef struct st_safe_mutex_t
  117. {
  118. native_mutex_t global, mutex;
  119. const char *file;
  120. uint line, count;
  121. my_thread_t thread;
  122. } my_mutex_t;
  123. void safe_mutex_global_init();
  124. int safe_mutex_init(my_mutex_t *mp, const native_mutexattr_t *attr,
  125. const char *file, uint line);
  126. int safe_mutex_lock(my_mutex_t *mp, my_bool try_lock, const char *file, uint line);
  127. int safe_mutex_unlock(my_mutex_t *mp, const char *file, uint line);
  128. int safe_mutex_destroy(my_mutex_t *mp, const char *file, uint line);
  129. static inline void safe_mutex_assert_owner(const my_mutex_t *mp)
  130. {
  131. DBUG_ASSERT(mp->count > 0 &&
  132. my_thread_equal(my_thread_self(), mp->thread));
  133. }
  134. static inline void safe_mutex_assert_not_owner(const my_mutex_t *mp)
  135. {
  136. DBUG_ASSERT(!mp->count ||
  137. !my_thread_equal(my_thread_self(), mp->thread));
  138. }
  139. #else
  140. typedef native_mutex_t my_mutex_t;
  141. #endif
  142. static inline int my_mutex_init(my_mutex_t *mp, const native_mutexattr_t *attr
  143. #ifdef SAFE_MUTEX
  144. , const char *file, uint line
  145. #endif
  146. )
  147. {
  148. #ifdef SAFE_MUTEX
  149. return safe_mutex_init(mp, attr, file, line);
  150. #else
  151. return native_mutex_init(mp, attr);
  152. #endif
  153. }
  154. static inline int my_mutex_lock(my_mutex_t *mp
  155. #ifdef SAFE_MUTEX
  156. , const char *file, uint line
  157. #endif
  158. )
  159. {
  160. #ifdef SAFE_MUTEX
  161. return safe_mutex_lock(mp, FALSE, file, line);
  162. #else
  163. return native_mutex_lock(mp);
  164. #endif
  165. }
  166. static inline int my_mutex_trylock(my_mutex_t *mp
  167. #ifdef SAFE_MUTEX
  168. , const char *file, uint line
  169. #endif
  170. )
  171. {
  172. #ifdef SAFE_MUTEX
  173. return safe_mutex_lock(mp, TRUE, file, line);
  174. #else
  175. return native_mutex_trylock(mp);
  176. #endif
  177. }
  178. static inline int my_mutex_unlock(my_mutex_t *mp
  179. #ifdef SAFE_MUTEX
  180. , const char *file, uint line
  181. #endif
  182. )
  183. {
  184. #ifdef SAFE_MUTEX
  185. return safe_mutex_unlock(mp, file, line);
  186. #else
  187. return native_mutex_unlock(mp);
  188. #endif
  189. }
  190. static inline int my_mutex_destroy(my_mutex_t *mp
  191. #ifdef SAFE_MUTEX
  192. , const char *file, uint line
  193. #endif
  194. )
  195. {
  196. #ifdef SAFE_MUTEX
  197. return safe_mutex_destroy(mp, file, line);
  198. #else
  199. return native_mutex_destroy(mp);
  200. #endif
  201. }
  202. C_MODE_END
  203. #endif /* THR_MUTEX_INCLUDED */