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.
30 #include "thr_private.h"
34 _thr_umutex_init(struct umutex *mtx)
36 static struct umutex default_mtx = DEFAULT_UMUTEX;
42 __thr_umutex_lock(struct umutex *mtx)
44 if (_umtx_op(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0) == 0)
50 __thr_umutex_timedlock(struct umutex *mtx,
51 const struct timespec *timeout)
53 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
54 timeout->tv_nsec <= 0)))
56 if (_umtx_op(mtx, UMTX_OP_MUTEX_LOCK, 0, 0,
57 __DECONST(void *, timeout)) == 0)
63 __thr_umutex_unlock(struct umutex *mtx)
65 if (_umtx_op(mtx, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0) == 0)
71 __thr_umutex_trylock(struct umutex *mtx)
73 if (_umtx_op(mtx, UMTX_OP_MUTEX_TRYLOCK, 0, 0, 0) == 0)
79 __thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling,
82 if (_umtx_op(mtx, UMTX_OP_SET_CEILING, ceiling, oldceiling, 0) == 0)
88 _thr_umtx_wait(volatile umtx_t *mtx, long id, const struct timespec *timeout)
90 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
91 timeout->tv_nsec <= 0)))
93 if (_umtx_op(__DEVOLATILE(void *, mtx), UMTX_OP_WAIT, id, 0,
94 __DECONST(void*, timeout)) == 0)
100 _thr_umtx_wake(volatile umtx_t *mtx, int nr_wakeup)
102 if (_umtx_op(__DEVOLATILE(void *, mtx), UMTX_OP_WAKE,
103 nr_wakeup, 0, 0) == 0)
109 _thr_ucond_init(struct ucond *cv)
111 bzero(cv, sizeof(struct ucond));
115 _thr_ucond_wait(struct ucond *cv, struct umutex *m,
116 const struct timespec *timeout, int check_unparking)
118 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
119 timeout->tv_nsec <= 0))) {
120 __thr_umutex_unlock(m);
123 if (_umtx_op(cv, UMTX_OP_CV_WAIT,
124 check_unparking ? UMTX_CHECK_UNPARKING : 0,
125 m, __DECONST(void*, timeout)) == 0) {
132 _thr_ucond_signal(struct ucond *cv)
134 if (!cv->c_has_waiters)
136 if (_umtx_op(cv, UMTX_OP_CV_SIGNAL, 0, NULL, NULL) == 0)
142 _thr_ucond_broadcast(struct ucond *cv)
144 if (!cv->c_has_waiters)
146 if (_umtx_op(cv, UMTX_OP_CV_BROADCAST, 0, NULL, NULL) == 0)