lock.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /* Locking in multithreaded situations.
  2. Copyright (C) 2005-2017 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 <http://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-solaris.h,
  15. gthr-win32.h. */
  16. /* This file contains locking primitives for use with a given thread library.
  17. It does not contain primitives for creating threads or for other
  18. synchronization primitives.
  19. Normal (non-recursive) locks:
  20. Type: gl_lock_t
  21. Declaration: gl_lock_define(extern, name)
  22. Initializer: gl_lock_define_initialized(, name)
  23. Initialization: gl_lock_init (name);
  24. Taking the lock: gl_lock_lock (name);
  25. Releasing the lock: gl_lock_unlock (name);
  26. De-initialization: gl_lock_destroy (name);
  27. Equivalent functions with control of error handling:
  28. Initialization: err = glthread_lock_init (&name);
  29. Taking the lock: err = glthread_lock_lock (&name);
  30. Releasing the lock: err = glthread_lock_unlock (&name);
  31. De-initialization: err = glthread_lock_destroy (&name);
  32. Read-Write (non-recursive) locks:
  33. Type: gl_rwlock_t
  34. Declaration: gl_rwlock_define(extern, name)
  35. Initializer: gl_rwlock_define_initialized(, name)
  36. Initialization: gl_rwlock_init (name);
  37. Taking the lock: gl_rwlock_rdlock (name);
  38. gl_rwlock_wrlock (name);
  39. Releasing the lock: gl_rwlock_unlock (name);
  40. De-initialization: gl_rwlock_destroy (name);
  41. Equivalent functions with control of error handling:
  42. Initialization: err = glthread_rwlock_init (&name);
  43. Taking the lock: err = glthread_rwlock_rdlock (&name);
  44. err = glthread_rwlock_wrlock (&name);
  45. Releasing the lock: err = glthread_rwlock_unlock (&name);
  46. De-initialization: err = glthread_rwlock_destroy (&name);
  47. Recursive locks:
  48. Type: gl_recursive_lock_t
  49. Declaration: gl_recursive_lock_define(extern, name)
  50. Initializer: gl_recursive_lock_define_initialized(, name)
  51. Initialization: gl_recursive_lock_init (name);
  52. Taking the lock: gl_recursive_lock_lock (name);
  53. Releasing the lock: gl_recursive_lock_unlock (name);
  54. De-initialization: gl_recursive_lock_destroy (name);
  55. Equivalent functions with control of error handling:
  56. Initialization: err = glthread_recursive_lock_init (&name);
  57. Taking the lock: err = glthread_recursive_lock_lock (&name);
  58. Releasing the lock: err = glthread_recursive_lock_unlock (&name);
  59. De-initialization: err = glthread_recursive_lock_destroy (&name);
  60. Once-only execution:
  61. Type: gl_once_t
  62. Initializer: gl_once_define(extern, name)
  63. Execution: gl_once (name, initfunction);
  64. Equivalent functions with control of error handling:
  65. Execution: err = glthread_once (&name, initfunction);
  66. */
  67. #ifndef _LOCK_H
  68. #define _LOCK_H
  69. #include <errno.h>
  70. #include <stdlib.h>
  71. /* ========================================================================= */
  72. #if USE_POSIX_THREADS
  73. /* Use the POSIX threads library. */
  74. # include <pthread.h>
  75. # ifdef __cplusplus
  76. extern "C" {
  77. # endif
  78. # if PTHREAD_IN_USE_DETECTION_HARD
  79. /* The pthread_in_use() detection needs to be done at runtime. */
  80. # define pthread_in_use() \
  81. glthread_in_use ()
  82. extern int glthread_in_use (void);
  83. # endif
  84. # if USE_POSIX_THREADS_WEAK
  85. /* Use weak references to the POSIX threads library. */
  86. /* Weak references avoid dragging in external libraries if the other parts
  87. of the program don't use them. Here we use them, because we don't want
  88. every program that uses libintl to depend on libpthread. This assumes
  89. that libpthread would not be loaded after libintl; i.e. if libintl is
  90. loaded first, by an executable that does not depend on libpthread, and
  91. then a module is dynamically loaded that depends on libpthread, libintl
  92. will not be multithread-safe. */
  93. /* The way to test at runtime whether libpthread is present is to test
  94. whether a function pointer's value, such as &pthread_mutex_init, is
  95. non-NULL. However, some versions of GCC have a bug through which, in
  96. PIC mode, &foo != NULL always evaluates to true if there is a direct
  97. call to foo(...) in the same function. To avoid this, we test the
  98. address of a function in libpthread that we don't use. */
  99. # pragma weak pthread_mutex_init
  100. # pragma weak pthread_mutex_lock
  101. # pragma weak pthread_mutex_unlock
  102. # pragma weak pthread_mutex_destroy
  103. # pragma weak pthread_rwlock_init
  104. # pragma weak pthread_rwlock_rdlock
  105. # pragma weak pthread_rwlock_wrlock
  106. # pragma weak pthread_rwlock_unlock
  107. # pragma weak pthread_rwlock_destroy
  108. # pragma weak pthread_once
  109. # pragma weak pthread_cond_init
  110. # pragma weak pthread_cond_wait
  111. # pragma weak pthread_cond_signal
  112. # pragma weak pthread_cond_broadcast
  113. # pragma weak pthread_cond_destroy
  114. # pragma weak pthread_mutexattr_init
  115. # pragma weak pthread_mutexattr_settype
  116. # pragma weak pthread_mutexattr_destroy
  117. # pragma weak pthread_rwlockattr_init
  118. # if __GNU_LIBRARY__ > 1
  119. # pragma weak pthread_rwlockattr_setkind_np
  120. # endif
  121. # pragma weak pthread_rwlockattr_destroy
  122. # ifndef pthread_self
  123. # pragma weak pthread_self
  124. # endif
  125. # if !PTHREAD_IN_USE_DETECTION_HARD
  126. # pragma weak pthread_cancel
  127. # define pthread_in_use() (pthread_cancel != NULL)
  128. # endif
  129. # else
  130. # if !PTHREAD_IN_USE_DETECTION_HARD
  131. # define pthread_in_use() 1
  132. # endif
  133. # endif
  134. /* -------------------------- gl_lock_t datatype -------------------------- */
  135. typedef pthread_mutex_t gl_lock_t;
  136. # define gl_lock_define(STORAGECLASS, NAME) \
  137. STORAGECLASS pthread_mutex_t NAME;
  138. # define gl_lock_define_initialized(STORAGECLASS, NAME) \
  139. STORAGECLASS pthread_mutex_t NAME = gl_lock_initializer;
  140. # define gl_lock_initializer \
  141. PTHREAD_MUTEX_INITIALIZER
  142. # define glthread_lock_init(LOCK) \
  143. (pthread_in_use () ? pthread_mutex_init (LOCK, NULL) : 0)
  144. # define glthread_lock_lock(LOCK) \
  145. (pthread_in_use () ? pthread_mutex_lock (LOCK) : 0)
  146. # define glthread_lock_unlock(LOCK) \
  147. (pthread_in_use () ? pthread_mutex_unlock (LOCK) : 0)
  148. # define glthread_lock_destroy(LOCK) \
  149. (pthread_in_use () ? pthread_mutex_destroy (LOCK) : 0)
  150. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  151. # if HAVE_PTHREAD_RWLOCK && (HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER || (defined PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP && (__GNU_LIBRARY__ > 1)))
  152. # ifdef PTHREAD_RWLOCK_INITIALIZER
  153. typedef pthread_rwlock_t gl_rwlock_t;
  154. # define gl_rwlock_define(STORAGECLASS, NAME) \
  155. STORAGECLASS pthread_rwlock_t NAME;
  156. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  157. STORAGECLASS pthread_rwlock_t NAME = gl_rwlock_initializer;
  158. # if HAVE_PTHREAD_RWLOCK_RDLOCK_PREFER_WRITER
  159. # define gl_rwlock_initializer \
  160. PTHREAD_RWLOCK_INITIALIZER
  161. # define glthread_rwlock_init(LOCK) \
  162. (pthread_in_use () ? pthread_rwlock_init (LOCK, NULL) : 0)
  163. # else /* glibc with bug https://sourceware.org/bugzilla/show_bug.cgi?id=13701 */
  164. # define gl_rwlock_initializer \
  165. PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
  166. # define glthread_rwlock_init(LOCK) \
  167. (pthread_in_use () ? glthread_rwlock_init_for_glibc (LOCK) : 0)
  168. extern int glthread_rwlock_init_for_glibc (pthread_rwlock_t *lock);
  169. # endif
  170. # define glthread_rwlock_rdlock(LOCK) \
  171. (pthread_in_use () ? pthread_rwlock_rdlock (LOCK) : 0)
  172. # define glthread_rwlock_wrlock(LOCK) \
  173. (pthread_in_use () ? pthread_rwlock_wrlock (LOCK) : 0)
  174. # define glthread_rwlock_unlock(LOCK) \
  175. (pthread_in_use () ? pthread_rwlock_unlock (LOCK) : 0)
  176. # define glthread_rwlock_destroy(LOCK) \
  177. (pthread_in_use () ? pthread_rwlock_destroy (LOCK) : 0)
  178. # else
  179. typedef struct
  180. {
  181. int initialized;
  182. pthread_mutex_t guard; /* protects the initialization */
  183. pthread_rwlock_t rwlock; /* read-write lock */
  184. }
  185. gl_rwlock_t;
  186. # define gl_rwlock_define(STORAGECLASS, NAME) \
  187. STORAGECLASS gl_rwlock_t NAME;
  188. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  189. STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
  190. # define gl_rwlock_initializer \
  191. { 0, PTHREAD_MUTEX_INITIALIZER }
  192. # define glthread_rwlock_init(LOCK) \
  193. (pthread_in_use () ? glthread_rwlock_init_multithreaded (LOCK) : 0)
  194. # define glthread_rwlock_rdlock(LOCK) \
  195. (pthread_in_use () ? glthread_rwlock_rdlock_multithreaded (LOCK) : 0)
  196. # define glthread_rwlock_wrlock(LOCK) \
  197. (pthread_in_use () ? glthread_rwlock_wrlock_multithreaded (LOCK) : 0)
  198. # define glthread_rwlock_unlock(LOCK) \
  199. (pthread_in_use () ? glthread_rwlock_unlock_multithreaded (LOCK) : 0)
  200. # define glthread_rwlock_destroy(LOCK) \
  201. (pthread_in_use () ? glthread_rwlock_destroy_multithreaded (LOCK) : 0)
  202. extern int glthread_rwlock_init_multithreaded (gl_rwlock_t *lock);
  203. extern int glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock);
  204. extern int glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock);
  205. extern int glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock);
  206. extern int glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock);
  207. # endif
  208. # else
  209. typedef struct
  210. {
  211. pthread_mutex_t lock; /* protects the remaining fields */
  212. pthread_cond_t waiting_readers; /* waiting readers */
  213. pthread_cond_t waiting_writers; /* waiting writers */
  214. unsigned int waiting_writers_count; /* number of waiting writers */
  215. int runcount; /* number of readers running, or -1 when a writer runs */
  216. }
  217. gl_rwlock_t;
  218. # define gl_rwlock_define(STORAGECLASS, NAME) \
  219. STORAGECLASS gl_rwlock_t NAME;
  220. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  221. STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
  222. # define gl_rwlock_initializer \
  223. { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0 }
  224. # define glthread_rwlock_init(LOCK) \
  225. (pthread_in_use () ? glthread_rwlock_init_multithreaded (LOCK) : 0)
  226. # define glthread_rwlock_rdlock(LOCK) \
  227. (pthread_in_use () ? glthread_rwlock_rdlock_multithreaded (LOCK) : 0)
  228. # define glthread_rwlock_wrlock(LOCK) \
  229. (pthread_in_use () ? glthread_rwlock_wrlock_multithreaded (LOCK) : 0)
  230. # define glthread_rwlock_unlock(LOCK) \
  231. (pthread_in_use () ? glthread_rwlock_unlock_multithreaded (LOCK) : 0)
  232. # define glthread_rwlock_destroy(LOCK) \
  233. (pthread_in_use () ? glthread_rwlock_destroy_multithreaded (LOCK) : 0)
  234. extern int glthread_rwlock_init_multithreaded (gl_rwlock_t *lock);
  235. extern int glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock);
  236. extern int glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock);
  237. extern int glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock);
  238. extern int glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock);
  239. # endif
  240. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  241. # if HAVE_PTHREAD_MUTEX_RECURSIVE
  242. # if defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER || defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
  243. typedef pthread_mutex_t gl_recursive_lock_t;
  244. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  245. STORAGECLASS pthread_mutex_t NAME;
  246. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  247. STORAGECLASS pthread_mutex_t NAME = gl_recursive_lock_initializer;
  248. # ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER
  249. # define gl_recursive_lock_initializer \
  250. PTHREAD_RECURSIVE_MUTEX_INITIALIZER
  251. # else
  252. # define gl_recursive_lock_initializer \
  253. PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
  254. # endif
  255. # define glthread_recursive_lock_init(LOCK) \
  256. (pthread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
  257. # define glthread_recursive_lock_lock(LOCK) \
  258. (pthread_in_use () ? pthread_mutex_lock (LOCK) : 0)
  259. # define glthread_recursive_lock_unlock(LOCK) \
  260. (pthread_in_use () ? pthread_mutex_unlock (LOCK) : 0)
  261. # define glthread_recursive_lock_destroy(LOCK) \
  262. (pthread_in_use () ? pthread_mutex_destroy (LOCK) : 0)
  263. extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
  264. # else
  265. typedef struct
  266. {
  267. pthread_mutex_t recmutex; /* recursive mutex */
  268. pthread_mutex_t guard; /* protects the initialization */
  269. int initialized;
  270. }
  271. gl_recursive_lock_t;
  272. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  273. STORAGECLASS gl_recursive_lock_t NAME;
  274. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  275. STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
  276. # define gl_recursive_lock_initializer \
  277. { PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, 0 }
  278. # define glthread_recursive_lock_init(LOCK) \
  279. (pthread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
  280. # define glthread_recursive_lock_lock(LOCK) \
  281. (pthread_in_use () ? glthread_recursive_lock_lock_multithreaded (LOCK) : 0)
  282. # define glthread_recursive_lock_unlock(LOCK) \
  283. (pthread_in_use () ? glthread_recursive_lock_unlock_multithreaded (LOCK) : 0)
  284. # define glthread_recursive_lock_destroy(LOCK) \
  285. (pthread_in_use () ? glthread_recursive_lock_destroy_multithreaded (LOCK) : 0)
  286. extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
  287. extern int glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock);
  288. extern int glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock);
  289. extern int glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock);
  290. # endif
  291. # else
  292. /* Old versions of POSIX threads on Solaris did not have recursive locks.
  293. We have to implement them ourselves. */
  294. typedef struct
  295. {
  296. pthread_mutex_t mutex;
  297. pthread_t owner;
  298. unsigned long depth;
  299. }
  300. gl_recursive_lock_t;
  301. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  302. STORAGECLASS gl_recursive_lock_t NAME;
  303. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  304. STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
  305. # define gl_recursive_lock_initializer \
  306. { PTHREAD_MUTEX_INITIALIZER, (pthread_t) 0, 0 }
  307. # define glthread_recursive_lock_init(LOCK) \
  308. (pthread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
  309. # define glthread_recursive_lock_lock(LOCK) \
  310. (pthread_in_use () ? glthread_recursive_lock_lock_multithreaded (LOCK) : 0)
  311. # define glthread_recursive_lock_unlock(LOCK) \
  312. (pthread_in_use () ? glthread_recursive_lock_unlock_multithreaded (LOCK) : 0)
  313. # define glthread_recursive_lock_destroy(LOCK) \
  314. (pthread_in_use () ? glthread_recursive_lock_destroy_multithreaded (LOCK) : 0)
  315. extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
  316. extern int glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock);
  317. extern int glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock);
  318. extern int glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock);
  319. # endif
  320. /* -------------------------- gl_once_t datatype -------------------------- */
  321. typedef pthread_once_t gl_once_t;
  322. # define gl_once_define(STORAGECLASS, NAME) \
  323. STORAGECLASS pthread_once_t NAME = PTHREAD_ONCE_INIT;
  324. # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
  325. (pthread_in_use () \
  326. ? pthread_once (ONCE_CONTROL, INITFUNCTION) \
  327. : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0))
  328. extern int glthread_once_singlethreaded (pthread_once_t *once_control);
  329. # ifdef __cplusplus
  330. }
  331. # endif
  332. #endif
  333. /* ========================================================================= */
  334. #if USE_PTH_THREADS
  335. /* Use the GNU Pth threads library. */
  336. # include <pth.h>
  337. # ifdef __cplusplus
  338. extern "C" {
  339. # endif
  340. # if USE_PTH_THREADS_WEAK
  341. /* Use weak references to the GNU Pth threads library. */
  342. # pragma weak pth_mutex_init
  343. # pragma weak pth_mutex_acquire
  344. # pragma weak pth_mutex_release
  345. # pragma weak pth_rwlock_init
  346. # pragma weak pth_rwlock_acquire
  347. # pragma weak pth_rwlock_release
  348. # pragma weak pth_once
  349. # pragma weak pth_cancel
  350. # define pth_in_use() (pth_cancel != NULL)
  351. # else
  352. # define pth_in_use() 1
  353. # endif
  354. /* -------------------------- gl_lock_t datatype -------------------------- */
  355. typedef pth_mutex_t gl_lock_t;
  356. # define gl_lock_define(STORAGECLASS, NAME) \
  357. STORAGECLASS pth_mutex_t NAME;
  358. # define gl_lock_define_initialized(STORAGECLASS, NAME) \
  359. STORAGECLASS pth_mutex_t NAME = gl_lock_initializer;
  360. # define gl_lock_initializer \
  361. PTH_MUTEX_INIT
  362. # define glthread_lock_init(LOCK) \
  363. (pth_in_use () && !pth_mutex_init (LOCK) ? errno : 0)
  364. # define glthread_lock_lock(LOCK) \
  365. (pth_in_use () && !pth_mutex_acquire (LOCK, 0, NULL) ? errno : 0)
  366. # define glthread_lock_unlock(LOCK) \
  367. (pth_in_use () && !pth_mutex_release (LOCK) ? errno : 0)
  368. # define glthread_lock_destroy(LOCK) \
  369. ((void)(LOCK), 0)
  370. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  371. /* Pth pth_rwlock_acquire always prefers readers. No autoconf test so far. */
  372. # if HAVE_PTH_RWLOCK_ACQUIRE_PREFER_WRITER
  373. typedef pth_rwlock_t gl_rwlock_t;
  374. # define gl_rwlock_define(STORAGECLASS, NAME) \
  375. STORAGECLASS pth_rwlock_t NAME;
  376. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  377. STORAGECLASS pth_rwlock_t NAME = gl_rwlock_initializer;
  378. # define gl_rwlock_initializer \
  379. PTH_RWLOCK_INIT
  380. # define glthread_rwlock_init(LOCK) \
  381. (pth_in_use () && !pth_rwlock_init (LOCK) ? errno : 0)
  382. # define glthread_rwlock_rdlock(LOCK) \
  383. (pth_in_use () && !pth_rwlock_acquire (LOCK, PTH_RWLOCK_RD, 0, NULL) ? errno : 0)
  384. # define glthread_rwlock_wrlock(LOCK) \
  385. (pth_in_use () && !pth_rwlock_acquire (LOCK, PTH_RWLOCK_RW, 0, NULL) ? errno : 0)
  386. # define glthread_rwlock_unlock(LOCK) \
  387. (pth_in_use () && !pth_rwlock_release (LOCK) ? errno : 0)
  388. # define glthread_rwlock_destroy(LOCK) \
  389. ((void)(LOCK), 0)
  390. # else
  391. typedef struct
  392. {
  393. int initialized;
  394. pth_mutex_t lock; /* protects the remaining fields */
  395. pth_cond_t waiting_readers; /* waiting readers */
  396. pth_cond_t waiting_writers; /* waiting writers */
  397. unsigned int waiting_writers_count; /* number of waiting writers */
  398. int runcount; /* number of readers running, or -1 when a writer runs */
  399. }
  400. gl_rwlock_t;
  401. # define gl_rwlock_define(STORAGECLASS, NAME) \
  402. STORAGECLASS gl_rwlock_t NAME;
  403. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  404. STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
  405. # define gl_rwlock_initializer \
  406. { 0 }
  407. # define glthread_rwlock_init(LOCK) \
  408. (pth_in_use () ? glthread_rwlock_init_multithreaded (LOCK) : 0)
  409. # define glthread_rwlock_rdlock(LOCK) \
  410. (pth_in_use () ? glthread_rwlock_rdlock_multithreaded (LOCK) : 0)
  411. # define glthread_rwlock_wrlock(LOCK) \
  412. (pth_in_use () ? glthread_rwlock_wrlock_multithreaded (LOCK) : 0)
  413. # define glthread_rwlock_unlock(LOCK) \
  414. (pth_in_use () ? glthread_rwlock_unlock_multithreaded (LOCK) : 0)
  415. # define glthread_rwlock_destroy(LOCK) \
  416. (pth_in_use () ? glthread_rwlock_destroy_multithreaded (LOCK) : 0)
  417. extern int glthread_rwlock_init_multithreaded (gl_rwlock_t *lock);
  418. extern int glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock);
  419. extern int glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock);
  420. extern int glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock);
  421. extern int glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock);
  422. # endif
  423. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  424. /* In Pth, mutexes are recursive by default. */
  425. typedef pth_mutex_t gl_recursive_lock_t;
  426. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  427. STORAGECLASS pth_mutex_t NAME;
  428. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  429. STORAGECLASS pth_mutex_t NAME = gl_recursive_lock_initializer;
  430. # define gl_recursive_lock_initializer \
  431. PTH_MUTEX_INIT
  432. # define glthread_recursive_lock_init(LOCK) \
  433. (pth_in_use () && !pth_mutex_init (LOCK) ? errno : 0)
  434. # define glthread_recursive_lock_lock(LOCK) \
  435. (pth_in_use () && !pth_mutex_acquire (LOCK, 0, NULL) ? errno : 0)
  436. # define glthread_recursive_lock_unlock(LOCK) \
  437. (pth_in_use () && !pth_mutex_release (LOCK) ? errno : 0)
  438. # define glthread_recursive_lock_destroy(LOCK) \
  439. ((void)(LOCK), 0)
  440. /* -------------------------- gl_once_t datatype -------------------------- */
  441. typedef pth_once_t gl_once_t;
  442. # define gl_once_define(STORAGECLASS, NAME) \
  443. STORAGECLASS pth_once_t NAME = PTH_ONCE_INIT;
  444. # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
  445. (pth_in_use () \
  446. ? glthread_once_multithreaded (ONCE_CONTROL, INITFUNCTION) \
  447. : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0))
  448. extern int glthread_once_multithreaded (pth_once_t *once_control, void (*initfunction) (void));
  449. extern int glthread_once_singlethreaded (pth_once_t *once_control);
  450. # ifdef __cplusplus
  451. }
  452. # endif
  453. #endif
  454. /* ========================================================================= */
  455. #if USE_SOLARIS_THREADS
  456. /* Use the old Solaris threads library. */
  457. # include <thread.h>
  458. # include <synch.h>
  459. # ifdef __cplusplus
  460. extern "C" {
  461. # endif
  462. # if USE_SOLARIS_THREADS_WEAK
  463. /* Use weak references to the old Solaris threads library. */
  464. # pragma weak mutex_init
  465. # pragma weak mutex_lock
  466. # pragma weak mutex_unlock
  467. # pragma weak mutex_destroy
  468. # pragma weak rwlock_init
  469. # pragma weak rw_rdlock
  470. # pragma weak rw_wrlock
  471. # pragma weak rw_unlock
  472. # pragma weak rwlock_destroy
  473. # pragma weak thr_self
  474. # pragma weak thr_suspend
  475. # define thread_in_use() (thr_suspend != NULL)
  476. # else
  477. # define thread_in_use() 1
  478. # endif
  479. /* -------------------------- gl_lock_t datatype -------------------------- */
  480. typedef mutex_t gl_lock_t;
  481. # define gl_lock_define(STORAGECLASS, NAME) \
  482. STORAGECLASS mutex_t NAME;
  483. # define gl_lock_define_initialized(STORAGECLASS, NAME) \
  484. STORAGECLASS mutex_t NAME = gl_lock_initializer;
  485. # define gl_lock_initializer \
  486. DEFAULTMUTEX
  487. # define glthread_lock_init(LOCK) \
  488. (thread_in_use () ? mutex_init (LOCK, USYNC_THREAD, NULL) : 0)
  489. # define glthread_lock_lock(LOCK) \
  490. (thread_in_use () ? mutex_lock (LOCK) : 0)
  491. # define glthread_lock_unlock(LOCK) \
  492. (thread_in_use () ? mutex_unlock (LOCK) : 0)
  493. # define glthread_lock_destroy(LOCK) \
  494. (thread_in_use () ? mutex_destroy (LOCK) : 0)
  495. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  496. typedef rwlock_t gl_rwlock_t;
  497. # define gl_rwlock_define(STORAGECLASS, NAME) \
  498. STORAGECLASS rwlock_t NAME;
  499. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  500. STORAGECLASS rwlock_t NAME = gl_rwlock_initializer;
  501. # define gl_rwlock_initializer \
  502. DEFAULTRWLOCK
  503. # define glthread_rwlock_init(LOCK) \
  504. (thread_in_use () ? rwlock_init (LOCK, USYNC_THREAD, NULL) : 0)
  505. # define glthread_rwlock_rdlock(LOCK) \
  506. (thread_in_use () ? rw_rdlock (LOCK) : 0)
  507. # define glthread_rwlock_wrlock(LOCK) \
  508. (thread_in_use () ? rw_wrlock (LOCK) : 0)
  509. # define glthread_rwlock_unlock(LOCK) \
  510. (thread_in_use () ? rw_unlock (LOCK) : 0)
  511. # define glthread_rwlock_destroy(LOCK) \
  512. (thread_in_use () ? rwlock_destroy (LOCK) : 0)
  513. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  514. /* Old Solaris threads did not have recursive locks.
  515. We have to implement them ourselves. */
  516. typedef struct
  517. {
  518. mutex_t mutex;
  519. thread_t owner;
  520. unsigned long depth;
  521. }
  522. gl_recursive_lock_t;
  523. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  524. STORAGECLASS gl_recursive_lock_t NAME;
  525. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  526. STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
  527. # define gl_recursive_lock_initializer \
  528. { DEFAULTMUTEX, (thread_t) 0, 0 }
  529. # define glthread_recursive_lock_init(LOCK) \
  530. (thread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
  531. # define glthread_recursive_lock_lock(LOCK) \
  532. (thread_in_use () ? glthread_recursive_lock_lock_multithreaded (LOCK) : 0)
  533. # define glthread_recursive_lock_unlock(LOCK) \
  534. (thread_in_use () ? glthread_recursive_lock_unlock_multithreaded (LOCK) : 0)
  535. # define glthread_recursive_lock_destroy(LOCK) \
  536. (thread_in_use () ? glthread_recursive_lock_destroy_multithreaded (LOCK) : 0)
  537. extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
  538. extern int glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock);
  539. extern int glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock);
  540. extern int glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock);
  541. /* -------------------------- gl_once_t datatype -------------------------- */
  542. typedef struct
  543. {
  544. volatile int inited;
  545. mutex_t mutex;
  546. }
  547. gl_once_t;
  548. # define gl_once_define(STORAGECLASS, NAME) \
  549. STORAGECLASS gl_once_t NAME = { 0, DEFAULTMUTEX };
  550. # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
  551. (thread_in_use () \
  552. ? glthread_once_multithreaded (ONCE_CONTROL, INITFUNCTION) \
  553. : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0))
  554. extern int glthread_once_multithreaded (gl_once_t *once_control, void (*initfunction) (void));
  555. extern int glthread_once_singlethreaded (gl_once_t *once_control);
  556. # ifdef __cplusplus
  557. }
  558. # endif
  559. #endif
  560. /* ========================================================================= */
  561. #if USE_WINDOWS_THREADS
  562. # define WIN32_LEAN_AND_MEAN /* avoid including junk */
  563. # include <windows.h>
  564. # ifdef __cplusplus
  565. extern "C" {
  566. # endif
  567. /* We can use CRITICAL_SECTION directly, rather than the native Windows Event,
  568. Mutex, Semaphore types, because
  569. - we need only to synchronize inside a single process (address space),
  570. not inter-process locking,
  571. - we don't need to support trylock operations. (TryEnterCriticalSection
  572. does not work on Windows 95/98/ME. Packages that need trylock usually
  573. define their own mutex type.) */
  574. /* There is no way to statically initialize a CRITICAL_SECTION. It needs
  575. to be done lazily, once only. For this we need spinlocks. */
  576. typedef struct { volatile int done; volatile long started; } gl_spinlock_t;
  577. /* -------------------------- gl_lock_t datatype -------------------------- */
  578. typedef struct
  579. {
  580. gl_spinlock_t guard; /* protects the initialization */
  581. CRITICAL_SECTION lock;
  582. }
  583. gl_lock_t;
  584. # define gl_lock_define(STORAGECLASS, NAME) \
  585. STORAGECLASS gl_lock_t NAME;
  586. # define gl_lock_define_initialized(STORAGECLASS, NAME) \
  587. STORAGECLASS gl_lock_t NAME = gl_lock_initializer;
  588. # define gl_lock_initializer \
  589. { { 0, -1 } }
  590. # define glthread_lock_init(LOCK) \
  591. (glthread_lock_init_func (LOCK), 0)
  592. # define glthread_lock_lock(LOCK) \
  593. glthread_lock_lock_func (LOCK)
  594. # define glthread_lock_unlock(LOCK) \
  595. glthread_lock_unlock_func (LOCK)
  596. # define glthread_lock_destroy(LOCK) \
  597. glthread_lock_destroy_func (LOCK)
  598. extern void glthread_lock_init_func (gl_lock_t *lock);
  599. extern int glthread_lock_lock_func (gl_lock_t *lock);
  600. extern int glthread_lock_unlock_func (gl_lock_t *lock);
  601. extern int glthread_lock_destroy_func (gl_lock_t *lock);
  602. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  603. /* It is impossible to implement read-write locks using plain locks, without
  604. introducing an extra thread dedicated to managing read-write locks.
  605. Therefore here we need to use the low-level Event type. */
  606. typedef struct
  607. {
  608. HANDLE *array; /* array of waiting threads, each represented by an event */
  609. unsigned int count; /* number of waiting threads */
  610. unsigned int alloc; /* length of allocated array */
  611. unsigned int offset; /* index of first waiting thread in array */
  612. }
  613. gl_carray_waitqueue_t;
  614. typedef struct
  615. {
  616. gl_spinlock_t guard; /* protects the initialization */
  617. CRITICAL_SECTION lock; /* protects the remaining fields */
  618. gl_carray_waitqueue_t waiting_readers; /* waiting readers */
  619. gl_carray_waitqueue_t waiting_writers; /* waiting writers */
  620. int runcount; /* number of readers running, or -1 when a writer runs */
  621. }
  622. gl_rwlock_t;
  623. # define gl_rwlock_define(STORAGECLASS, NAME) \
  624. STORAGECLASS gl_rwlock_t NAME;
  625. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  626. STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
  627. # define gl_rwlock_initializer \
  628. { { 0, -1 } }
  629. # define glthread_rwlock_init(LOCK) \
  630. (glthread_rwlock_init_func (LOCK), 0)
  631. # define glthread_rwlock_rdlock(LOCK) \
  632. glthread_rwlock_rdlock_func (LOCK)
  633. # define glthread_rwlock_wrlock(LOCK) \
  634. glthread_rwlock_wrlock_func (LOCK)
  635. # define glthread_rwlock_unlock(LOCK) \
  636. glthread_rwlock_unlock_func (LOCK)
  637. # define glthread_rwlock_destroy(LOCK) \
  638. glthread_rwlock_destroy_func (LOCK)
  639. extern void glthread_rwlock_init_func (gl_rwlock_t *lock);
  640. extern int glthread_rwlock_rdlock_func (gl_rwlock_t *lock);
  641. extern int glthread_rwlock_wrlock_func (gl_rwlock_t *lock);
  642. extern int glthread_rwlock_unlock_func (gl_rwlock_t *lock);
  643. extern int glthread_rwlock_destroy_func (gl_rwlock_t *lock);
  644. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  645. /* The native Windows documentation says that CRITICAL_SECTION already
  646. implements a recursive lock. But we need not rely on it: It's easy to
  647. implement a recursive lock without this assumption. */
  648. typedef struct
  649. {
  650. gl_spinlock_t guard; /* protects the initialization */
  651. DWORD owner;
  652. unsigned long depth;
  653. CRITICAL_SECTION lock;
  654. }
  655. gl_recursive_lock_t;
  656. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  657. STORAGECLASS gl_recursive_lock_t NAME;
  658. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  659. STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
  660. # define gl_recursive_lock_initializer \
  661. { { 0, -1 }, 0, 0 }
  662. # define glthread_recursive_lock_init(LOCK) \
  663. (glthread_recursive_lock_init_func (LOCK), 0)
  664. # define glthread_recursive_lock_lock(LOCK) \
  665. glthread_recursive_lock_lock_func (LOCK)
  666. # define glthread_recursive_lock_unlock(LOCK) \
  667. glthread_recursive_lock_unlock_func (LOCK)
  668. # define glthread_recursive_lock_destroy(LOCK) \
  669. glthread_recursive_lock_destroy_func (LOCK)
  670. extern void glthread_recursive_lock_init_func (gl_recursive_lock_t *lock);
  671. extern int glthread_recursive_lock_lock_func (gl_recursive_lock_t *lock);
  672. extern int glthread_recursive_lock_unlock_func (gl_recursive_lock_t *lock);
  673. extern int glthread_recursive_lock_destroy_func (gl_recursive_lock_t *lock);
  674. /* -------------------------- gl_once_t datatype -------------------------- */
  675. typedef struct
  676. {
  677. volatile int inited;
  678. volatile long started;
  679. CRITICAL_SECTION lock;
  680. }
  681. gl_once_t;
  682. # define gl_once_define(STORAGECLASS, NAME) \
  683. STORAGECLASS gl_once_t NAME = { -1, -1 };
  684. # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
  685. (glthread_once_func (ONCE_CONTROL, INITFUNCTION), 0)
  686. extern void glthread_once_func (gl_once_t *once_control, void (*initfunction) (void));
  687. # ifdef __cplusplus
  688. }
  689. # endif
  690. #endif
  691. /* ========================================================================= */
  692. #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WINDOWS_THREADS)
  693. /* Provide dummy implementation if threads are not supported. */
  694. /* -------------------------- gl_lock_t datatype -------------------------- */
  695. typedef int gl_lock_t;
  696. # define gl_lock_define(STORAGECLASS, NAME)
  697. # define gl_lock_define_initialized(STORAGECLASS, NAME)
  698. # define glthread_lock_init(NAME) 0
  699. # define glthread_lock_lock(NAME) 0
  700. # define glthread_lock_unlock(NAME) 0
  701. # define glthread_lock_destroy(NAME) 0
  702. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  703. typedef int gl_rwlock_t;
  704. # define gl_rwlock_define(STORAGECLASS, NAME)
  705. # define gl_rwlock_define_initialized(STORAGECLASS, NAME)
  706. # define glthread_rwlock_init(NAME) 0
  707. # define glthread_rwlock_rdlock(NAME) 0
  708. # define glthread_rwlock_wrlock(NAME) 0
  709. # define glthread_rwlock_unlock(NAME) 0
  710. # define glthread_rwlock_destroy(NAME) 0
  711. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  712. typedef int gl_recursive_lock_t;
  713. # define gl_recursive_lock_define(STORAGECLASS, NAME)
  714. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME)
  715. # define glthread_recursive_lock_init(NAME) 0
  716. # define glthread_recursive_lock_lock(NAME) 0
  717. # define glthread_recursive_lock_unlock(NAME) 0
  718. # define glthread_recursive_lock_destroy(NAME) 0
  719. /* -------------------------- gl_once_t datatype -------------------------- */
  720. typedef int gl_once_t;
  721. # define gl_once_define(STORAGECLASS, NAME) \
  722. STORAGECLASS gl_once_t NAME = 0;
  723. # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
  724. (*(ONCE_CONTROL) == 0 ? (*(ONCE_CONTROL) = ~ 0, INITFUNCTION (), 0) : 0)
  725. #endif
  726. /* ========================================================================= */
  727. /* Macros with built-in error handling. */
  728. /* -------------------------- gl_lock_t datatype -------------------------- */
  729. #define gl_lock_init(NAME) \
  730. do \
  731. { \
  732. if (glthread_lock_init (&NAME)) \
  733. abort (); \
  734. } \
  735. while (0)
  736. #define gl_lock_lock(NAME) \
  737. do \
  738. { \
  739. if (glthread_lock_lock (&NAME)) \
  740. abort (); \
  741. } \
  742. while (0)
  743. #define gl_lock_unlock(NAME) \
  744. do \
  745. { \
  746. if (glthread_lock_unlock (&NAME)) \
  747. abort (); \
  748. } \
  749. while (0)
  750. #define gl_lock_destroy(NAME) \
  751. do \
  752. { \
  753. if (glthread_lock_destroy (&NAME)) \
  754. abort (); \
  755. } \
  756. while (0)
  757. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  758. #define gl_rwlock_init(NAME) \
  759. do \
  760. { \
  761. if (glthread_rwlock_init (&NAME)) \
  762. abort (); \
  763. } \
  764. while (0)
  765. #define gl_rwlock_rdlock(NAME) \
  766. do \
  767. { \
  768. if (glthread_rwlock_rdlock (&NAME)) \
  769. abort (); \
  770. } \
  771. while (0)
  772. #define gl_rwlock_wrlock(NAME) \
  773. do \
  774. { \
  775. if (glthread_rwlock_wrlock (&NAME)) \
  776. abort (); \
  777. } \
  778. while (0)
  779. #define gl_rwlock_unlock(NAME) \
  780. do \
  781. { \
  782. if (glthread_rwlock_unlock (&NAME)) \
  783. abort (); \
  784. } \
  785. while (0)
  786. #define gl_rwlock_destroy(NAME) \
  787. do \
  788. { \
  789. if (glthread_rwlock_destroy (&NAME)) \
  790. abort (); \
  791. } \
  792. while (0)
  793. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  794. #define gl_recursive_lock_init(NAME) \
  795. do \
  796. { \
  797. if (glthread_recursive_lock_init (&NAME)) \
  798. abort (); \
  799. } \
  800. while (0)
  801. #define gl_recursive_lock_lock(NAME) \
  802. do \
  803. { \
  804. if (glthread_recursive_lock_lock (&NAME)) \
  805. abort (); \
  806. } \
  807. while (0)
  808. #define gl_recursive_lock_unlock(NAME) \
  809. do \
  810. { \
  811. if (glthread_recursive_lock_unlock (&NAME)) \
  812. abort (); \
  813. } \
  814. while (0)
  815. #define gl_recursive_lock_destroy(NAME) \
  816. do \
  817. { \
  818. if (glthread_recursive_lock_destroy (&NAME)) \
  819. abort (); \
  820. } \
  821. while (0)
  822. /* -------------------------- gl_once_t datatype -------------------------- */
  823. #define gl_once(NAME, INITFUNCTION) \
  824. do \
  825. { \
  826. if (glthread_once (&NAME, INITFUNCTION)) \
  827. abort (); \
  828. } \
  829. while (0)
  830. /* ========================================================================= */
  831. #endif /* _LOCK_H */