1 /* Locking in multithreaded situations.
2 Copyright (C) 2005-2008 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2005.
19 Based on GCC's gthr-posix.h, gthr-posix95.h, gthr-solaris.h,
22 /* This file contains locking primitives for use with a given thread library.
23 It does not contain primitives for creating threads or for other
24 synchronization primitives.
26 Normal (non-recursive) locks:
28 Declaration: gl_lock_define(extern, name)
29 Initializer: gl_lock_define_initialized(, name)
30 Initialization: gl_lock_init (name);
31 Taking the lock: gl_lock_lock (name);
32 Releasing the lock: gl_lock_unlock (name);
33 De-initialization: gl_lock_destroy (name);
34 Equivalent functions with control of error handling:
35 Initialization: err = glthread_lock_init (&name);
36 Taking the lock: err = glthread_lock_lock (&name);
37 Releasing the lock: err = glthread_lock_unlock (&name);
38 De-initialization: err = glthread_lock_destroy (&name);
40 Read-Write (non-recursive) locks:
42 Declaration: gl_rwlock_define(extern, name)
43 Initializer: gl_rwlock_define_initialized(, name)
44 Initialization: gl_rwlock_init (name);
45 Taking the lock: gl_rwlock_rdlock (name);
46 gl_rwlock_wrlock (name);
47 Releasing the lock: gl_rwlock_unlock (name);
48 De-initialization: gl_rwlock_destroy (name);
49 Equivalent functions with control of error handling:
50 Initialization: err = glthread_rwlock_init (&name);
51 Taking the lock: err = glthread_rwlock_rdlock (&name);
52 err = glthread_rwlock_wrlock (&name);
53 Releasing the lock: err = glthread_rwlock_unlock (&name);
54 De-initialization: err = glthread_rwlock_destroy (&name);
57 Type: gl_recursive_lock_t
58 Declaration: gl_recursive_lock_define(extern, name)
59 Initializer: gl_recursive_lock_define_initialized(, name)
60 Initialization: gl_recursive_lock_init (name);
61 Taking the lock: gl_recursive_lock_lock (name);
62 Releasing the lock: gl_recursive_lock_unlock (name);
63 De-initialization: gl_recursive_lock_destroy (name);
64 Equivalent functions with control of error handling:
65 Initialization: err = glthread_recursive_lock_init (&name);
66 Taking the lock: err = glthread_recursive_lock_lock (&name);
67 Releasing the lock: err = glthread_recursive_lock_unlock (&name);
68 De-initialization: err = glthread_recursive_lock_destroy (&name);
72 Initializer: gl_once_define(extern, name)
73 Execution: gl_once (name, initfunction);
74 Equivalent functions with control of error handling:
75 Execution: err = glthread_once (&name, initfunction);
84 /* ========================================================================= */
88 /* Use the POSIX threads library. */
97 # if PTHREAD_IN_USE_DETECTION_HARD
99 /* The pthread_in_use() detection needs to be done at runtime. */
100 # define pthread_in_use() \
102 extern int glthread_in_use (void);
106 # if USE_POSIX_THREADS_WEAK
108 /* Use weak references to the POSIX threads library. */
110 /* Weak references avoid dragging in external libraries if the other parts
111 of the program don't use them. Here we use them, because we don't want
112 every program that uses libintl to depend on libpthread. This assumes
113 that libpthread would not be loaded after libintl; i.e. if libintl is
114 loaded first, by an executable that does not depend on libpthread, and
115 then a module is dynamically loaded that depends on libpthread, libintl
116 will not be multithread-safe. */
118 /* The way to test at runtime whether libpthread is present is to test
119 whether a function pointer's value, such as &pthread_mutex_init, is
120 non-NULL. However, some versions of GCC have a bug through which, in
121 PIC mode, &foo != NULL always evaluates to true if there is a direct
122 call to foo(...) in the same function. To avoid this, we test the
123 address of a function in libpthread that we don't use. */
125 # pragma weak pthread_mutex_init
126 # pragma weak pthread_mutex_lock
127 # pragma weak pthread_mutex_unlock
128 # pragma weak pthread_mutex_destroy
129 # pragma weak pthread_rwlock_init
130 # pragma weak pthread_rwlock_rdlock
131 # pragma weak pthread_rwlock_wrlock
132 # pragma weak pthread_rwlock_unlock
133 # pragma weak pthread_rwlock_destroy
134 # pragma weak pthread_once
135 # pragma weak pthread_cond_init
136 # pragma weak pthread_cond_wait
137 # pragma weak pthread_cond_signal
138 # pragma weak pthread_cond_broadcast
139 # pragma weak pthread_cond_destroy
140 # pragma weak pthread_mutexattr_init
141 # pragma weak pthread_mutexattr_settype
142 # pragma weak pthread_mutexattr_destroy
143 # ifndef pthread_self
144 # pragma weak pthread_self
147 # if !PTHREAD_IN_USE_DETECTION_HARD
148 # pragma weak pthread_cancel
149 # define pthread_in_use() (pthread_cancel != NULL)
154 # if !PTHREAD_IN_USE_DETECTION_HARD
155 # define pthread_in_use() 1
160 /* -------------------------- gl_lock_t datatype -------------------------- */
162 typedef pthread_mutex_t gl_lock_t;
163 # define gl_lock_define(STORAGECLASS, NAME) \
164 STORAGECLASS pthread_mutex_t NAME;
165 # define gl_lock_define_initialized(STORAGECLASS, NAME) \
166 STORAGECLASS pthread_mutex_t NAME = gl_lock_initializer;
167 # define gl_lock_initializer \
168 PTHREAD_MUTEX_INITIALIZER
169 # define glthread_lock_init(LOCK) \
170 (pthread_in_use () ? pthread_mutex_init (LOCK, NULL) : 0)
171 # define glthread_lock_lock(LOCK) \
172 (pthread_in_use () ? pthread_mutex_lock (LOCK) : 0)
173 # define glthread_lock_unlock(LOCK) \
174 (pthread_in_use () ? pthread_mutex_unlock (LOCK) : 0)
175 # define glthread_lock_destroy(LOCK) \
176 (pthread_in_use () ? pthread_mutex_destroy (LOCK) : 0)
178 /* ------------------------- gl_rwlock_t datatype ------------------------- */
180 # if HAVE_PTHREAD_RWLOCK
182 # ifdef PTHREAD_RWLOCK_INITIALIZER
184 typedef pthread_rwlock_t gl_rwlock_t;
185 # define gl_rwlock_define(STORAGECLASS, NAME) \
186 STORAGECLASS pthread_rwlock_t NAME;
187 # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
188 STORAGECLASS pthread_rwlock_t NAME = gl_rwlock_initializer;
189 # define gl_rwlock_initializer \
190 PTHREAD_RWLOCK_INITIALIZER
191 # define glthread_rwlock_init(LOCK) \
192 (pthread_in_use () ? pthread_rwlock_init (LOCK, NULL) : 0)
193 # define glthread_rwlock_rdlock(LOCK) \
194 (pthread_in_use () ? pthread_rwlock_rdlock (LOCK) : 0)
195 # define glthread_rwlock_wrlock(LOCK) \
196 (pthread_in_use () ? pthread_rwlock_wrlock (LOCK) : 0)
197 # define glthread_rwlock_unlock(LOCK) \
198 (pthread_in_use () ? pthread_rwlock_unlock (LOCK) : 0)
199 # define glthread_rwlock_destroy(LOCK) \
200 (pthread_in_use () ? pthread_rwlock_destroy (LOCK) : 0)
207 pthread_mutex_t guard; /* protects the initialization */
208 pthread_rwlock_t rwlock; /* read-write lock */
211 # define gl_rwlock_define(STORAGECLASS, NAME) \
212 STORAGECLASS gl_rwlock_t NAME;
213 # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
214 STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
215 # define gl_rwlock_initializer \
216 { 0, PTHREAD_MUTEX_INITIALIZER }
217 # define glthread_rwlock_init(LOCK) \
218 (pthread_in_use () ? glthread_rwlock_init_multithreaded (LOCK) : 0)
219 # define glthread_rwlock_rdlock(LOCK) \
220 (pthread_in_use () ? glthread_rwlock_rdlock_multithreaded (LOCK) : 0)
221 # define glthread_rwlock_wrlock(LOCK) \
222 (pthread_in_use () ? glthread_rwlock_wrlock_multithreaded (LOCK) : 0)
223 # define glthread_rwlock_unlock(LOCK) \
224 (pthread_in_use () ? glthread_rwlock_unlock_multithreaded (LOCK) : 0)
225 # define glthread_rwlock_destroy(LOCK) \
226 (pthread_in_use () ? glthread_rwlock_destroy_multithreaded (LOCK) : 0)
227 extern int glthread_rwlock_init_multithreaded (gl_rwlock_t *lock);
228 extern int glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock);
229 extern int glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock);
230 extern int glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock);
231 extern int glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock);
239 pthread_mutex_t lock; /* protects the remaining fields */
240 pthread_cond_t waiting_readers; /* waiting readers */
241 pthread_cond_t waiting_writers; /* waiting writers */
242 unsigned int waiting_writers_count; /* number of waiting writers */
243 int runcount; /* number of readers running, or -1 when a writer runs */
246 # define gl_rwlock_define(STORAGECLASS, NAME) \
247 STORAGECLASS gl_rwlock_t NAME;
248 # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
249 STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
250 # define gl_rwlock_initializer \
251 { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0 }
252 # define glthread_rwlock_init(LOCK) \
253 (pthread_in_use () ? glthread_rwlock_init_multithreaded (LOCK) : 0)
254 # define glthread_rwlock_rdlock(LOCK) \
255 (pthread_in_use () ? glthread_rwlock_rdlock_multithreaded (LOCK) : 0)
256 # define glthread_rwlock_wrlock(LOCK) \
257 (pthread_in_use () ? glthread_rwlock_wrlock_multithreaded (LOCK) : 0)
258 # define glthread_rwlock_unlock(LOCK) \
259 (pthread_in_use () ? glthread_rwlock_unlock_multithreaded (LOCK) : 0)
260 # define glthread_rwlock_destroy(LOCK) \
261 (pthread_in_use () ? glthread_rwlock_destroy_multithreaded (LOCK) : 0)
262 extern int glthread_rwlock_init_multithreaded (gl_rwlock_t *lock);
263 extern int glthread_rwlock_rdlock_multithreaded (gl_rwlock_t *lock);
264 extern int glthread_rwlock_wrlock_multithreaded (gl_rwlock_t *lock);
265 extern int glthread_rwlock_unlock_multithreaded (gl_rwlock_t *lock);
266 extern int glthread_rwlock_destroy_multithreaded (gl_rwlock_t *lock);
270 /* --------------------- gl_recursive_lock_t datatype --------------------- */
272 # if HAVE_PTHREAD_MUTEX_RECURSIVE
274 # if defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER || defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
276 typedef pthread_mutex_t gl_recursive_lock_t;
277 # define gl_recursive_lock_define(STORAGECLASS, NAME) \
278 STORAGECLASS pthread_mutex_t NAME;
279 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
280 STORAGECLASS pthread_mutex_t NAME = gl_recursive_lock_initializer;
281 # ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER
282 # define gl_recursive_lock_initializer \
283 PTHREAD_RECURSIVE_MUTEX_INITIALIZER
285 # define gl_recursive_lock_initializer \
286 PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
288 # define glthread_recursive_lock_init(LOCK) \
289 (pthread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
290 # define glthread_recursive_lock_lock(LOCK) \
291 (pthread_in_use () ? pthread_mutex_lock (LOCK) : 0)
292 # define glthread_recursive_lock_unlock(LOCK) \
293 (pthread_in_use () ? pthread_mutex_unlock (LOCK) : 0)
294 # define glthread_recursive_lock_destroy(LOCK) \
295 (pthread_in_use () ? pthread_mutex_destroy (LOCK) : 0)
296 extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
302 pthread_mutex_t recmutex; /* recursive mutex */
303 pthread_mutex_t guard; /* protects the initialization */
307 # define gl_recursive_lock_define(STORAGECLASS, NAME) \
308 STORAGECLASS gl_recursive_lock_t NAME;
309 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
310 STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
311 # define gl_recursive_lock_initializer \
312 { PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, 0 }
313 # define glthread_recursive_lock_init(LOCK) \
314 (pthread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
315 # define glthread_recursive_lock_lock(LOCK) \
316 (pthread_in_use () ? glthread_recursive_lock_lock_multithreaded (LOCK) : 0)
317 # define glthread_recursive_lock_unlock(LOCK) \
318 (pthread_in_use () ? glthread_recursive_lock_unlock_multithreaded (LOCK) : 0)
319 # define glthread_recursive_lock_destroy(LOCK) \
320 (pthread_in_use () ? glthread_recursive_lock_destroy_multithreaded (LOCK) : 0)
321 extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
322 extern int glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock);
323 extern int glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock);
324 extern int glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock);
330 /* Old versions of POSIX threads on Solaris did not have recursive locks.
331 We have to implement them ourselves. */
335 pthread_mutex_t mutex;
340 # define gl_recursive_lock_define(STORAGECLASS, NAME) \
341 STORAGECLASS gl_recursive_lock_t NAME;
342 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
343 STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
344 # define gl_recursive_lock_initializer \
345 { PTHREAD_MUTEX_INITIALIZER, (pthread_t) 0, 0 }
346 # define glthread_recursive_lock_init(LOCK) \
347 (pthread_in_use () ? glthread_recursive_lock_init_multithreaded (LOCK) : 0)
348 # define glthread_recursive_lock_lock(LOCK) \
349 (pthread_in_use () ? glthread_recursive_lock_lock_multithreaded (LOCK) : 0)
350 # define glthread_recursive_lock_unlock(LOCK) \
351 (pthread_in_use () ? glthread_recursive_lock_unlock_multithreaded (LOCK) : 0)
352 # define glthread_recursive_lock_destroy(LOCK) \
353 (pthread_in_use () ? glthread_recursive_lock_destroy_multithreaded (LOCK) : 0)
354 extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
355 extern int glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock);
356 extern int glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock);
357 extern int glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock);
361 /* -------------------------- gl_once_t datatype -------------------------- */
363 typedef pthread_once_t gl_once_t;
364 # define gl_once_define(STORAGECLASS, NAME) \
365 STORAGECLASS pthread_once_t NAME = PTHREAD_ONCE_INIT;
366 # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
368 ? pthread_once (ONCE_CONTROL, INITFUNCTION) \
369 : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0))
370 extern int glthread_once_singlethreaded (pthread_once_t *once_control);
378 /* ========================================================================= */
382 /* Use the GNU Pth threads library. */
391 # if USE_PTH_THREADS_WEAK
393 /* Use weak references to the GNU Pth threads library. */
395 # pragma weak pth_mutex_init
396 # pragma weak pth_mutex_acquire
397 # pragma weak pth_mutex_release
398 # pragma weak pth_rwlock_init
399 # pragma weak pth_rwlock_acquire
400 # pragma weak pth_rwlock_release
401 # pragma weak pth_once
403 # pragma weak pth_cancel
404 # define pth_in_use() (pth_cancel != NULL)
408 # define pth_in_use() 1
412 /* -------------------------- gl_lock_t datatype -------------------------- */
414 typedef pth_mutex_t gl_lock_t;
415 # define gl_lock_define(STORAGECLASS, NAME) \
416 STORAGECLASS pth_mutex_t NAME;
417 # define gl_lock_define_initialized(STORAGECLASS, NAME) \
418 STORAGECLASS pth_mutex_t NAME = gl_lock_initializer;
419 # define gl_lock_initializer \
421 # define glthread_lock_init(LOCK) \
422 (pth_in_use() && !pth_mutex_init (LOCK) ? errno : 0)
423 # define glthread_lock_lock(LOCK) \
424 (pth_in_use() && !pth_mutex_acquire (LOCK, 0, NULL) ? errno : 0)
425 # define glthread_lock_unlock(LOCK) \
426 (pth_in_use() && !pth_mutex_release (LOCK) ? errno : 0)
427 # define glthread_lock_destroy(LOCK) \
430 /* ------------------------- gl_rwlock_t datatype ------------------------- */
432 typedef pth_rwlock_t gl_rwlock_t;
433 # define gl_rwlock_define(STORAGECLASS, NAME) \
434 STORAGECLASS pth_rwlock_t NAME;
435 # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
436 STORAGECLASS pth_rwlock_t NAME = gl_rwlock_initializer;
437 # define gl_rwlock_initializer \
439 # define glthread_rwlock_init(LOCK) \
440 (pth_in_use() && !pth_rwlock_init (LOCK) ? errno : 0)
441 # define glthread_rwlock_rdlock(LOCK) \
442 (pth_in_use() && !pth_rwlock_acquire (LOCK, PTH_RWLOCK_RD, 0, NULL) ? errno : 0)
443 # define glthread_rwlock_wrlock(LOCK) \
444 (pth_in_use() && !pth_rwlock_acquire (LOCK, PTH_RWLOCK_RW, 0, NULL) ? errno : 0)
445 # define glthread_rwlock_unlock(LOCK) \
446 (pth_in_use() && !pth_rwlock_release (LOCK) ? errno : 0)
447 # define glthread_rwlock_destroy(LOCK) \
450 /* --------------------- gl_recursive_lock_t datatype --------------------- */
452 /* In Pth, mutexes are recursive by default. */
453 typedef pth_mutex_t gl_recursive_lock_t;
454 # define gl_recursive_lock_define(STORAGECLASS, NAME) \
455 STORAGECLASS pth_mutex_t NAME;
456 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
457 STORAGECLASS pth_mutex_t NAME = gl_recursive_lock_initializer;
458 # define gl_recursive_lock_initializer \
460 # define glthread_recursive_lock_init(LOCK) \
461 (pth_in_use() && !pth_mutex_init (LOCK) ? errno : 0)
462 # define glthread_recursive_lock_lock(LOCK) \
463 (pth_in_use() && !pth_mutex_acquire (LOCK, 0, NULL) ? errno : 0)
464 # define glthread_recursive_lock_unlock(LOCK) \
465 (pth_in_use() && !pth_mutex_release (LOCK) ? errno : 0)
466 # define glthread_recursive_lock_destroy(LOCK) \
469 /* -------------------------- gl_once_t datatype -------------------------- */
471 typedef pth_once_t gl_once_t;
472 # define gl_once_define(STORAGECLASS, NAME) \
473 STORAGECLASS pth_once_t NAME = PTH_ONCE_INIT;
474 # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
476 ? glthread_once_multithreaded (ONCE_CONTROL, INITFUNCTION) \
477 : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0))
478 extern int glthread_once_multithreaded (pth_once_t *once_control, void (*initfunction) (void));
479 extern int glthread_once_singlethreaded (pth_once_t *once_control);
487 /* ========================================================================= */
489 #if USE_SOLARIS_THREADS
491 /* Use the old Solaris threads library. */
501 # if USE_SOLARIS_THREADS_WEAK
503 /* Use weak references to the old Solaris threads library. */
505 # pragma weak mutex_init
506 # pragma weak mutex_lock
507 # pragma weak mutex_unlock
508 # pragma weak mutex_destroy
509 # pragma weak rwlock_init
510 # pragma weak rw_rdlock
511 # pragma weak rw_wrlock
512 # pragma weak rw_unlock
513 # pragma weak rwlock_destroy
514 # pragma weak thr_self
516 # pragma weak thr_suspend
517 # define thread_in_use() (thr_suspend != NULL)
521 # define thread_in_use() 1
525 /* -------------------------- gl_lock_t datatype -------------------------- */
527 typedef mutex_t gl_lock_t;
528 # define gl_lock_define(STORAGECLASS, NAME) \
529 STORAGECLASS mutex_t NAME;
530 # define gl_lock_define_initialized(STORAGECLASS, NAME) \
531 STORAGECLASS mutex_t NAME = gl_lock_initializer;
532 # define gl_lock_initializer \
534 # define glthread_lock_init(LOCK) \
535 (thread_in_use () ? mutex_init (LOCK, USYNC_THREAD, NULL) : 0)
536 # define glthread_lock_lock(LOCK) \
537 (thread_in_use () ? mutex_lock (LOCK) : 0)
538 # define glthread_lock_unlock(LOCK) \
539 (thread_in_use () ? mutex_unlock (LOCK) : 0)
540 # define glthread_lock_destroy(LOCK) \
541 (thread_in_use () ? mutex_destroy (LOCK) : 0)
543 /* ------------------------- gl_rwlock_t datatype ------------------------- */
545 typedef rwlock_t gl_rwlock_t;
546 # define gl_rwlock_define(STORAGECLASS, NAME) \
547 STORAGECLASS rwlock_t NAME;
548 # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
549 STORAGECLASS rwlock_t NAME = gl_rwlock_initializer;
550 # define gl_rwlock_initializer \
552 # define glthread_rwlock_init(LOCK) \
553 (thread_in_use () ? rwlock_init (LOCK, USYNC_THREAD, NULL) : 0)
554 # define glthread_rwlock_rdlock(LOCK) \
555 (thread_in_use () ? rw_rdlock (LOCK) : 0)
556 # define glthread_rwlock_wrlock(LOCK) \
557 (thread_in_use () ? rw_wrlock (LOCK) : 0)
558 # define glthread_rwlock_unlock(LOCK) \
559 (thread_in_use () ? rw_unlock (LOCK) : 0)
560 # define glthread_rwlock_destroy(LOCK) \
561 (thread_in_use () ? rwlock_destroy (LOCK) : 0)
563 /* --------------------- gl_recursive_lock_t datatype --------------------- */
565 /* Old Solaris threads did not have recursive locks.
566 We have to implement them ourselves. */
575 # define gl_recursive_lock_define(STORAGECLASS, NAME) \
576 STORAGECLASS gl_recursive_lock_t NAME;
577 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
578 STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
579 # define gl_recursive_lock_initializer \
580 { DEFAULTMUTEX, (thread_t) 0, 0 }
581 # define glthread_recursive_lock_init(LOCK) \
584 if (thread_in_use ()) \
585 glthread_recursive_lock_init_multithreaded (LOCK); \
588 # define glthread_recursive_lock_lock(LOCK) \
591 if (thread_in_use ()) \
592 glthread_recursive_lock_lock_multithreaded (LOCK); \
595 # define glthread_recursive_lock_unlock(LOCK) \
598 if (thread_in_use ()) \
599 glthread_recursive_lock_unlock_multithreaded (LOCK); \
602 # define glthread_recursive_lock_destroy(LOCK) \
605 if (thread_in_use ()) \
606 glthread_recursive_lock_destroy_multithreaded (LOCK); \
609 extern int glthread_recursive_lock_init_multithreaded (gl_recursive_lock_t *lock);
610 extern int glthread_recursive_lock_lock_multithreaded (gl_recursive_lock_t *lock);
611 extern int glthread_recursive_lock_unlock_multithreaded (gl_recursive_lock_t *lock);
612 extern int glthread_recursive_lock_destroy_multithreaded (gl_recursive_lock_t *lock);
614 /* -------------------------- gl_once_t datatype -------------------------- */
622 # define gl_once_define(STORAGECLASS, NAME) \
623 STORAGECLASS gl_once_t NAME = { 0, DEFAULTMUTEX };
624 # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
626 ? glthread_once_multithreaded (ONCE_CONTROL, INITFUNCTION) \
627 : (glthread_once_singlethreaded (ONCE_CONTROL) ? (INITFUNCTION (), 0) : 0))
628 extern int glthread_once_multithreaded (gl_once_t *once_control, void (*initfunction) (void));
629 extern int glthread_once_singlethreaded (gl_once_t *once_control);
637 /* ========================================================================= */
639 #if USE_WIN32_THREADS
641 # include <windows.h>
647 /* We can use CRITICAL_SECTION directly, rather than the Win32 Event, Mutex,
648 Semaphore types, because
649 - we need only to synchronize inside a single process (address space),
650 not inter-process locking,
651 - we don't need to support trylock operations. (TryEnterCriticalSection
652 does not work on Windows 95/98/ME. Packages that need trylock usually
653 define their own mutex type.) */
655 /* There is no way to statically initialize a CRITICAL_SECTION. It needs
656 to be done lazily, once only. For this we need spinlocks. */
658 typedef struct { volatile int done; volatile long started; } gl_spinlock_t;
660 /* -------------------------- gl_lock_t datatype -------------------------- */
664 gl_spinlock_t guard; /* protects the initialization */
665 CRITICAL_SECTION lock;
668 # define gl_lock_define(STORAGECLASS, NAME) \
669 STORAGECLASS gl_lock_t NAME;
670 # define gl_lock_define_initialized(STORAGECLASS, NAME) \
671 STORAGECLASS gl_lock_t NAME = gl_lock_initializer;
672 # define gl_lock_initializer \
674 # define glthread_lock_init(LOCK) \
675 (glthread_lock_init_func (LOCK), 0)
676 # define glthread_lock_lock(LOCK) \
677 glthread_lock_lock_func (LOCK)
678 # define glthread_lock_unlock(LOCK) \
679 glthread_lock_unlock_func (LOCK)
680 # define glthread_lock_destroy(LOCK) \
681 glthread_lock_destroy_func (LOCK)
682 extern void glthread_lock_init_func (gl_lock_t *lock);
683 extern int glthread_lock_lock_func (gl_lock_t *lock);
684 extern int glthread_lock_unlock_func (gl_lock_t *lock);
685 extern int glthread_lock_destroy_func (gl_lock_t *lock);
687 /* ------------------------- gl_rwlock_t datatype ------------------------- */
689 /* It is impossible to implement read-write locks using plain locks, without
690 introducing an extra thread dedicated to managing read-write locks.
691 Therefore here we need to use the low-level Event type. */
695 HANDLE *array; /* array of waiting threads, each represented by an event */
696 unsigned int count; /* number of waiting threads */
697 unsigned int alloc; /* length of allocated array */
698 unsigned int offset; /* index of first waiting thread in array */
703 gl_spinlock_t guard; /* protects the initialization */
704 CRITICAL_SECTION lock; /* protects the remaining fields */
705 gl_waitqueue_t waiting_readers; /* waiting readers */
706 gl_waitqueue_t waiting_writers; /* waiting writers */
707 int runcount; /* number of readers running, or -1 when a writer runs */
710 # define gl_rwlock_define(STORAGECLASS, NAME) \
711 STORAGECLASS gl_rwlock_t NAME;
712 # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
713 STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
714 # define gl_rwlock_initializer \
716 # define glthread_rwlock_init(LOCK) \
717 (glthread_rwlock_init_func (LOCK), 0)
718 # define glthread_rwlock_rdlock(LOCK) \
719 glthread_rwlock_rdlock_func (LOCK)
720 # define glthread_rwlock_wrlock(LOCK) \
721 glthread_rwlock_wrlock_func (LOCK)
722 # define glthread_rwlock_unlock(LOCK) \
723 glthread_rwlock_unlock_func (LOCK)
724 # define glthread_rwlock_destroy(LOCK) \
725 glthread_rwlock_destroy_func (LOCK)
726 extern void glthread_rwlock_init_func (gl_rwlock_t *lock);
727 extern int glthread_rwlock_rdlock_func (gl_rwlock_t *lock);
728 extern int glthread_rwlock_wrlock_func (gl_rwlock_t *lock);
729 extern int glthread_rwlock_unlock_func (gl_rwlock_t *lock);
730 extern int glthread_rwlock_destroy_func (gl_rwlock_t *lock);
732 /* --------------------- gl_recursive_lock_t datatype --------------------- */
734 /* The Win32 documentation says that CRITICAL_SECTION already implements a
735 recursive lock. But we need not rely on it: It's easy to implement a
736 recursive lock without this assumption. */
740 gl_spinlock_t guard; /* protects the initialization */
743 CRITICAL_SECTION lock;
746 # define gl_recursive_lock_define(STORAGECLASS, NAME) \
747 STORAGECLASS gl_recursive_lock_t NAME;
748 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
749 STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
750 # define gl_recursive_lock_initializer \
752 # define glthread_recursive_lock_init(LOCK) \
753 (glthread_recursive_lock_init_func (LOCK), 0)
754 # define glthread_recursive_lock_lock(LOCK) \
755 glthread_recursive_lock_lock_func (LOCK)
756 # define glthread_recursive_lock_unlock(LOCK) \
757 glthread_recursive_lock_unlock_func (LOCK)
758 # define glthread_recursive_lock_destroy(LOCK) \
759 glthread_recursive_lock_destroy_func (LOCK)
760 extern void glthread_recursive_lock_init_func (gl_recursive_lock_t *lock);
761 extern int glthread_recursive_lock_lock_func (gl_recursive_lock_t *lock);
762 extern int glthread_recursive_lock_unlock_func (gl_recursive_lock_t *lock);
763 extern int glthread_recursive_lock_destroy_func (gl_recursive_lock_t *lock);
765 /* -------------------------- gl_once_t datatype -------------------------- */
770 volatile long started;
771 CRITICAL_SECTION lock;
774 # define gl_once_define(STORAGECLASS, NAME) \
775 STORAGECLASS gl_once_t NAME = { -1, -1 };
776 # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
777 (glthread_once_func (ONCE_CONTROL, INITFUNCTION), 0)
778 extern void glthread_once_func (gl_once_t *once_control, void (*initfunction) (void));
786 /* ========================================================================= */
788 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)
790 /* Provide dummy implementation if threads are not supported. */
792 /* -------------------------- gl_lock_t datatype -------------------------- */
794 typedef int gl_lock_t;
795 # define gl_lock_define(STORAGECLASS, NAME)
796 # define gl_lock_define_initialized(STORAGECLASS, NAME)
797 # define glthread_lock_init(NAME) 0
798 # define glthread_lock_lock(NAME) 0
799 # define glthread_lock_unlock(NAME) 0
800 # define glthread_lock_destroy(NAME) 0
802 /* ------------------------- gl_rwlock_t datatype ------------------------- */
804 typedef int gl_rwlock_t;
805 # define gl_rwlock_define(STORAGECLASS, NAME)
806 # define gl_rwlock_define_initialized(STORAGECLASS, NAME)
807 # define glthread_rwlock_init(NAME) 0
808 # define glthread_rwlock_rdlock(NAME) 0
809 # define glthread_rwlock_wrlock(NAME) 0
810 # define glthread_rwlock_unlock(NAME) 0
811 # define glthread_rwlock_destroy(NAME) 0
813 /* --------------------- gl_recursive_lock_t datatype --------------------- */
815 typedef int gl_recursive_lock_t;
816 # define gl_recursive_lock_define(STORAGECLASS, NAME)
817 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME)
818 # define glthread_recursive_lock_init(NAME) 0
819 # define glthread_recursive_lock_lock(NAME) 0
820 # define glthread_recursive_lock_unlock(NAME) 0
821 # define glthread_recursive_lock_destroy(NAME) 0
823 /* -------------------------- gl_once_t datatype -------------------------- */
825 typedef int gl_once_t;
826 # define gl_once_define(STORAGECLASS, NAME) \
827 STORAGECLASS gl_once_t NAME = 0;
828 # define glthread_once(ONCE_CONTROL, INITFUNCTION) \
829 (*(ONCE_CONTROL) == 0 ? (*(ONCE_CONTROL) = ~ 0, INITFUNCTION (), 0) : 0)
833 /* ========================================================================= */
835 /* Macros with built-in error handling. */
837 /* -------------------------- gl_lock_t datatype -------------------------- */
839 #define gl_lock_init(NAME) \
842 if (glthread_lock_init (&NAME)) \
846 #define gl_lock_lock(NAME) \
849 if (glthread_lock_lock (&NAME)) \
853 #define gl_lock_unlock(NAME) \
856 if (glthread_lock_unlock (&NAME)) \
860 #define gl_lock_destroy(NAME) \
863 if (glthread_lock_destroy (&NAME)) \
868 /* ------------------------- gl_rwlock_t datatype ------------------------- */
870 #define gl_rwlock_init(NAME) \
873 if (glthread_rwlock_init (&NAME)) \
877 #define gl_rwlock_rdlock(NAME) \
880 if (glthread_rwlock_rdlock (&NAME)) \
884 #define gl_rwlock_wrlock(NAME) \
887 if (glthread_rwlock_wrlock (&NAME)) \
891 #define gl_rwlock_unlock(NAME) \
894 if (glthread_rwlock_unlock (&NAME)) \
898 #define gl_rwlock_destroy(NAME) \
901 if (glthread_rwlock_destroy (&NAME)) \
906 /* --------------------- gl_recursive_lock_t datatype --------------------- */
908 #define gl_recursive_lock_init(NAME) \
911 if (glthread_recursive_lock_init (&NAME)) \
915 #define gl_recursive_lock_lock(NAME) \
918 if (glthread_recursive_lock_lock (&NAME)) \
922 #define gl_recursive_lock_unlock(NAME) \
925 if (glthread_recursive_lock_unlock (&NAME)) \
929 #define gl_recursive_lock_destroy(NAME) \
932 if (glthread_recursive_lock_destroy (&NAME)) \
937 /* -------------------------- gl_once_t datatype -------------------------- */
939 #define gl_once(NAME, INITFUNCTION) \
942 if (glthread_once (&NAME, INITFUNCTION)) \
947 /* ========================================================================= */