xref: /freebsd/lib/libthr/thread/thr_umtx.c (revision 8d6a11a0)
1 /*
2  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
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.
14  *
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.
25  *
26  * $FreeBSD$
27  *
28  */
29 
30 #include "thr_private.h"
31 #include "thr_umtx.h"
32 
33 #ifndef HAS__UMTX_OP_ERR
34 int _umtx_op_err(void *obj, int op, u_long val, void *uaddr, void *uaddr2)
35 {
36 	if (_umtx_op(obj, op, val, uaddr, uaddr2) == -1)
37 		return (errno);
38 	return (0);
39 }
40 #endif
41 
42 void
43 _thr_umutex_init(struct umutex *mtx)
44 {
45 	static struct umutex default_mtx = DEFAULT_UMUTEX;
46 
47 	*mtx = default_mtx;
48 }
49 
50 int
51 __thr_umutex_lock(struct umutex *mtx)
52 {
53 	return _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0);
54 }
55 
56 int
57 __thr_umutex_timedlock(struct umutex *mtx,
58 	const struct timespec *timeout)
59 {
60 	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
61 		timeout->tv_nsec <= 0)))
62 		return (ETIMEDOUT);
63 	return _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0,
64 		__DECONST(void *, timeout));
65 }
66 
67 int
68 __thr_umutex_unlock(struct umutex *mtx)
69 {
70 	return _umtx_op_err(mtx, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0);
71 }
72 
73 int
74 __thr_umutex_trylock(struct umutex *mtx)
75 {
76 	return _umtx_op_err(mtx, UMTX_OP_MUTEX_TRYLOCK, 0, 0, 0);
77 }
78 
79 int
80 __thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling,
81 	uint32_t *oldceiling)
82 {
83 	return _umtx_op_err(mtx, UMTX_OP_SET_CEILING, ceiling, oldceiling, 0);
84 }
85 
86 int
87 _thr_umtx_wait(volatile long *mtx, long id, const struct timespec *timeout)
88 {
89 	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
90 		timeout->tv_nsec <= 0)))
91 		return (ETIMEDOUT);
92 	return _umtx_op_err(__DEVOLATILE(void *, mtx), UMTX_OP_WAIT, id, 0,
93 		__DECONST(void*, timeout));
94 }
95 
96 int
97 _thr_umtx_wait_uint(volatile u_int *mtx, u_int id, const struct timespec *timeout, int shared)
98 {
99 	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
100 		timeout->tv_nsec <= 0)))
101 		return (ETIMEDOUT);
102 	return _umtx_op_err(__DEVOLATILE(void *, mtx),
103 			shared ? UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id, 0,
104 			__DECONST(void*, timeout));
105 }
106 
107 int
108 _thr_umtx_wake(volatile void *mtx, int nr_wakeup, int shared)
109 {
110 	return _umtx_op_err(__DEVOLATILE(void *, mtx), shared ? UMTX_OP_WAKE : UMTX_OP_WAKE_PRIVATE,
111 		nr_wakeup, 0, 0);
112 }
113 
114 void
115 _thr_ucond_init(struct ucond *cv)
116 {
117 	bzero(cv, sizeof(struct ucond));
118 }
119 
120 int
121 _thr_ucond_wait(struct ucond *cv, struct umutex *m,
122 	const struct timespec *timeout, int check_unparking)
123 {
124 	if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
125 	    timeout->tv_nsec <= 0))) {
126 		__thr_umutex_unlock(m);
127                 return (ETIMEDOUT);
128 	}
129 	return _umtx_op_err(cv, UMTX_OP_CV_WAIT,
130 		     check_unparking ? UMTX_CHECK_UNPARKING : 0,
131 		     m, __DECONST(void*, timeout));
132 }
133 
134 int
135 _thr_ucond_signal(struct ucond *cv)
136 {
137 	if (!cv->c_has_waiters)
138 		return (0);
139 	return _umtx_op_err(cv, UMTX_OP_CV_SIGNAL, 0, NULL, NULL);
140 }
141 
142 int
143 _thr_ucond_broadcast(struct ucond *cv)
144 {
145 	if (!cv->c_has_waiters)
146 		return (0);
147 	return _umtx_op_err(cv, UMTX_OP_CV_BROADCAST, 0, NULL, NULL);
148 }
149 
150 int
151 __thr_rwlock_rdlock(struct urwlock *rwlock, int flags, struct timespec *tsp)
152 {
153 	return _umtx_op_err(rwlock, UMTX_OP_RW_RDLOCK, flags, NULL, tsp);
154 }
155 
156 int
157 __thr_rwlock_wrlock(struct urwlock *rwlock, struct timespec *tsp)
158 {
159 	return _umtx_op_err(rwlock, UMTX_OP_RW_WRLOCK, 0, NULL, tsp);
160 }
161 
162 int
163 __thr_rwlock_unlock(struct urwlock *rwlock)
164 {
165 	return _umtx_op_err(rwlock, UMTX_OP_RW_UNLOCK, 0, NULL, NULL);
166 }
167