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