s/long/int
[dragonfly.git] / lib / libthread_xu / thread / thr_cond.c
1 /*
2  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $DragonFly: src/lib/libthread_xu/thread/thr_cond.c,v 1.9 2006/04/06 13:05:35 davidxu Exp $
27  */
28
29 #include "namespace.h"
30 #include <machine/tls.h>
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <pthread.h>
35 #include <limits.h>
36 #include "un-namespace.h"
37
38 #include "thr_private.h"
39
40 umtx_t          _cond_static_lock;
41
42 int     __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
43 int     __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
44                        const struct timespec *abstime);
45 /*
46  * Prototypes
47  */
48 static int cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
49 static int cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
50                     const struct timespec *abstime, int cancel);
51 static int cond_signal_common(pthread_cond_t *cond, int broadcast);
52
53 static int
54 cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
55 {
56         pthread_cond_t  pcond;
57         int             rval = 0;
58
59         if ((pcond = (pthread_cond_t)
60             malloc(sizeof(struct pthread_cond))) == NULL) {
61                 rval = ENOMEM;
62         } else {
63                 /*
64                  * Initialise the condition variable structure:
65                  */
66                 _thr_umtx_init(&pcond->c_lock);
67                 pcond->c_seqno = 0;
68                 pcond->c_waiters = 0;
69                 pcond->c_wakeups = 0;
70                 if (cond_attr == NULL || *cond_attr == NULL) {
71                         pcond->c_pshared = 0;
72                         pcond->c_clockid = CLOCK_REALTIME;
73                 } else {
74                         pcond->c_pshared = (*cond_attr)->c_pshared;
75                         pcond->c_clockid = (*cond_attr)->c_clockid;
76                 }
77                 *cond = pcond;
78         }
79         /* Return the completion status: */
80         return (rval);
81 }
82
83 static int
84 init_static(struct pthread *thread, pthread_cond_t *cond)
85 {
86         int ret;
87
88         THR_LOCK_ACQUIRE(thread, &_cond_static_lock);
89
90         if (*cond == NULL)
91                 ret = cond_init(cond, NULL);
92         else
93                 ret = 0;
94
95         THR_LOCK_RELEASE(thread, &_cond_static_lock);
96
97         return (ret);
98 }
99
100 int
101 _pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
102 {
103         *cond = NULL;
104         return cond_init(cond, cond_attr);
105 }
106
107 int
108 _pthread_cond_destroy(pthread_cond_t *cond)
109 {
110         struct pthread_cond     *cv;
111         struct pthread          *curthread = tls_get_curthread();
112         int                     rval = 0;
113
114         if (*cond == NULL)
115                 rval = EINVAL;
116         else {
117                 /* Lock the condition variable structure: */
118                 THR_LOCK_ACQUIRE(curthread, &(*cond)->c_lock);
119                 if ((*cond)->c_waiters + (*cond)->c_wakeups != 0) {
120                         THR_LOCK_RELEASE(curthread, &(*cond)->c_lock);
121                         return (EBUSY);
122                 }
123
124                 /*
125                  * NULL the caller's pointer now that the condition
126                  * variable has been destroyed:
127                  */
128                 cv = *cond;
129                 *cond = NULL;
130
131                 /* Unlock the condition variable structure: */
132                 THR_LOCK_RELEASE(curthread, &cv->c_lock);
133
134                 /* Free the cond lock structure: */
135
136                 /*
137                  * Free the memory allocated for the condition
138                  * variable structure:
139                  */
140                 free(cv);
141
142         }
143         /* Return the completion status: */
144         return (rval);
145 }
146
147 struct cond_cancel_info
148 {
149         pthread_mutex_t *mutex;
150         pthread_cond_t  *cond;
151         long            seqno;
152 };
153
154 static void
155 cond_cancel_handler(void *arg)
156 {
157         struct pthread *curthread = tls_get_curthread();
158         struct cond_cancel_info *cci = (struct cond_cancel_info *)arg;
159         pthread_cond_t cv;
160
161         cv = *(cci->cond);
162         THR_LOCK_ACQUIRE(curthread, &cv->c_lock);
163         if (cv->c_seqno != cci->seqno && cv->c_wakeups != 0) {
164                 if (cv->c_waiters > 0) {
165                         cv->c_seqno++;
166                         _thr_umtx_wake(&cv->c_seqno, 1);
167                 } else
168                         cv->c_wakeups--;
169         } else {
170                 cv->c_waiters--;
171         }
172         THR_LOCK_RELEASE(curthread, &cv->c_lock);
173
174         _mutex_cv_lock(cci->mutex);
175 }
176
177 static int
178 cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
179         const struct timespec *abstime, int cancel)
180 {
181         struct pthread  *curthread = tls_get_curthread();
182         struct timespec ts, ts2, *tsp;
183         struct cond_cancel_info cci;
184         pthread_cond_t  cv;
185         int             seq, oldseq;
186         int             oldcancel;
187         int             ret = 0;
188
189         /*
190          * If the condition variable is statically initialized,
191          * perform the dynamic initialization:
192          */
193         if (__predict_false(*cond == NULL &&
194             (ret = init_static(curthread, cond)) != 0))
195                 return (ret);
196
197         cv = *cond;
198         THR_LOCK_ACQUIRE(curthread, &cv->c_lock);
199         ret = _mutex_cv_unlock(mutex);
200         if (ret) {
201                 THR_LOCK_RELEASE(curthread, &cv->c_lock);
202                 return (ret);
203         }
204         oldseq = seq = cv->c_seqno;
205         cci.mutex = mutex;
206         cci.cond  = cond;
207         cci.seqno = oldseq;
208
209         cv->c_waiters++;
210         do {
211                 THR_LOCK_RELEASE(curthread, &cv->c_lock);
212
213                 if (abstime != NULL) {
214                         clock_gettime(cv->c_clockid, &ts);
215                         TIMESPEC_SUB(&ts2, abstime, &ts);
216                         tsp = &ts2;
217                 } else
218                         tsp = NULL;
219
220                 if (cancel) {
221                         THR_CLEANUP_PUSH(curthread, cond_cancel_handler, &cci);
222                         oldcancel = _thr_cancel_enter(curthread);
223                         ret = _thr_umtx_wait(&cv->c_seqno, seq, tsp,
224                                 cv->c_clockid);
225                         _thr_cancel_leave(curthread, oldcancel);
226                         THR_CLEANUP_POP(curthread, 0);
227                 } else {
228                         ret = _thr_umtx_wait(&cv->c_seqno, seq, tsp,
229                                 cv->c_clockid);
230                 }
231
232                 THR_LOCK_ACQUIRE(curthread, &cv->c_lock);
233                 seq = cv->c_seqno;
234                 if (abstime != NULL && ret == ETIMEDOUT)
235                         break;
236
237                 /*
238                  * loop if we have never been told to wake up
239                  * or we lost a race.
240                  */
241         } while (seq == oldseq || cv->c_wakeups == 0);
242         
243         if (seq != oldseq && cv->c_wakeups != 0) {
244                 cv->c_wakeups--;
245                 ret = 0;
246         } else {
247                 cv->c_waiters--;
248         }
249         THR_LOCK_RELEASE(curthread, &cv->c_lock);
250         _mutex_cv_lock(mutex);
251         return (ret);
252 }
253
254 int
255 _pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
256 {
257
258         return (cond_wait_common(cond, mutex, NULL, 0));
259 }
260
261 int
262 __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
263 {
264
265         return (cond_wait_common(cond, mutex, NULL, 1));
266 }
267
268 int
269 _pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
270                        const struct timespec * abstime)
271 {
272         if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
273             abstime->tv_nsec >= 1000000000)
274                 return (EINVAL);
275
276         return (cond_wait_common(cond, mutex, abstime, 0));
277 }
278
279 int
280 __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
281                        const struct timespec *abstime)
282 {
283         if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
284             abstime->tv_nsec >= 1000000000)
285                 return (EINVAL);
286
287         return (cond_wait_common(cond, mutex, abstime, 1));
288 }
289
290 static int
291 cond_signal_common(pthread_cond_t *cond, int broadcast)
292 {
293         struct pthread  *curthread = tls_get_curthread();
294         pthread_cond_t  cv;
295         int             ret = 0;
296
297         /*
298          * If the condition variable is statically initialized, perform dynamic
299          * initialization.
300          */
301         if (__predict_false(*cond == NULL &&
302             (ret = init_static(curthread, cond)) != 0))
303                 return (ret);
304
305         cv = *cond;
306         /* Lock the condition variable structure. */
307         THR_LOCK_ACQUIRE(curthread, &cv->c_lock);
308         if (cv->c_waiters) {
309                 if (!broadcast) {
310                         cv->c_wakeups++;
311                         cv->c_waiters--;
312                         cv->c_seqno++;
313                         _thr_umtx_wake(&cv->c_seqno, 1);
314                 } else {
315                         cv->c_wakeups += cv->c_waiters;
316                         cv->c_waiters = 0;
317                         cv->c_seqno++;
318                         _thr_umtx_wake(&cv->c_seqno, INT_MAX);
319                 }
320         }
321         THR_LOCK_RELEASE(curthread, &cv->c_lock);
322         return (ret);
323 }
324
325 int
326 _pthread_cond_signal(pthread_cond_t * cond)
327 {
328
329         return (cond_signal_common(cond, 0));
330 }
331
332 int
333 _pthread_cond_broadcast(pthread_cond_t * cond)
334 {
335
336         return (cond_signal_common(cond, 1));
337 }
338
339 /*
340  * Double underscore versions are cancellation points.  Single underscore
341  * versions are not and are provided for libc internal usage (which
342  * shouldn't introduce cancellation points).
343  */
344 __strong_reference(__pthread_cond_wait, pthread_cond_wait);
345 __strong_reference(__pthread_cond_timedwait, pthread_cond_timedwait);
346
347 __strong_reference(_pthread_cond_init, pthread_cond_init);
348 __strong_reference(_pthread_cond_destroy, pthread_cond_destroy);
349 __strong_reference(_pthread_cond_signal, pthread_cond_signal);
350 __strong_reference(_pthread_cond_broadcast, pthread_cond_broadcast);
351