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_urwlock_init(struct urwlock *rwl)
53 static struct urwlock default_rwl = DEFAULT_URWLOCK;
58 __thr_umutex_lock(struct umutex *mtx, uint32_t id)
62 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
65 _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, 0);
68 if ((owner & ~UMUTEX_CONTESTED) == 0 &&
69 atomic_cmpset_acq_32(&mtx->m_owner, owner, id|owner))
74 return _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0);
77 #define SPINLOOPS 1000
80 __thr_umutex_lock_spin(struct umutex *mtx, uint32_t id)
85 return __thr_umutex_lock(mtx, id);
87 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
89 int count = SPINLOOPS;
92 if ((owner & ~UMUTEX_CONTESTED) == 0) {
93 if (atomic_cmpset_acq_32(
103 _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, 0);
107 return _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0);
111 __thr_umutex_timedlock(struct umutex *mtx, uint32_t id,
112 const struct timespec *ets)
114 struct timespec timo, cts;
118 clock_gettime(CLOCK_REALTIME, &cts);
119 TIMESPEC_SUB(&timo, ets, &cts);
125 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
128 ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, &timo);
130 /* now try to lock it */
131 owner = mtx->m_owner;
132 if ((owner & ~UMUTEX_CONTESTED) == 0 &&
133 atomic_cmpset_acq_32(&mtx->m_owner, owner, id|owner))
136 ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, &timo);
140 if (ret == ETIMEDOUT)
142 clock_gettime(CLOCK_REALTIME, &cts);
143 TIMESPEC_SUB(&timo, ets, &cts);
144 if (timo.tv_sec < 0 || (timo.tv_sec == 0 && timo.tv_nsec == 0)) {
153 __thr_umutex_unlock(struct umutex *mtx, uint32_t id)
156 /* XXX this logic has a race-condition on ia64. */
157 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
158 atomic_cmpset_rel_32(&mtx->m_owner, id | UMUTEX_CONTESTED, UMUTEX_CONTESTED);
159 return _umtx_op_err(mtx, UMTX_OP_MUTEX_WAKE, 0, 0, 0);
161 #endif /* __ia64__ */
162 return _umtx_op_err(mtx, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0);
166 __thr_umutex_trylock(struct umutex *mtx)
168 return _umtx_op_err(mtx, UMTX_OP_MUTEX_TRYLOCK, 0, 0, 0);
172 __thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling,
173 uint32_t *oldceiling)
175 return _umtx_op_err(mtx, UMTX_OP_SET_CEILING, ceiling, oldceiling, 0);
179 _thr_umtx_wait(volatile long *mtx, long id, const struct timespec *timeout)
181 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
182 timeout->tv_nsec <= 0)))
184 return _umtx_op_err(__DEVOLATILE(void *, mtx), UMTX_OP_WAIT, id, 0,
185 __DECONST(void*, timeout));
189 _thr_umtx_wait_uint(volatile u_int *mtx, u_int id, const struct timespec *timeout, int shared)
191 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
192 timeout->tv_nsec <= 0)))
194 return _umtx_op_err(__DEVOLATILE(void *, mtx),
195 shared ? UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id, 0,
196 __DECONST(void*, timeout));
200 _thr_umtx_timedwait_uint(volatile u_int *mtx, u_int id, int clockid,
201 const struct timespec *abstime, int shared)
203 struct timespec ts, ts2, *tsp;
205 if (abstime != NULL) {
206 clock_gettime(clockid, &ts);
207 TIMESPEC_SUB(&ts2, abstime, &ts);
208 if (ts2.tv_sec < 0 || ts2.tv_nsec <= 0)
214 return _umtx_op_err(__DEVOLATILE(void *, mtx),
215 shared ? UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id, NULL,
220 _thr_umtx_wake(volatile void *mtx, int nr_wakeup, int shared)
222 return _umtx_op_err(__DEVOLATILE(void *, mtx), shared ? UMTX_OP_WAKE : UMTX_OP_WAKE_PRIVATE,
227 _thr_ucond_init(struct ucond *cv)
229 bzero(cv, sizeof(struct ucond));
233 _thr_ucond_wait(struct ucond *cv, struct umutex *m,
234 const struct timespec *timeout, int check_unparking)
236 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
237 timeout->tv_nsec <= 0))) {
238 struct pthread *curthread = _get_curthread();
239 _thr_umutex_unlock(m, TID(curthread));
242 return _umtx_op_err(cv, UMTX_OP_CV_WAIT,
243 check_unparking ? UMTX_CHECK_UNPARKING : 0,
244 m, __DECONST(void*, timeout));
248 _thr_ucond_signal(struct ucond *cv)
250 if (!cv->c_has_waiters)
252 return _umtx_op_err(cv, UMTX_OP_CV_SIGNAL, 0, NULL, NULL);
256 _thr_ucond_broadcast(struct ucond *cv)
258 if (!cv->c_has_waiters)
260 return _umtx_op_err(cv, UMTX_OP_CV_BROADCAST, 0, NULL, NULL);
264 __thr_rwlock_rdlock(struct urwlock *rwlock, int flags, struct timespec *tsp)
266 return _umtx_op_err(rwlock, UMTX_OP_RW_RDLOCK, flags, NULL, tsp);
270 __thr_rwlock_wrlock(struct urwlock *rwlock, struct timespec *tsp)
272 return _umtx_op_err(rwlock, UMTX_OP_RW_WRLOCK, 0, NULL, tsp);
276 __thr_rwlock_unlock(struct urwlock *rwlock)
278 return _umtx_op_err(rwlock, UMTX_OP_RW_UNLOCK, 0, NULL, NULL);
282 _thr_rwl_rdlock(struct urwlock *rwlock)
287 if (_thr_rwlock_tryrdlock(rwlock, URWLOCK_PREFER_READER) == 0)
289 ret = __thr_rwlock_rdlock(rwlock, URWLOCK_PREFER_READER, NULL);
293 PANIC("rdlock error");
298 _thr_rwl_wrlock(struct urwlock *rwlock)
303 if (_thr_rwlock_trywrlock(rwlock) == 0)
305 ret = __thr_rwlock_wrlock(rwlock, NULL);
309 PANIC("wrlock error");
314 _thr_rwl_unlock(struct urwlock *rwlock)
316 if (_thr_rwlock_unlock(rwlock))
317 PANIC("unlock error");