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.5 2005/04/05 23:04:22 davidxu Exp $
29 #include <machine/tls.h>
37 #include "thr_private.h"
42 static int cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
43 static int cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
44 const struct timespec *abstime, int cancel);
45 static int cond_signal_common(pthread_cond_t *cond, int broadcast);
48 * Double underscore versions are cancellation points. Single underscore
49 * versions are not and are provided for libc internal usage (which
50 * shouldn't introduce cancellation points).
52 __weak_reference(__pthread_cond_wait, pthread_cond_wait);
53 __weak_reference(__pthread_cond_timedwait, pthread_cond_timedwait);
55 __weak_reference(_pthread_cond_init, pthread_cond_init);
56 __weak_reference(_pthread_cond_destroy, pthread_cond_destroy);
57 __weak_reference(_pthread_cond_signal, pthread_cond_signal);
58 __weak_reference(_pthread_cond_broadcast, pthread_cond_broadcast);
61 cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
66 if ((pcond = (pthread_cond_t)
67 malloc(sizeof(struct pthread_cond))) == NULL) {
71 * Initialise the condition variable structure:
73 _thr_umtx_init(&pcond->c_lock);
77 if (cond_attr == NULL || *cond_attr == NULL) {
79 pcond->c_clockid = CLOCK_REALTIME;
81 pcond->c_pshared = (*cond_attr)->c_pshared;
82 pcond->c_clockid = (*cond_attr)->c_clockid;
86 /* Return the completion status: */
91 init_static(struct pthread *thread, pthread_cond_t *cond)
95 THR_LOCK_ACQUIRE(thread, &_cond_static_lock);
98 ret = cond_init(cond, NULL);
102 THR_LOCK_RELEASE(thread, &_cond_static_lock);
108 _pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
111 return cond_init(cond, cond_attr);
115 _pthread_cond_destroy(pthread_cond_t *cond)
117 struct pthread_cond *cv;
118 struct pthread *curthread = tls_get_curthread();
124 /* Lock the condition variable structure: */
125 THR_LOCK_ACQUIRE(curthread, &(*cond)->c_lock);
126 if ((*cond)->c_waiters + (*cond)->c_wakeups != 0) {
127 THR_LOCK_RELEASE(curthread, &(*cond)->c_lock);
132 * NULL the caller's pointer now that the condition
133 * variable has been destroyed:
138 /* Unlock the condition variable structure: */
139 THR_LOCK_RELEASE(curthread, &cv->c_lock);
141 /* Free the cond lock structure: */
144 * Free the memory allocated for the condition
145 * variable structure:
150 /* Return the completion status: */
154 struct cond_cancel_info
156 pthread_mutex_t *mutex;
157 pthread_cond_t *cond;
162 cond_cancel_handler(void *arg)
164 struct pthread *curthread = tls_get_curthread();
165 struct cond_cancel_info *cci = (struct cond_cancel_info *)arg;
169 THR_LOCK_ACQUIRE(curthread, &cv->c_lock);
170 if (cv->c_seqno != cci->seqno && cv->c_wakeups != 0) {
171 if (cv->c_waiters > 0) {
173 _thr_umtx_wake(&cv->c_seqno, 1);
179 THR_LOCK_RELEASE(curthread, &cv->c_lock);
181 _mutex_cv_lock(cci->mutex);
185 cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
186 const struct timespec *abstime, int cancel)
188 struct pthread *curthread = tls_get_curthread();
189 struct timespec ts, ts2, *tsp;
190 struct cond_cancel_info cci;
197 * If the condition variable is statically initialized,
198 * perform the dynamic initialization:
200 if (__predict_false(*cond == NULL &&
201 (ret = init_static(curthread, cond)) != 0))
205 THR_LOCK_ACQUIRE(curthread, &cv->c_lock);
206 ret = _mutex_cv_unlock(mutex);
208 THR_LOCK_RELEASE(curthread, &cv->c_lock);
211 oldseq = seq = cv->c_seqno;
218 THR_LOCK_RELEASE(curthread, &cv->c_lock);
220 if (abstime != NULL) {
221 clock_gettime(cv->c_clockid, &ts);
222 TIMESPEC_SUB(&ts2, abstime, &ts);
228 THR_CLEANUP_PUSH(curthread, cond_cancel_handler, &cci);
229 oldcancel = _thr_cancel_enter(curthread);
230 ret = _thr_umtx_wait(&cv->c_seqno, seq, tsp,
232 _thr_cancel_leave(curthread, oldcancel);
233 THR_CLEANUP_POP(curthread, 0);
235 ret = _thr_umtx_wait(&cv->c_seqno, seq, tsp,
239 THR_LOCK_ACQUIRE(curthread, &cv->c_lock);
241 if (abstime != NULL && ret == ETIMEDOUT)
245 * loop if we have never been told to wake up
248 } while (seq == oldseq || cv->c_wakeups == 0);
250 if (seq != oldseq && cv->c_wakeups != 0) {
256 THR_LOCK_RELEASE(curthread, &cv->c_lock);
257 _mutex_cv_lock(mutex);
262 _pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
265 return (cond_wait_common(cond, mutex, NULL, 0));
269 __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
272 return (cond_wait_common(cond, mutex, NULL, 1));
276 _pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
277 const struct timespec * abstime)
279 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
280 abstime->tv_nsec >= 1000000000)
283 return (cond_wait_common(cond, mutex, abstime, 0));
287 __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
288 const struct timespec *abstime)
290 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
291 abstime->tv_nsec >= 1000000000)
294 return (cond_wait_common(cond, mutex, abstime, 1));
298 cond_signal_common(pthread_cond_t *cond, int broadcast)
300 struct pthread *curthread = tls_get_curthread();
305 * If the condition variable is statically initialized, perform dynamic
308 if (__predict_false(*cond == NULL &&
309 (ret = init_static(curthread, cond)) != 0))
313 /* Lock the condition variable structure. */
314 THR_LOCK_ACQUIRE(curthread, &cv->c_lock);
320 _thr_umtx_wake(&cv->c_seqno, 1);
322 cv->c_wakeups += cv->c_waiters;
325 _thr_umtx_wake(&cv->c_seqno, INT_MAX);
328 THR_LOCK_RELEASE(curthread, &cv->c_lock);
333 _pthread_cond_signal(pthread_cond_t * cond)
336 return (cond_signal_common(cond, 0));
340 _pthread_cond_broadcast(pthread_cond_t * cond)
343 return (cond_signal_common(cond, 1));