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 const struct umutex default_mtx = DEFAULT_UMUTEX;
51 _thr_urwlock_init(struct urwlock *rwl)
53 static const struct urwlock default_rwl = DEFAULT_URWLOCK;
59 __thr_umutex_lock(struct umutex *mtx, uint32_t id)
63 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
66 _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, 0);
69 if ((owner & ~UMUTEX_CONTESTED) == 0 &&
70 atomic_cmpset_acq_32(&mtx->m_owner, owner, id|owner))
75 return _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0);
78 #define SPINLOOPS 1000
81 __thr_umutex_lock_spin(struct umutex *mtx, uint32_t id)
86 return __thr_umutex_lock(mtx, id);
88 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
90 int count = SPINLOOPS;
93 if ((owner & ~UMUTEX_CONTESTED) == 0) {
94 if (atomic_cmpset_acq_32(
104 _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, 0);
108 return _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0);
112 __thr_umutex_timedlock(struct umutex *mtx, uint32_t id,
113 const struct timespec *abstime)
115 struct _umtx_time *tm_p, timeout;
120 if (abstime == NULL) {
124 timeout._clockid = CLOCK_REALTIME;
125 timeout._flags = UMTX_ABSTIME;
126 timeout._timeout = *abstime;
128 tm_size = sizeof(timeout);
132 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
135 ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0,
136 (void *)tm_size, __DECONST(void *, tm_p));
138 /* now try to lock it */
139 owner = mtx->m_owner;
140 if ((owner & ~UMUTEX_CONTESTED) == 0 &&
141 atomic_cmpset_acq_32(&mtx->m_owner, owner, id|owner))
144 ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0,
145 (void *)tm_size, __DECONST(void *, tm_p));
149 if (ret == ETIMEDOUT)
156 __thr_umutex_unlock(struct umutex *mtx, uint32_t id)
158 return _umtx_op_err(mtx, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0);
162 __thr_umutex_trylock(struct umutex *mtx)
164 return _umtx_op_err(mtx, UMTX_OP_MUTEX_TRYLOCK, 0, 0, 0);
168 __thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling,
169 uint32_t *oldceiling)
171 return _umtx_op_err(mtx, UMTX_OP_SET_CEILING, ceiling, oldceiling, 0);
175 _thr_umtx_wait(volatile long *mtx, long id, const struct timespec *timeout)
177 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
178 timeout->tv_nsec <= 0)))
180 return _umtx_op_err(__DEVOLATILE(void *, mtx), UMTX_OP_WAIT, id, 0,
181 __DECONST(void*, timeout));
185 _thr_umtx_wait_uint(volatile u_int *mtx, u_int id, const struct timespec *timeout, int shared)
187 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
188 timeout->tv_nsec <= 0)))
190 return _umtx_op_err(__DEVOLATILE(void *, mtx),
191 shared ? UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id, 0,
192 __DECONST(void*, timeout));
196 _thr_umtx_timedwait_uint(volatile u_int *mtx, u_int id, int clockid,
197 const struct timespec *abstime, int shared)
199 struct _umtx_time *tm_p, timeout;
202 if (abstime == NULL) {
206 timeout._clockid = clockid;
207 timeout._flags = UMTX_ABSTIME;
208 timeout._timeout = *abstime;
210 tm_size = sizeof(timeout);
213 return _umtx_op_err(__DEVOLATILE(void *, mtx),
214 shared ? UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id,
215 (void *)tm_size, __DECONST(void *, tm_p));
219 _thr_umtx_wake(volatile void *mtx, int nr_wakeup, int shared)
221 return _umtx_op_err(__DEVOLATILE(void *, mtx), shared ? UMTX_OP_WAKE : UMTX_OP_WAKE_PRIVATE,
226 _thr_ucond_init(struct ucond *cv)
228 bzero(cv, sizeof(struct ucond));
232 _thr_ucond_wait(struct ucond *cv, struct umutex *m,
233 const struct timespec *timeout, int flags)
235 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
236 timeout->tv_nsec <= 0))) {
237 struct pthread *curthread = _get_curthread();
238 _thr_umutex_unlock(m, TID(curthread));
241 return _umtx_op_err(cv, UMTX_OP_CV_WAIT, flags,
242 m, __DECONST(void*, timeout));
246 _thr_ucond_signal(struct ucond *cv)
248 if (!cv->c_has_waiters)
250 return _umtx_op_err(cv, UMTX_OP_CV_SIGNAL, 0, NULL, NULL);
254 _thr_ucond_broadcast(struct ucond *cv)
256 if (!cv->c_has_waiters)
258 return _umtx_op_err(cv, UMTX_OP_CV_BROADCAST, 0, NULL, NULL);
262 __thr_rwlock_rdlock(struct urwlock *rwlock, int flags,
263 const struct timespec *tsp)
265 struct _umtx_time timeout, *tm_p;
272 timeout._timeout = *tsp;
273 timeout._flags = UMTX_ABSTIME;
274 timeout._clockid = CLOCK_REALTIME;
276 tm_size = sizeof(timeout);
278 return _umtx_op_err(rwlock, UMTX_OP_RW_RDLOCK, flags, (void *)tm_size, tm_p);
282 __thr_rwlock_wrlock(struct urwlock *rwlock, const struct timespec *tsp)
284 struct _umtx_time timeout, *tm_p;
291 timeout._timeout = *tsp;
292 timeout._flags = UMTX_ABSTIME;
293 timeout._clockid = CLOCK_REALTIME;
295 tm_size = sizeof(timeout);
297 return _umtx_op_err(rwlock, UMTX_OP_RW_WRLOCK, 0, (void *)tm_size, tm_p);
301 __thr_rwlock_unlock(struct urwlock *rwlock)
303 return _umtx_op_err(rwlock, UMTX_OP_RW_UNLOCK, 0, NULL, NULL);
307 _thr_rwl_rdlock(struct urwlock *rwlock)
312 if (_thr_rwlock_tryrdlock(rwlock, URWLOCK_PREFER_READER) == 0)
314 ret = __thr_rwlock_rdlock(rwlock, URWLOCK_PREFER_READER, NULL);
318 PANIC("rdlock error");
323 _thr_rwl_wrlock(struct urwlock *rwlock)
328 if (_thr_rwlock_trywrlock(rwlock) == 0)
330 ret = __thr_rwlock_wrlock(rwlock, NULL);
334 PANIC("wrlock error");
339 _thr_rwl_unlock(struct urwlock *rwlock)
341 if (_thr_rwlock_unlock(rwlock))
342 PANIC("unlock error");