xref: /netbsd-src/sys/rump/librump/rumpkern/locks.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: locks.c,v 1.2 2007/11/07 16:24:22 pooka 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 int
61 mutex_tryenter(kmutex_t *mtx)
62 {
63 	int rv;
64 
65 	rv = rumpuser_mutex_tryenter(mtx->kmtx_mtx);
66 	if (rv)
67 		return 0;
68 	else
69 		return 1;
70 }
71 
72 void
73 mutex_exit(kmutex_t *mtx)
74 {
75 
76 	rumpuser_mutex_exit(mtx->kmtx_mtx);
77 }
78 
79 int
80 mutex_owned(kmutex_t *mtx)
81 {
82 
83 	/* XXX */
84 	return 1;
85 }
86 
87 /* reader/writer locks */
88 
89 void
90 rw_init(krwlock_t *rw)
91 {
92 
93 	rumpuser_rw_init(&rw->krw_pthlock);
94 }
95 
96 void
97 rw_destroy(krwlock_t *rw)
98 {
99 
100 	rumpuser_rw_destroy(rw->krw_pthlock);
101 }
102 
103 void
104 rw_enter(krwlock_t *rw, const krw_t op)
105 {
106 
107 	rumpuser_rw_enter(rw->krw_pthlock, op == RW_WRITER);
108 }
109 
110 int
111 rw_tryenter(krwlock_t *rw, const krw_t op)
112 {
113 
114 	return rumpuser_rw_tryenter(rw->krw_pthlock, op == RW_WRITER);
115 }
116 
117 void
118 rw_exit(krwlock_t *rw)
119 {
120 
121 	rumpuser_rw_exit(rw->krw_pthlock);
122 }
123 
124 /* always fails */
125 int
126 rw_tryupgrade(krwlock_t *rw)
127 {
128 
129 	return 0;
130 }
131 
132 /* curriculum vitaes */
133 
134 /* forgive me for I have sinned */
135 #define RUMPCV(a) ((struct rumpuser_cv *)(__UNCONST((a)->cv_wmesg)))
136 
137 void
138 cv_init(kcondvar_t *cv, const char *msg)
139 {
140 
141 	rumpuser_cv_init((struct rumpuser_cv **)__UNCONST(&cv->cv_wmesg));
142 }
143 
144 void
145 cv_destroy(kcondvar_t *cv)
146 {
147 
148 	rumpuser_cv_destroy(RUMPCV(cv));
149 }
150 
151 void
152 cv_wait(kcondvar_t *cv, kmutex_t *mtx)
153 {
154 
155 	rumpuser_cv_wait(RUMPCV(cv), mtx->kmtx_mtx);
156 }
157 
158 void
159 cv_signal(kcondvar_t *cv)
160 {
161 
162 	rumpuser_cv_signal(RUMPCV(cv));
163 }
164 
165 /* kernel biglock, only for vnode_if */
166 
167 void
168 _kernel_lock(int nlocks, struct lwp *l)
169 {
170 
171 	KASSERT(nlocks == 1);
172 	mutex_enter(&rump_giantlock);
173 }
174 
175 void
176 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
177 {
178 
179 	KASSERT(nlocks == 1);
180 	mutex_exit(&rump_giantlock);
181 	if (countp)
182 		*countp = 1;
183 }
184