2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "thr_private.h"
35 #ifndef HAS__UMTX_OP_ERR
36 int _umtx_op_err(void *obj, int op, u_long val, void *uaddr, void *uaddr2)
39 if (_umtx_op(obj, op, val, uaddr, uaddr2) == -1)
46 _thr_umutex_init(struct umutex *mtx)
48 static const struct umutex default_mtx = DEFAULT_UMUTEX;
54 _thr_urwlock_init(struct urwlock *rwl)
56 static const struct urwlock default_rwl = DEFAULT_URWLOCK;
62 __thr_umutex_lock(struct umutex *mtx, uint32_t id)
66 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) != 0)
67 return (_umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0));
71 if ((owner & ~UMUTEX_CONTESTED) == 0 &&
72 atomic_cmpset_acq_32(&mtx->m_owner, owner, id | owner))
74 if (owner == UMUTEX_RB_OWNERDEAD &&
75 atomic_cmpset_acq_32(&mtx->m_owner, owner,
76 id | UMUTEX_CONTESTED))
78 if (owner == UMUTEX_RB_NOTRECOV)
79 return (ENOTRECOVERABLE);
82 _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, 0);
86 #define SPINLOOPS 1000
89 __thr_umutex_lock_spin(struct umutex *mtx, uint32_t id)
95 return (__thr_umutex_lock(mtx, id));
96 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) != 0)
97 return (_umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0));
102 owner = mtx->m_owner;
103 if ((owner & ~UMUTEX_CONTESTED) == 0 &&
104 atomic_cmpset_acq_32(&mtx->m_owner, owner,
107 if (__predict_false(owner == UMUTEX_RB_OWNERDEAD) &&
108 atomic_cmpset_acq_32(&mtx->m_owner, owner,
109 id | UMUTEX_CONTESTED))
111 if (__predict_false(owner == UMUTEX_RB_NOTRECOV))
112 return (ENOTRECOVERABLE);
117 _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, 0);
122 __thr_umutex_timedlock(struct umutex *mtx, uint32_t id,
123 const struct timespec *abstime)
125 struct _umtx_time *tm_p, timeout;
130 if (abstime == NULL) {
134 timeout._clockid = CLOCK_REALTIME;
135 timeout._flags = UMTX_ABSTIME;
136 timeout._timeout = *abstime;
138 tm_size = sizeof(timeout);
142 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT |
143 UMUTEX_PRIO_INHERIT)) == 0) {
145 owner = mtx->m_owner;
146 if ((owner & ~UMUTEX_CONTESTED) == 0 &&
147 atomic_cmpset_acq_32(&mtx->m_owner, owner,
150 if (__predict_false(owner == UMUTEX_RB_OWNERDEAD) &&
151 atomic_cmpset_acq_32(&mtx->m_owner, owner,
152 id | UMUTEX_CONTESTED))
154 if (__predict_false(owner == UMUTEX_RB_NOTRECOV))
155 return (ENOTRECOVERABLE);
157 ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0,
158 (void *)tm_size, __DECONST(void *, tm_p));
160 ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0,
161 (void *)tm_size, __DECONST(void *, tm_p));
162 if (ret == 0 || ret == EOWNERDEAD ||
163 ret == ENOTRECOVERABLE)
166 if (ret == ETIMEDOUT)
173 __thr_umutex_unlock(struct umutex *mtx)
176 return (_umtx_op_err(mtx, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0));
180 __thr_umutex_trylock(struct umutex *mtx)
183 return (_umtx_op_err(mtx, UMTX_OP_MUTEX_TRYLOCK, 0, 0, 0));
187 __thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling,
188 uint32_t *oldceiling)
191 return (_umtx_op_err(mtx, UMTX_OP_SET_CEILING, ceiling, oldceiling, 0));
195 _thr_umtx_wait(volatile long *mtx, long id, const struct timespec *timeout)
198 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
199 timeout->tv_nsec <= 0)))
201 return (_umtx_op_err(__DEVOLATILE(void *, mtx), UMTX_OP_WAIT, id, 0,
202 __DECONST(void*, timeout)));
206 _thr_umtx_wait_uint(volatile u_int *mtx, u_int id,
207 const struct timespec *timeout, int shared)
210 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
211 timeout->tv_nsec <= 0)))
213 return (_umtx_op_err(__DEVOLATILE(void *, mtx), shared ?
214 UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id, 0,
215 __DECONST(void*, timeout)));
219 _thr_umtx_timedwait_uint(volatile u_int *mtx, u_int id, int clockid,
220 const struct timespec *abstime, int shared)
222 struct _umtx_time *tm_p, timeout;
225 if (abstime == NULL) {
229 timeout._clockid = clockid;
230 timeout._flags = UMTX_ABSTIME;
231 timeout._timeout = *abstime;
233 tm_size = sizeof(timeout);
236 return (_umtx_op_err(__DEVOLATILE(void *, mtx), shared ?
237 UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id,
238 (void *)tm_size, __DECONST(void *, tm_p)));
242 _thr_umtx_wake(volatile void *mtx, int nr_wakeup, int shared)
245 return (_umtx_op_err(__DEVOLATILE(void *, mtx), shared ?
246 UMTX_OP_WAKE : UMTX_OP_WAKE_PRIVATE, nr_wakeup, 0, 0));
250 _thr_ucond_init(struct ucond *cv)
253 bzero(cv, sizeof(struct ucond));
257 _thr_ucond_wait(struct ucond *cv, struct umutex *m,
258 const struct timespec *timeout, int flags)
260 struct pthread *curthread;
262 if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
263 timeout->tv_nsec <= 0))) {
264 curthread = _get_curthread();
265 _thr_umutex_unlock(m, TID(curthread));
268 return (_umtx_op_err(cv, UMTX_OP_CV_WAIT, flags, m,
269 __DECONST(void*, timeout)));
273 _thr_ucond_signal(struct ucond *cv)
276 if (!cv->c_has_waiters)
278 return (_umtx_op_err(cv, UMTX_OP_CV_SIGNAL, 0, NULL, NULL));
282 _thr_ucond_broadcast(struct ucond *cv)
285 if (!cv->c_has_waiters)
287 return (_umtx_op_err(cv, UMTX_OP_CV_BROADCAST, 0, NULL, NULL));
291 __thr_rwlock_rdlock(struct urwlock *rwlock, int flags,
292 const struct timespec *tsp)
294 struct _umtx_time timeout, *tm_p;
301 timeout._timeout = *tsp;
302 timeout._flags = UMTX_ABSTIME;
303 timeout._clockid = CLOCK_REALTIME;
305 tm_size = sizeof(timeout);
307 return (_umtx_op_err(rwlock, UMTX_OP_RW_RDLOCK, flags,
308 (void *)tm_size, tm_p));
312 __thr_rwlock_wrlock(struct urwlock *rwlock, const struct timespec *tsp)
314 struct _umtx_time timeout, *tm_p;
321 timeout._timeout = *tsp;
322 timeout._flags = UMTX_ABSTIME;
323 timeout._clockid = CLOCK_REALTIME;
325 tm_size = sizeof(timeout);
327 return (_umtx_op_err(rwlock, UMTX_OP_RW_WRLOCK, 0, (void *)tm_size,
332 __thr_rwlock_unlock(struct urwlock *rwlock)
335 return (_umtx_op_err(rwlock, UMTX_OP_RW_UNLOCK, 0, NULL, NULL));
339 _thr_rwl_rdlock(struct urwlock *rwlock)
344 if (_thr_rwlock_tryrdlock(rwlock, URWLOCK_PREFER_READER) == 0)
346 ret = __thr_rwlock_rdlock(rwlock, URWLOCK_PREFER_READER, NULL);
350 PANIC("rdlock error");
355 _thr_rwl_wrlock(struct urwlock *rwlock)
360 if (_thr_rwlock_trywrlock(rwlock) == 0)
362 ret = __thr_rwlock_wrlock(rwlock, NULL);
366 PANIC("wrlock error");
371 _thr_rwl_unlock(struct urwlock *rwlock)
374 if (_thr_rwlock_unlock(rwlock))
375 PANIC("unlock error");