get-rusage-data tests: Avoid failure on Linux/glibc.
[gnulib/ericb.git] / lib / glthread / cond.h
blob9c0f5370b5e67c520dba615c33ba95b6b4e8040c
1 /* Condition variables for multithreading.
2 Copyright (C) 2005-2017 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)
7 any later version.
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, see <http://www.gnu.org/licenses/>. */
17 /* Written by Yoann Vandoorselaere <yoann@prelude-ids.org>, 2008.
18 Based on Bruno Haible <bruno@clisp.org> lock.h */
21 Condition variables can be used for waiting until a condition
22 becomes true. In this respect, they are similar to wait queues. But
23 contrary to wait queues, condition variables have different
24 semantics that allows events to be lost when there is no thread
25 waiting for them.
27 Condition variable:
28 Type: gl_cond_t
29 Declaration: gl_cond_define(extern, name)
30 Initializer: gl_cond_define_initialized(, name)
31 Initialization: gl_cond_init (name);
32 Waiting: gl_cond_wait (name, lock);
33 Timed wait: bool timedout = gl_cond_timedwait (name, lock, abstime);
34 where lock is a gl_lock_t variable (cf. <glthread/lock.h>)
35 Signaling: gl_cond_signal (name);
36 Broadcasting: gl_cond_broadcast (name);
37 De-initialization: gl_cond_destroy (name);
38 Equivalent functions with control of error handling:
39 Initialization: err = glthread_cond_init (&name);
40 Waiting: err = glthread_cond_wait (&name);
41 Timed wait: err = glthread_cond_timedwait (&name, &lock, abstime);
42 Signaling: err = glthread_cond_signal (&name);
43 Broadcasting: err = glthread_cond_broadcast (&name);
44 De-initialization: err = glthread_cond_destroy (&name);
48 #ifndef _GLTHREAD_COND_H
49 #define _GLTHREAD_COND_H
51 #include <errno.h>
52 #include <stdbool.h>
53 #include <stdlib.h>
54 #include <time.h>
56 #include "glthread/lock.h"
57 #ifndef _GL_INLINE_HEADER_BEGIN
58 #error "Please include config.h first."
59 #endif
61 _GL_INLINE_HEADER_BEGIN
62 #ifndef _GLTHREAD_COND_INLINE
63 # define _GLTHREAD_COND_INLINE _GL_INLINE
64 #endif
66 /* ========================================================================= */
68 #if USE_POSIX_THREADS
70 /* Use the POSIX threads library. */
72 # include <pthread.h>
74 # ifdef __cplusplus
75 extern "C" {
76 # endif
78 # if PTHREAD_IN_USE_DETECTION_HARD
80 /* The pthread_in_use() detection needs to be done at runtime. */
81 # define pthread_in_use() \
82 glthread_in_use ()
83 extern int glthread_in_use (void);
85 # endif
87 # if USE_POSIX_THREADS_WEAK
89 /* Use weak references to the POSIX threads library. */
91 /* Weak references avoid dragging in external libraries if the other parts
92 of the program don't use them. Here we use them, because we don't want
93 every program that uses libintl to depend on libpthread. This assumes
94 that libpthread would not be loaded after libintl; i.e. if libintl is
95 loaded first, by an executable that does not depend on libpthread, and
96 then a module is dynamically loaded that depends on libpthread, libintl
97 will not be multithread-safe. */
99 /* The way to test at runtime whether libpthread is present is to test
100 whether a function pointer's value, such as &pthread_mutex_init, is
101 non-NULL. However, some versions of GCC have a bug through which, in
102 PIC mode, &foo != NULL always evaluates to true if there is a direct
103 call to foo(...) in the same function. To avoid this, we test the
104 address of a function in libpthread that we don't use. */
106 # pragma weak pthread_cond_init
107 # pragma weak pthread_cond_wait
108 # pragma weak pthread_cond_timedwait
109 # pragma weak pthread_cond_signal
110 # pragma weak pthread_cond_broadcast
111 # pragma weak pthread_cond_destroy
112 # ifndef pthread_self
113 # pragma weak pthread_self
114 # endif
116 # if !PTHREAD_IN_USE_DETECTION_HARD
117 # pragma weak pthread_cancel
118 # define pthread_in_use() (pthread_cancel != NULL)
119 # endif
121 # else
123 # if !PTHREAD_IN_USE_DETECTION_HARD
124 # define pthread_in_use() 1
125 # endif
127 # endif
129 /* -------------------------- gl_cond_t datatype -------------------------- */
131 typedef pthread_cond_t gl_cond_t;
132 # define gl_cond_define(STORAGECLASS, NAME) \
133 STORAGECLASS gl_cond_t NAME;
134 # define gl_cond_define_initialized(STORAGECLASS, NAME) \
135 STORAGECLASS gl_cond_t NAME = gl_cond_initializer;
136 # define gl_cond_initializer \
137 PTHREAD_COND_INITIALIZER
138 # define glthread_cond_init(COND) \
139 (pthread_in_use () ? pthread_cond_init (COND, NULL) : 0)
140 # define glthread_cond_wait(COND, LOCK) \
141 (pthread_in_use () ? pthread_cond_wait (COND, LOCK) : 0)
142 # define glthread_cond_timedwait(COND, LOCK, ABSTIME) \
143 (pthread_in_use () ? pthread_cond_timedwait (COND, LOCK, ABSTIME) : 0)
144 # define glthread_cond_signal(COND) \
145 (pthread_in_use () ? pthread_cond_signal (COND) : 0)
146 # define glthread_cond_broadcast(COND) \
147 (pthread_in_use () ? pthread_cond_broadcast (COND) : 0)
148 # define glthread_cond_destroy(COND) \
149 (pthread_in_use () ? pthread_cond_destroy (COND) : 0)
151 # ifdef __cplusplus
153 # endif
155 #endif
157 /* ========================================================================= */
159 #if USE_PTH_THREADS
161 /* Use the GNU Pth threads library. */
163 # include <pth.h>
165 # ifdef __cplusplus
166 extern "C" {
167 # endif
169 # if USE_PTH_THREADS_WEAK
171 /* Use weak references to the GNU Pth threads library. */
173 # pragma weak pth_cond_init
174 # pragma weak pth_cond_await
175 # pragma weak pth_cond_notify
176 # pragma weak pth_event
177 # pragma weak pth_timeout
179 # pragma weak pth_cancel
180 # define pth_in_use() (pth_cancel != NULL)
182 # else
184 # define pth_in_use() 1
186 # endif
188 /* -------------------------- gl_cond_t datatype -------------------------- */
190 typedef pth_cond_t gl_cond_t;
191 # define gl_cond_define(STORAGECLASS, NAME) \
192 STORAGECLASS gl_cond_t NAME;
193 # define gl_cond_define_initialized(STORAGECLASS, NAME) \
194 STORAGECLASS gl_cond_t NAME = gl_cond_initializer;
195 # define gl_cond_initializer \
196 PTH_COND_INIT
197 # define glthread_cond_init(COND) \
198 (pth_in_use () && !pth_cond_init (COND) ? errno : 0)
199 # define glthread_cond_wait(COND, LOCK) \
200 (pth_in_use () && !pth_cond_await (COND, LOCK, NULL) ? errno : 0)
201 # define glthread_cond_timedwait(COND, LOCK, ABSTIME) \
202 (pth_in_use () ? glthread_cond_timedwait_multithreaded (COND, LOCK, ABSTIME) : 0)
203 # define glthread_cond_signal(COND) \
204 (pth_in_use () && !pth_cond_notify (COND, FALSE) ? errno : 0)
205 # define glthread_cond_broadcast(COND) \
206 (pth_in_use () && !pth_cond_notify (COND, TRUE) ? errno : 0)
207 # define glthread_cond_destroy(COND) 0
208 extern int glthread_cond_timedwait_multithreaded (gl_cond_t *cond, gl_lock_t *lock, struct timespec *abstime);
210 # ifdef __cplusplus
212 # endif
214 #endif
216 /* ========================================================================= */
218 #if USE_SOLARIS_THREADS
220 /* Use the old Solaris threads library. */
222 # include <thread.h>
223 # include <synch.h>
225 # ifdef __cplusplus
226 extern "C" {
227 # endif
229 # if USE_SOLARIS_THREADS_WEAK
231 /* Use weak references to the old Solaris threads library. */
233 # pragma weak cond_init
234 # pragma weak cond_wait
235 # pragma weak cond_timedwait
236 # pragma weak cond_signal
237 # pragma weak cond_broadcast
238 # pragma weak cond_destroy
239 # pragma weak thr_suspend
240 # define thread_in_use() (thr_suspend != NULL)
242 # else
244 # define thread_in_use() 1
246 # endif
248 /* -------------------------- gl_cond_t datatype -------------------------- */
250 typedef cond_t gl_cond_t;
251 # define gl_cond_define(STORAGECLASS, NAME) \
252 STORAGECLASS gl_cond_t NAME;
253 # define gl_cond_define_initialized(STORAGECLASS, NAME) \
254 STORAGECLASS gl_cond_t NAME = gl_cond_initializer;
255 # define gl_cond_initializer \
256 DEFAULTCV
257 # define glthread_cond_init(COND) \
258 (pthread_in_use () ? cond_init (COND, USYNC_THREAD, NULL) : 0)
259 # define glthread_cond_wait(COND, LOCK) \
260 (pthread_in_use () ? cond_wait (COND, LOCK) : 0)
261 # define glthread_cond_timedwait(COND, LOCK, ABSTIME) \
262 (pthread_in_use () ? glthread_cond_timedwait_multithreaded (COND, LOCK, ABSTIME) : 0)
263 # define glthread_cond_signal(COND) \
264 (pthread_in_use () ? cond_signal (COND) : 0)
265 # define glthread_cond_broadcast(COND) \
266 (pthread_in_use () ? cond_broadcast (COND) : 0)
267 # define glthread_cond_destroy(COND) \
268 (pthread_in_use () ? cond_destroy (COND) : 0)
269 extern int glthread_cond_timedwait_multithreaded (gl_cond_t *cond, gl_lock_t *lock, struct timespec *abstime);
271 # ifdef __cplusplus
273 # endif
275 #endif
277 /* ========================================================================= */
279 #if USE_WINDOWS_THREADS
281 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
282 # include <windows.h>
284 # ifdef __cplusplus
285 extern "C" {
286 # endif
288 /* -------------------------- gl_cond_t datatype -------------------------- */
290 struct gl_waitqueue_link
292 struct gl_waitqueue_link *wql_next;
293 struct gl_waitqueue_link *wql_prev;
295 typedef struct
297 struct gl_waitqueue_link wq_list; /* circular list of waiting threads */
299 gl_linked_waitqueue_t;
300 typedef struct
302 gl_spinlock_t guard; /* protects the initialization */
303 CRITICAL_SECTION lock; /* protects the remaining fields */
304 gl_linked_waitqueue_t waiters; /* waiting threads */
306 gl_cond_t;
307 # define gl_cond_define(STORAGECLASS, NAME) \
308 STORAGECLASS gl_cond_t NAME;
309 # define gl_cond_define_initialized(STORAGECLASS, NAME) \
310 STORAGECLASS gl_cond_t NAME = gl_cond_initializer;
311 # define gl_cond_initializer \
312 { { 0, -1 } }
313 # define glthread_cond_init(COND) \
314 glthread_cond_init_func (COND)
315 # define glthread_cond_wait(COND, LOCK) \
316 glthread_cond_wait_func (COND, LOCK)
317 # define glthread_cond_timedwait(COND, LOCK, ABSTIME) \
318 glthread_cond_timedwait_func (COND, LOCK, ABSTIME)
319 # define glthread_cond_signal(COND) \
320 glthread_cond_signal_func (COND)
321 # define glthread_cond_broadcast(COND) \
322 glthread_cond_broadcast_func (COND)
323 # define glthread_cond_destroy(COND) \
324 glthread_cond_destroy_func (COND)
325 extern int glthread_cond_init_func (gl_cond_t *cond);
326 extern int glthread_cond_wait_func (gl_cond_t *cond, gl_lock_t *lock);
327 extern int glthread_cond_timedwait_func (gl_cond_t *cond, gl_lock_t *lock, struct timespec *abstime);
328 extern int glthread_cond_signal_func (gl_cond_t *cond);
329 extern int glthread_cond_broadcast_func (gl_cond_t *cond);
330 extern int glthread_cond_destroy_func (gl_cond_t *cond);
332 # ifdef __cplusplus
334 # endif
336 #endif
338 /* ========================================================================= */
340 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WINDOWS_THREADS)
342 /* Provide dummy implementation if threads are not supported. */
344 typedef int gl_cond_t;
345 # define gl_cond_define(STORAGECLASS, NAME)
346 # define gl_cond_define_initialized(STORAGECLASS, NAME)
347 # define glthread_cond_init(COND) 0
348 # define glthread_cond_wait(COND, LOCK) 0
349 # define glthread_cond_timedwait(COND, LOCK, ABSTIME) 0
350 # define glthread_cond_signal(COND) 0
351 # define glthread_cond_broadcast(COND) 0
352 # define glthread_cond_destroy(COND) 0
354 #endif
356 /* ========================================================================= */
358 /* Macros with built-in error handling. */
360 #ifdef __cplusplus
361 extern "C" {
362 #endif
364 #define gl_cond_init(COND) \
365 do \
367 if (glthread_cond_init (&COND)) \
368 abort (); \
370 while (0)
371 #define gl_cond_wait(COND, LOCK) \
372 do \
374 if (glthread_cond_wait (&COND, &LOCK)) \
375 abort (); \
377 while (0)
378 #define gl_cond_timedwait(COND, LOCK, ABSTIME) \
379 gl_cond_timedwait_func (&COND, &LOCK, ABSTIME)
380 _GLTHREAD_COND_INLINE bool
381 gl_cond_timedwait_func (gl_cond_t *cond, gl_lock_t *lock, struct timespec *abstime)
383 int err = glthread_cond_timedwait (cond, lock, abstime);
384 if (err == ETIMEDOUT)
385 return true;
386 if (err != 0)
387 abort ();
388 return false;
390 #define gl_cond_signal(COND) \
391 do \
393 if (glthread_cond_signal (&COND)) \
394 abort (); \
396 while (0)
397 #define gl_cond_broadcast(COND) \
398 do \
400 if (glthread_cond_broadcast (&COND)) \
401 abort (); \
403 while (0)
404 #define gl_cond_destroy(COND) \
405 do \
407 if (glthread_cond_destroy (&COND)) \
408 abort (); \
410 while (0)
412 #ifdef __cplusplus
414 #endif
416 _GL_INLINE_HEADER_END
418 #endif /* _GLTHREAD_COND_H */