2 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
26 * $DragonFly: src/lib/libthread_xu/thread/thr_cond.c,v 1.10 2006/04/06 23:50:13 davidxu Exp $
29 #include "namespace.h"
30 #include <machine/tls.h>
36 #include "un-namespace.h"
38 #include "thr_private.h"
40 umtx_t _cond_static_lock;
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);
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);
54 cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
59 if ((pcond = (pthread_cond_t)
60 malloc(sizeof(struct pthread_cond))) == NULL) {
64 * Initialise the condition variable structure:
66 _thr_umtx_init(&pcond->c_lock);
70 if (cond_attr == NULL || *cond_attr == NULL) {
72 pcond->c_clockid = CLOCK_REALTIME;
74 pcond->c_pshared = (*cond_attr)->c_pshared;
75 pcond->c_clockid = (*cond_attr)->c_clockid;
79 /* Return the completion status: */
84 init_static(struct pthread *thread, pthread_cond_t *cond)
88 THR_LOCK_ACQUIRE(thread, &_cond_static_lock);
91 ret = cond_init(cond, NULL);
95 THR_LOCK_RELEASE(thread, &_cond_static_lock);
101 _pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
104 return cond_init(cond, cond_attr);
108 _pthread_cond_destroy(pthread_cond_t *cond)
110 struct pthread_cond *cv;
111 struct pthread *curthread = tls_get_curthread();
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);
125 * NULL the caller's pointer now that the condition
126 * variable has been destroyed:
131 /* Unlock the condition variable structure: */
132 THR_LOCK_RELEASE(curthread, &cv->c_lock);
134 /* Free the cond lock structure: */
137 * Free the memory allocated for the condition
138 * variable structure:
143 /* Return the completion status: */
147 struct cond_cancel_info
149 pthread_mutex_t *mutex;
150 pthread_cond_t *cond;
156 cond_cancel_handler(void *arg)
158 struct pthread *curthread = tls_get_curthread();
159 struct cond_cancel_info *info = (struct cond_cancel_info *)arg;
163 THR_LOCK_ACQUIRE(curthread, &cv->c_lock);
164 if (cv->c_seqno != info->seqno && cv->c_wakeups != 0) {
165 if (cv->c_waiters > 0) {
167 _thr_umtx_wake(&cv->c_seqno, 1);
173 THR_LOCK_RELEASE(curthread, &cv->c_lock);
175 _mutex_cv_lock(info->mutex, info->count);
179 cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
180 const struct timespec *abstime, int cancel)
182 struct pthread *curthread = tls_get_curthread();
183 struct timespec ts, ts2, *tsp;
184 struct cond_cancel_info info;
191 * If the condition variable is statically initialized,
192 * perform the dynamic initialization:
194 if (__predict_false(*cond == NULL &&
195 (ret = init_static(curthread, cond)) != 0))
199 THR_LOCK_ACQUIRE(curthread, &cv->c_lock);
200 ret = _mutex_cv_unlock(mutex, &info.count);
202 THR_LOCK_RELEASE(curthread, &cv->c_lock);
205 oldseq = seq = cv->c_seqno;
212 THR_LOCK_RELEASE(curthread, &cv->c_lock);
214 if (abstime != NULL) {
215 clock_gettime(cv->c_clockid, &ts);
216 TIMESPEC_SUB(&ts2, abstime, &ts);
222 THR_CLEANUP_PUSH(curthread, cond_cancel_handler, &info);
223 oldcancel = _thr_cancel_enter(curthread);
224 ret = _thr_umtx_wait(&cv->c_seqno, seq, tsp,
226 _thr_cancel_leave(curthread, oldcancel);
227 THR_CLEANUP_POP(curthread, 0);
229 ret = _thr_umtx_wait(&cv->c_seqno, seq, tsp,
233 THR_LOCK_ACQUIRE(curthread, &cv->c_lock);
235 if (abstime != NULL && ret == ETIMEDOUT)
239 * loop if we have never been told to wake up
242 } while (seq == oldseq || cv->c_wakeups == 0);
244 if (seq != oldseq && cv->c_wakeups != 0) {
250 THR_LOCK_RELEASE(curthread, &cv->c_lock);
251 _mutex_cv_lock(mutex, info.count);
256 _pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
259 return (cond_wait_common(cond, mutex, NULL, 0));
263 __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
266 return (cond_wait_common(cond, mutex, NULL, 1));
270 _pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
271 const struct timespec * abstime)
273 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
274 abstime->tv_nsec >= 1000000000)
277 return (cond_wait_common(cond, mutex, abstime, 0));
281 __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
282 const struct timespec *abstime)
284 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
285 abstime->tv_nsec >= 1000000000)
288 return (cond_wait_common(cond, mutex, abstime, 1));
292 cond_signal_common(pthread_cond_t *cond, int broadcast)
294 struct pthread *curthread = tls_get_curthread();
299 * If the condition variable is statically initialized, perform dynamic
302 if (__predict_false(*cond == NULL &&
303 (ret = init_static(curthread, cond)) != 0))
307 /* Lock the condition variable structure. */
308 THR_LOCK_ACQUIRE(curthread, &cv->c_lock);
314 _thr_umtx_wake(&cv->c_seqno, 1);
316 cv->c_wakeups += cv->c_waiters;
319 _thr_umtx_wake(&cv->c_seqno, INT_MAX);
322 THR_LOCK_RELEASE(curthread, &cv->c_lock);
327 _pthread_cond_signal(pthread_cond_t * cond)
330 return (cond_signal_common(cond, 0));
334 _pthread_cond_broadcast(pthread_cond_t * cond)
337 return (cond_signal_common(cond, 1));
341 * Double underscore versions are cancellation points. Single underscore
342 * versions are not and are provided for libc internal usage (which
343 * shouldn't introduce cancellation points).
345 __strong_reference(__pthread_cond_wait, pthread_cond_wait);
346 __strong_reference(__pthread_cond_timedwait, pthread_cond_timedwait);
348 __strong_reference(_pthread_cond_init, pthread_cond_init);
349 __strong_reference(_pthread_cond_destroy, pthread_cond_destroy);
350 __strong_reference(_pthread_cond_signal, pthread_cond_signal);
351 __strong_reference(_pthread_cond_broadcast, pthread_cond_broadcast);