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"
33 #ifndef HAS__UMTX_OP_ERR
34 int _umtx_op_err(void *obj, int op, u_long val, void *uaddr, void *uaddr2)
36 if (_umtx_op(obj, op, val, uaddr, uaddr2) == -1)
43 _thr_umutex_init(struct umutex *mtx)
45 static struct umutex default_mtx = DEFAULT_UMUTEX;
51 __thr_umutex_lock(struct umutex *mtx, uint32_t id)
55 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
58 _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, 0);
61 if ((owner & ~UMUTEX_CONTESTED) == 0 &&
62 atomic_cmpset_acq_32(&mtx->m_owner, owner, id|owner))
67 return _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0);
71 __thr_umutex_timedlock(struct umutex *mtx, uint32_t id,
72 const struct timespec *ets)
74 struct timespec timo, cts;
78 clock_gettime(CLOCK_REALTIME, &cts);
79 TIMESPEC_SUB(&timo, ets, &cts);
85 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
88 ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, &timo);
90 /* now try to lock it */
92 if ((owner & ~UMUTEX_CONTESTED) == 0 &&
93 atomic_cmpset_acq_32(&mtx->m_owner, owner, id|owner))
96 ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, &timo);
100 if (ret == ETIMEDOUT)
102 clock_gettime(CLOCK_REALTIME, &cts);
103 TIMESPEC_SUB(&timo, ets, &cts);
104 if (timo.tv_sec < 0 || (timo.tv_sec == 0 && timo.tv_nsec == 0)) {
113 __thr_umutex_unlock(struct umutex *mtx, uint32_t id)
115 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
116 atomic_cmpset_rel_32(&mtx->m_owner, id | UMUTEX_CONTESTED, UMUTEX_CONTESTED);
117 return _umtx_op_err(mtx, UMTX_OP_MUTEX_WAKE, 0, 0, 0);
119 return _umtx_op_err(mtx, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0);
123 __thr_umutex_trylock(struct umutex *mtx)
125 return _umtx_op_err(mtx, UMTX_OP_MUTEX_TRYLOCK, 0, 0, 0);
129 __thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling,
130 uint32_t *oldceiling)
132 return _umtx_op_err(mtx, UMTX_OP_SET_CEILING, ceiling, oldceiling, 0);
136 _thr_umtx_wait(volatile long *mtx, long id, const struct timespec *timeout)
138 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
139 timeout->tv_nsec <= 0)))
141 return _umtx_op_err(__DEVOLATILE(void *, mtx), UMTX_OP_WAIT, id, 0,
142 __DECONST(void*, timeout));
146 _thr_umtx_wait_uint(volatile u_int *mtx, u_int id, const struct timespec *timeout, int shared)
148 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
149 timeout->tv_nsec <= 0)))
151 return _umtx_op_err(__DEVOLATILE(void *, mtx),
152 shared ? UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id, 0,
153 __DECONST(void*, timeout));
157 _thr_umtx_wake(volatile void *mtx, int nr_wakeup, int shared)
159 return _umtx_op_err(__DEVOLATILE(void *, mtx), shared ? UMTX_OP_WAKE : UMTX_OP_WAKE_PRIVATE,
164 _thr_ucond_init(struct ucond *cv)
166 bzero(cv, sizeof(struct ucond));
170 _thr_ucond_wait(struct ucond *cv, struct umutex *m,
171 const struct timespec *timeout, int check_unparking)
173 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
174 timeout->tv_nsec <= 0))) {
175 struct pthread *curthread = _get_curthread();
176 _thr_umutex_unlock(m, TID(curthread));
179 return _umtx_op_err(cv, UMTX_OP_CV_WAIT,
180 check_unparking ? UMTX_CHECK_UNPARKING : 0,
181 m, __DECONST(void*, timeout));
185 _thr_ucond_signal(struct ucond *cv)
187 if (!cv->c_has_waiters)
189 return _umtx_op_err(cv, UMTX_OP_CV_SIGNAL, 0, NULL, NULL);
193 _thr_ucond_broadcast(struct ucond *cv)
195 if (!cv->c_has_waiters)
197 return _umtx_op_err(cv, UMTX_OP_CV_BROADCAST, 0, NULL, NULL);
201 __thr_rwlock_rdlock(struct urwlock *rwlock, int flags, struct timespec *tsp)
203 return _umtx_op_err(rwlock, UMTX_OP_RW_RDLOCK, flags, NULL, tsp);
207 __thr_rwlock_wrlock(struct urwlock *rwlock, struct timespec *tsp)
209 return _umtx_op_err(rwlock, UMTX_OP_RW_WRLOCK, 0, NULL, tsp);
213 __thr_rwlock_unlock(struct urwlock *rwlock)
215 return _umtx_op_err(rwlock, UMTX_OP_RW_UNLOCK, 0, NULL, NULL);