thr_mutex.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #ifndef THR_MUTEX_INCLUDED
  2. #define THR_MUTEX_INCLUDED
  3. /* Copyright (c) 2014, 2019, 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. @file include/thr_mutex.h
  22. MySQL mutex implementation.
  23. There are three "layers":
  24. 1) native_mutex_*()
  25. Functions that map directly down to OS primitives.
  26. Windows - CriticalSection
  27. Other OSes - pthread
  28. 2) my_mutex_*()
  29. Functions that implement SAFE_MUTEX (default for debug),
  30. Otherwise native_mutex_*() is used.
  31. 3) mysql_mutex_*()
  32. Functions that include Performance Schema instrumentation.
  33. See include/mysql/psi/mysql_thread.h
  34. */
  35. #include <stddef.h>
  36. #include <sys/types.h>
  37. #include "my_dbug.h"
  38. #include "my_inttypes.h"
  39. #include "my_macros.h"
  40. #include "my_thread.h"
  41. /*
  42. The following are part of the services ABI:
  43. - native_mutex_t
  44. - my_mutex_t
  45. */
  46. #include "mysql/components/services/thr_mutex_bits.h"
  47. /* Define mutex types, see my_thr_init.c */
  48. #define MY_MUTEX_INIT_SLOW NULL
  49. /* Can be set in /usr/include/pthread.h */
  50. #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
  51. extern native_mutexattr_t my_fast_mutexattr;
  52. #define MY_MUTEX_INIT_FAST &my_fast_mutexattr
  53. #else
  54. #define MY_MUTEX_INIT_FAST NULL
  55. #endif
  56. /* Can be set in /usr/include/pthread.h */
  57. #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
  58. extern native_mutexattr_t my_errorcheck_mutexattr;
  59. #define MY_MUTEX_INIT_ERRCHK &my_errorcheck_mutexattr
  60. #else
  61. #define MY_MUTEX_INIT_ERRCHK NULL
  62. #endif
  63. static inline int native_mutex_init(native_mutex_t *mutex,
  64. const native_mutexattr_t *attr
  65. MY_ATTRIBUTE((unused))) {
  66. #ifdef _WIN32
  67. InitializeCriticalSection(mutex);
  68. return 0;
  69. #else
  70. return pthread_mutex_init(mutex, attr);
  71. #endif
  72. }
  73. static inline int native_mutex_lock(native_mutex_t *mutex) {
  74. #ifdef _WIN32
  75. EnterCriticalSection(mutex);
  76. return 0;
  77. #else
  78. return pthread_mutex_lock(mutex);
  79. #endif
  80. }
  81. static inline int native_mutex_trylock(native_mutex_t *mutex) {
  82. #ifdef _WIN32
  83. if (TryEnterCriticalSection(mutex)) {
  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. #ifdef _WIN32
  98. LeaveCriticalSection(mutex);
  99. return 0;
  100. #else
  101. return pthread_mutex_unlock(mutex);
  102. #endif
  103. }
  104. static inline int native_mutex_destroy(native_mutex_t *mutex) {
  105. #ifdef _WIN32
  106. DeleteCriticalSection(mutex);
  107. return 0;
  108. #else
  109. return pthread_mutex_destroy(mutex);
  110. #endif
  111. }
  112. #ifdef SAFE_MUTEX
  113. /* safe_mutex adds checking to mutex for easier debugging */
  114. struct safe_mutex_t {
  115. native_mutex_t global, mutex;
  116. const char *file;
  117. uint line, count;
  118. my_thread_t thread;
  119. };
  120. void safe_mutex_global_init();
  121. int safe_mutex_init(safe_mutex_t *mp, const native_mutexattr_t *attr,
  122. const char *file, uint line);
  123. int safe_mutex_lock(safe_mutex_t *mp, bool try_lock, const char *file,
  124. uint line);
  125. int safe_mutex_unlock(safe_mutex_t *mp, const char *file, uint line);
  126. int safe_mutex_destroy(safe_mutex_t *mp, const char *file, uint line);
  127. static inline void safe_mutex_assert_owner(safe_mutex_t *mp) {
  128. DBUG_ASSERT(mp != NULL);
  129. native_mutex_lock(&mp->global);
  130. DBUG_ASSERT(mp->count > 0 && my_thread_equal(my_thread_self(), mp->thread));
  131. native_mutex_unlock(&mp->global);
  132. }
  133. static inline void safe_mutex_assert_not_owner(safe_mutex_t *mp) {
  134. DBUG_ASSERT(mp != NULL);
  135. native_mutex_lock(&mp->global);
  136. DBUG_ASSERT(!mp->count || !my_thread_equal(my_thread_self(), mp->thread));
  137. native_mutex_unlock(&mp->global);
  138. }
  139. #endif /* SAFE_MUTEX */
  140. static inline int my_mutex_init(my_mutex_t *mp, const native_mutexattr_t *attr
  141. #ifdef SAFE_MUTEX
  142. ,
  143. const char *file, uint line
  144. #endif
  145. ) {
  146. #ifdef SAFE_MUTEX
  147. DBUG_ASSERT(mp != NULL);
  148. mp->m_u.m_safe_ptr = (safe_mutex_t *)malloc(sizeof(safe_mutex_t));
  149. return safe_mutex_init(mp->m_u.m_safe_ptr, attr, file, line);
  150. #else
  151. return native_mutex_init(&mp->m_u.m_native, attr);
  152. #endif
  153. }
  154. static inline int my_mutex_lock(my_mutex_t *mp
  155. #ifdef SAFE_MUTEX
  156. ,
  157. const char *file, uint line
  158. #endif
  159. ) {
  160. #ifdef SAFE_MUTEX
  161. DBUG_ASSERT(mp != NULL);
  162. DBUG_ASSERT(mp->m_u.m_safe_ptr != NULL);
  163. return safe_mutex_lock(mp->m_u.m_safe_ptr, false, file, line);
  164. #else
  165. return native_mutex_lock(&mp->m_u.m_native);
  166. #endif
  167. }
  168. static inline int my_mutex_trylock(my_mutex_t *mp
  169. #ifdef SAFE_MUTEX
  170. ,
  171. const char *file, uint line
  172. #endif
  173. ) {
  174. #ifdef SAFE_MUTEX
  175. DBUG_ASSERT(mp != NULL);
  176. DBUG_ASSERT(mp->m_u.m_safe_ptr != NULL);
  177. return safe_mutex_lock(mp->m_u.m_safe_ptr, true, file, line);
  178. #else
  179. return native_mutex_trylock(&mp->m_u.m_native);
  180. #endif
  181. }
  182. static inline int my_mutex_unlock(my_mutex_t *mp
  183. #ifdef SAFE_MUTEX
  184. ,
  185. const char *file, uint line
  186. #endif
  187. ) {
  188. #ifdef SAFE_MUTEX
  189. DBUG_ASSERT(mp != NULL);
  190. DBUG_ASSERT(mp->m_u.m_safe_ptr != NULL);
  191. return safe_mutex_unlock(mp->m_u.m_safe_ptr, file, line);
  192. #else
  193. return native_mutex_unlock(&mp->m_u.m_native);
  194. #endif
  195. }
  196. static inline int my_mutex_destroy(my_mutex_t *mp
  197. #ifdef SAFE_MUTEX
  198. ,
  199. const char *file, uint line
  200. #endif
  201. ) {
  202. #ifdef SAFE_MUTEX
  203. DBUG_ASSERT(mp != NULL);
  204. DBUG_ASSERT(mp->m_u.m_safe_ptr != NULL);
  205. int rc = safe_mutex_destroy(mp->m_u.m_safe_ptr, file, line);
  206. free(mp->m_u.m_safe_ptr);
  207. mp->m_u.m_safe_ptr = NULL;
  208. return rc;
  209. #else
  210. return native_mutex_destroy(&mp->m_u.m_native);
  211. #endif
  212. }
  213. #endif /* THR_MUTEX_INCLUDED */