locks.c revision 1.1.6.4 1 /* $NetBSD: locks.c,v 1.1.6.4 2008/01/09 01:58:00 matt 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 /* XXX */
98 return 1;
99 }
100
101 /* reader/writer locks */
102
103 void
104 rw_init(krwlock_t *rw)
105 {
106
107 rumpuser_rw_init(&rw->krw_pthlock);
108 }
109
110 void
111 rw_destroy(krwlock_t *rw)
112 {
113
114 rumpuser_rw_destroy(rw->krw_pthlock);
115 }
116
117 void
118 rw_enter(krwlock_t *rw, const krw_t op)
119 {
120
121 rumpuser_rw_enter(rw->krw_pthlock, op == RW_WRITER);
122 }
123
124 int
125 rw_tryenter(krwlock_t *rw, const krw_t op)
126 {
127
128 return rumpuser_rw_tryenter(rw->krw_pthlock, op == RW_WRITER);
129 }
130
131 void
132 rw_exit(krwlock_t *rw)
133 {
134
135 rumpuser_rw_exit(rw->krw_pthlock);
136 }
137
138 /* always fails */
139 int
140 rw_tryupgrade(krwlock_t *rw)
141 {
142
143 return 0;
144 }
145
146 int
147 rw_write_held(krwlock_t *rw)
148 {
149
150 /* XXX: always held for now */
151 return 1;
152 }
153
154 /* curriculum vitaes */
155
156 /* forgive me for I have sinned */
157 #define RUMPCV(a) ((struct rumpuser_cv *)(__UNCONST((a)->cv_wmesg)))
158
159 void
160 cv_init(kcondvar_t *cv, const char *msg)
161 {
162
163 rumpuser_cv_init((struct rumpuser_cv **)__UNCONST(&cv->cv_wmesg));
164 }
165
166 void
167 cv_destroy(kcondvar_t *cv)
168 {
169
170 rumpuser_cv_destroy(RUMPCV(cv));
171 }
172
173 void
174 cv_wait(kcondvar_t *cv, kmutex_t *mtx)
175 {
176
177 rumpuser_cv_wait(RUMPCV(cv), mtx->kmtx_mtx);
178 }
179
180 int
181 cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
182 {
183
184 rumpuser_cv_wait(RUMPCV(cv), mtx->kmtx_mtx);
185 return 0;
186 }
187
188 int
189 cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int ticks)
190 {
191 extern int hz;
192
193 KASSERT(hz == 100);
194 return rumpuser_cv_timedwait(RUMPCV(cv), mtx->kmtx_mtx, ticks);
195 }
196
197 int
198 cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int ticks)
199 {
200 extern int hz;
201
202 KASSERT(hz == 100);
203 return rumpuser_cv_timedwait(RUMPCV(cv), mtx->kmtx_mtx, ticks);
204 }
205
206 void
207 cv_signal(kcondvar_t *cv)
208 {
209
210 rumpuser_cv_signal(RUMPCV(cv));
211 }
212
213 void
214 cv_broadcast(kcondvar_t *cv)
215 {
216
217 rumpuser_cv_broadcast(RUMPCV(cv));
218 }
219
220 /* kernel biglock, only for vnode_if */
221
222 void
223 _kernel_lock(int nlocks, struct lwp *l)
224 {
225
226 KASSERT(nlocks == 1);
227 mutex_enter(&rump_giantlock);
228 }
229
230 void
231 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
232 {
233
234 KASSERT(nlocks == 1);
235 mutex_exit(&rump_giantlock);
236 if (countp)
237 *countp = 1;
238 }
239