locks.c revision 1.44 1 1.44 pooka /* $NetBSD: locks.c,v 1.44 2010/12/01 17:22:51 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.22 pooka * Copyright (c) 2007, 2008 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by the
7 1.1 pooka * Finnish Cultural Foundation.
8 1.1 pooka *
9 1.1 pooka * Redistribution and use in source and binary forms, with or without
10 1.1 pooka * modification, are permitted provided that the following conditions
11 1.1 pooka * are met:
12 1.1 pooka * 1. Redistributions of source code must retain the above copyright
13 1.1 pooka * notice, this list of conditions and the following disclaimer.
14 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 pooka * notice, this list of conditions and the following disclaimer in the
16 1.1 pooka * documentation and/or other materials provided with the distribution.
17 1.1 pooka *
18 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 pooka * SUCH DAMAGE.
29 1.1 pooka */
30 1.1 pooka
31 1.23 pooka #include <sys/cdefs.h>
32 1.44 pooka __KERNEL_RCSID(0, "$NetBSD: locks.c,v 1.44 2010/12/01 17:22:51 pooka Exp $");
33 1.23 pooka
34 1.1 pooka #include <sys/param.h>
35 1.26 pooka #include <sys/kmem.h>
36 1.1 pooka #include <sys/mutex.h>
37 1.1 pooka #include <sys/rwlock.h>
38 1.1 pooka
39 1.18 pooka #include <rump/rumpuser.h>
40 1.18 pooka
41 1.2 pooka #include "rump_private.h"
42 1.2 pooka
43 1.22 pooka /*
44 1.22 pooka * We map locks to pthread routines. The difference between kernel
45 1.22 pooka * and rumpuser routines is that while the kernel uses static
46 1.22 pooka * storage, rumpuser allocates the object from the heap. This
47 1.22 pooka * indirection is necessary because we don't know the size of
48 1.38 snj * pthread objects here. It is also beneficial, since we can
49 1.22 pooka * be easily compatible with the kernel ABI because all kernel
50 1.22 pooka * objects regardless of machine architecture are always at least
51 1.22 pooka * the size of a pointer. The downside, of course, is a performance
52 1.22 pooka * penalty.
53 1.22 pooka */
54 1.22 pooka
55 1.22 pooka #define RUMPMTX(mtx) (*(struct rumpuser_mtx **)(mtx))
56 1.22 pooka
57 1.1 pooka void
58 1.1 pooka mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl)
59 1.1 pooka {
60 1.1 pooka
61 1.22 pooka CTASSERT(sizeof(kmutex_t) >= sizeof(void *));
62 1.22 pooka
63 1.43 pooka rumpuser_mutex_init_kmutex((struct rumpuser_mtx **)mtx);
64 1.1 pooka }
65 1.1 pooka
66 1.1 pooka void
67 1.1 pooka mutex_destroy(kmutex_t *mtx)
68 1.1 pooka {
69 1.1 pooka
70 1.22 pooka rumpuser_mutex_destroy(RUMPMTX(mtx));
71 1.1 pooka }
72 1.1 pooka
73 1.1 pooka void
74 1.1 pooka mutex_enter(kmutex_t *mtx)
75 1.1 pooka {
76 1.1 pooka
77 1.22 pooka rumpuser_mutex_enter(RUMPMTX(mtx));
78 1.1 pooka }
79 1.1 pooka
80 1.6 pooka void
81 1.6 pooka mutex_spin_enter(kmutex_t *mtx)
82 1.6 pooka {
83 1.6 pooka
84 1.37 pooka mutex_enter(mtx);
85 1.6 pooka }
86 1.6 pooka
87 1.1 pooka int
88 1.1 pooka mutex_tryenter(kmutex_t *mtx)
89 1.1 pooka {
90 1.1 pooka
91 1.22 pooka return rumpuser_mutex_tryenter(RUMPMTX(mtx));
92 1.1 pooka }
93 1.1 pooka
94 1.1 pooka void
95 1.1 pooka mutex_exit(kmutex_t *mtx)
96 1.1 pooka {
97 1.1 pooka
98 1.22 pooka rumpuser_mutex_exit(RUMPMTX(mtx));
99 1.1 pooka }
100 1.1 pooka
101 1.6 pooka void
102 1.6 pooka mutex_spin_exit(kmutex_t *mtx)
103 1.6 pooka {
104 1.6 pooka
105 1.37 pooka mutex_exit(mtx);
106 1.6 pooka }
107 1.6 pooka
108 1.1 pooka int
109 1.1 pooka mutex_owned(kmutex_t *mtx)
110 1.1 pooka {
111 1.1 pooka
112 1.44 pooka return mutex_owner(mtx) == curlwp;
113 1.44 pooka }
114 1.44 pooka
115 1.44 pooka struct lwp *
116 1.44 pooka mutex_owner(kmutex_t *mtx)
117 1.44 pooka {
118 1.44 pooka
119 1.44 pooka return rumpuser_mutex_owner(RUMPMTX(mtx));
120 1.1 pooka }
121 1.1 pooka
122 1.22 pooka #define RUMPRW(rw) (*(struct rumpuser_rw **)(rw))
123 1.22 pooka
124 1.1 pooka /* reader/writer locks */
125 1.1 pooka
126 1.1 pooka void
127 1.1 pooka rw_init(krwlock_t *rw)
128 1.1 pooka {
129 1.1 pooka
130 1.22 pooka CTASSERT(sizeof(krwlock_t) >= sizeof(void *));
131 1.22 pooka
132 1.22 pooka rumpuser_rw_init((struct rumpuser_rw **)rw);
133 1.1 pooka }
134 1.1 pooka
135 1.1 pooka void
136 1.1 pooka rw_destroy(krwlock_t *rw)
137 1.1 pooka {
138 1.1 pooka
139 1.22 pooka rumpuser_rw_destroy(RUMPRW(rw));
140 1.1 pooka }
141 1.1 pooka
142 1.1 pooka void
143 1.1 pooka rw_enter(krwlock_t *rw, const krw_t op)
144 1.1 pooka {
145 1.1 pooka
146 1.22 pooka rumpuser_rw_enter(RUMPRW(rw), op == RW_WRITER);
147 1.1 pooka }
148 1.1 pooka
149 1.1 pooka int
150 1.1 pooka rw_tryenter(krwlock_t *rw, const krw_t op)
151 1.1 pooka {
152 1.1 pooka
153 1.22 pooka return rumpuser_rw_tryenter(RUMPRW(rw), op == RW_WRITER);
154 1.1 pooka }
155 1.1 pooka
156 1.1 pooka void
157 1.1 pooka rw_exit(krwlock_t *rw)
158 1.1 pooka {
159 1.1 pooka
160 1.22 pooka rumpuser_rw_exit(RUMPRW(rw));
161 1.1 pooka }
162 1.1 pooka
163 1.1 pooka /* always fails */
164 1.1 pooka int
165 1.1 pooka rw_tryupgrade(krwlock_t *rw)
166 1.1 pooka {
167 1.1 pooka
168 1.1 pooka return 0;
169 1.1 pooka }
170 1.1 pooka
171 1.6 pooka int
172 1.6 pooka rw_write_held(krwlock_t *rw)
173 1.6 pooka {
174 1.6 pooka
175 1.22 pooka return rumpuser_rw_wrheld(RUMPRW(rw));
176 1.10 ad }
177 1.10 ad
178 1.10 ad int
179 1.10 ad rw_read_held(krwlock_t *rw)
180 1.10 ad {
181 1.10 ad
182 1.22 pooka return rumpuser_rw_rdheld(RUMPRW(rw));
183 1.10 ad }
184 1.10 ad
185 1.10 ad int
186 1.10 ad rw_lock_held(krwlock_t *rw)
187 1.10 ad {
188 1.10 ad
189 1.22 pooka return rumpuser_rw_held(RUMPRW(rw));
190 1.6 pooka }
191 1.6 pooka
192 1.1 pooka /* curriculum vitaes */
193 1.1 pooka
194 1.24 pooka #define RUMPCV(cv) (*(struct rumpuser_cv **)(cv))
195 1.1 pooka
196 1.1 pooka void
197 1.1 pooka cv_init(kcondvar_t *cv, const char *msg)
198 1.1 pooka {
199 1.1 pooka
200 1.25 pooka CTASSERT(sizeof(kcondvar_t) >= sizeof(void *));
201 1.25 pooka
202 1.24 pooka rumpuser_cv_init((struct rumpuser_cv **)cv);
203 1.1 pooka }
204 1.1 pooka
205 1.1 pooka void
206 1.1 pooka cv_destroy(kcondvar_t *cv)
207 1.1 pooka {
208 1.1 pooka
209 1.1 pooka rumpuser_cv_destroy(RUMPCV(cv));
210 1.1 pooka }
211 1.1 pooka
212 1.1 pooka void
213 1.1 pooka cv_wait(kcondvar_t *cv, kmutex_t *mtx)
214 1.1 pooka {
215 1.1 pooka
216 1.42 pooka if (__predict_false(rump_threads == 0))
217 1.28 pooka panic("cv_wait without threads");
218 1.22 pooka rumpuser_cv_wait(RUMPCV(cv), RUMPMTX(mtx));
219 1.1 pooka }
220 1.1 pooka
221 1.3 pooka int
222 1.5 pooka cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
223 1.5 pooka {
224 1.5 pooka
225 1.42 pooka if (__predict_false(rump_threads == 0))
226 1.42 pooka panic("cv_wait without threads");
227 1.22 pooka rumpuser_cv_wait(RUMPCV(cv), RUMPMTX(mtx));
228 1.5 pooka return 0;
229 1.5 pooka }
230 1.5 pooka
231 1.5 pooka int
232 1.3 pooka cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int ticks)
233 1.3 pooka {
234 1.27 pooka struct timespec ts, tick;
235 1.3 pooka extern int hz;
236 1.27 pooka
237 1.9 pooka if (ticks == 0) {
238 1.9 pooka cv_wait(cv, mtx);
239 1.9 pooka return 0;
240 1.9 pooka } else {
241 1.42 pooka /*
242 1.42 pooka * XXX: this fetches rump kernel time, but
243 1.42 pooka * rumpuser_cv_timedwait uses host time.
244 1.42 pooka */
245 1.42 pooka nanotime(&ts);
246 1.42 pooka tick.tv_sec = ticks / hz;
247 1.42 pooka tick.tv_nsec = (ticks % hz) * (1000000000/hz);
248 1.42 pooka timespecadd(&ts, &tick, &ts);
249 1.42 pooka
250 1.34 pooka if (rumpuser_cv_timedwait(RUMPCV(cv), RUMPMTX(mtx),
251 1.34 pooka ts.tv_sec, ts.tv_nsec))
252 1.34 pooka return EWOULDBLOCK;
253 1.34 pooka else
254 1.34 pooka return 0;
255 1.9 pooka }
256 1.3 pooka }
257 1.3 pooka
258 1.5 pooka int
259 1.5 pooka cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int ticks)
260 1.5 pooka {
261 1.5 pooka
262 1.9 pooka return cv_timedwait(cv, mtx, ticks);
263 1.5 pooka }
264 1.5 pooka
265 1.1 pooka void
266 1.1 pooka cv_signal(kcondvar_t *cv)
267 1.1 pooka {
268 1.1 pooka
269 1.1 pooka rumpuser_cv_signal(RUMPCV(cv));
270 1.1 pooka }
271 1.2 pooka
272 1.4 pooka void
273 1.4 pooka cv_broadcast(kcondvar_t *cv)
274 1.4 pooka {
275 1.4 pooka
276 1.4 pooka rumpuser_cv_broadcast(RUMPCV(cv));
277 1.4 pooka }
278 1.4 pooka
279 1.17 pooka bool
280 1.17 pooka cv_has_waiters(kcondvar_t *cv)
281 1.17 pooka {
282 1.17 pooka
283 1.17 pooka return rumpuser_cv_has_waiters(RUMPCV(cv));
284 1.17 pooka }
285 1.17 pooka
286 1.35 pooka /* this is not much of an attempt, but ... */
287 1.35 pooka bool
288 1.35 pooka cv_is_valid(kcondvar_t *cv)
289 1.35 pooka {
290 1.35 pooka
291 1.35 pooka return RUMPCV(cv) != NULL;
292 1.35 pooka }
293