2 * SPDX-License-Identifier: BSD-2-Clause
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 #include "namespace.h"
40 #include "un-namespace.h"
42 #include "thr_private.h"
44 _Static_assert(sizeof(struct pthread_cond) <= THR_PAGE_SIZE_MIN,
45 "pthread_cond too large");
50 int __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
51 const struct timespec * abstime);
52 static int cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
53 static int cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
54 const struct timespec *abstime, int cancel);
55 static int cond_signal_common(pthread_cond_t *cond);
56 static int cond_broadcast_common(pthread_cond_t *cond);
59 * Double underscore versions are cancellation points. Single underscore
60 * versions are not and are provided for libc internal usage (which
61 * shouldn't introduce cancellation points).
63 __weak_reference(__thr_cond_wait, pthread_cond_wait);
64 __weak_reference(__thr_cond_wait, __pthread_cond_wait);
65 __weak_reference(_thr_cond_wait, _pthread_cond_wait);
66 __weak_reference(__pthread_cond_timedwait, pthread_cond_timedwait);
67 __weak_reference(_thr_cond_init, pthread_cond_init);
68 __weak_reference(_thr_cond_init, _pthread_cond_init);
69 __weak_reference(_thr_cond_destroy, pthread_cond_destroy);
70 __weak_reference(_thr_cond_destroy, _pthread_cond_destroy);
71 __weak_reference(_thr_cond_signal, pthread_cond_signal);
72 __weak_reference(_thr_cond_signal, _pthread_cond_signal);
73 __weak_reference(_thr_cond_broadcast, pthread_cond_broadcast);
74 __weak_reference(_thr_cond_broadcast, _pthread_cond_broadcast);
76 #define CV_PSHARED(cvp) (((cvp)->kcond.c_flags & USYNC_PROCESS_SHARED) != 0)
79 cond_init_body(struct pthread_cond *cvp, const struct pthread_cond_attr *cattr)
83 cvp->kcond.c_clockid = CLOCK_REALTIME;
86 cvp->kcond.c_flags |= USYNC_PROCESS_SHARED;
87 cvp->kcond.c_clockid = cattr->c_clockid;
92 cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
94 struct pthread_cond *cvp;
95 const struct pthread_cond_attr *cattr;
98 cattr = cond_attr != NULL ? *cond_attr : NULL;
99 if (cattr == NULL || cattr->c_pshared == PTHREAD_PROCESS_PRIVATE) {
101 cvp = calloc(1, sizeof(struct pthread_cond));
106 cvp = __thr_pshared_offpage(cond, 1);
112 * Initialise the condition variable structure:
114 cond_init_body(cvp, cattr);
115 *cond = pshared ? THR_PSHARED_PTR : cvp;
120 init_static(struct pthread *thread, pthread_cond_t *cond)
124 THR_LOCK_ACQUIRE(thread, &_cond_static_lock);
127 ret = cond_init(cond, NULL);
131 THR_LOCK_RELEASE(thread, &_cond_static_lock);
136 #define CHECK_AND_INIT_COND \
137 if (*cond == THR_PSHARED_PTR) { \
138 cvp = __thr_pshared_offpage(cond, 0); \
141 } else if (__predict_false((cvp = (*cond)) <= THR_COND_DESTROYED)) { \
142 if (cvp == THR_COND_INITIALIZER) { \
144 ret = init_static(_get_curthread(), cond); \
147 } else if (cvp == THR_COND_DESTROYED) { \
154 _thr_cond_init(pthread_cond_t * __restrict cond,
155 const pthread_condattr_t * __restrict cond_attr)
159 return (cond_init(cond, cond_attr));
163 _thr_cond_destroy(pthread_cond_t *cond)
165 struct pthread_cond *cvp;
169 if (*cond == THR_PSHARED_PTR) {
170 cvp = __thr_pshared_offpage(cond, 0);
172 if (cvp->kcond.c_has_waiters)
175 __thr_pshared_destroy(cond);
178 *cond = THR_COND_DESTROYED;
179 } else if ((cvp = *cond) == THR_COND_INITIALIZER) {
181 } else if (cvp == THR_COND_DESTROYED) {
185 if (cvp->__has_user_waiters || cvp->kcond.c_has_waiters)
188 *cond = THR_COND_DESTROYED;
196 * Cancellation behavior:
197 * Thread may be canceled at start, if thread is canceled, it means it
198 * did not get a wakeup from pthread_cond_signal(), otherwise, it is
200 * Thread cancellation never cause wakeup from pthread_cond_signal()
204 cond_wait_kernel(struct pthread_cond *cvp, struct pthread_mutex *mp,
205 const struct timespec *abstime, int cancel)
207 struct pthread *curthread;
208 int error, error2, recurse, robust;
210 curthread = _get_curthread();
211 robust = _mutex_enter_robust(curthread, mp);
213 error = _mutex_cv_detach(mp, &recurse);
216 _mutex_leave_robust(curthread, mp);
221 _thr_cancel_enter2(curthread, 0);
222 error = _thr_ucond_wait(&cvp->kcond, &mp->m_lock, abstime,
223 CVWAIT_ABSTIME | CVWAIT_CLOCKID);
225 _thr_cancel_leave(curthread, 0);
228 * Note that PP mutex and ROBUST mutex may return
229 * interesting error codes.
232 error2 = _mutex_cv_lock(mp, recurse, true);
233 } else if (error == EINTR || error == ETIMEDOUT) {
234 error2 = _mutex_cv_lock(mp, recurse, true);
236 * Do not do cancellation on EOWNERDEAD there. The
237 * cancellation cleanup handler will use the protected
238 * state and unlock the mutex without making the state
239 * consistent and the state will be unrecoverable.
241 if (error2 == 0 && cancel) {
243 _mutex_leave_robust(curthread, mp);
246 _thr_testcancel(curthread);
252 /* We know that it didn't unlock the mutex. */
253 _mutex_cv_attach(mp, recurse);
256 _mutex_leave_robust(curthread, mp);
259 _thr_testcancel(curthread);
264 _mutex_leave_robust(curthread, mp);
265 return (error2 != 0 ? error2 : error);
269 * Thread waits in userland queue whenever possible, when thread
270 * is signaled or broadcasted, it is removed from the queue, and
271 * is saved in curthread's defer_waiters[] buffer, but won't be
272 * woken up until mutex is unlocked.
276 cond_wait_user(struct pthread_cond *cvp, struct pthread_mutex *mp,
277 const struct timespec *abstime, int cancel)
279 struct pthread *curthread;
280 struct sleepqueue *sq;
281 int deferred, error, error2, recurse;
283 curthread = _get_curthread();
284 if (curthread->wchan != NULL)
285 PANIC("thread %p was already on queue.", curthread);
288 _thr_testcancel(curthread);
292 * set __has_user_waiters before unlocking mutex, this allows
293 * us to check it without locking in pthread_cond_signal().
295 cvp->__has_user_waiters = 1;
297 (void)_mutex_cv_unlock(mp, &recurse, &deferred);
298 curthread->mutex_obj = mp;
299 _sleepq_add(cvp, curthread);
301 _thr_clear_wake(curthread);
305 if ((mp->m_lock.m_owner & UMUTEX_CONTESTED) == 0)
306 (void)_umtx_op_err(&mp->m_lock,
307 UMTX_OP_MUTEX_WAKE2, mp->m_lock.m_flags,
310 if (curthread->nwaiter_defer > 0) {
311 _thr_wake_all(curthread->defer_waiters,
312 curthread->nwaiter_defer);
313 curthread->nwaiter_defer = 0;
317 _thr_cancel_enter2(curthread, 0);
318 error = _thr_sleep(curthread, cvp->kcond.c_clockid, abstime);
320 _thr_cancel_leave(curthread, 0);
323 if (curthread->wchan == NULL) {
326 } else if (cancel && SHOULD_CANCEL(curthread)) {
327 sq = _sleepq_lookup(cvp);
328 cvp->__has_user_waiters = _sleepq_remove(sq, curthread);
330 curthread->mutex_obj = NULL;
331 error2 = _mutex_cv_lock(mp, recurse, false);
332 if (!THR_IN_CRITICAL(curthread))
333 _pthread_exit(PTHREAD_CANCELED);
334 else /* this should not happen */
336 } else if (error == ETIMEDOUT) {
337 sq = _sleepq_lookup(cvp);
338 cvp->__has_user_waiters =
339 _sleepq_remove(sq, curthread);
344 curthread->mutex_obj = NULL;
345 error2 = _mutex_cv_lock(mp, recurse, false);
352 cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
353 const struct timespec *abstime, int cancel)
355 struct pthread *curthread = _get_curthread();
356 struct pthread_cond *cvp;
357 struct pthread_mutex *mp;
362 if (*mutex == THR_PSHARED_PTR) {
363 mp = __thr_pshared_offpage(mutex, 0);
370 if ((error = _mutex_owned(curthread, mp)) != 0)
373 if (curthread->attr.sched_policy != SCHED_OTHER ||
374 (mp->m_lock.m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT |
375 USYNC_PROCESS_SHARED)) != 0 || CV_PSHARED(cvp))
376 return (cond_wait_kernel(cvp, mp, abstime, cancel));
378 return (cond_wait_user(cvp, mp, abstime, cancel));
382 _thr_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
385 return (cond_wait_common(cond, mutex, NULL, 0));
389 __thr_cond_wait(pthread_cond_t * __restrict cond,
390 pthread_mutex_t * __restrict mutex)
393 return (cond_wait_common(cond, mutex, NULL, 1));
397 _thr_cond_timedwait(pthread_cond_t * __restrict cond,
398 pthread_mutex_t * __restrict mutex,
399 const struct timespec * __restrict abstime)
402 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
403 abstime->tv_nsec >= 1000000000)
406 return (cond_wait_common(cond, mutex, abstime, 0));
410 __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
411 const struct timespec *abstime)
414 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
415 abstime->tv_nsec >= 1000000000)
418 return (cond_wait_common(cond, mutex, abstime, 1));
422 cond_signal_common(pthread_cond_t *cond)
424 struct pthread *curthread = _get_curthread();
426 struct pthread_cond *cvp;
427 struct pthread_mutex *mp;
428 struct sleepqueue *sq;
433 * If the condition variable is statically initialized, perform dynamic
438 pshared = CV_PSHARED(cvp);
440 _thr_ucond_signal(&cvp->kcond);
442 if (pshared || cvp->__has_user_waiters == 0)
445 curthread = _get_curthread();
448 sq = _sleepq_lookup(cvp);
454 td = _sleepq_first(sq);
456 cvp->__has_user_waiters = _sleepq_remove(sq, td);
457 if (PMUTEX_OWNER_ID(mp) == TID(curthread)) {
458 if (curthread->nwaiter_defer >= MAX_DEFER_WAITERS) {
459 _thr_wake_all(curthread->defer_waiters,
460 curthread->nwaiter_defer);
461 curthread->nwaiter_defer = 0;
463 curthread->defer_waiters[curthread->nwaiter_defer++] =
464 &td->wake_addr->value;
465 mp->m_flags |= PMUTEX_FLAG_DEFERRED;
467 waddr = &td->wake_addr->value;
471 _thr_set_wake(waddr);
475 struct broadcast_arg {
476 struct pthread *curthread;
477 unsigned int *waddrs[MAX_DEFER_WAITERS];
482 drop_cb(struct pthread *td, void *arg)
484 struct broadcast_arg *ba = arg;
485 struct pthread_mutex *mp;
486 struct pthread *curthread = ba->curthread;
489 if (PMUTEX_OWNER_ID(mp) == TID(curthread)) {
490 if (curthread->nwaiter_defer >= MAX_DEFER_WAITERS) {
491 _thr_wake_all(curthread->defer_waiters,
492 curthread->nwaiter_defer);
493 curthread->nwaiter_defer = 0;
495 curthread->defer_waiters[curthread->nwaiter_defer++] =
496 &td->wake_addr->value;
497 mp->m_flags |= PMUTEX_FLAG_DEFERRED;
499 if (ba->count >= MAX_DEFER_WAITERS) {
500 _thr_wake_all(ba->waddrs, ba->count);
503 ba->waddrs[ba->count++] = &td->wake_addr->value;
508 cond_broadcast_common(pthread_cond_t *cond)
511 struct pthread_cond *cvp;
512 struct sleepqueue *sq;
513 struct broadcast_arg ba;
516 * If the condition variable is statically initialized, perform dynamic
521 pshared = CV_PSHARED(cvp);
523 _thr_ucond_broadcast(&cvp->kcond);
525 if (pshared || cvp->__has_user_waiters == 0)
528 ba.curthread = _get_curthread();
532 sq = _sleepq_lookup(cvp);
537 _sleepq_drop(sq, drop_cb, &ba);
538 cvp->__has_user_waiters = 0;
541 _thr_wake_all(ba.waddrs, ba.count);
546 _thr_cond_signal(pthread_cond_t * cond)
549 return (cond_signal_common(cond));
553 _thr_cond_broadcast(pthread_cond_t * cond)
556 return (cond_broadcast_common(cond));