locks.c revision 1.10 1 /* $NetBSD: locks.c,v 1.10 2008/01/30 09:50:24 ad Exp $ */
2
3 /*
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by the
7 * Finnish Cultural Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/param.h>
32 #include <sys/mutex.h>
33 #include <sys/rwlock.h>
34
35 #include "rump_private.h"
36
37 #include "rumpuser.h"
38
39 void
40 mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl)
41 {
42
43 rumpuser_mutex_init(&mtx->kmtx_mtx);
44 }
45
46 void
47 mutex_destroy(kmutex_t *mtx)
48 {
49
50 rumpuser_mutex_destroy(mtx->kmtx_mtx);
51 }
52
53 void
54 mutex_enter(kmutex_t *mtx)
55 {
56
57 rumpuser_mutex_enter(mtx->kmtx_mtx);
58 }
59
60 void
61 mutex_spin_enter(kmutex_t *mtx)
62 {
63
64 mutex_enter(mtx);
65 }
66
67 int
68 mutex_tryenter(kmutex_t *mtx)
69 {
70 int rv;
71
72 rv = rumpuser_mutex_tryenter(mtx->kmtx_mtx);
73 if (rv)
74 return 0;
75 else
76 return 1;
77 }
78
79 void
80 mutex_exit(kmutex_t *mtx)
81 {
82
83 rumpuser_mutex_exit(mtx->kmtx_mtx);
84 }
85
86 void
87 mutex_spin_exit(kmutex_t *mtx)
88 {
89
90 mutex_exit(mtx);
91 }
92
93 int
94 mutex_owned(kmutex_t *mtx)
95 {
96
97 return rumpuser_mutex_held(mtx->kmtx_mtx);
98 }
99
100 /* reader/writer locks */
101
102 void
103 rw_init(krwlock_t *rw)
104 {
105
106 rumpuser_rw_init(&rw->krw_pthlock);
107 }
108
109 void
110 rw_destroy(krwlock_t *rw)
111 {
112
113 rumpuser_rw_destroy(rw->krw_pthlock);
114 }
115
116 void
117 rw_enter(krwlock_t *rw, const krw_t op)
118 {
119
120 rumpuser_rw_enter(rw->krw_pthlock, op == RW_WRITER);
121 }
122
123 int
124 rw_tryenter(krwlock_t *rw, const krw_t op)
125 {
126
127 return rumpuser_rw_tryenter(rw->krw_pthlock, op == RW_WRITER);
128 }
129
130 void
131 rw_exit(krwlock_t *rw)
132 {
133
134 rumpuser_rw_exit(rw->krw_pthlock);
135 }
136
137 /* always fails */
138 int
139 rw_tryupgrade(krwlock_t *rw)
140 {
141
142 return 0;
143 }
144
145 int
146 rw_write_held(krwlock_t *rw)
147 {
148
149 return rumpuser_rw_wrheld(rw->krw_pthlock);
150 }
151
152 int
153 rw_read_held(krwlock_t *rw)
154 {
155
156 return rumpuser_rw_rdheld(rw->krw_pthlock);
157 return 1;
158 }
159
160 int
161 rw_lock_held(krwlock_t *rw)
162 {
163
164 return rumpuser_rw_held(rw->krw_pthlock);
165 return 1;
166 }
167
168 /* curriculum vitaes */
169
170 /* forgive me for I have sinned */
171 #define RUMPCV(a) ((struct rumpuser_cv *)(__UNCONST((a)->cv_wmesg)))
172
173 void
174 cv_init(kcondvar_t *cv, const char *msg)
175 {
176
177 rumpuser_cv_init((struct rumpuser_cv **)__UNCONST(&cv->cv_wmesg));
178 }
179
180 void
181 cv_destroy(kcondvar_t *cv)
182 {
183
184 rumpuser_cv_destroy(RUMPCV(cv));
185 }
186
187 void
188 cv_wait(kcondvar_t *cv, kmutex_t *mtx)
189 {
190
191 rumpuser_cv_wait(RUMPCV(cv), mtx->kmtx_mtx);
192 }
193
194 int
195 cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
196 {
197
198 rumpuser_cv_wait(RUMPCV(cv), mtx->kmtx_mtx);
199 return 0;
200 }
201
202 int
203 cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int ticks)
204 {
205 extern int hz;
206
207 if (ticks == 0) {
208 cv_wait(cv, mtx);
209 return 0;
210 } else {
211 KASSERT(hz == 100);
212 return rumpuser_cv_timedwait(RUMPCV(cv), mtx->kmtx_mtx, ticks);
213 }
214 }
215
216 int
217 cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int ticks)
218 {
219
220 return cv_timedwait(cv, mtx, ticks);
221 }
222
223 void
224 cv_signal(kcondvar_t *cv)
225 {
226
227 rumpuser_cv_signal(RUMPCV(cv));
228 }
229
230 void
231 cv_broadcast(kcondvar_t *cv)
232 {
233
234 rumpuser_cv_broadcast(RUMPCV(cv));
235 }
236
237 /* kernel biglock, only for vnode_if */
238
239 void
240 _kernel_lock(int nlocks, struct lwp *l)
241 {
242
243 KASSERT(nlocks == 1);
244 mutex_enter(&rump_giantlock);
245 }
246
247 void
248 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
249 {
250
251 KASSERT(nlocks == 1);
252 mutex_exit(&rump_giantlock);
253 if (countp)
254 *countp = 1;
255 }
256