headers: check that _GL_INLINE_HEADER_BEGIN is defined
[gnulib.git] / lib / glthread / cond.h
1 /* Condition variables for multithreading.
2    Copyright (C) 2005-2013 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Written by Yoann Vandoorselaere <yoann@prelude-ids.org>, 2008.
18    Based on Bruno Haible <bruno@clisp.org> lock.h */
19
20 /*
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.
26
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);
45 */
46
47
48 #ifndef _GLTHREAD_COND_H
49 #define _GLTHREAD_COND_H
50
51 #include <errno.h>
52 #include <stdbool.h>
53 #include <stdlib.h>
54 #include <time.h>
55
56 #include "glthread/lock.h"
57 #ifndef _GL_INLINE_HEADER_BEGIN
58  #error "Please include config.h first."
59 #endif
60
61 _GL_INLINE_HEADER_BEGIN
62 #ifndef _GLTHREAD_COND_INLINE
63 # define _GLTHREAD_COND_INLINE _GL_INLINE
64 #endif
65
66 /* ========================================================================= */
67
68 #if USE_POSIX_THREADS
69
70 /* Use the POSIX threads library.  */
71
72 # include <pthread.h>
73
74 # ifdef __cplusplus
75 extern "C" {
76 # endif
77
78 # if PTHREAD_IN_USE_DETECTION_HARD
79
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);
84
85 # endif
86
87 # if USE_POSIX_THREADS_WEAK
88
89 /* Use weak references to the POSIX threads library.  */
90
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.  */
98
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.  */
105
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
115
116 #  if !PTHREAD_IN_USE_DETECTION_HARD
117 #   pragma weak pthread_cancel
118 #   define pthread_in_use() (pthread_cancel != NULL)
119 #  endif
120
121 # else
122
123 #  if !PTHREAD_IN_USE_DETECTION_HARD
124 #   define pthread_in_use() 1
125 #  endif
126
127 # endif
128
129 /* -------------------------- gl_cond_t datatype -------------------------- */
130
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)
150
151 # ifdef __cplusplus
152 }
153 # endif
154
155 #endif
156
157 /* ========================================================================= */
158
159 #if USE_PTH_THREADS
160
161 /* Use the GNU Pth threads library.  */
162
163 # include <pth.h>
164
165 # ifdef __cplusplus
166 extern "C" {
167 # endif
168
169 # if USE_PTH_THREADS_WEAK
170
171 /* Use weak references to the GNU Pth threads library.  */
172
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
178
179 #  pragma weak pth_cancel
180 #  define pth_in_use() (pth_cancel != NULL)
181
182 # else
183
184 #  define pth_in_use() 1
185
186 # endif
187
188 /* -------------------------- gl_cond_t datatype -------------------------- */
189
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);
209
210 # ifdef __cplusplus
211 }
212 # endif
213
214 #endif
215
216 /* ========================================================================= */
217
218 #if USE_SOLARIS_THREADS
219
220 /* Use the old Solaris threads library.  */
221
222 # include <thread.h>
223 # include <synch.h>
224
225 # ifdef __cplusplus
226 extern "C" {
227 # endif
228
229 # if USE_SOLARIS_THREADS_WEAK
230
231 /* Use weak references to the old Solaris threads library.  */
232
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)
241
242 # else
243
244 #  define thread_in_use() 1
245
246 # endif
247
248 /* -------------------------- gl_cond_t datatype -------------------------- */
249
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);
270
271 # ifdef __cplusplus
272 }
273 # endif
274
275 #endif
276
277 /* ========================================================================= */
278
279 #if USE_WINDOWS_THREADS
280
281 # define WIN32_LEAN_AND_MEAN  /* avoid including junk */
282 # include <windows.h>
283
284 # ifdef __cplusplus
285 extern "C" {
286 # endif
287
288 /* -------------------------- gl_cond_t datatype -------------------------- */
289
290 struct gl_waitqueue_link
291 {
292   struct gl_waitqueue_link *wql_next;
293   struct gl_waitqueue_link *wql_prev;
294 };
295 typedef struct
296         {
297           struct gl_waitqueue_link wq_list; /* circular list of waiting threads */
298         }
299         gl_linked_waitqueue_t;
300 typedef struct
301         {
302           gl_spinlock_t guard; /* protects the initialization */
303           CRITICAL_SECTION lock; /* protects the remaining fields */
304           gl_linked_waitqueue_t waiters; /* waiting threads */
305         }
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);
331
332 # ifdef __cplusplus
333 }
334 # endif
335
336 #endif
337
338 /* ========================================================================= */
339
340 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WINDOWS_THREADS)
341
342 /* Provide dummy implementation if threads are not supported.  */
343
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
353
354 #endif
355
356 /* ========================================================================= */
357
358 /* Macros with built-in error handling.  */
359
360 #ifdef __cplusplus
361 extern "C" {
362 #endif
363
364 #define gl_cond_init(COND)             \
365    do                                  \
366      {                                 \
367        if (glthread_cond_init (&COND)) \
368          abort ();                     \
369      }                                 \
370    while (0)
371 #define gl_cond_wait(COND, LOCK)              \
372    do                                         \
373      {                                        \
374        if (glthread_cond_wait (&COND, &LOCK)) \
375          abort ();                            \
376      }                                        \
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)
382 {
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;
389 }
390 #define gl_cond_signal(COND)             \
391    do                                    \
392      {                                   \
393        if (glthread_cond_signal (&COND)) \
394          abort ();                       \
395      }                                   \
396    while (0)
397 #define gl_cond_broadcast(COND)             \
398    do                                       \
399      {                                      \
400        if (glthread_cond_broadcast (&COND)) \
401          abort ();                          \
402      }                                      \
403    while (0)
404 #define gl_cond_destroy(COND)             \
405    do                                     \
406      {                                    \
407        if (glthread_cond_destroy (&COND)) \
408          abort ();                        \
409      }                                    \
410    while (0)
411
412 #ifdef __cplusplus
413 }
414 #endif
415
416 _GL_INLINE_HEADER_END
417
418 #endif /* _GLTHREAD_COND_H */