locks.c revision 1.2.4.5 1 /* $NetBSD: locks.c,v 1.2.4.5 2008/02/04 09:24:51 yamt 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 }
158
159 int
160 rw_lock_held(krwlock_t *rw)
161 {
162
163 return rumpuser_rw_held(rw->krw_pthlock);
164 }
165
166 /* curriculum vitaes */
167
168 /* forgive me for I have sinned */
169 #define RUMPCV(a) ((struct rumpuser_cv *)(__UNCONST((a)->cv_wmesg)))
170
171 void
172 cv_init(kcondvar_t *cv, const char *msg)
173 {
174
175 rumpuser_cv_init((struct rumpuser_cv **)__UNCONST(&cv->cv_wmesg));
176 }
177
178 void
179 cv_destroy(kcondvar_t *cv)
180 {
181
182 rumpuser_cv_destroy(RUMPCV(cv));
183 }
184
185 void
186 cv_wait(kcondvar_t *cv, kmutex_t *mtx)
187 {
188
189 rumpuser_cv_wait(RUMPCV(cv), mtx->kmtx_mtx);
190 }
191
192 int
193 cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
194 {
195
196 rumpuser_cv_wait(RUMPCV(cv), mtx->kmtx_mtx);
197 return 0;
198 }
199
200 int
201 cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int ticks)
202 {
203 extern int hz;
204
205 if (ticks == 0) {
206 cv_wait(cv, mtx);
207 return 0;
208 } else {
209 KASSERT(hz == 100);
210 return rumpuser_cv_timedwait(RUMPCV(cv), mtx->kmtx_mtx, ticks);
211 }
212 }
213
214 int
215 cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int ticks)
216 {
217
218 return cv_timedwait(cv, mtx, ticks);
219 }
220
221 void
222 cv_signal(kcondvar_t *cv)
223 {
224
225 rumpuser_cv_signal(RUMPCV(cv));
226 }
227
228 void
229 cv_broadcast(kcondvar_t *cv)
230 {
231
232 rumpuser_cv_broadcast(RUMPCV(cv));
233 }
234
235 /* kernel biglock, only for vnode_if */
236
237 void
238 _kernel_lock(int nlocks, struct lwp *l)
239 {
240
241 KASSERT(nlocks == 1);
242 mutex_enter(&rump_giantlock);
243 }
244
245 void
246 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
247 {
248
249 KASSERT(nlocks == 1);
250 mutex_exit(&rump_giantlock);
251 if (countp)
252 *countp = 1;
253 }
254