locks.c revision 1.34 1 1.34 pooka /* $NetBSD: locks.c,v 1.34 2009/11/11 16:46:50 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.34 pooka __KERNEL_RCSID(0, "$NetBSD: locks.c,v 1.34 2009/11/11 16:46:50 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.22 pooka * pthread objects here. It is also benefitial, 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.22 pooka rumpuser_mutex_init((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.20 pooka if (__predict_true(mtx != RUMP_LMUTEX_MAGIC))
85 1.20 pooka mutex_enter(mtx);
86 1.6 pooka }
87 1.6 pooka
88 1.1 pooka int
89 1.1 pooka mutex_tryenter(kmutex_t *mtx)
90 1.1 pooka {
91 1.1 pooka
92 1.22 pooka return rumpuser_mutex_tryenter(RUMPMTX(mtx));
93 1.1 pooka }
94 1.1 pooka
95 1.1 pooka void
96 1.1 pooka mutex_exit(kmutex_t *mtx)
97 1.1 pooka {
98 1.1 pooka
99 1.22 pooka rumpuser_mutex_exit(RUMPMTX(mtx));
100 1.1 pooka }
101 1.1 pooka
102 1.6 pooka void
103 1.6 pooka mutex_spin_exit(kmutex_t *mtx)
104 1.6 pooka {
105 1.6 pooka
106 1.20 pooka if (__predict_true(mtx != RUMP_LMUTEX_MAGIC))
107 1.20 pooka mutex_exit(mtx);
108 1.6 pooka }
109 1.6 pooka
110 1.1 pooka int
111 1.1 pooka mutex_owned(kmutex_t *mtx)
112 1.1 pooka {
113 1.1 pooka
114 1.22 pooka return rumpuser_mutex_held(RUMPMTX(mtx));
115 1.1 pooka }
116 1.1 pooka
117 1.22 pooka #define RUMPRW(rw) (*(struct rumpuser_rw **)(rw))
118 1.22 pooka
119 1.1 pooka /* reader/writer locks */
120 1.1 pooka
121 1.1 pooka void
122 1.1 pooka rw_init(krwlock_t *rw)
123 1.1 pooka {
124 1.1 pooka
125 1.22 pooka CTASSERT(sizeof(krwlock_t) >= sizeof(void *));
126 1.22 pooka
127 1.22 pooka rumpuser_rw_init((struct rumpuser_rw **)rw);
128 1.1 pooka }
129 1.1 pooka
130 1.1 pooka void
131 1.1 pooka rw_destroy(krwlock_t *rw)
132 1.1 pooka {
133 1.1 pooka
134 1.22 pooka rumpuser_rw_destroy(RUMPRW(rw));
135 1.1 pooka }
136 1.1 pooka
137 1.1 pooka void
138 1.1 pooka rw_enter(krwlock_t *rw, const krw_t op)
139 1.1 pooka {
140 1.1 pooka
141 1.22 pooka rumpuser_rw_enter(RUMPRW(rw), op == RW_WRITER);
142 1.1 pooka }
143 1.1 pooka
144 1.1 pooka int
145 1.1 pooka rw_tryenter(krwlock_t *rw, const krw_t op)
146 1.1 pooka {
147 1.1 pooka
148 1.22 pooka return rumpuser_rw_tryenter(RUMPRW(rw), op == RW_WRITER);
149 1.1 pooka }
150 1.1 pooka
151 1.1 pooka void
152 1.1 pooka rw_exit(krwlock_t *rw)
153 1.1 pooka {
154 1.1 pooka
155 1.22 pooka rumpuser_rw_exit(RUMPRW(rw));
156 1.1 pooka }
157 1.1 pooka
158 1.1 pooka /* always fails */
159 1.1 pooka int
160 1.1 pooka rw_tryupgrade(krwlock_t *rw)
161 1.1 pooka {
162 1.1 pooka
163 1.1 pooka return 0;
164 1.1 pooka }
165 1.1 pooka
166 1.6 pooka int
167 1.6 pooka rw_write_held(krwlock_t *rw)
168 1.6 pooka {
169 1.6 pooka
170 1.22 pooka return rumpuser_rw_wrheld(RUMPRW(rw));
171 1.10 ad }
172 1.10 ad
173 1.10 ad int
174 1.10 ad rw_read_held(krwlock_t *rw)
175 1.10 ad {
176 1.10 ad
177 1.22 pooka return rumpuser_rw_rdheld(RUMPRW(rw));
178 1.10 ad }
179 1.10 ad
180 1.10 ad int
181 1.10 ad rw_lock_held(krwlock_t *rw)
182 1.10 ad {
183 1.10 ad
184 1.22 pooka return rumpuser_rw_held(RUMPRW(rw));
185 1.6 pooka }
186 1.6 pooka
187 1.1 pooka /* curriculum vitaes */
188 1.1 pooka
189 1.24 pooka #define RUMPCV(cv) (*(struct rumpuser_cv **)(cv))
190 1.1 pooka
191 1.1 pooka void
192 1.1 pooka cv_init(kcondvar_t *cv, const char *msg)
193 1.1 pooka {
194 1.1 pooka
195 1.25 pooka CTASSERT(sizeof(kcondvar_t) >= sizeof(void *));
196 1.25 pooka
197 1.24 pooka rumpuser_cv_init((struct rumpuser_cv **)cv);
198 1.1 pooka }
199 1.1 pooka
200 1.1 pooka void
201 1.1 pooka cv_destroy(kcondvar_t *cv)
202 1.1 pooka {
203 1.1 pooka
204 1.1 pooka rumpuser_cv_destroy(RUMPCV(cv));
205 1.1 pooka }
206 1.1 pooka
207 1.1 pooka void
208 1.1 pooka cv_wait(kcondvar_t *cv, kmutex_t *mtx)
209 1.1 pooka {
210 1.1 pooka
211 1.28 pooka if (rump_threads == 0)
212 1.28 pooka panic("cv_wait without threads");
213 1.22 pooka rumpuser_cv_wait(RUMPCV(cv), RUMPMTX(mtx));
214 1.1 pooka }
215 1.1 pooka
216 1.3 pooka int
217 1.5 pooka cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
218 1.5 pooka {
219 1.5 pooka
220 1.22 pooka rumpuser_cv_wait(RUMPCV(cv), RUMPMTX(mtx));
221 1.5 pooka return 0;
222 1.5 pooka }
223 1.5 pooka
224 1.5 pooka int
225 1.3 pooka cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int ticks)
226 1.3 pooka {
227 1.27 pooka struct timespec ts, tick;
228 1.3 pooka extern int hz;
229 1.27 pooka
230 1.27 pooka nanotime(&ts);
231 1.27 pooka tick.tv_sec = ticks / hz;
232 1.27 pooka tick.tv_nsec = (ticks % hz) * (1000000000/hz);
233 1.27 pooka timespecadd(&ts, &tick, &ts);
234 1.3 pooka
235 1.9 pooka if (ticks == 0) {
236 1.9 pooka cv_wait(cv, mtx);
237 1.9 pooka return 0;
238 1.9 pooka } else {
239 1.34 pooka if (rumpuser_cv_timedwait(RUMPCV(cv), RUMPMTX(mtx),
240 1.34 pooka ts.tv_sec, ts.tv_nsec))
241 1.34 pooka return EWOULDBLOCK;
242 1.34 pooka else
243 1.34 pooka return 0;
244 1.9 pooka }
245 1.3 pooka }
246 1.3 pooka
247 1.5 pooka int
248 1.5 pooka cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int ticks)
249 1.5 pooka {
250 1.5 pooka
251 1.9 pooka return cv_timedwait(cv, mtx, ticks);
252 1.5 pooka }
253 1.5 pooka
254 1.1 pooka void
255 1.1 pooka cv_signal(kcondvar_t *cv)
256 1.1 pooka {
257 1.1 pooka
258 1.1 pooka rumpuser_cv_signal(RUMPCV(cv));
259 1.1 pooka }
260 1.2 pooka
261 1.4 pooka void
262 1.4 pooka cv_broadcast(kcondvar_t *cv)
263 1.4 pooka {
264 1.4 pooka
265 1.4 pooka rumpuser_cv_broadcast(RUMPCV(cv));
266 1.4 pooka }
267 1.4 pooka
268 1.17 pooka bool
269 1.17 pooka cv_has_waiters(kcondvar_t *cv)
270 1.17 pooka {
271 1.17 pooka
272 1.17 pooka return rumpuser_cv_has_waiters(RUMPCV(cv));
273 1.17 pooka }
274 1.17 pooka
275 1.19 pooka /*
276 1.19 pooka * giant lock
277 1.19 pooka */
278 1.2 pooka
279 1.21 pooka static volatile int lockcnt;
280 1.29 pooka
281 1.29 pooka bool
282 1.29 pooka kernel_biglocked()
283 1.29 pooka {
284 1.29 pooka
285 1.29 pooka return rumpuser_mutex_held(rump_giantlock) && lockcnt > 0;
286 1.29 pooka }
287 1.29 pooka
288 1.29 pooka void
289 1.29 pooka kernel_unlock_allbutone(int *countp)
290 1.29 pooka {
291 1.29 pooka int minusone = lockcnt-1;
292 1.29 pooka
293 1.29 pooka KASSERT(kernel_biglocked());
294 1.29 pooka if (minusone) {
295 1.29 pooka _kernel_unlock(minusone, countp);
296 1.29 pooka }
297 1.29 pooka KASSERT(lockcnt == 1);
298 1.29 pooka *countp = minusone;
299 1.29 pooka
300 1.29 pooka /*
301 1.29 pooka * We drop lockcnt to 0 since rumpuser doesn't know that the
302 1.29 pooka * kernel biglock is being used as the interlock for cv in
303 1.29 pooka * tsleep.
304 1.29 pooka */
305 1.29 pooka lockcnt = 0;
306 1.29 pooka }
307 1.29 pooka
308 1.29 pooka void
309 1.29 pooka kernel_ununlock_allbutone(int nlocks)
310 1.29 pooka {
311 1.29 pooka
312 1.29 pooka KASSERT(rumpuser_mutex_held(rump_giantlock) && lockcnt == 0);
313 1.29 pooka lockcnt = 1;
314 1.29 pooka _kernel_lock(nlocks);
315 1.29 pooka }
316 1.29 pooka
317 1.2 pooka void
318 1.13 drochner _kernel_lock(int nlocks)
319 1.2 pooka {
320 1.2 pooka
321 1.19 pooka while (nlocks--) {
322 1.29 pooka if (!rumpuser_mutex_tryenter(rump_giantlock)) {
323 1.30 pooka struct lwp *l = curlwp;
324 1.30 pooka
325 1.30 pooka rump_unschedule_cpu(l);
326 1.29 pooka rumpuser_mutex_enter_nowrap(rump_giantlock);
327 1.32 pooka rump_schedule_cpu(l);
328 1.29 pooka }
329 1.19 pooka lockcnt++;
330 1.19 pooka }
331 1.2 pooka }
332 1.2 pooka
333 1.2 pooka void
334 1.13 drochner _kernel_unlock(int nlocks, int *countp)
335 1.2 pooka {
336 1.2 pooka
337 1.22 pooka if (!rumpuser_mutex_held(rump_giantlock)) {
338 1.19 pooka KASSERT(nlocks == 0);
339 1.19 pooka if (countp)
340 1.19 pooka *countp = 0;
341 1.19 pooka return;
342 1.19 pooka }
343 1.19 pooka
344 1.2 pooka if (countp)
345 1.19 pooka *countp = lockcnt;
346 1.19 pooka if (nlocks == 0)
347 1.19 pooka nlocks = lockcnt;
348 1.19 pooka if (nlocks == -1) {
349 1.19 pooka KASSERT(lockcnt == 1);
350 1.19 pooka nlocks = 1;
351 1.19 pooka }
352 1.19 pooka KASSERT(nlocks <= lockcnt);
353 1.19 pooka while (nlocks--) {
354 1.19 pooka lockcnt--;
355 1.22 pooka rumpuser_mutex_exit(rump_giantlock);
356 1.19 pooka }
357 1.2 pooka }
358 1.14 ad
359 1.29 pooka void
360 1.29 pooka rump_user_unschedule(int nlocks, int *countp)
361 1.29 pooka {
362 1.29 pooka
363 1.29 pooka _kernel_unlock(nlocks, countp);
364 1.30 pooka rump_unschedule_cpu(curlwp);
365 1.29 pooka }
366 1.29 pooka
367 1.29 pooka void
368 1.29 pooka rump_user_schedule(int nlocks)
369 1.29 pooka {
370 1.29 pooka
371 1.32 pooka rump_schedule_cpu(curlwp);
372 1.29 pooka
373 1.29 pooka if (nlocks)
374 1.29 pooka _kernel_lock(nlocks);
375 1.29 pooka }
376