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.
29 #include "namespace.h"
35 #include "un-namespace.h"
37 #include "thr_private.h"
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 static int cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
46 static int cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
47 const struct timespec *abstime, int cancel);
48 static int cond_signal_common(pthread_cond_t *cond, int broadcast);
51 * Double underscore versions are cancellation points. Single underscore
52 * versions are not and are provided for libc internal usage (which
53 * shouldn't introduce cancellation points).
55 __weak_reference(__pthread_cond_wait, pthread_cond_wait);
56 __weak_reference(__pthread_cond_timedwait, pthread_cond_timedwait);
58 __weak_reference(_pthread_cond_init, pthread_cond_init);
59 __weak_reference(_pthread_cond_destroy, pthread_cond_destroy);
60 __weak_reference(_pthread_cond_signal, pthread_cond_signal);
61 __weak_reference(_pthread_cond_broadcast, pthread_cond_broadcast);
64 cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
69 if ((pcond = (pthread_cond_t)
70 calloc(1, sizeof(struct pthread_cond))) == NULL) {
74 * Initialise the condition variable structure:
76 if (cond_attr == NULL || *cond_attr == NULL) {
78 pcond->c_clockid = CLOCK_REALTIME;
80 pcond->c_pshared = (*cond_attr)->c_pshared;
81 pcond->c_clockid = (*cond_attr)->c_clockid;
83 _thr_umutex_init(&pcond->c_lock);
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)
112 return (cond_init(cond, cond_attr));
116 _pthread_cond_destroy(pthread_cond_t *cond)
118 struct pthread *curthread = _get_curthread();
119 struct pthread_cond *cv;
126 THR_UMUTEX_LOCK(curthread, &cv->c_lock);
128 * NULL the caller's pointer now that the condition
129 * variable has been destroyed:
132 THR_UMUTEX_UNLOCK(curthread, &cv->c_lock);
135 * Free the memory allocated for the condition
136 * variable structure:
140 /* Return the completion status: */
144 struct cond_cancel_info
146 pthread_mutex_t *mutex;
147 pthread_cond_t *cond;
152 cond_cancel_handler(void *arg)
154 struct pthread *curthread = _get_curthread();
155 struct cond_cancel_info *info = (struct cond_cancel_info *)arg;
158 if (info->cond != NULL) {
160 THR_UMUTEX_UNLOCK(curthread, &cv->c_lock);
162 _mutex_cv_lock(info->mutex, info->count);
166 cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
167 const struct timespec *abstime, int cancel)
169 struct pthread *curthread = _get_curthread();
170 struct timespec ts, ts2, *tsp;
171 struct cond_cancel_info info;
176 * If the condition variable is statically initialized,
177 * perform the dynamic initialization:
179 if (__predict_false(*cond == NULL &&
180 (ret = init_static(curthread, cond)) != 0))
184 THR_UMUTEX_LOCK(curthread, &cv->c_lock);
185 ret = _mutex_cv_unlock(mutex, &info.count);
187 THR_UMUTEX_UNLOCK(curthread, &cv->c_lock);
194 if (abstime != NULL) {
195 clock_gettime(cv->c_clockid, &ts);
196 TIMESPEC_SUB(&ts2, abstime, &ts);
202 THR_CLEANUP_PUSH(curthread, cond_cancel_handler, &info);
203 _thr_cancel_enter_defer(curthread);
204 ret = _thr_ucond_wait(&cv->c_kerncv, &cv->c_lock, tsp, 1);
206 _thr_cancel_leave_defer(curthread, ret);
207 THR_CLEANUP_POP(curthread, 0);
209 ret = _thr_ucond_wait(&cv->c_kerncv, &cv->c_lock, tsp, 0);
213 _mutex_cv_lock(mutex, info.count);
218 _pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
221 return (cond_wait_common(cond, mutex, NULL, 0));
225 __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
228 return (cond_wait_common(cond, mutex, NULL, 1));
232 _pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
233 const struct timespec * abstime)
236 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
237 abstime->tv_nsec >= 1000000000)
240 return (cond_wait_common(cond, mutex, abstime, 0));
244 __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
245 const struct timespec *abstime)
248 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
249 abstime->tv_nsec >= 1000000000)
252 return (cond_wait_common(cond, mutex, abstime, 1));
256 cond_signal_common(pthread_cond_t *cond, int broadcast)
258 struct pthread *curthread = _get_curthread();
263 * If the condition variable is statically initialized, perform dynamic
266 if (__predict_false(*cond == NULL &&
267 (ret = init_static(curthread, cond)) != 0))
271 THR_UMUTEX_LOCK(curthread, &cv->c_lock);
273 ret = _thr_ucond_signal(&cv->c_kerncv);
275 ret = _thr_ucond_broadcast(&cv->c_kerncv);
276 THR_UMUTEX_UNLOCK(curthread, &cv->c_lock);
281 _pthread_cond_signal(pthread_cond_t * cond)
284 return (cond_signal_common(cond, 0));
288 _pthread_cond_broadcast(pthread_cond_t * cond)
291 return (cond_signal_common(cond, 1));