2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
5 * Copyright (c) 2015 The FreeBSD Foundation
8 * Portions of this software were developed by Konstantin Belousov
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice unmodified, this list of conditions, and the following
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "namespace.h"
42 #include "un-namespace.h"
44 #include "thr_private.h"
46 _Static_assert(sizeof(struct pthread_cond) <= PAGE_SIZE,
47 "pthread_cond too large");
52 int __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
53 int __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
54 const struct timespec * abstime);
55 static int cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
56 static int cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
57 const struct timespec *abstime, int cancel);
58 static int cond_signal_common(pthread_cond_t *cond);
59 static int cond_broadcast_common(pthread_cond_t *cond);
62 * Double underscore versions are cancellation points. Single underscore
63 * versions are not and are provided for libc internal usage (which
64 * shouldn't introduce cancellation points).
66 __weak_reference(__pthread_cond_wait, pthread_cond_wait);
67 __weak_reference(__pthread_cond_timedwait, pthread_cond_timedwait);
69 __weak_reference(_pthread_cond_init, pthread_cond_init);
70 __weak_reference(_pthread_cond_destroy, pthread_cond_destroy);
71 __weak_reference(_pthread_cond_signal, pthread_cond_signal);
72 __weak_reference(_pthread_cond_broadcast, pthread_cond_broadcast);
74 #define CV_PSHARED(cvp) (((cvp)->kcond.c_flags & USYNC_PROCESS_SHARED) != 0)
77 cond_init_body(struct pthread_cond *cvp, const struct pthread_cond_attr *cattr)
81 cvp->kcond.c_clockid = CLOCK_REALTIME;
84 cvp->kcond.c_flags |= USYNC_PROCESS_SHARED;
85 cvp->kcond.c_clockid = cattr->c_clockid;
90 cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
92 struct pthread_cond *cvp;
93 const struct pthread_cond_attr *cattr;
96 cattr = cond_attr != NULL ? *cond_attr : NULL;
97 if (cattr == NULL || cattr->c_pshared == PTHREAD_PROCESS_PRIVATE) {
99 cvp = calloc(1, sizeof(struct pthread_cond));
104 cvp = __thr_pshared_offpage(cond, 1);
110 * Initialise the condition variable structure:
112 cond_init_body(cvp, cattr);
113 *cond = pshared ? THR_PSHARED_PTR : cvp;
118 init_static(struct pthread *thread, pthread_cond_t *cond)
122 THR_LOCK_ACQUIRE(thread, &_cond_static_lock);
125 ret = cond_init(cond, NULL);
129 THR_LOCK_RELEASE(thread, &_cond_static_lock);
134 #define CHECK_AND_INIT_COND \
135 if (*cond == THR_PSHARED_PTR) { \
136 cvp = __thr_pshared_offpage(cond, 0); \
139 } else if (__predict_false((cvp = (*cond)) <= THR_COND_DESTROYED)) { \
140 if (cvp == THR_COND_INITIALIZER) { \
142 ret = init_static(_get_curthread(), cond); \
145 } else if (cvp == THR_COND_DESTROYED) { \
152 _pthread_cond_init(pthread_cond_t * __restrict cond,
153 const pthread_condattr_t * __restrict cond_attr)
157 return (cond_init(cond, cond_attr));
161 _pthread_cond_destroy(pthread_cond_t *cond)
163 struct pthread_cond *cvp;
167 if (*cond == THR_PSHARED_PTR) {
168 cvp = __thr_pshared_offpage(cond, 0);
170 __thr_pshared_destroy(cond);
171 *cond = THR_COND_DESTROYED;
172 } else if ((cvp = *cond) == THR_COND_INITIALIZER) {
174 } else if (cvp == THR_COND_DESTROYED) {
178 *cond = THR_COND_DESTROYED;
185 * Cancellation behavior:
186 * Thread may be canceled at start, if thread is canceled, it means it
187 * did not get a wakeup from pthread_cond_signal(), otherwise, it is
189 * Thread cancellation never cause wakeup from pthread_cond_signal()
193 cond_wait_kernel(struct pthread_cond *cvp, struct pthread_mutex *mp,
194 const struct timespec *abstime, int cancel)
196 struct pthread *curthread;
197 int error, error2, recurse, robust;
199 curthread = _get_curthread();
200 robust = _mutex_enter_robust(curthread, mp);
202 error = _mutex_cv_detach(mp, &recurse);
205 _mutex_leave_robust(curthread, mp);
210 _thr_cancel_enter2(curthread, 0);
211 error = _thr_ucond_wait(&cvp->kcond, &mp->m_lock, abstime,
212 CVWAIT_ABSTIME | CVWAIT_CLOCKID);
214 _thr_cancel_leave(curthread, 0);
217 * Note that PP mutex and ROBUST mutex may return
218 * interesting error codes.
221 error2 = _mutex_cv_lock(mp, recurse, true);
222 } else if (error == EINTR || error == ETIMEDOUT) {
223 error2 = _mutex_cv_lock(mp, recurse, true);
225 * Do not do cancellation on EOWNERDEAD there. The
226 * cancellation cleanup handler will use the protected
227 * state and unlock the mutex without making the state
228 * consistent and the state will be unrecoverable.
230 if (error2 == 0 && cancel) {
232 _mutex_leave_robust(curthread, mp);
235 _thr_testcancel(curthread);
241 /* We know that it didn't unlock the mutex. */
242 _mutex_cv_attach(mp, recurse);
245 _mutex_leave_robust(curthread, mp);
248 _thr_testcancel(curthread);
253 _mutex_leave_robust(curthread, mp);
254 return (error2 != 0 ? error2 : error);
258 * Thread waits in userland queue whenever possible, when thread
259 * is signaled or broadcasted, it is removed from the queue, and
260 * is saved in curthread's defer_waiters[] buffer, but won't be
261 * woken up until mutex is unlocked.
265 cond_wait_user(struct pthread_cond *cvp, struct pthread_mutex *mp,
266 const struct timespec *abstime, int cancel)
268 struct pthread *curthread;
269 struct sleepqueue *sq;
270 int deferred, error, error2, recurse;
272 curthread = _get_curthread();
273 if (curthread->wchan != NULL)
274 PANIC("thread %p was already on queue.", curthread);
277 _thr_testcancel(curthread);
281 * set __has_user_waiters before unlocking mutex, this allows
282 * us to check it without locking in pthread_cond_signal().
284 cvp->__has_user_waiters = 1;
286 (void)_mutex_cv_unlock(mp, &recurse, &deferred);
287 curthread->mutex_obj = mp;
288 _sleepq_add(cvp, curthread);
290 _thr_clear_wake(curthread);
294 if ((mp->m_lock.m_owner & UMUTEX_CONTESTED) == 0)
295 (void)_umtx_op_err(&mp->m_lock,
296 UMTX_OP_MUTEX_WAKE2, mp->m_lock.m_flags,
299 if (curthread->nwaiter_defer > 0) {
300 _thr_wake_all(curthread->defer_waiters,
301 curthread->nwaiter_defer);
302 curthread->nwaiter_defer = 0;
306 _thr_cancel_enter2(curthread, 0);
307 error = _thr_sleep(curthread, cvp->kcond.c_clockid, abstime);
309 _thr_cancel_leave(curthread, 0);
312 if (curthread->wchan == NULL) {
315 } else if (cancel && SHOULD_CANCEL(curthread)) {
316 sq = _sleepq_lookup(cvp);
317 cvp->__has_user_waiters = _sleepq_remove(sq, curthread);
319 curthread->mutex_obj = NULL;
320 error2 = _mutex_cv_lock(mp, recurse, false);
321 if (!THR_IN_CRITICAL(curthread))
322 _pthread_exit(PTHREAD_CANCELED);
323 else /* this should not happen */
325 } else if (error == ETIMEDOUT) {
326 sq = _sleepq_lookup(cvp);
327 cvp->__has_user_waiters =
328 _sleepq_remove(sq, curthread);
333 curthread->mutex_obj = NULL;
334 error2 = _mutex_cv_lock(mp, recurse, false);
341 cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
342 const struct timespec *abstime, int cancel)
344 struct pthread *curthread = _get_curthread();
345 struct pthread_cond *cvp;
346 struct pthread_mutex *mp;
351 if (*mutex == THR_PSHARED_PTR) {
352 mp = __thr_pshared_offpage(mutex, 0);
359 if ((error = _mutex_owned(curthread, mp)) != 0)
362 if (curthread->attr.sched_policy != SCHED_OTHER ||
363 (mp->m_lock.m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT |
364 USYNC_PROCESS_SHARED)) != 0 || CV_PSHARED(cvp))
365 return (cond_wait_kernel(cvp, mp, abstime, cancel));
367 return (cond_wait_user(cvp, mp, abstime, cancel));
371 _pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
374 return (cond_wait_common(cond, mutex, NULL, 0));
378 __pthread_cond_wait(pthread_cond_t * __restrict cond,
379 pthread_mutex_t * __restrict mutex)
382 return (cond_wait_common(cond, mutex, NULL, 1));
386 _pthread_cond_timedwait(pthread_cond_t * __restrict cond,
387 pthread_mutex_t * __restrict mutex,
388 const struct timespec * __restrict abstime)
391 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
392 abstime->tv_nsec >= 1000000000)
395 return (cond_wait_common(cond, mutex, abstime, 0));
399 __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
400 const struct timespec *abstime)
403 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
404 abstime->tv_nsec >= 1000000000)
407 return (cond_wait_common(cond, mutex, abstime, 1));
411 cond_signal_common(pthread_cond_t *cond)
413 struct pthread *curthread = _get_curthread();
415 struct pthread_cond *cvp;
416 struct pthread_mutex *mp;
417 struct sleepqueue *sq;
422 * If the condition variable is statically initialized, perform dynamic
427 pshared = CV_PSHARED(cvp);
429 _thr_ucond_signal(&cvp->kcond);
431 if (pshared || cvp->__has_user_waiters == 0)
434 curthread = _get_curthread();
437 sq = _sleepq_lookup(cvp);
443 td = _sleepq_first(sq);
445 cvp->__has_user_waiters = _sleepq_remove(sq, td);
446 if (PMUTEX_OWNER_ID(mp) == TID(curthread)) {
447 if (curthread->nwaiter_defer >= MAX_DEFER_WAITERS) {
448 _thr_wake_all(curthread->defer_waiters,
449 curthread->nwaiter_defer);
450 curthread->nwaiter_defer = 0;
452 curthread->defer_waiters[curthread->nwaiter_defer++] =
453 &td->wake_addr->value;
454 mp->m_flags |= PMUTEX_FLAG_DEFERRED;
456 waddr = &td->wake_addr->value;
460 _thr_set_wake(waddr);
464 struct broadcast_arg {
465 struct pthread *curthread;
466 unsigned int *waddrs[MAX_DEFER_WAITERS];
471 drop_cb(struct pthread *td, void *arg)
473 struct broadcast_arg *ba = arg;
474 struct pthread_mutex *mp;
475 struct pthread *curthread = ba->curthread;
478 if (PMUTEX_OWNER_ID(mp) == TID(curthread)) {
479 if (curthread->nwaiter_defer >= MAX_DEFER_WAITERS) {
480 _thr_wake_all(curthread->defer_waiters,
481 curthread->nwaiter_defer);
482 curthread->nwaiter_defer = 0;
484 curthread->defer_waiters[curthread->nwaiter_defer++] =
485 &td->wake_addr->value;
486 mp->m_flags |= PMUTEX_FLAG_DEFERRED;
488 if (ba->count >= MAX_DEFER_WAITERS) {
489 _thr_wake_all(ba->waddrs, ba->count);
492 ba->waddrs[ba->count++] = &td->wake_addr->value;
497 cond_broadcast_common(pthread_cond_t *cond)
500 struct pthread_cond *cvp;
501 struct sleepqueue *sq;
502 struct broadcast_arg ba;
505 * If the condition variable is statically initialized, perform dynamic
510 pshared = CV_PSHARED(cvp);
512 _thr_ucond_broadcast(&cvp->kcond);
514 if (pshared || cvp->__has_user_waiters == 0)
517 ba.curthread = _get_curthread();
521 sq = _sleepq_lookup(cvp);
526 _sleepq_drop(sq, drop_cb, &ba);
527 cvp->__has_user_waiters = 0;
530 _thr_wake_all(ba.waddrs, ba.count);
535 _pthread_cond_signal(pthread_cond_t * cond)
538 return (cond_signal_common(cond));
542 _pthread_cond_broadcast(pthread_cond_t * cond)
545 return (cond_broadcast_common(cond));