1 /* Locking in multithreaded situations.
2 Copyright (C) 2005-2006 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);
35 Read-Write (non-recursive) locks:
37 Declaration: gl_rwlock_define(extern, name)
38 Initializer: gl_rwlock_define_initialized(, name)
39 Initialization: gl_rwlock_init (name);
40 Taking the lock: gl_rwlock_rdlock (name);
41 gl_rwlock_wrlock (name);
42 Releasing the lock: gl_rwlock_unlock (name);
43 De-initialization: gl_rwlock_destroy (name);
46 Type: gl_recursive_lock_t
47 Declaration: gl_recursive_lock_define(extern, name)
48 Initializer: gl_recursive_lock_define_initialized(, name)
49 Initialization: gl_recursive_lock_init (name);
50 Taking the lock: gl_recursive_lock_lock (name);
51 Releasing the lock: gl_recursive_lock_unlock (name);
52 De-initialization: gl_recursive_lock_destroy (name);
56 Initializer: gl_once_define(extern, name)
57 Execution: gl_once (name, initfunction);
64 /* ========================================================================= */
68 /* Use the POSIX threads library. */
77 # if PTHREAD_IN_USE_DETECTION_HARD
79 /* The pthread_in_use() detection needs to be done at runtime. */
80 # define pthread_in_use() \
82 extern int glthread_in_use (void);
86 # if USE_POSIX_THREADS_WEAK
88 /* Use weak references to the POSIX threads library. */
90 /* Weak references avoid dragging in external libraries if the other parts
91 of the program don't use them. Here we use them, because we don't want
92 every program that uses libintl to depend on libpthread. This assumes
93 that libpthread would not be loaded after libintl; i.e. if libintl is
94 loaded first, by an executable that does not depend on libpthread, and
95 then a module is dynamically loaded that depends on libpthread, libintl
96 will not be multithread-safe. */
98 /* The way to test at runtime whether libpthread is present is to test
99 whether a function pointer's value, such as &pthread_mutex_init, is
100 non-NULL. However, some versions of GCC have a bug through which, in
101 PIC mode, &foo != NULL always evaluates to true if there is a direct
102 call to foo(...) in the same function. To avoid this, we test the
103 address of a function in libpthread that we don't use. */
105 # pragma weak pthread_mutex_init
106 # pragma weak pthread_mutex_lock
107 # pragma weak pthread_mutex_unlock
108 # pragma weak pthread_mutex_destroy
109 # pragma weak pthread_rwlock_init
110 # pragma weak pthread_rwlock_rdlock
111 # pragma weak pthread_rwlock_wrlock
112 # pragma weak pthread_rwlock_unlock
113 # pragma weak pthread_rwlock_destroy
114 # pragma weak pthread_once
115 # pragma weak pthread_cond_init
116 # pragma weak pthread_cond_wait
117 # pragma weak pthread_cond_signal
118 # pragma weak pthread_cond_broadcast
119 # pragma weak pthread_cond_destroy
120 # pragma weak pthread_mutexattr_init
121 # pragma weak pthread_mutexattr_settype
122 # pragma weak pthread_mutexattr_destroy
123 # ifndef pthread_self
124 # pragma weak pthread_self
127 # if !PTHREAD_IN_USE_DETECTION_HARD
128 # pragma weak pthread_cancel
129 # define pthread_in_use() (pthread_cancel != NULL)
134 # if !PTHREAD_IN_USE_DETECTION_HARD
135 # define pthread_in_use() 1
140 /* -------------------------- gl_lock_t datatype -------------------------- */
142 typedef pthread_mutex_t gl_lock_t;
143 # define gl_lock_define(STORAGECLASS, NAME) \
144 STORAGECLASS pthread_mutex_t NAME;
145 # define gl_lock_define_initialized(STORAGECLASS, NAME) \
146 STORAGECLASS pthread_mutex_t NAME = gl_lock_initializer;
147 # define gl_lock_initializer \
148 PTHREAD_MUTEX_INITIALIZER
149 # define gl_lock_init(NAME) \
150 if (pthread_in_use () && pthread_mutex_init (&NAME, NULL) != 0) abort ()
151 # define gl_lock_lock(NAME) \
152 if (pthread_in_use () && pthread_mutex_lock (&NAME) != 0) abort ()
153 # define gl_lock_unlock(NAME) \
154 if (pthread_in_use () && pthread_mutex_unlock (&NAME) != 0) abort ()
155 # define gl_lock_destroy(NAME) \
156 if (pthread_in_use () && pthread_mutex_destroy (&NAME) != 0) abort ()
158 /* ------------------------- gl_rwlock_t datatype ------------------------- */
160 # if HAVE_PTHREAD_RWLOCK
162 # ifdef PTHREAD_RWLOCK_INITIALIZER
164 typedef pthread_rwlock_t gl_rwlock_t;
165 # define gl_rwlock_define(STORAGECLASS, NAME) \
166 STORAGECLASS pthread_rwlock_t NAME;
167 # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
168 STORAGECLASS pthread_rwlock_t NAME = gl_rwlock_initializer;
169 # define gl_rwlock_initializer \
170 PTHREAD_RWLOCK_INITIALIZER
171 # define gl_rwlock_init(NAME) \
172 if (pthread_in_use () && pthread_rwlock_init (&NAME, NULL) != 0) abort ()
173 # define gl_rwlock_rdlock(NAME) \
174 if (pthread_in_use () && pthread_rwlock_rdlock (&NAME) != 0) abort ()
175 # define gl_rwlock_wrlock(NAME) \
176 if (pthread_in_use () && pthread_rwlock_wrlock (&NAME) != 0) abort ()
177 # define gl_rwlock_unlock(NAME) \
178 if (pthread_in_use () && pthread_rwlock_unlock (&NAME) != 0) abort ()
179 # define gl_rwlock_destroy(NAME) \
180 if (pthread_in_use () && pthread_rwlock_destroy (&NAME) != 0) abort ()
187 pthread_mutex_t guard; /* protects the initialization */
188 pthread_rwlock_t rwlock; /* read-write lock */
191 # define gl_rwlock_define(STORAGECLASS, NAME) \
192 STORAGECLASS gl_rwlock_t NAME;
193 # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
194 STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
195 # define gl_rwlock_initializer \
196 { 0, PTHREAD_MUTEX_INITIALIZER }
197 # define gl_rwlock_init(NAME) \
198 if (pthread_in_use ()) glthread_rwlock_init (&NAME)
199 # define gl_rwlock_rdlock(NAME) \
200 if (pthread_in_use ()) glthread_rwlock_rdlock (&NAME)
201 # define gl_rwlock_wrlock(NAME) \
202 if (pthread_in_use ()) glthread_rwlock_wrlock (&NAME)
203 # define gl_rwlock_unlock(NAME) \
204 if (pthread_in_use ()) glthread_rwlock_unlock (&NAME)
205 # define gl_rwlock_destroy(NAME) \
206 if (pthread_in_use ()) glthread_rwlock_destroy (&NAME)
207 extern void glthread_rwlock_init (gl_rwlock_t *lock);
208 extern void glthread_rwlock_rdlock (gl_rwlock_t *lock);
209 extern void glthread_rwlock_wrlock (gl_rwlock_t *lock);
210 extern void glthread_rwlock_unlock (gl_rwlock_t *lock);
211 extern void glthread_rwlock_destroy (gl_rwlock_t *lock);
219 pthread_mutex_t lock; /* protects the remaining fields */
220 pthread_cond_t waiting_readers; /* waiting readers */
221 pthread_cond_t waiting_writers; /* waiting writers */
222 unsigned int waiting_writers_count; /* number of waiting writers */
223 int runcount; /* number of readers running, or -1 when a writer runs */
226 # define gl_rwlock_define(STORAGECLASS, NAME) \
227 STORAGECLASS gl_rwlock_t NAME;
228 # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
229 STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
230 # define gl_rwlock_initializer \
231 { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0 }
232 # define gl_rwlock_init(NAME) \
233 if (pthread_in_use ()) glthread_rwlock_init (&NAME)
234 # define gl_rwlock_rdlock(NAME) \
235 if (pthread_in_use ()) glthread_rwlock_rdlock (&NAME)
236 # define gl_rwlock_wrlock(NAME) \
237 if (pthread_in_use ()) glthread_rwlock_wrlock (&NAME)
238 # define gl_rwlock_unlock(NAME) \
239 if (pthread_in_use ()) glthread_rwlock_unlock (&NAME)
240 # define gl_rwlock_destroy(NAME) \
241 if (pthread_in_use ()) glthread_rwlock_destroy (&NAME)
242 extern void glthread_rwlock_init (gl_rwlock_t *lock);
243 extern void glthread_rwlock_rdlock (gl_rwlock_t *lock);
244 extern void glthread_rwlock_wrlock (gl_rwlock_t *lock);
245 extern void glthread_rwlock_unlock (gl_rwlock_t *lock);
246 extern void glthread_rwlock_destroy (gl_rwlock_t *lock);
250 /* --------------------- gl_recursive_lock_t datatype --------------------- */
252 # if HAVE_PTHREAD_MUTEX_RECURSIVE
254 # if defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER || defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
256 typedef pthread_mutex_t gl_recursive_lock_t;
257 # define gl_recursive_lock_define(STORAGECLASS, NAME) \
258 STORAGECLASS pthread_mutex_t NAME;
259 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
260 STORAGECLASS pthread_mutex_t NAME = gl_recursive_lock_initializer;
261 # ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER
262 # define gl_recursive_lock_initializer \
263 PTHREAD_RECURSIVE_MUTEX_INITIALIZER
265 # define gl_recursive_lock_initializer \
266 PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
268 # define gl_recursive_lock_init(NAME) \
269 if (pthread_in_use () && pthread_mutex_init (&NAME, NULL) != 0) abort ()
270 # define gl_recursive_lock_lock(NAME) \
271 if (pthread_in_use () && pthread_mutex_lock (&NAME) != 0) abort ()
272 # define gl_recursive_lock_unlock(NAME) \
273 if (pthread_in_use () && pthread_mutex_unlock (&NAME) != 0) abort ()
274 # define gl_recursive_lock_destroy(NAME) \
275 if (pthread_in_use () && pthread_mutex_destroy (&NAME) != 0) abort ()
281 pthread_mutex_t recmutex; /* recursive mutex */
282 pthread_mutex_t guard; /* protects the initialization */
286 # define gl_recursive_lock_define(STORAGECLASS, NAME) \
287 STORAGECLASS gl_recursive_lock_t NAME;
288 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
289 STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
290 # define gl_recursive_lock_initializer \
291 { PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, 0 }
292 # define gl_recursive_lock_init(NAME) \
293 if (pthread_in_use ()) glthread_recursive_lock_init (&NAME)
294 # define gl_recursive_lock_lock(NAME) \
295 if (pthread_in_use ()) glthread_recursive_lock_lock (&NAME)
296 # define gl_recursive_lock_unlock(NAME) \
297 if (pthread_in_use ()) glthread_recursive_lock_unlock (&NAME)
298 # define gl_recursive_lock_destroy(NAME) \
299 if (pthread_in_use ()) glthread_recursive_lock_destroy (&NAME)
300 extern void glthread_recursive_lock_init (gl_recursive_lock_t *lock);
301 extern void glthread_recursive_lock_lock (gl_recursive_lock_t *lock);
302 extern void glthread_recursive_lock_unlock (gl_recursive_lock_t *lock);
303 extern void glthread_recursive_lock_destroy (gl_recursive_lock_t *lock);
309 /* Old versions of POSIX threads on Solaris did not have recursive locks.
310 We have to implement them ourselves. */
314 pthread_mutex_t mutex;
319 # define gl_recursive_lock_define(STORAGECLASS, NAME) \
320 STORAGECLASS gl_recursive_lock_t NAME;
321 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
322 STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
323 # define gl_recursive_lock_initializer \
324 { PTHREAD_MUTEX_INITIALIZER, (pthread_t) 0, 0 }
325 # define gl_recursive_lock_init(NAME) \
326 if (pthread_in_use ()) glthread_recursive_lock_init (&NAME)
327 # define gl_recursive_lock_lock(NAME) \
328 if (pthread_in_use ()) glthread_recursive_lock_lock (&NAME)
329 # define gl_recursive_lock_unlock(NAME) \
330 if (pthread_in_use ()) glthread_recursive_lock_unlock (&NAME)
331 # define gl_recursive_lock_destroy(NAME) \
332 if (pthread_in_use ()) glthread_recursive_lock_destroy (&NAME)
333 extern void glthread_recursive_lock_init (gl_recursive_lock_t *lock);
334 extern void glthread_recursive_lock_lock (gl_recursive_lock_t *lock);
335 extern void glthread_recursive_lock_unlock (gl_recursive_lock_t *lock);
336 extern void glthread_recursive_lock_destroy (gl_recursive_lock_t *lock);
340 /* -------------------------- gl_once_t datatype -------------------------- */
342 typedef pthread_once_t gl_once_t;
343 # define gl_once_define(STORAGECLASS, NAME) \
344 STORAGECLASS pthread_once_t NAME = PTHREAD_ONCE_INIT;
345 # define gl_once(NAME, INITFUNCTION) \
348 if (pthread_in_use ()) \
350 if (pthread_once (&NAME, INITFUNCTION) != 0) \
355 if (glthread_once_singlethreaded (&NAME)) \
360 extern int glthread_once_singlethreaded (pthread_once_t *once_control);
368 /* ========================================================================= */
372 /* Use the GNU Pth threads library. */
381 # if USE_PTH_THREADS_WEAK
383 /* Use weak references to the GNU Pth threads library. */
385 # pragma weak pth_mutex_init
386 # pragma weak pth_mutex_acquire
387 # pragma weak pth_mutex_release
388 # pragma weak pth_rwlock_init
389 # pragma weak pth_rwlock_acquire
390 # pragma weak pth_rwlock_release
391 # pragma weak pth_once
393 # pragma weak pth_cancel
394 # define pth_in_use() (pth_cancel != NULL)
398 # define pth_in_use() 1
402 /* -------------------------- gl_lock_t datatype -------------------------- */
404 typedef pth_mutex_t gl_lock_t;
405 # define gl_lock_define(STORAGECLASS, NAME) \
406 STORAGECLASS pth_mutex_t NAME;
407 # define gl_lock_define_initialized(STORAGECLASS, NAME) \
408 STORAGECLASS pth_mutex_t NAME = gl_lock_initializer;
409 # define gl_lock_initializer \
411 # define gl_lock_init(NAME) \
412 if (pth_in_use() && !pth_mutex_init (&NAME)) abort ()
413 # define gl_lock_lock(NAME) \
414 if (pth_in_use() && !pth_mutex_acquire (&NAME, 0, NULL)) abort ()
415 # define gl_lock_unlock(NAME) \
416 if (pth_in_use() && !pth_mutex_release (&NAME)) abort ()
417 # define gl_lock_destroy(NAME) \
420 /* ------------------------- gl_rwlock_t datatype ------------------------- */
422 typedef pth_rwlock_t gl_rwlock_t;
423 # define gl_rwlock_define(STORAGECLASS, NAME) \
424 STORAGECLASS pth_rwlock_t NAME;
425 # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
426 STORAGECLASS pth_rwlock_t NAME = gl_rwlock_initializer;
427 # define gl_rwlock_initializer \
429 # define gl_rwlock_init(NAME) \
430 if (pth_in_use() && !pth_rwlock_init (&NAME)) abort ()
431 # define gl_rwlock_rdlock(NAME) \
432 if (pth_in_use() && !pth_rwlock_acquire (&NAME, PTH_RWLOCK_RD, 0, NULL)) abort ()
433 # define gl_rwlock_wrlock(NAME) \
434 if (pth_in_use() && !pth_rwlock_acquire (&NAME, PTH_RWLOCK_RW, 0, NULL)) abort ()
435 # define gl_rwlock_unlock(NAME) \
436 if (pth_in_use() && !pth_rwlock_release (&NAME)) abort ()
437 # define gl_rwlock_destroy(NAME) \
440 /* --------------------- gl_recursive_lock_t datatype --------------------- */
442 /* In Pth, mutexes are recursive by default. */
443 typedef pth_mutex_t gl_recursive_lock_t;
444 # define gl_recursive_lock_define(STORAGECLASS, NAME) \
445 STORAGECLASS pth_mutex_t NAME;
446 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
447 STORAGECLASS pth_mutex_t NAME = gl_recursive_lock_initializer;
448 # define gl_recursive_lock_initializer \
450 # define gl_recursive_lock_init(NAME) \
451 if (pth_in_use() && !pth_mutex_init (&NAME)) abort ()
452 # define gl_recursive_lock_lock(NAME) \
453 if (pth_in_use() && !pth_mutex_acquire (&NAME, 0, NULL)) abort ()
454 # define gl_recursive_lock_unlock(NAME) \
455 if (pth_in_use() && !pth_mutex_release (&NAME)) abort ()
456 # define gl_recursive_lock_destroy(NAME) \
459 /* -------------------------- gl_once_t datatype -------------------------- */
461 typedef pth_once_t gl_once_t;
462 # define gl_once_define(STORAGECLASS, NAME) \
463 STORAGECLASS pth_once_t NAME = PTH_ONCE_INIT;
464 # define gl_once(NAME, INITFUNCTION) \
469 void (*gl_once_temp) (void) = INITFUNCTION; \
470 if (!pth_once (&NAME, glthread_once_call, &gl_once_temp)) \
475 if (glthread_once_singlethreaded (&NAME)) \
480 extern void glthread_once_call (void *arg);
481 extern int glthread_once_singlethreaded (pth_once_t *once_control);
489 /* ========================================================================= */
491 #if USE_SOLARIS_THREADS
493 /* Use the old Solaris threads library. */
503 # if USE_SOLARIS_THREADS_WEAK
505 /* Use weak references to the old Solaris threads library. */
507 # pragma weak mutex_init
508 # pragma weak mutex_lock
509 # pragma weak mutex_unlock
510 # pragma weak mutex_destroy
511 # pragma weak rwlock_init
512 # pragma weak rw_rdlock
513 # pragma weak rw_wrlock
514 # pragma weak rw_unlock
515 # pragma weak rwlock_destroy
516 # pragma weak thr_self
518 # pragma weak thr_suspend
519 # define thread_in_use() (thr_suspend != NULL)
523 # define thread_in_use() 1
527 /* -------------------------- gl_lock_t datatype -------------------------- */
529 typedef mutex_t gl_lock_t;
530 # define gl_lock_define(STORAGECLASS, NAME) \
531 STORAGECLASS mutex_t NAME;
532 # define gl_lock_define_initialized(STORAGECLASS, NAME) \
533 STORAGECLASS mutex_t NAME = gl_lock_initializer;
534 # define gl_lock_initializer \
536 # define gl_lock_init(NAME) \
537 if (thread_in_use () && mutex_init (&NAME, USYNC_THREAD, NULL) != 0) abort ()
538 # define gl_lock_lock(NAME) \
539 if (thread_in_use () && mutex_lock (&NAME) != 0) abort ()
540 # define gl_lock_unlock(NAME) \
541 if (thread_in_use () && mutex_unlock (&NAME) != 0) abort ()
542 # define gl_lock_destroy(NAME) \
543 if (thread_in_use () && mutex_destroy (&NAME) != 0) abort ()
545 /* ------------------------- gl_rwlock_t datatype ------------------------- */
547 typedef rwlock_t gl_rwlock_t;
548 # define gl_rwlock_define(STORAGECLASS, NAME) \
549 STORAGECLASS rwlock_t NAME;
550 # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
551 STORAGECLASS rwlock_t NAME = gl_rwlock_initializer;
552 # define gl_rwlock_initializer \
554 # define gl_rwlock_init(NAME) \
555 if (thread_in_use () && rwlock_init (&NAME, USYNC_THREAD, NULL) != 0) abort ()
556 # define gl_rwlock_rdlock(NAME) \
557 if (thread_in_use () && rw_rdlock (&NAME) != 0) abort ()
558 # define gl_rwlock_wrlock(NAME) \
559 if (thread_in_use () && rw_wrlock (&NAME) != 0) abort ()
560 # define gl_rwlock_unlock(NAME) \
561 if (thread_in_use () && rw_unlock (&NAME) != 0) abort ()
562 # define gl_rwlock_destroy(NAME) \
563 if (thread_in_use () && rwlock_destroy (&NAME) != 0) abort ()
565 /* --------------------- gl_recursive_lock_t datatype --------------------- */
567 /* Old Solaris threads did not have recursive locks.
568 We have to implement them ourselves. */
577 # define gl_recursive_lock_define(STORAGECLASS, NAME) \
578 STORAGECLASS gl_recursive_lock_t NAME;
579 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
580 STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
581 # define gl_recursive_lock_initializer \
582 { DEFAULTMUTEX, (thread_t) 0, 0 }
583 # define gl_recursive_lock_init(NAME) \
584 if (thread_in_use ()) glthread_recursive_lock_init (&NAME)
585 # define gl_recursive_lock_lock(NAME) \
586 if (thread_in_use ()) glthread_recursive_lock_lock (&NAME)
587 # define gl_recursive_lock_unlock(NAME) \
588 if (thread_in_use ()) glthread_recursive_lock_unlock (&NAME)
589 # define gl_recursive_lock_destroy(NAME) \
590 if (thread_in_use ()) glthread_recursive_lock_destroy (&NAME)
591 extern void glthread_recursive_lock_init (gl_recursive_lock_t *lock);
592 extern void glthread_recursive_lock_lock (gl_recursive_lock_t *lock);
593 extern void glthread_recursive_lock_unlock (gl_recursive_lock_t *lock);
594 extern void glthread_recursive_lock_destroy (gl_recursive_lock_t *lock);
596 /* -------------------------- gl_once_t datatype -------------------------- */
604 # define gl_once_define(STORAGECLASS, NAME) \
605 STORAGECLASS gl_once_t NAME = { 0, DEFAULTMUTEX };
606 # define gl_once(NAME, INITFUNCTION) \
609 if (thread_in_use ()) \
611 glthread_once (&NAME, INITFUNCTION); \
615 if (glthread_once_singlethreaded (&NAME)) \
620 extern void glthread_once (gl_once_t *once_control, void (*initfunction) (void));
621 extern int glthread_once_singlethreaded (gl_once_t *once_control);
629 /* ========================================================================= */
631 #if USE_WIN32_THREADS
633 # include <windows.h>
639 /* We can use CRITICAL_SECTION directly, rather than the Win32 Event, Mutex,
640 Semaphore types, because
641 - we need only to synchronize inside a single process (address space),
642 not inter-process locking,
643 - we don't need to support trylock operations. (TryEnterCriticalSection
644 does not work on Windows 95/98/ME. Packages that need trylock usually
645 define their own mutex type.) */
647 /* There is no way to statically initialize a CRITICAL_SECTION. It needs
648 to be done lazily, once only. For this we need spinlocks. */
650 typedef struct { volatile int done; volatile long started; } gl_spinlock_t;
652 /* -------------------------- gl_lock_t datatype -------------------------- */
656 gl_spinlock_t guard; /* protects the initialization */
657 CRITICAL_SECTION lock;
660 # define gl_lock_define(STORAGECLASS, NAME) \
661 STORAGECLASS gl_lock_t NAME;
662 # define gl_lock_define_initialized(STORAGECLASS, NAME) \
663 STORAGECLASS gl_lock_t NAME = gl_lock_initializer;
664 # define gl_lock_initializer \
666 # define gl_lock_init(NAME) \
667 glthread_lock_init (&NAME)
668 # define gl_lock_lock(NAME) \
669 glthread_lock_lock (&NAME)
670 # define gl_lock_unlock(NAME) \
671 glthread_lock_unlock (&NAME)
672 # define gl_lock_destroy(NAME) \
673 glthread_lock_destroy (&NAME)
674 extern void glthread_lock_init (gl_lock_t *lock);
675 extern void glthread_lock_lock (gl_lock_t *lock);
676 extern void glthread_lock_unlock (gl_lock_t *lock);
677 extern void glthread_lock_destroy (gl_lock_t *lock);
679 /* ------------------------- gl_rwlock_t datatype ------------------------- */
681 /* It is impossible to implement read-write locks using plain locks, without
682 introducing an extra thread dedicated to managing read-write locks.
683 Therefore here we need to use the low-level Event type. */
687 HANDLE *array; /* array of waiting threads, each represented by an event */
688 unsigned int count; /* number of waiting threads */
689 unsigned int alloc; /* length of allocated array */
690 unsigned int offset; /* index of first waiting thread in array */
695 gl_spinlock_t guard; /* protects the initialization */
696 CRITICAL_SECTION lock; /* protects the remaining fields */
697 gl_waitqueue_t waiting_readers; /* waiting readers */
698 gl_waitqueue_t waiting_writers; /* waiting writers */
699 int runcount; /* number of readers running, or -1 when a writer runs */
702 # define gl_rwlock_define(STORAGECLASS, NAME) \
703 STORAGECLASS gl_rwlock_t NAME;
704 # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
705 STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
706 # define gl_rwlock_initializer \
708 # define gl_rwlock_init(NAME) \
709 glthread_rwlock_init (&NAME)
710 # define gl_rwlock_rdlock(NAME) \
711 glthread_rwlock_rdlock (&NAME)
712 # define gl_rwlock_wrlock(NAME) \
713 glthread_rwlock_wrlock (&NAME)
714 # define gl_rwlock_unlock(NAME) \
715 glthread_rwlock_unlock (&NAME)
716 # define gl_rwlock_destroy(NAME) \
717 glthread_rwlock_destroy (&NAME)
718 extern void glthread_rwlock_init (gl_rwlock_t *lock);
719 extern void glthread_rwlock_rdlock (gl_rwlock_t *lock);
720 extern void glthread_rwlock_wrlock (gl_rwlock_t *lock);
721 extern void glthread_rwlock_unlock (gl_rwlock_t *lock);
722 extern void glthread_rwlock_destroy (gl_rwlock_t *lock);
724 /* --------------------- gl_recursive_lock_t datatype --------------------- */
726 /* The Win32 documentation says that CRITICAL_SECTION already implements a
727 recursive lock. But we need not rely on it: It's easy to implement a
728 recursive lock without this assumption. */
732 gl_spinlock_t guard; /* protects the initialization */
735 CRITICAL_SECTION lock;
738 # define gl_recursive_lock_define(STORAGECLASS, NAME) \
739 STORAGECLASS gl_recursive_lock_t NAME;
740 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
741 STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
742 # define gl_recursive_lock_initializer \
744 # define gl_recursive_lock_init(NAME) \
745 glthread_recursive_lock_init (&NAME)
746 # define gl_recursive_lock_lock(NAME) \
747 glthread_recursive_lock_lock (&NAME)
748 # define gl_recursive_lock_unlock(NAME) \
749 glthread_recursive_lock_unlock (&NAME)
750 # define gl_recursive_lock_destroy(NAME) \
751 glthread_recursive_lock_destroy (&NAME)
752 extern void glthread_recursive_lock_init (gl_recursive_lock_t *lock);
753 extern void glthread_recursive_lock_lock (gl_recursive_lock_t *lock);
754 extern void glthread_recursive_lock_unlock (gl_recursive_lock_t *lock);
755 extern void glthread_recursive_lock_destroy (gl_recursive_lock_t *lock);
757 /* -------------------------- gl_once_t datatype -------------------------- */
762 volatile long started;
763 CRITICAL_SECTION lock;
766 # define gl_once_define(STORAGECLASS, NAME) \
767 STORAGECLASS gl_once_t NAME = { -1, -1 };
768 # define gl_once(NAME, INITFUNCTION) \
769 glthread_once (&NAME, INITFUNCTION)
770 extern void glthread_once (gl_once_t *once_control, void (*initfunction) (void));
778 /* ========================================================================= */
780 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)
782 /* Provide dummy implementation if threads are not supported. */
784 /* -------------------------- gl_lock_t datatype -------------------------- */
786 typedef int gl_lock_t;
787 # define gl_lock_define(STORAGECLASS, NAME)
788 # define gl_lock_define_initialized(STORAGECLASS, NAME)
789 # define gl_lock_init(NAME)
790 # define gl_lock_lock(NAME)
791 # define gl_lock_unlock(NAME)
793 /* ------------------------- gl_rwlock_t datatype ------------------------- */
795 typedef int gl_rwlock_t;
796 # define gl_rwlock_define(STORAGECLASS, NAME)
797 # define gl_rwlock_define_initialized(STORAGECLASS, NAME)
798 # define gl_rwlock_init(NAME)
799 # define gl_rwlock_rdlock(NAME)
800 # define gl_rwlock_wrlock(NAME)
801 # define gl_rwlock_unlock(NAME)
803 /* --------------------- gl_recursive_lock_t datatype --------------------- */
805 typedef int gl_recursive_lock_t;
806 # define gl_recursive_lock_define(STORAGECLASS, NAME)
807 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME)
808 # define gl_recursive_lock_init(NAME)
809 # define gl_recursive_lock_lock(NAME)
810 # define gl_recursive_lock_unlock(NAME)
812 /* -------------------------- gl_once_t datatype -------------------------- */
814 typedef int gl_once_t;
815 # define gl_once_define(STORAGECLASS, NAME) \
816 STORAGECLASS gl_once_t NAME = 0;
817 # define gl_once(NAME, INITFUNCTION) \
830 /* ========================================================================= */