1 /* $NetBSD: rumpuser_pth_dummy.c,v 1.16 2013/05/15 14:52:49 pooka 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 #include <sys/cdefs.h> 31 #if !defined(lint) 32 __RCSID("$NetBSD: rumpuser_pth_dummy.c,v 1.16 2013/05/15 14:52:49 pooka Exp $"); 33 #endif /* !lint */ 34 35 #include <sys/time.h> 36 37 #include <assert.h> 38 #include <errno.h> 39 #include <stdlib.h> 40 #include <stdio.h> 41 #include <string.h> 42 #include <stdint.h> 43 #include <time.h> 44 45 #include <rump/rumpuser.h> 46 47 #include "rumpuser_int.h" 48 49 static struct lwp *curlwp; 50 51 struct rumpuser_cv {}; 52 53 struct rumpuser_mtx { 54 int v; 55 struct lwp *o; 56 }; 57 58 struct rumpuser_rw { 59 int v; 60 }; 61 62 void 63 rumpuser__thrinit(void) 64 { 65 66 return; 67 } 68 69 /*ARGSUSED*/ 70 int 71 rumpuser_thread_create(void *(*f)(void *), void *arg, const char *thrname, 72 int joinable, int pri, int cpuidx, void **tptr) 73 { 74 75 fprintf(stderr, "rumpuser: threads not available\n"); 76 abort(); 77 return 0; 78 } 79 80 void 81 rumpuser_thread_exit(void) 82 { 83 84 fprintf(stderr, "rumpuser: threads not available\n"); 85 abort(); 86 } 87 88 int 89 rumpuser_thread_join(void *p) 90 { 91 92 return 0; 93 } 94 95 void 96 rumpuser_mutex_init(struct rumpuser_mtx **mtx, int flgas) 97 { 98 99 *mtx = calloc(1, sizeof(struct rumpuser_mtx)); 100 } 101 102 void 103 rumpuser_mutex_enter(struct rumpuser_mtx *mtx) 104 { 105 106 mtx->v++; 107 mtx->o = curlwp; 108 } 109 110 void 111 rumpuser_mutex_enter_nowrap(struct rumpuser_mtx *mtx) 112 { 113 114 rumpuser_mutex_enter(mtx); 115 } 116 117 int 118 rumpuser_mutex_tryenter(struct rumpuser_mtx *mtx) 119 { 120 121 mtx->v++; 122 return 0; 123 } 124 125 void 126 rumpuser_mutex_exit(struct rumpuser_mtx *mtx) 127 { 128 129 assert(mtx->v > 0); 130 if (--mtx->v == 0) 131 mtx->o = NULL; 132 } 133 134 void 135 rumpuser_mutex_destroy(struct rumpuser_mtx *mtx) 136 { 137 138 free(mtx); 139 } 140 141 void 142 rumpuser_mutex_owner(struct rumpuser_mtx *mtx, struct lwp **lp) 143 { 144 145 *lp = mtx->o; 146 } 147 148 void 149 rumpuser_rw_init(struct rumpuser_rw **rw) 150 { 151 152 *rw = calloc(1, sizeof(struct rumpuser_rw)); 153 } 154 155 void 156 rumpuser_rw_enter(int enum_rumprwlock, struct rumpuser_rw *rw) 157 { 158 enum rumprwlock lk = enum_rumprwlock; 159 160 switch (lk) { 161 case RUMPUSER_RW_WRITER: 162 rw->v++; 163 assert(rw->v == 1); 164 break; 165 case RUMPUSER_RW_READER: 166 assert(rw->v <= 0); 167 rw->v--; 168 break; 169 } 170 } 171 172 int 173 rumpuser_rw_tryenter(int enum_rumprwlock, struct rumpuser_rw *rw) 174 { 175 176 rumpuser_rw_enter(enum_rumprwlock, rw); 177 return 0; 178 } 179 180 void 181 rumpuser_rw_exit(struct rumpuser_rw *rw) 182 { 183 184 if (rw->v > 0) { 185 assert(rw->v == 1); 186 rw->v--; 187 } else { 188 rw->v++; 189 } 190 } 191 192 void 193 rumpuser_rw_destroy(struct rumpuser_rw *rw) 194 { 195 196 free(rw); 197 } 198 199 void 200 rumpuser_rw_held(int enum_rumprwlock, struct rumpuser_rw *rw, int *rvp) 201 { 202 enum rumprwlock lk = enum_rumprwlock; 203 204 switch (lk) { 205 case RUMPUSER_RW_WRITER: 206 *rvp = rw->v > 0; 207 break; 208 case RUMPUSER_RW_READER: 209 *rvp = rw->v < 0; 210 break; 211 } 212 } 213 214 void 215 rumpuser_rw_downgrade(struct rumpuser_rw *rw) 216 { 217 218 assert(rw->v == 1); 219 rw->v = -1; 220 } 221 222 int 223 rumpuser_rw_tryupgrade(struct rumpuser_rw *rw) 224 { 225 226 if (rw->v == -1) { 227 rw->v = 1; 228 return 0; 229 } 230 231 return EBUSY; 232 } 233 234 /*ARGSUSED*/ 235 void 236 rumpuser_cv_init(struct rumpuser_cv **cv) 237 { 238 239 } 240 241 /*ARGSUSED*/ 242 void 243 rumpuser_cv_destroy(struct rumpuser_cv *cv) 244 { 245 246 } 247 248 /*ARGSUSED*/ 249 void 250 rumpuser_cv_wait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx) 251 { 252 253 } 254 255 /*ARGSUSED*/ 256 void 257 rumpuser_cv_wait_nowrap(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx) 258 { 259 260 } 261 262 /*ARGSUSED*/ 263 int 264 rumpuser_cv_timedwait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx, 265 int64_t sec, int64_t nsec) 266 { 267 struct timespec ts; 268 269 /*LINTED*/ 270 ts.tv_sec = sec; 271 /*LINTED*/ 272 ts.tv_nsec = nsec; 273 274 nanosleep(&ts, NULL); 275 return 0; 276 } 277 278 /*ARGSUSED*/ 279 void 280 rumpuser_cv_signal(struct rumpuser_cv *cv) 281 { 282 283 } 284 285 /*ARGSUSED*/ 286 void 287 rumpuser_cv_broadcast(struct rumpuser_cv *cv) 288 { 289 290 } 291 292 /*ARGSUSED*/ 293 void 294 rumpuser_cv_has_waiters(struct rumpuser_cv *cv, int *rvp) 295 { 296 297 *rvp = 0; 298 } 299 300 /* 301 * curlwp 302 */ 303 304 void 305 rumpuser_curlwpop(int enum_rumplwpop, struct lwp *l) 306 { 307 enum rumplwpop op = enum_rumplwpop; 308 309 switch (op) { 310 case RUMPUSER_LWP_CREATE: 311 case RUMPUSER_LWP_DESTROY: 312 break; 313 case RUMPUSER_LWP_SET: 314 curlwp = l; 315 break; 316 case RUMPUSER_LWP_CLEAR: 317 assert(curlwp == l); 318 curlwp = NULL; 319 break; 320 } 321 } 322 323 struct lwp * 324 rumpuser_curlwp(void) 325 { 326 327 return curlwp; 328 } 329