2 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3 * Copyright (c) 2015 The FreeBSD Foundation
6 * Portions of this software were developed by Konstantin Belousov
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "namespace.h"
40 #include "un-namespace.h"
42 #include "thr_private.h"
44 _Static_assert(sizeof(struct pthread_cond) <= PAGE_SIZE,
45 "pthread_cond too large");
50 int __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
51 int __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
52 const struct timespec * abstime);
53 static int cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
54 static int cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
55 const struct timespec *abstime, int cancel);
56 static int cond_signal_common(pthread_cond_t *cond);
57 static int cond_broadcast_common(pthread_cond_t *cond);
60 * Double underscore versions are cancellation points. Single underscore
61 * versions are not and are provided for libc internal usage (which
62 * shouldn't introduce cancellation points).
64 __weak_reference(__pthread_cond_wait, pthread_cond_wait);
65 __weak_reference(__pthread_cond_timedwait, pthread_cond_timedwait);
67 __weak_reference(_pthread_cond_init, pthread_cond_init);
68 __weak_reference(_pthread_cond_destroy, pthread_cond_destroy);
69 __weak_reference(_pthread_cond_signal, pthread_cond_signal);
70 __weak_reference(_pthread_cond_broadcast, pthread_cond_broadcast);
72 #define CV_PSHARED(cvp) (((cvp)->kcond.c_flags & USYNC_PROCESS_SHARED) != 0)
75 cond_init_body(struct pthread_cond *cvp, const struct pthread_cond_attr *cattr)
79 cvp->kcond.c_clockid = CLOCK_REALTIME;
82 cvp->kcond.c_flags |= USYNC_PROCESS_SHARED;
83 cvp->kcond.c_clockid = cattr->c_clockid;
88 cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
90 struct pthread_cond *cvp;
91 const struct pthread_cond_attr *cattr;
94 cattr = cond_attr != NULL ? *cond_attr : NULL;
95 if (cattr == NULL || cattr->c_pshared == PTHREAD_PROCESS_PRIVATE) {
97 cvp = calloc(1, sizeof(struct pthread_cond));
102 cvp = __thr_pshared_offpage(cond, 1);
108 * Initialise the condition variable structure:
110 cond_init_body(cvp, cattr);
111 *cond = pshared ? THR_PSHARED_PTR : cvp;
116 init_static(struct pthread *thread, pthread_cond_t *cond)
120 THR_LOCK_ACQUIRE(thread, &_cond_static_lock);
123 ret = cond_init(cond, NULL);
127 THR_LOCK_RELEASE(thread, &_cond_static_lock);
132 #define CHECK_AND_INIT_COND \
133 if (*cond == THR_PSHARED_PTR) { \
134 cvp = __thr_pshared_offpage(cond, 0); \
137 } else if (__predict_false((cvp = (*cond)) <= THR_COND_DESTROYED)) { \
138 if (cvp == THR_COND_INITIALIZER) { \
140 ret = init_static(_get_curthread(), cond); \
143 } else if (cvp == THR_COND_DESTROYED) { \
150 _pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
154 return (cond_init(cond, cond_attr));
158 _pthread_cond_destroy(pthread_cond_t *cond)
160 struct pthread_cond *cvp;
164 if (*cond == THR_PSHARED_PTR) {
165 cvp = __thr_pshared_offpage(cond, 0);
167 __thr_pshared_destroy(cond);
168 *cond = THR_COND_DESTROYED;
169 } else if ((cvp = *cond) == THR_COND_INITIALIZER) {
171 } else if (cvp == THR_COND_DESTROYED) {
175 *cond = THR_COND_DESTROYED;
182 * Cancellation behavior:
183 * Thread may be canceled at start, if thread is canceled, it means it
184 * did not get a wakeup from pthread_cond_signal(), otherwise, it is
186 * Thread cancellation never cause wakeup from pthread_cond_signal()
190 cond_wait_kernel(struct pthread_cond *cvp, struct pthread_mutex *mp,
191 const struct timespec *abstime, int cancel)
193 struct pthread *curthread;
194 int error, error2, recurse, robust;
196 curthread = _get_curthread();
197 robust = _mutex_enter_robust(curthread, mp);
199 error = _mutex_cv_detach(mp, &recurse);
202 _mutex_leave_robust(curthread, mp);
207 _thr_cancel_enter2(curthread, 0);
208 error = _thr_ucond_wait(&cvp->kcond, &mp->m_lock, abstime,
209 CVWAIT_ABSTIME | CVWAIT_CLOCKID);
211 _thr_cancel_leave(curthread, 0);
214 * Note that PP mutex and ROBUST mutex may return
215 * interesting error codes.
218 error2 = _mutex_cv_lock(mp, recurse, true);
219 } else if (error == EINTR || error == ETIMEDOUT) {
220 error2 = _mutex_cv_lock(mp, recurse, true);
222 * Do not do cancellation on EOWNERDEAD there. The
223 * cancellation cleanup handler will use the protected
224 * state and unlock the mutex without making the state
225 * consistent and the state will be unrecoverable.
227 if (error2 == 0 && cancel) {
229 _mutex_leave_robust(curthread, mp);
232 _thr_testcancel(curthread);
238 /* We know that it didn't unlock the mutex. */
239 _mutex_cv_attach(mp, recurse);
242 _mutex_leave_robust(curthread, mp);
245 _thr_testcancel(curthread);
250 _mutex_leave_robust(curthread, mp);
251 return (error2 != 0 ? error2 : error);
255 * Thread waits in userland queue whenever possible, when thread
256 * is signaled or broadcasted, it is removed from the queue, and
257 * is saved in curthread's defer_waiters[] buffer, but won't be
258 * woken up until mutex is unlocked.
262 cond_wait_user(struct pthread_cond *cvp, struct pthread_mutex *mp,
263 const struct timespec *abstime, int cancel)
265 struct pthread *curthread;
266 struct sleepqueue *sq;
267 int deferred, error, error2, recurse;
269 curthread = _get_curthread();
270 if (curthread->wchan != NULL)
271 PANIC("thread %p was already on queue.", curthread);
274 _thr_testcancel(curthread);
278 * set __has_user_waiters before unlocking mutex, this allows
279 * us to check it without locking in pthread_cond_signal().
281 cvp->__has_user_waiters = 1;
283 (void)_mutex_cv_unlock(mp, &recurse, &deferred);
284 curthread->mutex_obj = mp;
285 _sleepq_add(cvp, curthread);
287 _thr_clear_wake(curthread);
291 if ((mp->m_lock.m_owner & UMUTEX_CONTESTED) == 0)
292 (void)_umtx_op_err(&mp->m_lock,
293 UMTX_OP_MUTEX_WAKE2, mp->m_lock.m_flags,
296 if (curthread->nwaiter_defer > 0) {
297 _thr_wake_all(curthread->defer_waiters,
298 curthread->nwaiter_defer);
299 curthread->nwaiter_defer = 0;
303 _thr_cancel_enter2(curthread, 0);
304 error = _thr_sleep(curthread, cvp->kcond.c_clockid, abstime);
306 _thr_cancel_leave(curthread, 0);
309 if (curthread->wchan == NULL) {
312 } else if (cancel && SHOULD_CANCEL(curthread)) {
313 sq = _sleepq_lookup(cvp);
314 cvp->__has_user_waiters = _sleepq_remove(sq, curthread);
316 curthread->mutex_obj = NULL;
317 error2 = _mutex_cv_lock(mp, recurse, false);
318 if (!THR_IN_CRITICAL(curthread))
319 _pthread_exit(PTHREAD_CANCELED);
320 else /* this should not happen */
322 } else if (error == ETIMEDOUT) {
323 sq = _sleepq_lookup(cvp);
324 cvp->__has_user_waiters =
325 _sleepq_remove(sq, curthread);
330 curthread->mutex_obj = NULL;
331 error2 = _mutex_cv_lock(mp, recurse, false);
338 cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
339 const struct timespec *abstime, int cancel)
341 struct pthread *curthread = _get_curthread();
342 struct pthread_cond *cvp;
343 struct pthread_mutex *mp;
348 if (*mutex == THR_PSHARED_PTR) {
349 mp = __thr_pshared_offpage(mutex, 0);
356 if ((error = _mutex_owned(curthread, mp)) != 0)
359 if (curthread->attr.sched_policy != SCHED_OTHER ||
360 (mp->m_lock.m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT |
361 USYNC_PROCESS_SHARED)) != 0 || CV_PSHARED(cvp))
362 return (cond_wait_kernel(cvp, mp, abstime, cancel));
364 return (cond_wait_user(cvp, mp, abstime, cancel));
368 _pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
371 return (cond_wait_common(cond, mutex, NULL, 0));
375 __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
378 return (cond_wait_common(cond, mutex, NULL, 1));
382 _pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
383 const struct timespec * abstime)
386 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
387 abstime->tv_nsec >= 1000000000)
390 return (cond_wait_common(cond, mutex, abstime, 0));
394 __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
395 const struct timespec *abstime)
398 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
399 abstime->tv_nsec >= 1000000000)
402 return (cond_wait_common(cond, mutex, abstime, 1));
406 cond_signal_common(pthread_cond_t *cond)
408 struct pthread *curthread = _get_curthread();
410 struct pthread_cond *cvp;
411 struct pthread_mutex *mp;
412 struct sleepqueue *sq;
417 * If the condition variable is statically initialized, perform dynamic
422 pshared = CV_PSHARED(cvp);
424 _thr_ucond_signal(&cvp->kcond);
426 if (pshared || cvp->__has_user_waiters == 0)
429 curthread = _get_curthread();
432 sq = _sleepq_lookup(cvp);
438 td = _sleepq_first(sq);
440 cvp->__has_user_waiters = _sleepq_remove(sq, td);
441 if (PMUTEX_OWNER_ID(mp) == TID(curthread)) {
442 if (curthread->nwaiter_defer >= MAX_DEFER_WAITERS) {
443 _thr_wake_all(curthread->defer_waiters,
444 curthread->nwaiter_defer);
445 curthread->nwaiter_defer = 0;
447 curthread->defer_waiters[curthread->nwaiter_defer++] =
448 &td->wake_addr->value;
449 mp->m_flags |= PMUTEX_FLAG_DEFERRED;
451 waddr = &td->wake_addr->value;
455 _thr_set_wake(waddr);
459 struct broadcast_arg {
460 struct pthread *curthread;
461 unsigned int *waddrs[MAX_DEFER_WAITERS];
466 drop_cb(struct pthread *td, void *arg)
468 struct broadcast_arg *ba = arg;
469 struct pthread_mutex *mp;
470 struct pthread *curthread = ba->curthread;
473 if (PMUTEX_OWNER_ID(mp) == TID(curthread)) {
474 if (curthread->nwaiter_defer >= MAX_DEFER_WAITERS) {
475 _thr_wake_all(curthread->defer_waiters,
476 curthread->nwaiter_defer);
477 curthread->nwaiter_defer = 0;
479 curthread->defer_waiters[curthread->nwaiter_defer++] =
480 &td->wake_addr->value;
481 mp->m_flags |= PMUTEX_FLAG_DEFERRED;
483 if (ba->count >= MAX_DEFER_WAITERS) {
484 _thr_wake_all(ba->waddrs, ba->count);
487 ba->waddrs[ba->count++] = &td->wake_addr->value;
492 cond_broadcast_common(pthread_cond_t *cond)
495 struct pthread_cond *cvp;
496 struct sleepqueue *sq;
497 struct broadcast_arg ba;
500 * If the condition variable is statically initialized, perform dynamic
505 pshared = CV_PSHARED(cvp);
507 _thr_ucond_broadcast(&cvp->kcond);
509 if (pshared || cvp->__has_user_waiters == 0)
512 ba.curthread = _get_curthread();
516 sq = _sleepq_lookup(cvp);
521 _sleepq_drop(sq, drop_cb, &ba);
522 cvp->__has_user_waiters = 0;
525 _thr_wake_all(ba.waddrs, ba.count);
530 _pthread_cond_signal(pthread_cond_t * cond)
533 return (cond_signal_common(cond));
537 _pthread_cond_broadcast(pthread_cond_t * cond)
540 return (cond_broadcast_common(cond));