lock.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /* Locking in multithreaded situations.
  2. Copyright (C) 2005-2016 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. # ifndef pthread_self
  118. # pragma weak pthread_self
  119. # endif
  120. # if !PTHREAD_IN_USE_DETECTION_HARD
  121. # pragma weak pthread_cancel
  122. # define pthread_in_use() (pthread_cancel != NULL)
  123. # endif
  124. # else
  125. # if !PTHREAD_IN_USE_DETECTION_HARD
  126. # define pthread_in_use() 1
  127. # endif
  128. # endif
  129. /* -------------------------- gl_lock_t datatype -------------------------- */
  130. typedef pthread_mutex_t gl_lock_t;
  131. # define gl_lock_define(STORAGECLASS, NAME) \
  132. STORAGECLASS pthread_mutex_t NAME;
  133. # define gl_lock_define_initialized(STORAGECLASS, NAME) \
  134. STORAGECLASS pthread_mutex_t NAME = gl_lock_initializer;
  135. # define gl_lock_initializer \
  136. PTHREAD_MUTEX_INITIALIZER
  137. # define glthread_lock_init(LOCK) \
  138. (pthread_in_use () ? pthread_mutex_init (LOCK, NULL) : 0)
  139. # define glthread_lock_lock(LOCK) \
  140. (pthread_in_use () ? pthread_mutex_lock (LOCK) : 0)
  141. # define glthread_lock_unlock(LOCK) \
  142. (pthread_in_use () ? pthread_mutex_unlock (LOCK) : 0)
  143. # define glthread_lock_destroy(LOCK) \
  144. (pthread_in_use () ? pthread_mutex_destroy (LOCK) : 0)
  145. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  146. # if HAVE_PTHREAD_RWLOCK
  147. # ifdef PTHREAD_RWLOCK_INITIALIZER
  148. typedef pthread_rwlock_t gl_rwlock_t;
  149. # define gl_rwlock_define(STORAGECLASS, NAME) \
  150. STORAGECLASS pthread_rwlock_t NAME;
  151. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  152. STORAGECLASS pthread_rwlock_t NAME = gl_rwlock_initializer;
  153. # define gl_rwlock_initializer \
  154. PTHREAD_RWLOCK_INITIALIZER
  155. # define glthread_rwlock_init(LOCK) \
  156. (pthread_in_use () ? pthread_rwlock_init (LOCK, NULL) : 0)
  157. # define glthread_rwlock_rdlock(LOCK) \
  158. (pthread_in_use () ? pthread_rwlock_rdlock (LOCK) : 0)
  159. # define glthread_rwlock_wrlock(LOCK) \
  160. (pthread_in_use () ? pthread_rwlock_wrlock (LOCK) : 0)
  161. # define glthread_rwlock_unlock(LOCK) \
  162. (pthread_in_use () ? pthread_rwlock_unlock (LOCK) : 0)
  163. # define glthread_rwlock_destroy(LOCK) \
  164. (pthread_in_use () ? pthread_rwlock_destroy (LOCK) : 0)
  165. # else
  166. typedef struct
  167. {
  168. int initialized;
  169. pthread_mutex_t guard; /* protects the initialization */
  170. pthread_rwlock_t rwlock; /* read-write lock */
  171. }
  172. gl_rwlock_t;
  173. # define gl_rwlock_define(STORAGECLASS, NAME) \
  174. STORAGECLASS gl_rwlock_t NAME;
  175. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  176. STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
  177. # define gl_rwlock_initializer \
  178. { 0, PTHREAD_MUTEX_INITIALIZER }
  179. # define glthread_rwlock_init(LOCK) \
  180. (pthread_in_use () ? glthread_rwlock_init_multithreaded (LOCK) : 0)
  181. # define glthread_rwlock_rdlock(LOCK) \
  182. (pthread_in_use () ? glthread_rwlock_rdlock_multithreaded (LOCK) : 0)
  183. # define glthread_rwlock_wrlock(LOCK) \
  184. (pthread_in_use () ? glthread_rwlock_wrlock_multithreaded (LOCK) : 0)
  185. # define glthread_rwlock_unlock(LOCK) \
  186. (pthread_in_use () ? glthread_rwlock_unlock_multithreaded (LOCK) : 0)
  187. # define glthread_rwlock_destroy(LOCK) \
  188. (pthread_in_use () ? glthread_rwlock_destroy_multithreaded (LOCK) : 0)
  189. extern int glthread_rwlock_init_multithreaded (gl_rwlock_t *lock);
  190. extern int glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock);
  191. extern int glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock);
  192. extern int glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock);
  193. extern int glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock);
  194. # endif
  195. # else
  196. typedef struct
  197. {
  198. pthread_mutex_t lock; /* protects the remaining fields */
  199. pthread_cond_t waiting_readers; /* waiting readers */
  200. pthread_cond_t waiting_writers; /* waiting writers */
  201. unsigned int waiting_writers_count; /* number of waiting writers */
  202. int runcount; /* number of readers running, or -1 when a writer runs */
  203. }
  204. gl_rwlock_t;
  205. # define gl_rwlock_define(STORAGECLASS, NAME) \
  206. STORAGECLASS gl_rwlock_t NAME;
  207. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  208. STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
  209. # define gl_rwlock_initializer \
  210. { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0 }
  211. # define glthread_rwlock_init(LOCK) \
  212. (pthread_in_use () ? glthread_rwlock_init_multithreaded (LOCK) : 0)
  213. # define glthread_rwlock_rdlock(LOCK) \
  214. (pthread_in_use () ? glthread_rwlock_rdlock_multithreaded (LOCK) : 0)
  215. # define glthread_rwlock_wrlock(LOCK) \
  216. (pthread_in_use () ? glthread_rwlock_wrlock_multithreaded (LOCK) : 0)
  217. # define glthread_rwlock_unlock(LOCK) \
  218. (pthread_in_use () ? glthread_rwlock_unlock_multithreaded (LOCK) : 0)
  219. # define glthread_rwlock_destroy(LOCK) \
  220. (pthread_in_use () ? glthread_rwlock_destroy_multithreaded (LOCK) : 0)
  221. extern int glthread_rwlock_init_multithreaded (gl_rwlock_t *lock);
  222. extern int glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock);
  223. extern int glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock);
  224. extern int glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock);
  225. extern int glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock);
  226. # endif
  227. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  228. # if HAVE_PTHREAD_MUTEX_RECURSIVE
  229. # if defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER || defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
  230. typedef pthread_mutex_t gl_recursive_lock_t;
  231. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  232. STORAGECLASS pthread_mutex_t NAME;
  233. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  234. STORAGECLASS pthread_mutex_t NAME = gl_recursive_lock_initializer;
  235. # ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER
  236. # define gl_recursive_lock_initializer \
  237. PTHREAD_RECURSIVE_MUTEX_INITIALIZER
  238. # else
  239. # define gl_recursive_lock_initializer \
  240. PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
  241. # endif
  242. # define glthread_recursive_lock_init(LOCK) \
  243. (pthread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
  244. # define glthread_recursive_lock_lock(LOCK) \
  245. (pthread_in_use () ? pthread_mutex_lock (LOCK) : 0)
  246. # define glthread_recursive_lock_unlock(LOCK) \
  247. (pthread_in_use () ? pthread_mutex_unlock (LOCK) : 0)
  248. # define glthread_recursive_lock_destroy(LOCK) \
  249. (pthread_in_use () ? pthread_mutex_destroy (LOCK) : 0)
  250. extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
  251. # else
  252. typedef struct
  253. {
  254. pthread_mutex_t recmutex; /* recursive mutex */
  255. pthread_mutex_t guard; /* protects the initialization */
  256. int initialized;
  257. }
  258. gl_recursive_lock_t;
  259. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  260. STORAGECLASS gl_recursive_lock_t NAME;
  261. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  262. STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
  263. # define gl_recursive_lock_initializer \
  264. { PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, 0 }
  265. # define glthread_recursive_lock_init(LOCK) \
  266. (pthread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
  267. # define glthread_recursive_lock_lock(LOCK) \
  268. (pthread_in_use () ? glthread_recursive_lock_lock_multithreaded (LOCK) : 0)
  269. # define glthread_recursive_lock_unlock(LOCK) \
  270. (pthread_in_use () ? glthread_recursive_lock_unlock_multithreaded (LOCK) : 0)
  271. # define glthread_recursive_lock_destroy(LOCK) \
  272. (pthread_in_use () ? glthread_recursive_lock_destroy_multithreaded (LOCK) : 0)
  273. extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
  274. extern int glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock);
  275. extern int glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock);
  276. extern int glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock);
  277. # endif
  278. # else
  279. /* Old versions of POSIX threads on Solaris did not have recursive locks.
  280. We have to implement them ourselves. */
  281. typedef struct
  282. {
  283. pthread_mutex_t mutex;
  284. pthread_t owner;
  285. unsigned long depth;
  286. }
  287. gl_recursive_lock_t;
  288. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  289. STORAGECLASS gl_recursive_lock_t NAME;
  290. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  291. STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
  292. # define gl_recursive_lock_initializer \
  293. { PTHREAD_MUTEX_INITIALIZER, (pthread_t) 0, 0 }
  294. # define glthread_recursive_lock_init(LOCK) \
  295. (pthread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
  296. # define glthread_recursive_lock_lock(LOCK) \
  297. (pthread_in_use () ? glthread_recursive_lock_lock_multithreaded (LOCK) : 0)
  298. # define glthread_recursive_lock_unlock(LOCK) \
  299. (pthread_in_use () ? glthread_recursive_lock_unlock_multithreaded (LOCK) : 0)
  300. # define glthread_recursive_lock_destroy(LOCK) \
  301. (pthread_in_use () ? glthread_recursive_lock_destroy_multithreaded (LOCK) : 0)
  302. extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
  303. extern int glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock);
  304. extern int glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock);
  305. extern int glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock);
  306. # endif
  307. /* -------------------------- gl_once_t datatype -------------------------- */
  308. typedef pthread_once_t gl_once_t;
  309. # define gl_once_define(STORAGECLASS, NAME) \
  310. STORAGECLASS pthread_once_t NAME = PTHREAD_ONCE_INIT;
  311. # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
  312. (pthread_in_use () \
  313. ? pthread_once (ONCE_CONTROL, INITFUNCTION) \
  314. : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0))
  315. extern int glthread_once_singlethreaded (pthread_once_t *once_control);
  316. # ifdef __cplusplus
  317. }
  318. # endif
  319. #endif
  320. /* ========================================================================= */
  321. #if USE_PTH_THREADS
  322. /* Use the GNU Pth threads library. */
  323. # include <pth.h>
  324. # ifdef __cplusplus
  325. extern "C" {
  326. # endif
  327. # if USE_PTH_THREADS_WEAK
  328. /* Use weak references to the GNU Pth threads library. */
  329. # pragma weak pth_mutex_init
  330. # pragma weak pth_mutex_acquire
  331. # pragma weak pth_mutex_release
  332. # pragma weak pth_rwlock_init
  333. # pragma weak pth_rwlock_acquire
  334. # pragma weak pth_rwlock_release
  335. # pragma weak pth_once
  336. # pragma weak pth_cancel
  337. # define pth_in_use() (pth_cancel != NULL)
  338. # else
  339. # define pth_in_use() 1
  340. # endif
  341. /* -------------------------- gl_lock_t datatype -------------------------- */
  342. typedef pth_mutex_t gl_lock_t;
  343. # define gl_lock_define(STORAGECLASS, NAME) \
  344. STORAGECLASS pth_mutex_t NAME;
  345. # define gl_lock_define_initialized(STORAGECLASS, NAME) \
  346. STORAGECLASS pth_mutex_t NAME = gl_lock_initializer;
  347. # define gl_lock_initializer \
  348. PTH_MUTEX_INIT
  349. # define glthread_lock_init(LOCK) \
  350. (pth_in_use () && !pth_mutex_init (LOCK) ? errno : 0)
  351. # define glthread_lock_lock(LOCK) \
  352. (pth_in_use () && !pth_mutex_acquire (LOCK, 0, NULL) ? errno : 0)
  353. # define glthread_lock_unlock(LOCK) \
  354. (pth_in_use () && !pth_mutex_release (LOCK) ? errno : 0)
  355. # define glthread_lock_destroy(LOCK) \
  356. ((void)(LOCK), 0)
  357. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  358. typedef pth_rwlock_t gl_rwlock_t;
  359. # define gl_rwlock_define(STORAGECLASS, NAME) \
  360. STORAGECLASS pth_rwlock_t NAME;
  361. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  362. STORAGECLASS pth_rwlock_t NAME = gl_rwlock_initializer;
  363. # define gl_rwlock_initializer \
  364. PTH_RWLOCK_INIT
  365. # define glthread_rwlock_init(LOCK) \
  366. (pth_in_use () && !pth_rwlock_init (LOCK) ? errno : 0)
  367. # define glthread_rwlock_rdlock(LOCK) \
  368. (pth_in_use () && !pth_rwlock_acquire (LOCK, PTH_RWLOCK_RD, 0, NULL) ? errno : 0)
  369. # define glthread_rwlock_wrlock(LOCK) \
  370. (pth_in_use () && !pth_rwlock_acquire (LOCK, PTH_RWLOCK_RW, 0, NULL) ? errno : 0)
  371. # define glthread_rwlock_unlock(LOCK) \
  372. (pth_in_use () && !pth_rwlock_release (LOCK) ? errno : 0)
  373. # define glthread_rwlock_destroy(LOCK) \
  374. ((void)(LOCK), 0)
  375. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  376. /* In Pth, mutexes are recursive by default. */
  377. typedef pth_mutex_t gl_recursive_lock_t;
  378. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  379. STORAGECLASS pth_mutex_t NAME;
  380. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  381. STORAGECLASS pth_mutex_t NAME = gl_recursive_lock_initializer;
  382. # define gl_recursive_lock_initializer \
  383. PTH_MUTEX_INIT
  384. # define glthread_recursive_lock_init(LOCK) \
  385. (pth_in_use () && !pth_mutex_init (LOCK) ? errno : 0)
  386. # define glthread_recursive_lock_lock(LOCK) \
  387. (pth_in_use () && !pth_mutex_acquire (LOCK, 0, NULL) ? errno : 0)
  388. # define glthread_recursive_lock_unlock(LOCK) \
  389. (pth_in_use () && !pth_mutex_release (LOCK) ? errno : 0)
  390. # define glthread_recursive_lock_destroy(LOCK) \
  391. ((void)(LOCK), 0)
  392. /* -------------------------- gl_once_t datatype -------------------------- */
  393. typedef pth_once_t gl_once_t;
  394. # define gl_once_define(STORAGECLASS, NAME) \
  395. STORAGECLASS pth_once_t NAME = PTH_ONCE_INIT;
  396. # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
  397. (pth_in_use () \
  398. ? glthread_once_multithreaded (ONCE_CONTROL, INITFUNCTION) \
  399. : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0))
  400. extern int glthread_once_multithreaded (pth_once_t *once_control, void (*initfunction) (void));
  401. extern int glthread_once_singlethreaded (pth_once_t *once_control);
  402. # ifdef __cplusplus
  403. }
  404. # endif
  405. #endif
  406. /* ========================================================================= */
  407. #if USE_SOLARIS_THREADS
  408. /* Use the old Solaris threads library. */
  409. # include <thread.h>
  410. # error #include <synch.h>
  411. # ifdef __cplusplus
  412. extern "C" {
  413. # endif
  414. # if USE_SOLARIS_THREADS_WEAK
  415. /* Use weak references to the old Solaris threads library. */
  416. # pragma weak mutex_init
  417. # pragma weak mutex_lock
  418. # pragma weak mutex_unlock
  419. # pragma weak mutex_destroy
  420. # pragma weak rwlock_init
  421. # pragma weak rw_rdlock
  422. # pragma weak rw_wrlock
  423. # pragma weak rw_unlock
  424. # pragma weak rwlock_destroy
  425. # pragma weak thr_self
  426. # pragma weak thr_suspend
  427. # define thread_in_use() (thr_suspend != NULL)
  428. # else
  429. # define thread_in_use() 1
  430. # endif
  431. /* -------------------------- gl_lock_t datatype -------------------------- */
  432. typedef mutex_t gl_lock_t;
  433. # define gl_lock_define(STORAGECLASS, NAME) \
  434. STORAGECLASS mutex_t NAME;
  435. # define gl_lock_define_initialized(STORAGECLASS, NAME) \
  436. STORAGECLASS mutex_t NAME = gl_lock_initializer;
  437. # define gl_lock_initializer \
  438. DEFAULTMUTEX
  439. # define glthread_lock_init(LOCK) \
  440. (thread_in_use () ? mutex_init (LOCK, USYNC_THREAD, NULL) : 0)
  441. # define glthread_lock_lock(LOCK) \
  442. (thread_in_use () ? mutex_lock (LOCK) : 0)
  443. # define glthread_lock_unlock(LOCK) \
  444. (thread_in_use () ? mutex_unlock (LOCK) : 0)
  445. # define glthread_lock_destroy(LOCK) \
  446. (thread_in_use () ? mutex_destroy (LOCK) : 0)
  447. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  448. typedef rwlock_t gl_rwlock_t;
  449. # define gl_rwlock_define(STORAGECLASS, NAME) \
  450. STORAGECLASS rwlock_t NAME;
  451. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  452. STORAGECLASS rwlock_t NAME = gl_rwlock_initializer;
  453. # define gl_rwlock_initializer \
  454. DEFAULTRWLOCK
  455. # define glthread_rwlock_init(LOCK) \
  456. (thread_in_use () ? rwlock_init (LOCK, USYNC_THREAD, NULL) : 0)
  457. # define glthread_rwlock_rdlock(LOCK) \
  458. (thread_in_use () ? rw_rdlock (LOCK) : 0)
  459. # define glthread_rwlock_wrlock(LOCK) \
  460. (thread_in_use () ? rw_wrlock (LOCK) : 0)
  461. # define glthread_rwlock_unlock(LOCK) \
  462. (thread_in_use () ? rw_unlock (LOCK) : 0)
  463. # define glthread_rwlock_destroy(LOCK) \
  464. (thread_in_use () ? rwlock_destroy (LOCK) : 0)
  465. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  466. /* Old Solaris threads did not have recursive locks.
  467. We have to implement them ourselves. */
  468. typedef struct
  469. {
  470. mutex_t mutex;
  471. thread_t owner;
  472. unsigned long depth;
  473. }
  474. gl_recursive_lock_t;
  475. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  476. STORAGECLASS gl_recursive_lock_t NAME;
  477. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  478. STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
  479. # define gl_recursive_lock_initializer \
  480. { DEFAULTMUTEX, (thread_t) 0, 0 }
  481. # define glthread_recursive_lock_init(LOCK) \
  482. (thread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
  483. # define glthread_recursive_lock_lock(LOCK) \
  484. (thread_in_use () ? glthread_recursive_lock_lock_multithreaded (LOCK) : 0)
  485. # define glthread_recursive_lock_unlock(LOCK) \
  486. (thread_in_use () ? glthread_recursive_lock_unlock_multithreaded (LOCK) : 0)
  487. # define glthread_recursive_lock_destroy(LOCK) \
  488. (thread_in_use () ? glthread_recursive_lock_destroy_multithreaded (LOCK) : 0)
  489. extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
  490. extern int glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock);
  491. extern int glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock);
  492. extern int glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock);
  493. /* -------------------------- gl_once_t datatype -------------------------- */
  494. typedef struct
  495. {
  496. volatile int inited;
  497. mutex_t mutex;
  498. }
  499. gl_once_t;
  500. # define gl_once_define(STORAGECLASS, NAME) \
  501. STORAGECLASS gl_once_t NAME = { 0, DEFAULTMUTEX };
  502. # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
  503. (thread_in_use () \
  504. ? glthread_once_multithreaded (ONCE_CONTROL, INITFUNCTION) \
  505. : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0))
  506. extern int glthread_once_multithreaded (gl_once_t *once_control, void (*initfunction) (void));
  507. extern int glthread_once_singlethreaded (gl_once_t *once_control);
  508. # ifdef __cplusplus
  509. }
  510. # endif
  511. #endif
  512. /* ========================================================================= */
  513. #if USE_WINDOWS_THREADS
  514. # define WIN32_LEAN_AND_MEAN /* avoid including junk */
  515. # include <windows.h>
  516. # ifdef __cplusplus
  517. extern "C" {
  518. # endif
  519. /* We can use CRITICAL_SECTION directly, rather than the native Windows Event,
  520. Mutex, Semaphore types, because
  521. - we need only to synchronize inside a single process (address space),
  522. not inter-process locking,
  523. - we don't need to support trylock operations. (TryEnterCriticalSection
  524. does not work on Windows 95/98/ME. Packages that need trylock usually
  525. define their own mutex type.) */
  526. /* There is no way to statically initialize a CRITICAL_SECTION. It needs
  527. to be done lazily, once only. For this we need spinlocks. */
  528. typedef struct { volatile int done; volatile long started; } gl_spinlock_t;
  529. /* -------------------------- gl_lock_t datatype -------------------------- */
  530. typedef struct
  531. {
  532. gl_spinlock_t guard; /* protects the initialization */
  533. CRITICAL_SECTION lock;
  534. }
  535. gl_lock_t;
  536. # define gl_lock_define(STORAGECLASS, NAME) \
  537. STORAGECLASS gl_lock_t NAME;
  538. # define gl_lock_define_initialized(STORAGECLASS, NAME) \
  539. STORAGECLASS gl_lock_t NAME = gl_lock_initializer;
  540. # define gl_lock_initializer \
  541. { { 0, -1 } }
  542. # define glthread_lock_init(LOCK) \
  543. (glthread_lock_init_func (LOCK), 0)
  544. # define glthread_lock_lock(LOCK) \
  545. glthread_lock_lock_func (LOCK)
  546. # define glthread_lock_unlock(LOCK) \
  547. glthread_lock_unlock_func (LOCK)
  548. # define glthread_lock_destroy(LOCK) \
  549. glthread_lock_destroy_func (LOCK)
  550. extern void glthread_lock_init_func (gl_lock_t *lock);
  551. extern int glthread_lock_lock_func (gl_lock_t *lock);
  552. extern int glthread_lock_unlock_func (gl_lock_t *lock);
  553. extern int glthread_lock_destroy_func (gl_lock_t *lock);
  554. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  555. /* It is impossible to implement read-write locks using plain locks, without
  556. introducing an extra thread dedicated to managing read-write locks.
  557. Therefore here we need to use the low-level Event type. */
  558. typedef struct
  559. {
  560. HANDLE *array; /* array of waiting threads, each represented by an event */
  561. unsigned int count; /* number of waiting threads */
  562. unsigned int alloc; /* length of allocated array */
  563. unsigned int offset; /* index of first waiting thread in array */
  564. }
  565. gl_carray_waitqueue_t;
  566. typedef struct
  567. {
  568. gl_spinlock_t guard; /* protects the initialization */
  569. CRITICAL_SECTION lock; /* protects the remaining fields */
  570. gl_carray_waitqueue_t waiting_readers; /* waiting readers */
  571. gl_carray_waitqueue_t waiting_writers; /* waiting writers */
  572. int runcount; /* number of readers running, or -1 when a writer runs */
  573. }
  574. gl_rwlock_t;
  575. # define gl_rwlock_define(STORAGECLASS, NAME) \
  576. STORAGECLASS gl_rwlock_t NAME;
  577. # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
  578. STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
  579. # define gl_rwlock_initializer \
  580. { { 0, -1 } }
  581. # define glthread_rwlock_init(LOCK) \
  582. (glthread_rwlock_init_func (LOCK), 0)
  583. # define glthread_rwlock_rdlock(LOCK) \
  584. glthread_rwlock_rdlock_func (LOCK)
  585. # define glthread_rwlock_wrlock(LOCK) \
  586. glthread_rwlock_wrlock_func (LOCK)
  587. # define glthread_rwlock_unlock(LOCK) \
  588. glthread_rwlock_unlock_func (LOCK)
  589. # define glthread_rwlock_destroy(LOCK) \
  590. glthread_rwlock_destroy_func (LOCK)
  591. extern void glthread_rwlock_init_func (gl_rwlock_t *lock);
  592. extern int glthread_rwlock_rdlock_func (gl_rwlock_t *lock);
  593. extern int glthread_rwlock_wrlock_func (gl_rwlock_t *lock);
  594. extern int glthread_rwlock_unlock_func (gl_rwlock_t *lock);
  595. extern int glthread_rwlock_destroy_func (gl_rwlock_t *lock);
  596. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  597. /* The native Windows documentation says that CRITICAL_SECTION already
  598. implements a recursive lock. But we need not rely on it: It's easy to
  599. implement a recursive lock without this assumption. */
  600. typedef struct
  601. {
  602. gl_spinlock_t guard; /* protects the initialization */
  603. DWORD owner;
  604. unsigned long depth;
  605. CRITICAL_SECTION lock;
  606. }
  607. gl_recursive_lock_t;
  608. # define gl_recursive_lock_define(STORAGECLASS, NAME) \
  609. STORAGECLASS gl_recursive_lock_t NAME;
  610. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
  611. STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
  612. # define gl_recursive_lock_initializer \
  613. { { 0, -1 }, 0, 0 }
  614. # define glthread_recursive_lock_init(LOCK) \
  615. (glthread_recursive_lock_init_func (LOCK), 0)
  616. # define glthread_recursive_lock_lock(LOCK) \
  617. glthread_recursive_lock_lock_func (LOCK)
  618. # define glthread_recursive_lock_unlock(LOCK) \
  619. glthread_recursive_lock_unlock_func (LOCK)
  620. # define glthread_recursive_lock_destroy(LOCK) \
  621. glthread_recursive_lock_destroy_func (LOCK)
  622. extern void glthread_recursive_lock_init_func (gl_recursive_lock_t *lock);
  623. extern int glthread_recursive_lock_lock_func (gl_recursive_lock_t *lock);
  624. extern int glthread_recursive_lock_unlock_func (gl_recursive_lock_t *lock);
  625. extern int glthread_recursive_lock_destroy_func (gl_recursive_lock_t *lock);
  626. /* -------------------------- gl_once_t datatype -------------------------- */
  627. typedef struct
  628. {
  629. volatile int inited;
  630. volatile long started;
  631. CRITICAL_SECTION lock;
  632. }
  633. gl_once_t;
  634. # define gl_once_define(STORAGECLASS, NAME) \
  635. STORAGECLASS gl_once_t NAME = { -1, -1 };
  636. # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
  637. (glthread_once_func (ONCE_CONTROL, INITFUNCTION), 0)
  638. extern void glthread_once_func (gl_once_t *once_control, void (*initfunction) (void));
  639. # ifdef __cplusplus
  640. }
  641. # endif
  642. #endif
  643. /* ========================================================================= */
  644. #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WINDOWS_THREADS)
  645. /* Provide dummy implementation if threads are not supported. */
  646. /* -------------------------- gl_lock_t datatype -------------------------- */
  647. typedef int gl_lock_t;
  648. # define gl_lock_define(STORAGECLASS, NAME)
  649. # define gl_lock_define_initialized(STORAGECLASS, NAME)
  650. # define glthread_lock_init(NAME) 0
  651. # define glthread_lock_lock(NAME) 0
  652. # define glthread_lock_unlock(NAME) 0
  653. # define glthread_lock_destroy(NAME) 0
  654. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  655. typedef int gl_rwlock_t;
  656. # define gl_rwlock_define(STORAGECLASS, NAME)
  657. # define gl_rwlock_define_initialized(STORAGECLASS, NAME)
  658. # define glthread_rwlock_init(NAME) 0
  659. # define glthread_rwlock_rdlock(NAME) 0
  660. # define glthread_rwlock_wrlock(NAME) 0
  661. # define glthread_rwlock_unlock(NAME) 0
  662. # define glthread_rwlock_destroy(NAME) 0
  663. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  664. typedef int gl_recursive_lock_t;
  665. # define gl_recursive_lock_define(STORAGECLASS, NAME)
  666. # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME)
  667. # define glthread_recursive_lock_init(NAME) 0
  668. # define glthread_recursive_lock_lock(NAME) 0
  669. # define glthread_recursive_lock_unlock(NAME) 0
  670. # define glthread_recursive_lock_destroy(NAME) 0
  671. /* -------------------------- gl_once_t datatype -------------------------- */
  672. typedef int gl_once_t;
  673. # define gl_once_define(STORAGECLASS, NAME) \
  674. STORAGECLASS gl_once_t NAME = 0;
  675. # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
  676. (*(ONCE_CONTROL) == 0 ? (*(ONCE_CONTROL) = ~ 0, INITFUNCTION (), 0) : 0)
  677. #endif
  678. /* ========================================================================= */
  679. /* Macros with built-in error handling. */
  680. /* -------------------------- gl_lock_t datatype -------------------------- */
  681. #define gl_lock_init(NAME) \
  682. do \
  683. { \
  684. if (glthread_lock_init (&NAME)) \
  685. abort (); \
  686. } \
  687. while (0)
  688. #define gl_lock_lock(NAME) \
  689. do \
  690. { \
  691. if (glthread_lock_lock (&NAME)) \
  692. abort (); \
  693. } \
  694. while (0)
  695. #define gl_lock_unlock(NAME) \
  696. do \
  697. { \
  698. if (glthread_lock_unlock (&NAME)) \
  699. abort (); \
  700. } \
  701. while (0)
  702. #define gl_lock_destroy(NAME) \
  703. do \
  704. { \
  705. if (glthread_lock_destroy (&NAME)) \
  706. abort (); \
  707. } \
  708. while (0)
  709. /* ------------------------- gl_rwlock_t datatype ------------------------- */
  710. #define gl_rwlock_init(NAME) \
  711. do \
  712. { \
  713. if (glthread_rwlock_init (&NAME)) \
  714. abort (); \
  715. } \
  716. while (0)
  717. #define gl_rwlock_rdlock(NAME) \
  718. do \
  719. { \
  720. if (glthread_rwlock_rdlock (&NAME)) \
  721. abort (); \
  722. } \
  723. while (0)
  724. #define gl_rwlock_wrlock(NAME) \
  725. do \
  726. { \
  727. if (glthread_rwlock_wrlock (&NAME)) \
  728. abort (); \
  729. } \
  730. while (0)
  731. #define gl_rwlock_unlock(NAME) \
  732. do \
  733. { \
  734. if (glthread_rwlock_unlock (&NAME)) \
  735. abort (); \
  736. } \
  737. while (0)
  738. #define gl_rwlock_destroy(NAME) \
  739. do \
  740. { \
  741. if (glthread_rwlock_destroy (&NAME)) \
  742. abort (); \
  743. } \
  744. while (0)
  745. /* --------------------- gl_recursive_lock_t datatype --------------------- */
  746. #define gl_recursive_lock_init(NAME) \
  747. do \
  748. { \
  749. if (glthread_recursive_lock_init (&NAME)) \
  750. abort (); \
  751. } \
  752. while (0)
  753. #define gl_recursive_lock_lock(NAME) \
  754. do \
  755. { \
  756. if (glthread_recursive_lock_lock (&NAME)) \
  757. abort (); \
  758. } \
  759. while (0)
  760. #define gl_recursive_lock_unlock(NAME) \
  761. do \
  762. { \
  763. if (glthread_recursive_lock_unlock (&NAME)) \
  764. abort (); \
  765. } \
  766. while (0)
  767. #define gl_recursive_lock_destroy(NAME) \
  768. do \
  769. { \
  770. if (glthread_recursive_lock_destroy (&NAME)) \
  771. abort (); \
  772. } \
  773. while (0)
  774. /* -------------------------- gl_once_t datatype -------------------------- */
  775. #define gl_once(NAME, INITFUNCTION) \
  776. do \
  777. { \
  778. if (glthread_once (&NAME, INITFUNCTION)) \
  779. abort (); \
  780. } \
  781. while (0)
  782. /* ========================================================================= */
  783. #endif /* _LOCK_H */