e8ed12ced7c6e8f659a692db90c0231ebcbdac30
[gnulib.git] / lib / glthread / cond.h
1 /* Condition variables for multithreading.
2    Copyright (C) 2005-2008 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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Yoann Vandoorselaere <yoann@prelude-ids.org>, 2008.
19    Based on Bruno Haible <bruno@clisp.org> lock.h */
20
21 /*
22    Condition variables can be used for waiting until a condition
23    becomes true. In this respect, they are similar to wait queues. But
24    contrary to wait queues, condition variables have different
25    semantics that allows events to be lost when there is no thread
26    waiting for them.
27
28    Condition variable:
29      Type:                gl_cond_t
30      Declaration:         gl_cond_define(extern, name)
31      Initializer:         gl_cond_define_initialized(, name)
32      Initialization:      gl_cond_init (name);
33      Waiting:             gl_cond_wait (name, lock);
34      Timed wait:          bool timedout = gl_cond_timedwait (name, lock, abstime);
35                           where lock is a gl_lock_t variable (cf. <glthread/lock.h>)
36      Signaling:           gl_cond_signal (name);
37      Broadcasting:        gl_cond_broadcast (name);
38      De-initialization:   gl_cond_destroy (name);
39    Equivalent functions with control of error handling:
40      Initialization:      err = glthread_cond_init (&name);
41      Waiting:             err = glthread_cond_wait (&name);
42      Timed wait:          err = glthread_cond_timedwait (&name, &lock, abstime);
43      Signaling:           err = glthread_cond_signal (&name);
44      Broadcasting:        err = glthread_cond_broadcast (&name);
45      De-initialization:   err = glthread_cond_destroy (&name);
46 */
47
48
49 #ifndef _GLTHREAD_COND_H
50 #define _GLTHREAD_COND_H
51
52 #include <errno.h>
53 #include <stdbool.h>
54
55 #include "glthread/lock.h"
56
57 /* ========================================================================= */
58
59 #if USE_POSIX_THREADS
60
61 /* Use the POSIX threads library.  */
62
63 # include <pthread.h>
64 # include <stdlib.h>
65
66 # ifdef __cplusplus
67 extern "C" {
68 # endif
69
70 # if PTHREAD_IN_USE_DETECTION_HARD
71
72 /* The pthread_in_use() detection needs to be done at runtime.  */
73 #  define pthread_in_use() \
74      glthread_in_use ()
75 extern int glthread_in_use (void);
76
77 # endif
78
79 # if USE_POSIX_THREADS_WEAK
80
81 /* Use weak references to the POSIX threads library.  */
82
83 /* Weak references avoid dragging in external libraries if the other parts
84    of the program don't use them.  Here we use them, because we don't want
85    every program that uses libintl to depend on libpthread.  This assumes
86    that libpthread would not be loaded after libintl; i.e. if libintl is
87    loaded first, by an executable that does not depend on libpthread, and
88    then a module is dynamically loaded that depends on libpthread, libintl
89    will not be multithread-safe.  */
90
91 /* The way to test at runtime whether libpthread is present is to test
92    whether a function pointer's value, such as &pthread_mutex_init, is
93    non-NULL.  However, some versions of GCC have a bug through which, in
94    PIC mode, &foo != NULL always evaluates to true if there is a direct
95    call to foo(...) in the same function.  To avoid this, we test the
96    address of a function in libpthread that we don't use.  */
97
98 #  pragma weak pthread_cond_init
99 #  pragma weak pthread_cond_wait
100 #  pragma weak pthread_cond_timedwait
101 #  pragma weak pthread_cond_signal
102 #  pragma weak pthread_cond_broadcast
103 #  pragma weak pthread_cond_destroy
104 #  ifndef pthread_self
105 #   pragma weak pthread_self
106 #  endif
107
108 #  if !PTHREAD_IN_USE_DETECTION_HARD
109 #   pragma weak pthread_cancel
110 #   define pthread_in_use() (pthread_cancel != NULL)
111 #  endif
112
113 # else
114
115 #  if !PTHREAD_IN_USE_DETECTION_HARD
116 #   define pthread_in_use() 1
117 #  endif
118
119 # endif
120
121 /* -------------------------- gl_cond_t datatype -------------------------- */
122
123 typedef pthread_cond_t gl_cond_t;
124 # define gl_cond_define(STORAGECLASS, NAME) \
125     STORAGECLASS pthread_cond_t NAME;
126 # define gl_cond_define_initialized(STORAGECLASS, NAME) \
127     STORAGECLASS pthread_cond_t NAME = gl_cond_initializer;
128 # define gl_cond_initializer \
129     PTHREAD_COND_INITIALIZER
130 # define glthread_cond_init(COND) \
131     (pthread_in_use () ? pthread_cond_init (COND, NULL) : 0)
132 # define glthread_cond_wait(COND, LOCK) \
133     (pthread_in_use () ? pthread_cond_wait (COND, LOCK) : 0)
134 # define glthread_cond_timedwait(COND, LOCK, ABSTIME) \
135     (pthread_in_use () ? pthread_cond_timedwait (COND, LOCK, ABSTIME) : 0)
136 # define glthread_cond_signal(COND) \
137     (pthread_in_use () ? pthread_cond_signal (COND) : 0)
138 # define glthread_cond_broadcast(COND) \
139     (pthread_in_use () ? pthread_cond_broadcast (COND) : 0)
140 # define glthread_cond_destroy(COND) \
141     (pthread_in_use () ? pthread_cond_destroy (COND) : 0)
142
143 #endif
144
145 /* ========================================================================= */
146
147 #if USE_PTH_THREADS
148
149 /* Use the GNU Pth threads library.  */
150
151 # include <pth.h>
152 # include <stdlib.h>
153
154 # ifdef __cplusplus
155 extern "C" {
156 # endif
157
158 # if USE_PTH_THREADS_WEAK
159
160 /* Use weak references to the GNU Pth threads library.  */
161
162 #  pragma weak pth_cond_init
163 #  pragma weak pth_cond_await
164 #  pragma weak pth_cond_notify
165 #  pragma weak pth_event
166 #  pragma weak pth_timeout
167
168 #  pragma weak pth_cancel
169 #  define pth_in_use() (pth_cancel != NULL)
170
171 # else
172
173 #  define pth_in_use() 1
174
175 # endif
176
177 /* -------------------------- gl_cond_t datatype -------------------------- */
178
179 typedef pth_cond_t gl_cond_t;
180 # define gl_cond_define(STORAGECLASS, NAME) \
181     STORAGECLASS pth_cond_t NAME;
182 # define gl_cond_define_initialized(STORAGECLASS, NAME) \
183     STORAGECLASS pth_cond_t NAME = gl_cond_initializer;
184 # define gl_cond_initializer \
185     PTH_COND_INIT
186 # define glthread_cond_init(COND) \
187     (pth_in_use () && !pth_cond_init (COND) ? errno : 0)
188 # define glthread_cond_wait(COND, LOCK) \
189     (pth_in_use () && !pth_cond_await (COND, LOCK, NULL) ? errno : 0)
190 # define glthread_cond_timedwait(COND, LOCK, ABSTIME) \
191     (pth_in_use () ? glthread_cond_timedwait_multithreaded (COND, LOCK, ABSTIME) : 0)
192 # define glthread_cond_signal(COND) \
193     (pth_in_use () && !pth_cond_notify (COND, FALSE) ? errno : 0)
194 # define glthread_cond_broadcast(COND) \
195     (pth_in_use () && !pth_cond_notify (COND, TRUE) ? errno : 0)
196 # define glthread_cond_destroy(COND) 0
197 extern int glthread_cond_timedwait_multithreaded (gl_cond_t *cond, gl_lock_t *lock, struct timespec *abstime);
198
199 #endif
200
201 /* ========================================================================= */
202
203 #if USE_SOLARIS_THREADS
204
205 /* Use the old Solaris threads library.  */
206
207 # include <thread.h>
208 # include <synch.h>
209 # include <stdlib.h>
210
211 # ifdef __cplusplus
212 extern "C" {
213 # endif
214
215 # if USE_SOLARIS_THREADS_WEAK
216
217 /* Use weak references to the old Solaris threads library.  */
218
219 #  pragma weak cond_init
220 #  pragma weak cond_wait
221 #  pragma weak cond_timedwait
222 #  pragma weak cond_signal
223 #  pragma weak cond_broadcast
224 #  pragma weak cond_destroy
225 #  pragma weak thr_suspend
226 #  define thread_in_use() (thr_suspend != NULL)
227
228 # else
229
230 #  define thread_in_use() 1
231
232 # endif
233
234 /* -------------------------- gl_cond_t datatype -------------------------- */
235
236 #define ETIMEDOUT ETIME
237
238 typedef pthread_cond_t gl_cond_t;
239 # define gl_cond_define(STORAGECLASS, NAME) \
240     STORAGECLASS cond_t NAME;
241 # define gl_cond_define_initialized(STORAGECLASS, NAME) \
242     STORAGECLASS cond_t NAME = gl_cond_initializer;
243 # define gl_cond_initializer \
244     DEFAULTCV
245 # define glthread_cond_init(COND) \
246     (pthread_in_use () ? cond_init (COND, USYNC_THREAD, NULL) : 0)
247 # define glthread_cond_wait(COND, LOCK) \
248     (pthread_in_use () ? cond_wait (COND, LOCK) : 0)
249 # define glthread_cond_timedwait(COND, LOCK, ABSTIME) \
250     (pthread_in_use () ? cond_timedwait (COND, LOCK, ABSTIME) : 0)
251 # define glthread_cond_signal(COND) \
252     (pthread_in_use () ? cond_signal (COND) : 0)
253 # define glthread_cond_broadcast(COND) \
254     (pthread_in_use () ? cond_broadcast (COND) : 0)
255 # define glthread_cond_destroy(COND) \
256     (pthread_in_use () ? cond_destroy (COND) : 0)
257
258 #endif
259
260 /* ========================================================================= */
261
262 #if !(USE_POSIX_THREADS || USE_PTH_THREADS || USE_SOLARIS_THREADS || USE_WIN32_THREADS)
263
264 /* Provide dummy implementation if threads are not supported.  */
265
266 typedef int gl_cond_t;
267 # define gl_cond_define(STORAGECLASS, NAME)
268 # define gl_cond_define_initialized(STORAGECLASS, NAME)
269 # define glthread_cond_init(COND) 0
270 # define glthread_cond_wait(COND, LOCK) 0
271 # define glthread_cond_timedwait(COND, LOCK, ABSTIME) 0
272 # define glthread_cond_signal(COND) 0
273 # define glthread_cond_broadcast(COND) 0
274 # define glthread_cond_destroy(COND) 0
275
276 #endif
277
278 /* ========================================================================= */
279
280 /* Macros with built-in error handling.  */
281
282 #define gl_cond_init(COND)             \
283    do                                  \
284      {                                 \
285        if (glthread_cond_init (&COND)) \
286          abort ();                     \
287      }                                 \
288    while (0)
289 #define gl_cond_wait(COND, LOCK)              \
290    do                                         \
291      {                                        \
292        if (glthread_cond_wait (&COND, &LOCK)) \
293          abort ();                            \
294      }                                        \
295    while (0)
296 #define gl_cond_timedwait(COND, LOCK, ABSTIME) \
297   gl_cond_timedwait_func (&COND, &LOCK, ABSTIME)
298 static inline bool
299 gl_cond_timedwait_func (gl_cond_t *cond, gl_lock_t *lock, struct timespec *abstime)
300 {
301   int err = glthread_cond_timedwait (cond, lock, abstime);
302   if (err == ETIMEDOUT)
303     return true;
304   if (err != 0)
305     abort ();
306   return false;
307 }
308 #define gl_cond_signal(COND)             \
309    do                                    \
310      {                                   \
311        if (glthread_cond_signal (&COND)) \
312          abort ();                       \
313      }                                   \
314    while (0)
315 #define gl_cond_broadcast(COND)             \
316    do                                       \
317      {                                      \
318        if (glthread_cond_broadcast (&COND)) \
319          abort ();                          \
320      }                                      \
321    while (0)
322 #define gl_cond_destroy(COND)             \
323    do                                     \
324      {                                    \
325        if (glthread_cond_destroy (&COND)) \
326          abort ();                        \
327      }                                    \
328    while (0)
329
330 #endif /* _GLTHREAD_COND_H */