Debian Codegraph @

lib/glthread/lock.h

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