xref: /freebsd-14-stable/lib/libthr/thread/thr_umtx.c (revision 91c1c36102a6cded3848e46fd2d242db46a76684)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
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.
16  *
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.
27  */
28 
29 #include "thr_private.h"
30 #include "thr_umtx.h"
31 
32 #ifndef HAS__UMTX_OP_ERR
_umtx_op_err(void * obj,int op,u_long val,void * uaddr,void * uaddr2)33 int _umtx_op_err(void *obj, int op, u_long val, void *uaddr, void *uaddr2)
34 {
35 
36 	if (_umtx_op(obj, op, val, uaddr, uaddr2) == -1)
37 		return (errno);
38 	return (0);
39 }
40 #endif
41 
42 void
_thr_umutex_init(struct umutex * mtx)43 _thr_umutex_init(struct umutex *mtx)
44 {
45 	static const struct umutex default_mtx = DEFAULT_UMUTEX;
46 
47 	*mtx = default_mtx;
48 }
49 
50 void
_thr_urwlock_init(struct urwlock * rwl)51 _thr_urwlock_init(struct urwlock *rwl)
52 {
53 	static const struct urwlock default_rwl = DEFAULT_URWLOCK;
54 
55 	*rwl = default_rwl;
56 }
57 
58 int
__thr_umutex_lock(struct umutex * mtx,uint32_t id)59 __thr_umutex_lock(struct umutex *mtx, uint32_t id)
60 {
61 	uint32_t owner;
62 
63 	if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) != 0)
64 		return	(_umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0));
65 
66 	for (;;) {
67 		owner = mtx->m_owner;
68 		if ((owner & ~UMUTEX_CONTESTED) == 0 &&
69 		     atomic_cmpset_acq_32(&mtx->m_owner, owner, id | owner))
70 			return (0);
71 		if (owner == UMUTEX_RB_OWNERDEAD &&
72 		     atomic_cmpset_acq_32(&mtx->m_owner, owner,
73 		     id | UMUTEX_CONTESTED))
74 			return (EOWNERDEAD);
75 		if (owner == UMUTEX_RB_NOTRECOV)
76 			return (ENOTRECOVERABLE);
77 
78 		/* wait in kernel */
79 		_umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, 0);
80 	}
81 }
82 
83 #define SPINLOOPS 1000
84 
85 int
__thr_umutex_lock_spin(struct umutex * mtx,uint32_t id)86 __thr_umutex_lock_spin(struct umutex *mtx, uint32_t id)
87 {
88 	uint32_t owner;
89 	int count;
90 
91 	if (!_thr_is_smp)
92 		return (__thr_umutex_lock(mtx, id));
93 	if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) != 0)
94 		return	(_umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0));
95 
96 	for (;;) {
97 		count = SPINLOOPS;
98 		while (count--) {
99 			owner = mtx->m_owner;
100 			if ((owner & ~UMUTEX_CONTESTED) == 0 &&
101 			    atomic_cmpset_acq_32(&mtx->m_owner, owner,
102 			    id | owner))
103 				return (0);
104 			if (__predict_false(owner == UMUTEX_RB_OWNERDEAD) &&
105 			    atomic_cmpset_acq_32(&mtx->m_owner, owner,
106 			    id | UMUTEX_CONTESTED))
107 				return (EOWNERDEAD);
108 			if (__predict_false(owner == UMUTEX_RB_NOTRECOV))
109 				return (ENOTRECOVERABLE);
110 			CPU_SPINWAIT;
111 		}
112 
113 		/* wait in kernel */
114 		_umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, 0);
115 	}
116 }
117 
118 int
__thr_umutex_timedlock(struct umutex * mtx,uint32_t id,const struct timespec * abstime)119 __thr_umutex_timedlock(struct umutex *mtx, uint32_t id,
120 	const struct timespec *abstime)
121 {
122 	struct _umtx_time *tm_p, timeout;
123 	size_t tm_size;
124 	uint32_t owner;
125 	int ret;
126 
127 	if (abstime == NULL) {
128 		tm_p = NULL;
129 		tm_size = 0;
130 	} else {
131 		timeout._clockid = CLOCK_REALTIME;
132 		timeout._flags = UMTX_ABSTIME;
133 		timeout._timeout = *abstime;
134 		tm_p = &timeout;
135 		tm_size = sizeof(timeout);
136 	}
137 
138 	for (;;) {
139 		if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT |
140 		    UMUTEX_PRIO_INHERIT)) == 0) {
141 			/* try to lock it */
142 			owner = mtx->m_owner;
143 			if ((owner & ~UMUTEX_CONTESTED) == 0 &&
144 			     atomic_cmpset_acq_32(&mtx->m_owner, owner,
145 			     id | owner))
146 				return (0);
147 			if (__predict_false(owner == UMUTEX_RB_OWNERDEAD) &&
148 			     atomic_cmpset_acq_32(&mtx->m_owner, owner,
149 			     id | UMUTEX_CONTESTED))
150 				return (EOWNERDEAD);
151 			if (__predict_false(owner == UMUTEX_RB_NOTRECOV))
152 				return (ENOTRECOVERABLE);
153 			/* wait in kernel */
154 			ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0,
155 			    (void *)tm_size, __DECONST(void *, tm_p));
156 		} else {
157 			ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0,
158 			    (void *)tm_size, __DECONST(void *, tm_p));
159 			if (ret == 0 || ret == EOWNERDEAD ||
160 			    ret == ENOTRECOVERABLE)
161 				break;
162 		}
163 		if (ret == ETIMEDOUT)
164 			break;
165 	}
166 	return (ret);
167 }
168 
169 int
__thr_umutex_unlock(struct umutex * mtx)170 __thr_umutex_unlock(struct umutex *mtx)
171 {
172 
173 	return (_umtx_op_err(mtx, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0));
174 }
175 
176 int
__thr_umutex_trylock(struct umutex * mtx)177 __thr_umutex_trylock(struct umutex *mtx)
178 {
179 
180 	return (_umtx_op_err(mtx, UMTX_OP_MUTEX_TRYLOCK, 0, 0, 0));
181 }
182 
183 int
__thr_umutex_set_ceiling(struct umutex * mtx,uint32_t ceiling,uint32_t * oldceiling)184 __thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling,
185     uint32_t *oldceiling)
186 {
187 
188 	return (_umtx_op_err(mtx, UMTX_OP_SET_CEILING, ceiling, oldceiling, 0));
189 }
190 
191 int
_thr_umtx_wait(volatile long * mtx,long id,const struct timespec * timeout)192 _thr_umtx_wait(volatile long *mtx, long id, const struct timespec *timeout)
193 {
194 
195 	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
196 	    timeout->tv_nsec <= 0)))
197 		return (ETIMEDOUT);
198 	return (_umtx_op_err(__DEVOLATILE(void *, mtx), UMTX_OP_WAIT, id, 0,
199 	    __DECONST(void*, timeout)));
200 }
201 
202 int
_thr_umtx_wait_uint(volatile u_int * mtx,u_int id,const struct timespec * timeout,int shared)203 _thr_umtx_wait_uint(volatile u_int *mtx, u_int id,
204     const struct timespec *timeout, int shared)
205 {
206 
207 	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
208 	    timeout->tv_nsec <= 0)))
209 		return (ETIMEDOUT);
210 	return (_umtx_op_err(__DEVOLATILE(void *, mtx), shared ?
211 	    UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id, 0,
212 	    __DECONST(void*, timeout)));
213 }
214 
215 int
_thr_umtx_timedwait_uint(volatile u_int * mtx,u_int id,int clockid,const struct timespec * abstime,int shared)216 _thr_umtx_timedwait_uint(volatile u_int *mtx, u_int id, int clockid,
217     const struct timespec *abstime, int shared)
218 {
219 	struct _umtx_time *tm_p, timeout;
220 	size_t tm_size;
221 
222 	if (abstime == NULL) {
223 		tm_p = NULL;
224 		tm_size = 0;
225 	} else {
226 		timeout._clockid = clockid;
227 		timeout._flags = UMTX_ABSTIME;
228 		timeout._timeout = *abstime;
229 		tm_p = &timeout;
230 		tm_size = sizeof(timeout);
231 	}
232 
233 	return (_umtx_op_err(__DEVOLATILE(void *, mtx), shared ?
234 	    UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id,
235 	    (void *)tm_size, __DECONST(void *, tm_p)));
236 }
237 
238 int
_thr_umtx_wake(volatile void * mtx,int nr_wakeup,int shared)239 _thr_umtx_wake(volatile void *mtx, int nr_wakeup, int shared)
240 {
241 
242 	return (_umtx_op_err(__DEVOLATILE(void *, mtx), shared ?
243 	    UMTX_OP_WAKE : UMTX_OP_WAKE_PRIVATE, nr_wakeup, 0, 0));
244 }
245 
246 void
_thr_ucond_init(struct ucond * cv)247 _thr_ucond_init(struct ucond *cv)
248 {
249 
250 	bzero(cv, sizeof(struct ucond));
251 }
252 
253 int
_thr_ucond_wait(struct ucond * cv,struct umutex * m,const struct timespec * timeout,int flags)254 _thr_ucond_wait(struct ucond *cv, struct umutex *m,
255 	const struct timespec *timeout, int flags)
256 {
257 	struct pthread *curthread;
258 
259 	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
260 	    timeout->tv_nsec <= 0))) {
261 		curthread = _get_curthread();
262 		_thr_umutex_unlock(m, TID(curthread));
263                 return (ETIMEDOUT);
264 	}
265 	return (_umtx_op_err(cv, UMTX_OP_CV_WAIT, flags, m,
266 	    __DECONST(void*, timeout)));
267 }
268 
269 int
_thr_ucond_signal(struct ucond * cv)270 _thr_ucond_signal(struct ucond *cv)
271 {
272 
273 	if (!cv->c_has_waiters)
274 		return (0);
275 	return (_umtx_op_err(cv, UMTX_OP_CV_SIGNAL, 0, NULL, NULL));
276 }
277 
278 int
_thr_ucond_broadcast(struct ucond * cv)279 _thr_ucond_broadcast(struct ucond *cv)
280 {
281 
282 	if (!cv->c_has_waiters)
283 		return (0);
284 	return (_umtx_op_err(cv, UMTX_OP_CV_BROADCAST, 0, NULL, NULL));
285 }
286 
287 int
__thr_rwlock_rdlock(struct urwlock * rwlock,int flags,const struct timespec * tsp)288 __thr_rwlock_rdlock(struct urwlock *rwlock, int flags,
289 	const struct timespec *tsp)
290 {
291 	struct _umtx_time timeout, *tm_p;
292 	size_t tm_size;
293 
294 	if (tsp == NULL) {
295 		tm_p = NULL;
296 		tm_size = 0;
297 	} else {
298 		timeout._timeout = *tsp;
299 		timeout._flags = UMTX_ABSTIME;
300 		timeout._clockid = CLOCK_REALTIME;
301 		tm_p = &timeout;
302 		tm_size = sizeof(timeout);
303 	}
304 	return (_umtx_op_err(rwlock, UMTX_OP_RW_RDLOCK, flags,
305 	    (void *)tm_size, tm_p));
306 }
307 
308 int
__thr_rwlock_wrlock(struct urwlock * rwlock,const struct timespec * tsp)309 __thr_rwlock_wrlock(struct urwlock *rwlock, const struct timespec *tsp)
310 {
311 	struct _umtx_time timeout, *tm_p;
312 	size_t tm_size;
313 
314 	if (tsp == NULL) {
315 		tm_p = NULL;
316 		tm_size = 0;
317 	} else {
318 		timeout._timeout = *tsp;
319 		timeout._flags = UMTX_ABSTIME;
320 		timeout._clockid = CLOCK_REALTIME;
321 		tm_p = &timeout;
322 		tm_size = sizeof(timeout);
323 	}
324 	return (_umtx_op_err(rwlock, UMTX_OP_RW_WRLOCK, 0, (void *)tm_size,
325 	    tm_p));
326 }
327 
328 int
__thr_rwlock_unlock(struct urwlock * rwlock)329 __thr_rwlock_unlock(struct urwlock *rwlock)
330 {
331 
332 	return (_umtx_op_err(rwlock, UMTX_OP_RW_UNLOCK, 0, NULL, NULL));
333 }
334 
335 void
_thr_rwl_rdlock(struct urwlock * rwlock)336 _thr_rwl_rdlock(struct urwlock *rwlock)
337 {
338 	int ret;
339 
340 	for (;;) {
341 		if (_thr_rwlock_tryrdlock(rwlock, URWLOCK_PREFER_READER) == 0)
342 			return;
343 		ret = __thr_rwlock_rdlock(rwlock, URWLOCK_PREFER_READER, NULL);
344 		if (ret == 0)
345 			return;
346 		if (ret != EINTR)
347 			PANIC("rdlock error");
348 	}
349 }
350 
351 void
_thr_rwl_wrlock(struct urwlock * rwlock)352 _thr_rwl_wrlock(struct urwlock *rwlock)
353 {
354 	int ret;
355 
356 	for (;;) {
357 		if (_thr_rwlock_trywrlock(rwlock) == 0)
358 			return;
359 		ret = __thr_rwlock_wrlock(rwlock, NULL);
360 		if (ret == 0)
361 			return;
362 		if (ret != EINTR)
363 			PANIC("wrlock error");
364 	}
365 }
366 
367 void
_thr_rwl_unlock(struct urwlock * rwlock)368 _thr_rwl_unlock(struct urwlock *rwlock)
369 {
370 
371 	if (_thr_rwlock_unlock(rwlock))
372 		PANIC("unlock error");
373 }
374