mono-os-mutex.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
  2. /**
  3. * \file
  4. * Portability wrappers around POSIX Mutexes
  5. *
  6. * Authors: Jeffrey Stedfast <fejj@ximian.com>
  7. *
  8. * Copyright 2002 Ximian, Inc. (www.ximian.com)
  9. *
  10. * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  11. */
  12. #ifndef __MONO_OS_MUTEX_H__
  13. #define __MONO_OS_MUTEX_H__
  14. #include <config.h>
  15. #include <glib.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <time.h>
  19. #if !defined(HOST_WIN32)
  20. #include <pthread.h>
  21. #include <errno.h>
  22. #else
  23. #include <winsock2.h>
  24. #include <windows.h>
  25. #endif
  26. #ifdef HAVE_SYS_TIME_H
  27. #include <sys/time.h>
  28. #endif
  29. #define MONO_INFINITE_WAIT ((guint32) 0xFFFFFFFF)
  30. #if !defined(HOST_WIN32)
  31. #if !defined(CLOCK_MONOTONIC) || defined(HOST_DARWIN) || defined(HOST_ANDROID) || defined(HOST_WASM)
  32. #define BROKEN_CLOCK_SOURCE
  33. #endif
  34. typedef pthread_mutex_t mono_mutex_t;
  35. typedef pthread_cond_t mono_cond_t;
  36. #ifndef DISABLE_THREADS
  37. static inline void
  38. mono_os_mutex_init_type (mono_mutex_t *mutex, int type)
  39. {
  40. int res;
  41. pthread_mutexattr_t attr;
  42. res = pthread_mutexattr_init (&attr);
  43. if (G_UNLIKELY (res != 0))
  44. g_error ("%s: pthread_mutexattr_init failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  45. res = pthread_mutexattr_settype (&attr, type);
  46. if (G_UNLIKELY (res != 0))
  47. g_error ("%s: pthread_mutexattr_settype failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  48. #if !defined(__HAIKU__) && !defined(MUSL) && defined (PTHREAD_PRIO_INHERIT) && HAVE_DECL_PTHREAD_MUTEXATTR_SETPROTOCOL
  49. /* use PTHREAD_PRIO_INHERIT if possible */
  50. res = pthread_mutexattr_setprotocol (&attr, PTHREAD_PRIO_INHERIT);
  51. if (G_UNLIKELY (res != 0 && res != ENOTSUP))
  52. g_error ("%s: pthread_mutexattr_setprotocol failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  53. #endif
  54. res = pthread_mutex_init (mutex, &attr);
  55. if (G_UNLIKELY (res != 0))
  56. g_error ("%s: pthread_mutex_init failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  57. res = pthread_mutexattr_destroy (&attr);
  58. if (G_UNLIKELY (res != 0))
  59. g_error ("%s: pthread_mutexattr_destroy failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  60. }
  61. static inline void
  62. mono_os_mutex_init (mono_mutex_t *mutex)
  63. {
  64. mono_os_mutex_init_type(mutex, PTHREAD_MUTEX_DEFAULT);
  65. }
  66. static inline void
  67. mono_os_mutex_init_recursive (mono_mutex_t *mutex)
  68. {
  69. mono_os_mutex_init_type(mutex, PTHREAD_MUTEX_RECURSIVE);
  70. }
  71. static inline void
  72. mono_os_mutex_destroy (mono_mutex_t *mutex)
  73. {
  74. int res;
  75. res = pthread_mutex_destroy (mutex);
  76. if (G_UNLIKELY (res != 0))
  77. g_error ("%s: pthread_mutex_destroy failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  78. }
  79. static inline void
  80. mono_os_mutex_lock (mono_mutex_t *mutex)
  81. {
  82. int res;
  83. res = pthread_mutex_lock (mutex);
  84. if (G_UNLIKELY (res != 0))
  85. g_error ("%s: pthread_mutex_lock failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  86. }
  87. static inline int
  88. mono_os_mutex_trylock (mono_mutex_t *mutex)
  89. {
  90. int res;
  91. res = pthread_mutex_trylock (mutex);
  92. if (G_UNLIKELY (res != 0 && res != EBUSY))
  93. g_error ("%s: pthread_mutex_trylock failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  94. return res != 0 ? -1 : 0;
  95. }
  96. static inline void
  97. mono_os_mutex_unlock (mono_mutex_t *mutex)
  98. {
  99. int res;
  100. res = pthread_mutex_unlock (mutex);
  101. if (G_UNLIKELY (res != 0))
  102. g_error ("%s: pthread_mutex_unlock failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  103. }
  104. #else /* DISABLE_THREADS */
  105. static inline void
  106. mono_os_mutex_init_type (mono_mutex_t *mutex, int type)
  107. {
  108. }
  109. static inline void
  110. mono_os_mutex_init (mono_mutex_t *mutex)
  111. {
  112. }
  113. static inline void
  114. mono_os_mutex_init_recursive (mono_mutex_t *mutex)
  115. {
  116. }
  117. static inline void
  118. mono_os_mutex_destroy (mono_mutex_t *mutex)
  119. {
  120. }
  121. static inline void
  122. mono_os_mutex_lock (mono_mutex_t *mutex)
  123. {
  124. }
  125. static inline int
  126. mono_os_mutex_trylock (mono_mutex_t *mutex)
  127. {
  128. return 0;
  129. }
  130. static inline void
  131. mono_os_mutex_unlock (mono_mutex_t *mutex)
  132. {
  133. }
  134. #endif /* DISABLE_THREADS */
  135. static inline void
  136. mono_os_cond_init (mono_cond_t *cond)
  137. {
  138. int res;
  139. #ifdef BROKEN_CLOCK_SOURCE
  140. res = pthread_cond_init (cond, NULL);
  141. if (G_UNLIKELY (res != 0))
  142. g_error ("%s: pthread_cond_init failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  143. #else
  144. /* POSIX standard does not compel to have CLOCK_MONOTONIC */
  145. pthread_condattr_t attr;
  146. res = pthread_condattr_init (&attr);
  147. if (G_UNLIKELY (res != 0))
  148. g_error ("%s: pthread_condattr_init failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  149. res = pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
  150. if (G_UNLIKELY (res != 0))
  151. g_error ("%s: pthread_condattr_setclock failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  152. /* Attach an attribute having CLOCK_MONOTONIC to condition */
  153. res = pthread_cond_init (cond, &attr);
  154. if (G_UNLIKELY (res != 0))
  155. g_error ("%s: pthread_cond_init failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  156. res = pthread_condattr_destroy (&attr);
  157. if (G_UNLIKELY (res != 0))
  158. g_error ("%s: pthread_condattr_destroy failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  159. #endif
  160. }
  161. static inline void
  162. mono_os_cond_destroy (mono_cond_t *cond)
  163. {
  164. int res;
  165. res = pthread_cond_destroy (cond);
  166. if (G_UNLIKELY (res != 0))
  167. g_error ("%s: pthread_cond_destroy failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  168. }
  169. static inline void
  170. mono_os_cond_wait (mono_cond_t *cond, mono_mutex_t *mutex)
  171. {
  172. int res;
  173. res = pthread_cond_wait (cond, mutex);
  174. if (G_UNLIKELY (res != 0))
  175. g_error ("%s: pthread_cond_wait failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  176. }
  177. int
  178. mono_os_cond_timedwait (mono_cond_t *cond, mono_mutex_t *mutex, guint32 timeout_ms);
  179. static inline void
  180. mono_os_cond_signal (mono_cond_t *cond)
  181. {
  182. int res;
  183. res = pthread_cond_signal (cond);
  184. if (G_UNLIKELY (res != 0))
  185. g_error ("%s: pthread_cond_signal failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  186. }
  187. static inline void
  188. mono_os_cond_broadcast (mono_cond_t *cond)
  189. {
  190. int res;
  191. res = pthread_cond_broadcast (cond);
  192. if (G_UNLIKELY (res != 0))
  193. g_error ("%s: pthread_cond_broadcast failed with \"%s\" (%d)", __func__, g_strerror (res), res);
  194. }
  195. #else
  196. // FIXME mono_mutex_t and mono_mutex_recursive_t.
  197. typedef struct mono_mutex_t {
  198. union {
  199. CRITICAL_SECTION critical_section;
  200. SRWLOCK srwlock;
  201. };
  202. gboolean recursive;
  203. } mono_mutex_t;
  204. typedef CONDITION_VARIABLE mono_cond_t;
  205. static inline void
  206. mono_os_mutex_init (mono_mutex_t *mutex)
  207. {
  208. mutex->recursive = FALSE;
  209. InitializeSRWLock (&mutex->srwlock);
  210. }
  211. static inline void
  212. mono_os_mutex_init_recursive (mono_mutex_t *mutex)
  213. {
  214. mutex->recursive = TRUE;
  215. const BOOL res = InitializeCriticalSectionEx (&mutex->critical_section, 0, 0);
  216. if (G_UNLIKELY (res == 0))
  217. g_error ("%s: InitializeCriticalSectionEx failed with error %d", __func__, GetLastError ());
  218. }
  219. static inline void
  220. mono_os_mutex_destroy (mono_mutex_t *mutex)
  221. {
  222. // There is no way to destroy a Win32 SRWLOCK.
  223. if (mutex->recursive)
  224. DeleteCriticalSection (&mutex->critical_section);
  225. }
  226. static inline void
  227. mono_os_mutex_lock (mono_mutex_t *mutex)
  228. {
  229. mutex->recursive ?
  230. EnterCriticalSection (&mutex->critical_section) :
  231. AcquireSRWLockExclusive (&mutex->srwlock);
  232. }
  233. static inline int
  234. mono_os_mutex_trylock (mono_mutex_t *mutex)
  235. {
  236. return (mutex->recursive ?
  237. TryEnterCriticalSection (&mutex->critical_section) :
  238. TryAcquireSRWLockExclusive (&mutex->srwlock)) ? 0 : -1;
  239. }
  240. static inline void
  241. mono_os_mutex_unlock (mono_mutex_t *mutex)
  242. {
  243. mutex->recursive ?
  244. LeaveCriticalSection (&mutex->critical_section) :
  245. ReleaseSRWLockExclusive (&mutex->srwlock);
  246. }
  247. static inline void
  248. mono_os_cond_init (mono_cond_t *cond)
  249. {
  250. InitializeConditionVariable (cond);
  251. }
  252. static inline void
  253. mono_os_cond_destroy (mono_cond_t *cond)
  254. {
  255. // There is no way to destroy a Win32 condition variable.
  256. }
  257. static inline void
  258. mono_os_cond_wait (mono_cond_t *cond, mono_mutex_t *mutex)
  259. {
  260. const BOOL res = mutex->recursive ?
  261. SleepConditionVariableCS (cond, &mutex->critical_section, INFINITE) :
  262. SleepConditionVariableSRW (cond, &mutex->srwlock, INFINITE, 0);
  263. if (G_UNLIKELY (res == 0))
  264. g_error ("%s: SleepConditionVariable failed with error %d", __func__, GetLastError ());
  265. }
  266. static inline int
  267. mono_os_cond_timedwait (mono_cond_t *cond, mono_mutex_t *mutex, guint32 timeout_ms)
  268. {
  269. const BOOL res = mutex->recursive ?
  270. SleepConditionVariableCS (cond, &mutex->critical_section, timeout_ms) :
  271. SleepConditionVariableSRW (cond, &mutex->srwlock, timeout_ms, 0);
  272. if (G_UNLIKELY (res == 0 && GetLastError () != ERROR_TIMEOUT))
  273. g_error ("%s: SleepConditionVariable failed with error %d", __func__, GetLastError ());
  274. return res ? 0 : -1;
  275. }
  276. static inline void
  277. mono_os_cond_signal (mono_cond_t *cond)
  278. {
  279. WakeConditionVariable (cond);
  280. }
  281. static inline void
  282. mono_os_cond_broadcast (mono_cond_t *cond)
  283. {
  284. WakeAllConditionVariable (cond);
  285. }
  286. #endif
  287. #endif /* __MONO_OS_MUTEX_H__ */