xref: /netbsd-src/lib/librumpuser/rumpuser_pth_dummy.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
1 /*	$NetBSD: rumpuser_pth_dummy.c,v 1.18 2017/12/27 09:01:53 ozaki-r Exp $	*/
2 
3 /*
4  * Copyright (c) 2009 Antti Kantee.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include "rumpuser_port.h"
29 
30 #if !defined(lint)
31 __RCSID("$NetBSD: rumpuser_pth_dummy.c,v 1.18 2017/12/27 09:01:53 ozaki-r Exp $");
32 #endif /* !lint */
33 
34 #include <sys/time.h>
35 
36 #include <assert.h>
37 #include <errno.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdint.h>
42 #include <time.h>
43 
44 #include <rump/rumpuser.h>
45 
46 #include "rumpuser_int.h"
47 
48 static struct lwp *curlwp;
49 
50 struct rumpuser_cv {};
51 
52 struct rumpuser_mtx {
53 	int v;
54 	struct lwp *o;
55 };
56 
57 struct rumpuser_rw {
58 	int v;
59 };
60 
61 void
62 rumpuser__thrinit(void)
63 {
64 
65 	return;
66 }
67 
68 /*ARGSUSED*/
69 int
70 rumpuser_thread_create(void *(*f)(void *), void *arg, const char *thrname,
71 	int joinable, int pri, int cpuidx, void **tptr)
72 {
73 
74 	fprintf(stderr, "rumpuser: threads not available\n");
75 	abort();
76 	return 0;
77 }
78 
79 void
80 rumpuser_thread_exit(void)
81 {
82 
83 	fprintf(stderr, "rumpuser: threads not available\n");
84 	abort();
85 }
86 
87 int
88 rumpuser_thread_join(void *p)
89 {
90 
91 	return 0;
92 }
93 
94 void
95 rumpuser_mutex_init(struct rumpuser_mtx **mtx, int flgas)
96 {
97 
98 	*mtx = calloc(1, sizeof(struct rumpuser_mtx));
99 }
100 
101 int
102 rumpuser_mutex_spin_p(struct rumpuser_mtx *mtx)
103 {
104 
105 	return false; /* XXX */
106 }
107 
108 void
109 rumpuser_mutex_enter(struct rumpuser_mtx *mtx)
110 {
111 
112 	mtx->v++;
113 	mtx->o = curlwp;
114 }
115 
116 void
117 rumpuser_mutex_enter_nowrap(struct rumpuser_mtx *mtx)
118 {
119 
120 	rumpuser_mutex_enter(mtx);
121 }
122 
123 int
124 rumpuser_mutex_tryenter(struct rumpuser_mtx *mtx)
125 {
126 
127 	mtx->v++;
128 	return 0;
129 }
130 
131 void
132 rumpuser_mutex_exit(struct rumpuser_mtx *mtx)
133 {
134 
135 	assert(mtx->v > 0);
136 	if (--mtx->v == 0)
137 		mtx->o = NULL;
138 }
139 
140 void
141 rumpuser_mutex_destroy(struct rumpuser_mtx *mtx)
142 {
143 
144 	free(mtx);
145 }
146 
147 void
148 rumpuser_mutex_owner(struct rumpuser_mtx *mtx, struct lwp **lp)
149 {
150 
151 	*lp = mtx->o;
152 }
153 
154 void
155 rumpuser_rw_init(struct rumpuser_rw **rw)
156 {
157 
158 	*rw = calloc(1, sizeof(struct rumpuser_rw));
159 }
160 
161 void
162 rumpuser_rw_enter(int enum_rumprwlock, struct rumpuser_rw *rw)
163 {
164 	enum rumprwlock lk = enum_rumprwlock;
165 
166 	switch (lk) {
167 	case RUMPUSER_RW_WRITER:
168 		rw->v++;
169 		assert(rw->v == 1);
170 		break;
171 	case RUMPUSER_RW_READER:
172 		assert(rw->v <= 0);
173 		rw->v--;
174 		break;
175 	}
176 }
177 
178 int
179 rumpuser_rw_tryenter(int enum_rumprwlock, struct rumpuser_rw *rw)
180 {
181 
182 	rumpuser_rw_enter(enum_rumprwlock, rw);
183 	return 0;
184 }
185 
186 void
187 rumpuser_rw_exit(struct rumpuser_rw *rw)
188 {
189 
190 	if (rw->v > 0) {
191 		assert(rw->v == 1);
192 		rw->v--;
193 	} else {
194 		rw->v++;
195 	}
196 }
197 
198 void
199 rumpuser_rw_destroy(struct rumpuser_rw *rw)
200 {
201 
202 	free(rw);
203 }
204 
205 void
206 rumpuser_rw_held(int enum_rumprwlock, struct rumpuser_rw *rw, int *rvp)
207 {
208 	enum rumprwlock lk = enum_rumprwlock;
209 
210 	switch (lk) {
211 	case RUMPUSER_RW_WRITER:
212 		*rvp = rw->v > 0;
213 		break;
214 	case RUMPUSER_RW_READER:
215 		*rvp = rw->v < 0;
216 		break;
217 	}
218 }
219 
220 void
221 rumpuser_rw_downgrade(struct rumpuser_rw *rw)
222 {
223 
224 	assert(rw->v == 1);
225 	rw->v = -1;
226 }
227 
228 int
229 rumpuser_rw_tryupgrade(struct rumpuser_rw *rw)
230 {
231 
232 	if (rw->v == -1) {
233 		rw->v = 1;
234 		return 0;
235 	}
236 
237 	return EBUSY;
238 }
239 
240 /*ARGSUSED*/
241 void
242 rumpuser_cv_init(struct rumpuser_cv **cv)
243 {
244 
245 }
246 
247 /*ARGSUSED*/
248 void
249 rumpuser_cv_destroy(struct rumpuser_cv *cv)
250 {
251 
252 }
253 
254 /*ARGSUSED*/
255 void
256 rumpuser_cv_wait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx)
257 {
258 
259 }
260 
261 /*ARGSUSED*/
262 void
263 rumpuser_cv_wait_nowrap(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx)
264 {
265 
266 }
267 
268 /*ARGSUSED*/
269 int
270 rumpuser_cv_timedwait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx,
271 	int64_t sec, int64_t nsec)
272 {
273 	struct timespec ts;
274 
275 	/*LINTED*/
276 	ts.tv_sec = sec;
277 	/*LINTED*/
278 	ts.tv_nsec = nsec;
279 
280 	nanosleep(&ts, NULL);
281 	return 0;
282 }
283 
284 /*ARGSUSED*/
285 void
286 rumpuser_cv_signal(struct rumpuser_cv *cv)
287 {
288 
289 }
290 
291 /*ARGSUSED*/
292 void
293 rumpuser_cv_broadcast(struct rumpuser_cv *cv)
294 {
295 
296 }
297 
298 /*ARGSUSED*/
299 void
300 rumpuser_cv_has_waiters(struct rumpuser_cv *cv, int *rvp)
301 {
302 
303 	*rvp = 0;
304 }
305 
306 /*
307  * curlwp
308  */
309 
310 void
311 rumpuser_curlwpop(int enum_rumplwpop, struct lwp *l)
312 {
313 	enum rumplwpop op = enum_rumplwpop;
314 
315 	switch (op) {
316 	case RUMPUSER_LWP_CREATE:
317 	case RUMPUSER_LWP_DESTROY:
318 		break;
319 	case RUMPUSER_LWP_SET:
320 		curlwp = l;
321 		break;
322 	case RUMPUSER_LWP_CLEAR:
323 		assert(curlwp == l);
324 		curlwp = NULL;
325 		break;
326 	}
327 }
328 
329 struct lwp *
330 rumpuser_curlwp(void)
331 {
332 
333 	return curlwp;
334 }
335