lock.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /* Locking in multithreaded situations.
  2. Copyright (C) 2005-2020 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Bruno Haible <bruno@clisp.org>, 2005.
  14. Based on GCC's gthr-posix.h, gthr-posix95.h, gthr-win32.h. */
  15. /* This file contains locking primitives for use with a given thread library.
  16. It does not contain primitives for creating threads or for other
  17. synchronization primitives.
  18. Normal (non-recursive) locks:
  19. Type: gl_lock_t
  20. Declaration: gl_lock_define(extern, name)
  21. Initializer: gl_lock_define_initialized(, name)
  22. Initialization: gl_lock_init (name);
  23. Taking the lock: gl_lock_lock (name);
  24. Releasing the lock: gl_lock_unlock (name);
  25. De-initialization: gl_lock_destroy (name);
  26. Equivalent functions with control of error handling:
  27. Initialization: err = glthread_lock_init (&name);
  28. Taking the lock: err = glthread_lock_lock (&name);
  29. Releasing the lock: err = glthread_lock_unlock (&name);
  30. De-initialization: err = glthread_lock_destroy (&name);
  31. Read-Write (non-recursive) locks:
  32. Type: gl_rwlock_t
  33. Declaration: gl_rwlock_define(extern, name)
  34. Initializer: gl_rwlock_define_initialized(, name)
  35. Initialization: gl_rwlock_init (name);
  36. Taking the lock: gl_rwlock_rdlock (name);
  37. gl_rwlock_wrlock (name);
  38. Releasing the lock: gl_rwlock_unlock (name);
  39. De-initialization: gl_rwlock_destroy (name);
  40. Equivalent functions with control of error handling:
  41. Initialization: err = glthread_rwlock_init (&name);
  42. Taking the lock: err = glthread_rwlock_rdlock (&name);
  43. err = glthread_rwlock_wrlock (&name);
  44. Releasing the lock: err = glthread_rwlock_unlock (&name);
  45. De-initialization: err = glthread_rwlock_destroy (&name);
  46. Recursive locks:
  47. Type: gl_recursive_lock_t
  48. Declaration: gl_recursive_lock_define(extern, name)
  49. Initializer: gl_recursive_lock_define_initialized(, name)
  50. Initialization: gl_recursive_lock_init (name);
  51. Taking the lock: gl_recursive_lock_lock (name);
  52. Releasing the lock: gl_recursive_lock_unlock (name);
  53. De-initialization: gl_recursive_lock_destroy (name);
  54. Equivalent functions with control of error handling:
  55. Initialization: err = glthread_recursive_lock_init (&name);
  56. Taking the lock: err = glthread_recursive_lock_lock (&name);
  57. Releasing the lock: err = glthread_recursive_lock_unlock (&name);
  58. De-initialization: err = glthread_recursive_lock_destroy (&name);
  59. Once-only execution:
  60. Type: gl_once_t
  61. Initializer: gl_once_define(extern, name)
  62. Execution: gl_once (name, initfunction);
  63. Equivalent functions with control of error handling:
  64. Execution: err = glthread_once (&name, initfunction);
  65. */
  66. #ifndef _LOCK_H
  67. #define _LOCK_H
  68. #include <errno.h>
  69. #include <stdlib.h>
  70. #if !defined c11_threads_in_use
  71. # if HAVE_THREADS_H && USE_POSIX_THREADS_WEAK
  72. # include <threads.h>
  73. # pragma weak thrd_exit
  74. # define c11_threads_in_use() (thrd_exit != NULL)
  75. # else
  76. # define c11_threads_in_use() 0
  77. # endif
  78. #endif
  79. /* ========================================================================= */
  80. #if USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS
  81. /* Use the ISO C threads library. */
  82. # include <threads.h>
  83. # ifdef __cplusplus
  84. extern "C" {
  85. # endif
  86. /* -------------------------- gl_lock_t datatype -------------------------- */
  87. typedef struct
  88. {
  89. int volatile init_needed;
  90. once_flag init_once;
  91. void (*init_func) (void);
  92. mtx_t mutex;
  93. }
  94. gl_lock_t;
  95. # define gl_lock_define(STORAGECLASS, NAME) \
  96. STORAGECLASS gl_lock_t NAME;
  97. # define gl_lock_define_initialized(STORAGECLASS, NAME) \
  98. static void _atomic_init_##NAME (void); \
  99. STORAGECLASS gl_lock_t NAME = \
  100. { 1, ONCE_FLAG_INIT, _atomic_init_##NAME }; \
  101. static void _atomic_init_##NAME (void) \
  102. { \
  103. if (glthread_lock_init (&(NAME))) \
  104. abort (); \
  105. }
  106. extern int glthread_lock_init (gl_lock_t *lock);
  107. extern int glthread_lock_lock (gl_lock_t *lock);
  108. extern int glthread_lock_unlock (gl_lock_t *lock);
  109. extern int glthread_lock_destroy (gl_lock_t *lock);
  110. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  111. typedef struct
  112. {
  113. int volatile init_needed;
  114. once_flag init_once;
  115. void (*init_func) (void);
  116. mtx_t lock; /* protects the remaining fields */
  117. cnd_t waiting_readers; /* waiting readers */
  118. cnd_t waiting_writers; /* waiting writers */
  119. unsigned int waiting_writers_count; /* number of waiting writers */
  120. int runcount; /* number of readers running, or -1 when a writer runs */
  121. }
  122. gl_rwlock_t;
  123. # define gl_rwlock_define(STORAGECLASS, NAME) \
  124. STORAGECLASS gl_rwlock_t NAME;
  125. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  126. static void _atomic_init_##NAME (void); \
  127. STORAGECLASS gl_rwlock_t NAME = \
  128. { 1, ONCE_FLAG_INIT, _atomic_init_##NAME }; \
  129. static void _atomic_init_##NAME (void) \
  130. { \
  131. if (glthread_rwlock_init (&(NAME))) \
  132. abort (); \
  133. }
  134. extern int glthread_rwlock_init (gl_rwlock_t *lock);
  135. extern int glthread_rwlock_rdlock (gl_rwlock_t *lock);
  136. extern int glthread_rwlock_wrlock (gl_rwlock_t *lock);
  137. extern int glthread_rwlock_unlock (gl_rwlock_t *lock);
  138. extern int glthread_rwlock_destroy (gl_rwlock_t *lock);
  139. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  140. typedef struct
  141. {
  142. int volatile init_needed;
  143. once_flag init_once;
  144. void (*init_func) (void);
  145. mtx_t mutex;
  146. }
  147. gl_recursive_lock_t;
  148. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  149. STORAGECLASS gl_recursive_lock_t NAME;
  150. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  151. static void _atomic_init_##NAME (void); \
  152. STORAGECLASS gl_recursive_lock_t NAME = \
  153. { 1, ONCE_FLAG_INIT, _atomic_init_##NAME }; \
  154. static void _atomic_init_##NAME (void) \
  155. { \
  156. if (glthread_recursive_lock_init (&(NAME))) \
  157. abort (); \
  158. }
  159. extern int glthread_recursive_lock_init (gl_recursive_lock_t *lock);
  160. extern int glthread_recursive_lock_lock (gl_recursive_lock_t *lock);
  161. extern int glthread_recursive_lock_unlock (gl_recursive_lock_t *lock);
  162. extern int glthread_recursive_lock_destroy (gl_recursive_lock_t *lock);
  163. /* -------------------------- gl_once_t datatype -------------------------- */
  164. typedef once_flag gl_once_t;
  165. # define gl_once_define(STORAGECLASS, NAME) \
  166. STORAGECLASS once_flag NAME = ONCE_FLAG_INIT;
  167. # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
  168. (call_once (ONCE_CONTROL, INITFUNCTION), 0)
  169. # ifdef __cplusplus
  170. }
  171. # endif
  172. #endif
  173. /* ========================================================================= */
  174. #if USE_POSIX_THREADS
  175. /* Use the POSIX threads library. */
  176. # include <pthread.h>
  177. # ifdef __cplusplus
  178. extern "C" {
  179. # endif
  180. # if PTHREAD_IN_USE_DETECTION_HARD
  181. /* The pthread_in_use() detection needs to be done at runtime. */
  182. # define pthread_in_use() \
  183. glthread_in_use ()
  184. extern int glthread_in_use (void);
  185. # endif
  186. # if USE_POSIX_THREADS_WEAK
  187. /* Use weak references to the POSIX threads library. */
  188. /* Weak references avoid dragging in external libraries if the other parts
  189. of the program don't use them. Here we use them, because we don't want
  190. every program that uses libintl to depend on libpthread. This assumes
  191. that libpthread would not be loaded after libintl; i.e. if libintl is
  192. loaded first, by an executable that does not depend on libpthread, and
  193. then a module is dynamically loaded that depends on libpthread, libintl
  194. will not be multithread-safe. */
  195. /* The way to test at runtime whether libpthread is present is to test
  196. whether a function pointer's value, such as &pthread_mutex_init, is
  197. non-NULL. However, some versions of GCC have a bug through which, in
  198. PIC mode, &foo != NULL always evaluates to true if there is a direct
  199. call to foo(...) in the same function. To avoid this, we test the
  200. address of a function in libpthread that we don't use. */
  201. # pragma weak pthread_mutex_init
  202. # pragma weak pthread_mutex_lock
  203. # pragma weak pthread_mutex_unlock
  204. # pragma weak pthread_mutex_destroy
  205. # pragma weak pthread_rwlock_init
  206. # pragma weak pthread_rwlock_rdlock
  207. # pragma weak pthread_rwlock_wrlock
  208. # pragma weak pthread_rwlock_unlock
  209. # pragma weak pthread_rwlock_destroy
  210. # pragma weak pthread_once
  211. # pragma weak pthread_cond_init
  212. # pragma weak pthread_cond_wait
  213. # pragma weak pthread_cond_signal
  214. # pragma weak pthread_cond_broadcast
  215. # pragma weak pthread_cond_destroy
  216. # pragma weak pthread_mutexattr_init
  217. # pragma weak pthread_mutexattr_settype
  218. # pragma weak pthread_mutexattr_destroy
  219. # pragma weak pthread_rwlockattr_init
  220. # if __GNU_LIBRARY__ > 1
  221. # pragma weak pthread_rwlockattr_setkind_np
  222. # endif
  223. # pragma weak pthread_rwlockattr_destroy
  224. # ifndef pthread_self
  225. # pragma weak pthread_self
  226. # endif
  227. # if !PTHREAD_IN_USE_DETECTION_HARD
  228. /* Considering all platforms with USE_POSIX_THREADS_WEAK, only few symbols
  229. can be used to determine whether libpthread is in use. These are:
  230. pthread_mutexattr_gettype
  231. pthread_rwlockattr_destroy
  232. pthread_rwlockattr_init
  233. */
  234. # pragma weak pthread_mutexattr_gettype
  235. # define pthread_in_use() \
  236. (pthread_mutexattr_gettype != NULL || c11_threads_in_use ())
  237. # endif
  238. # else
  239. # if !PTHREAD_IN_USE_DETECTION_HARD
  240. # define pthread_in_use() 1
  241. # endif
  242. # endif
  243. /* -------------------------- gl_lock_t datatype -------------------------- */
  244. typedef pthread_mutex_t gl_lock_t;
  245. # define gl_lock_define(STORAGECLASS, NAME) \
  246. STORAGECLASS pthread_mutex_t NAME;
  247. # define gl_lock_define_initialized(STORAGECLASS, NAME) \
  248. STORAGECLASS pthread_mutex_t NAME = gl_lock_initializer;
  249. # define gl_lock_initializer \
  250. PTHREAD_MUTEX_INITIALIZER
  251. # define glthread_lock_init(LOCK) \
  252. (pthread_in_use () ? pthread_mutex_init (LOCK, NULL) : 0)
  253. # define glthread_lock_lock(LOCK) \
  254. (pthread_in_use () ? pthread_mutex_lock (LOCK) : 0)
  255. # define glthread_lock_unlock(LOCK) \
  256. (pthread_in_use () ? pthread_mutex_unlock (LOCK) : 0)
  257. # define glthread_lock_destroy(LOCK) \
  258. (pthread_in_use () ? pthread_mutex_destroy (LOCK) : 0)
  259. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  260. # if HAVE_PTHREAD_RWLOCK && (HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER || (defined PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP && (__GNU_LIBRARY__ > 1)))
  261. # if defined PTHREAD_RWLOCK_INITIALIZER || defined PTHREAD_RWLOCK_INITIALIZER_NP
  262. typedef pthread_rwlock_t gl_rwlock_t;
  263. # define gl_rwlock_define(STORAGECLASS, NAME) \
  264. STORAGECLASS pthread_rwlock_t NAME;
  265. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  266. STORAGECLASS pthread_rwlock_t NAME = gl_rwlock_initializer;
  267. # if HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER
  268. # if defined PTHREAD_RWLOCK_INITIALIZER
  269. # define gl_rwlock_initializer \
  270. PTHREAD_RWLOCK_INITIALIZER
  271. # else
  272. # define gl_rwlock_initializer \
  273. PTHREAD_RWLOCK_INITIALIZER_NP
  274. # endif
  275. # define glthread_rwlock_init(LOCK) \
  276. (pthread_in_use () ? pthread_rwlock_init (LOCK, NULL) : 0)
  277. # else /* glibc with bug https://sourceware.org/bugzilla/show_bug.cgi?id=13701 */
  278. # define gl_rwlock_initializer \
  279. PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
  280. # define glthread_rwlock_init(LOCK) \
  281. (pthread_in_use () ? glthread_rwlock_init_for_glibc (LOCK) : 0)
  282. extern int glthread_rwlock_init_for_glibc (pthread_rwlock_t *lock);
  283. # endif
  284. # define glthread_rwlock_rdlock(LOCK) \
  285. (pthread_in_use () ? pthread_rwlock_rdlock (LOCK) : 0)
  286. # define glthread_rwlock_wrlock(LOCK) \
  287. (pthread_in_use () ? pthread_rwlock_wrlock (LOCK) : 0)
  288. # define glthread_rwlock_unlock(LOCK) \
  289. (pthread_in_use () ? pthread_rwlock_unlock (LOCK) : 0)
  290. # define glthread_rwlock_destroy(LOCK) \
  291. (pthread_in_use () ? pthread_rwlock_destroy (LOCK) : 0)
  292. # else
  293. typedef struct
  294. {
  295. int initialized;
  296. pthread_mutex_t guard; /* protects the initialization */
  297. pthread_rwlock_t rwlock; /* read-write lock */
  298. }
  299. gl_rwlock_t;
  300. # define gl_rwlock_define(STORAGECLASS, NAME) \
  301. STORAGECLASS gl_rwlock_t NAME;
  302. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  303. STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
  304. # define gl_rwlock_initializer \
  305. { 0, PTHREAD_MUTEX_INITIALIZER }
  306. # define glthread_rwlock_init(LOCK) \
  307. (pthread_in_use () ? glthread_rwlock_init_multithreaded (LOCK) : 0)
  308. # define glthread_rwlock_rdlock(LOCK) \
  309. (pthread_in_use () ? glthread_rwlock_rdlock_multithreaded (LOCK) : 0)
  310. # define glthread_rwlock_wrlock(LOCK) \
  311. (pthread_in_use () ? glthread_rwlock_wrlock_multithreaded (LOCK) : 0)
  312. # define glthread_rwlock_unlock(LOCK) \
  313. (pthread_in_use () ? glthread_rwlock_unlock_multithreaded (LOCK) : 0)
  314. # define glthread_rwlock_destroy(LOCK) \
  315. (pthread_in_use () ? glthread_rwlock_destroy_multithreaded (LOCK) : 0)
  316. extern int glthread_rwlock_init_multithreaded (gl_rwlock_t *lock);
  317. extern int glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock);
  318. extern int glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock);
  319. extern int glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock);
  320. extern int glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock);
  321. # endif
  322. # else
  323. typedef struct
  324. {
  325. pthread_mutex_t lock; /* protects the remaining fields */
  326. pthread_cond_t waiting_readers; /* waiting readers */
  327. pthread_cond_t waiting_writers; /* waiting writers */
  328. unsigned int waiting_writers_count; /* number of waiting writers */
  329. int runcount; /* number of readers running, or -1 when a writer runs */
  330. }
  331. gl_rwlock_t;
  332. # define gl_rwlock_define(STORAGECLASS, NAME) \
  333. STORAGECLASS gl_rwlock_t NAME;
  334. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  335. STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
  336. # define gl_rwlock_initializer \
  337. { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0 }
  338. # define glthread_rwlock_init(LOCK) \
  339. (pthread_in_use () ? glthread_rwlock_init_multithreaded (LOCK) : 0)
  340. # define glthread_rwlock_rdlock(LOCK) \
  341. (pthread_in_use () ? glthread_rwlock_rdlock_multithreaded (LOCK) : 0)
  342. # define glthread_rwlock_wrlock(LOCK) \
  343. (pthread_in_use () ? glthread_rwlock_wrlock_multithreaded (LOCK) : 0)
  344. # define glthread_rwlock_unlock(LOCK) \
  345. (pthread_in_use () ? glthread_rwlock_unlock_multithreaded (LOCK) : 0)
  346. # define glthread_rwlock_destroy(LOCK) \
  347. (pthread_in_use () ? glthread_rwlock_destroy_multithreaded (LOCK) : 0)
  348. extern int glthread_rwlock_init_multithreaded (gl_rwlock_t *lock);
  349. extern int glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock);
  350. extern int glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock);
  351. extern int glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock);
  352. extern int glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock);
  353. # endif
  354. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  355. # if HAVE_PTHREAD_MUTEX_RECURSIVE
  356. # if defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER || defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
  357. typedef pthread_mutex_t gl_recursive_lock_t;
  358. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  359. STORAGECLASS pthread_mutex_t NAME;
  360. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  361. STORAGECLASS pthread_mutex_t NAME = gl_recursive_lock_initializer;
  362. # ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER
  363. # define gl_recursive_lock_initializer \
  364. PTHREAD_RECURSIVE_MUTEX_INITIALIZER
  365. # else
  366. # define gl_recursive_lock_initializer \
  367. PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
  368. # endif
  369. # define glthread_recursive_lock_init(LOCK) \
  370. (pthread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
  371. # define glthread_recursive_lock_lock(LOCK) \
  372. (pthread_in_use () ? pthread_mutex_lock (LOCK) : 0)
  373. # define glthread_recursive_lock_unlock(LOCK) \
  374. (pthread_in_use () ? pthread_mutex_unlock (LOCK) : 0)
  375. # define glthread_recursive_lock_destroy(LOCK) \
  376. (pthread_in_use () ? pthread_mutex_destroy (LOCK) : 0)
  377. extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
  378. # else
  379. typedef struct
  380. {
  381. pthread_mutex_t recmutex; /* recursive mutex */
  382. pthread_mutex_t guard; /* protects the initialization */
  383. int initialized;
  384. }
  385. gl_recursive_lock_t;
  386. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  387. STORAGECLASS gl_recursive_lock_t NAME;
  388. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  389. STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
  390. # define gl_recursive_lock_initializer \
  391. { PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, 0 }
  392. # define glthread_recursive_lock_init(LOCK) \
  393. (pthread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
  394. # define glthread_recursive_lock_lock(LOCK) \
  395. (pthread_in_use () ? glthread_recursive_lock_lock_multithreaded (LOCK) : 0)
  396. # define glthread_recursive_lock_unlock(LOCK) \
  397. (pthread_in_use () ? glthread_recursive_lock_unlock_multithreaded (LOCK) : 0)
  398. # define glthread_recursive_lock_destroy(LOCK) \
  399. (pthread_in_use () ? glthread_recursive_lock_destroy_multithreaded (LOCK) : 0)
  400. extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
  401. extern int glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock);
  402. extern int glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock);
  403. extern int glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock);
  404. # endif
  405. # else
  406. /* Old versions of POSIX threads on Solaris did not have recursive locks.
  407. We have to implement them ourselves. */
  408. typedef struct
  409. {
  410. pthread_mutex_t mutex;
  411. pthread_t owner;
  412. unsigned long depth;
  413. }
  414. gl_recursive_lock_t;
  415. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  416. STORAGECLASS gl_recursive_lock_t NAME;
  417. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  418. STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
  419. # define gl_recursive_lock_initializer \
  420. { PTHREAD_MUTEX_INITIALIZER, (pthread_t) 0, 0 }
  421. # define glthread_recursive_lock_init(LOCK) \
  422. (pthread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
  423. # define glthread_recursive_lock_lock(LOCK) \
  424. (pthread_in_use () ? glthread_recursive_lock_lock_multithreaded (LOCK) : 0)
  425. # define glthread_recursive_lock_unlock(LOCK) \
  426. (pthread_in_use () ? glthread_recursive_lock_unlock_multithreaded (LOCK) : 0)
  427. # define glthread_recursive_lock_destroy(LOCK) \
  428. (pthread_in_use () ? glthread_recursive_lock_destroy_multithreaded (LOCK) : 0)
  429. extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
  430. extern int glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock);
  431. extern int glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock);
  432. extern int glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock);
  433. # endif
  434. /* -------------------------- gl_once_t datatype -------------------------- */
  435. typedef pthread_once_t gl_once_t;
  436. # define gl_once_define(STORAGECLASS, NAME) \
  437. STORAGECLASS pthread_once_t NAME = PTHREAD_ONCE_INIT;
  438. # if PTHREAD_IN_USE_DETECTION_HARD || USE_POSIX_THREADS_WEAK
  439. # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
  440. (pthread_in_use () \
  441. ? pthread_once (ONCE_CONTROL, INITFUNCTION) \
  442. : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0))
  443. # else
  444. # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
  445. (pthread_in_use () \
  446. ? glthread_once_multithreaded (ONCE_CONTROL, INITFUNCTION) \
  447. : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0))
  448. extern int glthread_once_multithreaded (pthread_once_t *once_control,
  449. void (*init_function) (void));
  450. # endif
  451. extern int glthread_once_singlethreaded (pthread_once_t *once_control);
  452. # ifdef __cplusplus
  453. }
  454. # endif
  455. #endif
  456. /* ========================================================================= */
  457. #if USE_WINDOWS_THREADS
  458. # define WIN32_LEAN_AND_MEAN /* avoid including junk */
  459. # include <windows.h>
  460. # include "windows-mutex.h"
  461. # include "windows-rwlock.h"
  462. # include "windows-recmutex.h"
  463. # include "windows-once.h"
  464. # ifdef __cplusplus
  465. extern "C" {
  466. # endif
  467. /* We can use CRITICAL_SECTION directly, rather than the native Windows Event,
  468. Mutex, Semaphore types, because
  469. - we need only to synchronize inside a single process (address space),
  470. not inter-process locking,
  471. - we don't need to support trylock operations. (TryEnterCriticalSection
  472. does not work on Windows 95/98/ME. Packages that need trylock usually
  473. define their own mutex type.) */
  474. /* There is no way to statically initialize a CRITICAL_SECTION. It needs
  475. to be done lazily, once only. For this we need spinlocks. */
  476. /* -------------------------- gl_lock_t datatype -------------------------- */
  477. typedef glwthread_mutex_t gl_lock_t;
  478. # define gl_lock_define(STORAGECLASS, NAME) \
  479. STORAGECLASS gl_lock_t NAME;
  480. # define gl_lock_define_initialized(STORAGECLASS, NAME) \
  481. STORAGECLASS gl_lock_t NAME = gl_lock_initializer;
  482. # define gl_lock_initializer \
  483. GLWTHREAD_MUTEX_INIT
  484. # define glthread_lock_init(LOCK) \
  485. (glwthread_mutex_init (LOCK), 0)
  486. # define glthread_lock_lock(LOCK) \
  487. glwthread_mutex_lock (LOCK)
  488. # define glthread_lock_unlock(LOCK) \
  489. glwthread_mutex_unlock (LOCK)
  490. # define glthread_lock_destroy(LOCK) \
  491. glwthread_mutex_destroy (LOCK)
  492. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  493. typedef glwthread_rwlock_t gl_rwlock_t;
  494. # define gl_rwlock_define(STORAGECLASS, NAME) \
  495. STORAGECLASS gl_rwlock_t NAME;
  496. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  497. STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
  498. # define gl_rwlock_initializer \
  499. GLWTHREAD_RWLOCK_INIT
  500. # define glthread_rwlock_init(LOCK) \
  501. (glwthread_rwlock_init (LOCK), 0)
  502. # define glthread_rwlock_rdlock(LOCK) \
  503. glwthread_rwlock_rdlock (LOCK)
  504. # define glthread_rwlock_wrlock(LOCK) \
  505. glwthread_rwlock_wrlock (LOCK)
  506. # define glthread_rwlock_unlock(LOCK) \
  507. glwthread_rwlock_unlock (LOCK)
  508. # define glthread_rwlock_destroy(LOCK) \
  509. glwthread_rwlock_destroy (LOCK)
  510. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  511. typedef glwthread_recmutex_t gl_recursive_lock_t;
  512. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  513. STORAGECLASS gl_recursive_lock_t NAME;
  514. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  515. STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
  516. # define gl_recursive_lock_initializer \
  517. GLWTHREAD_RECMUTEX_INIT
  518. # define glthread_recursive_lock_init(LOCK) \
  519. (glwthread_recmutex_init (LOCK), 0)
  520. # define glthread_recursive_lock_lock(LOCK) \
  521. glwthread_recmutex_lock (LOCK)
  522. # define glthread_recursive_lock_unlock(LOCK) \
  523. glwthread_recmutex_unlock (LOCK)
  524. # define glthread_recursive_lock_destroy(LOCK) \
  525. glwthread_recmutex_destroy (LOCK)
  526. /* -------------------------- gl_once_t datatype -------------------------- */
  527. typedef glwthread_once_t gl_once_t;
  528. # define gl_once_define(STORAGECLASS, NAME) \
  529. STORAGECLASS gl_once_t NAME = GLWTHREAD_ONCE_INIT;
  530. # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
  531. (glwthread_once (ONCE_CONTROL, INITFUNCTION), 0)
  532. # ifdef __cplusplus
  533. }
  534. # endif
  535. #endif
  536. /* ========================================================================= */
  537. #if !(USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS)
  538. /* Provide dummy implementation if threads are not supported. */
  539. /* -------------------------- gl_lock_t datatype -------------------------- */
  540. typedef int gl_lock_t;
  541. # define gl_lock_define(STORAGECLASS, NAME)
  542. # define gl_lock_define_initialized(STORAGECLASS, NAME)
  543. # define glthread_lock_init(NAME) 0
  544. # define glthread_lock_lock(NAME) 0
  545. # define glthread_lock_unlock(NAME) 0
  546. # define glthread_lock_destroy(NAME) 0
  547. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  548. typedef int gl_rwlock_t;
  549. # define gl_rwlock_define(STORAGECLASS, NAME)
  550. # define gl_rwlock_define_initialized(STORAGECLASS, NAME)
  551. # define glthread_rwlock_init(NAME) 0
  552. # define glthread_rwlock_rdlock(NAME) 0
  553. # define glthread_rwlock_wrlock(NAME) 0
  554. # define glthread_rwlock_unlock(NAME) 0
  555. # define glthread_rwlock_destroy(NAME) 0
  556. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  557. typedef int gl_recursive_lock_t;
  558. # define gl_recursive_lock_define(STORAGECLASS, NAME)
  559. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME)
  560. # define glthread_recursive_lock_init(NAME) 0
  561. # define glthread_recursive_lock_lock(NAME) 0
  562. # define glthread_recursive_lock_unlock(NAME) 0
  563. # define glthread_recursive_lock_destroy(NAME) 0
  564. /* -------------------------- gl_once_t datatype -------------------------- */
  565. typedef int gl_once_t;
  566. # define gl_once_define(STORAGECLASS, NAME) \
  567. STORAGECLASS gl_once_t NAME = 0;
  568. # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
  569. (*(ONCE_CONTROL) == 0 ? (*(ONCE_CONTROL) = ~ 0, INITFUNCTION (), 0) : 0)
  570. #endif
  571. /* ========================================================================= */
  572. /* Macros with built-in error handling. */
  573. /* -------------------------- gl_lock_t datatype -------------------------- */
  574. #define gl_lock_init(NAME) \
  575. do \
  576. { \
  577. if (glthread_lock_init (&NAME)) \
  578. abort (); \
  579. } \
  580. while (0)
  581. #define gl_lock_lock(NAME) \
  582. do \
  583. { \
  584. if (glthread_lock_lock (&NAME)) \
  585. abort (); \
  586. } \
  587. while (0)
  588. #define gl_lock_unlock(NAME) \
  589. do \
  590. { \
  591. if (glthread_lock_unlock (&NAME)) \
  592. abort (); \
  593. } \
  594. while (0)
  595. #define gl_lock_destroy(NAME) \
  596. do \
  597. { \
  598. if (glthread_lock_destroy (&NAME)) \
  599. abort (); \
  600. } \
  601. while (0)
  602. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  603. #define gl_rwlock_init(NAME) \
  604. do \
  605. { \
  606. if (glthread_rwlock_init (&NAME)) \
  607. abort (); \
  608. } \
  609. while (0)
  610. #define gl_rwlock_rdlock(NAME) \
  611. do \
  612. { \
  613. if (glthread_rwlock_rdlock (&NAME)) \
  614. abort (); \
  615. } \
  616. while (0)
  617. #define gl_rwlock_wrlock(NAME) \
  618. do \
  619. { \
  620. if (glthread_rwlock_wrlock (&NAME)) \
  621. abort (); \
  622. } \
  623. while (0)
  624. #define gl_rwlock_unlock(NAME) \
  625. do \
  626. { \
  627. if (glthread_rwlock_unlock (&NAME)) \
  628. abort (); \
  629. } \
  630. while (0)
  631. #define gl_rwlock_destroy(NAME) \
  632. do \
  633. { \
  634. if (glthread_rwlock_destroy (&NAME)) \
  635. abort (); \
  636. } \
  637. while (0)
  638. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  639. #define gl_recursive_lock_init(NAME) \
  640. do \
  641. { \
  642. if (glthread_recursive_lock_init (&NAME)) \
  643. abort (); \
  644. } \
  645. while (0)
  646. #define gl_recursive_lock_lock(NAME) \
  647. do \
  648. { \
  649. if (glthread_recursive_lock_lock (&NAME)) \
  650. abort (); \
  651. } \
  652. while (0)
  653. #define gl_recursive_lock_unlock(NAME) \
  654. do \
  655. { \
  656. if (glthread_recursive_lock_unlock (&NAME)) \
  657. abort (); \
  658. } \
  659. while (0)
  660. #define gl_recursive_lock_destroy(NAME) \
  661. do \
  662. { \
  663. if (glthread_recursive_lock_destroy (&NAME)) \
  664. abort (); \
  665. } \
  666. while (0)
  667. /* -------------------------- gl_once_t datatype -------------------------- */
  668. #define gl_once(NAME, INITFUNCTION) \
  669. do \
  670. { \
  671. if (glthread_once (&NAME, INITFUNCTION)) \
  672. abort (); \
  673. } \
  674. while (0)
  675. /* ========================================================================= */
  676. #endif /* _LOCK_H */