1 /* $NetBSD: rumpuser_pth_dummy.c,v 1.17 2014/06/17 06:43:21 alnsn 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.17 2014/06/17 06:43:21 alnsn 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 void 102 rumpuser_mutex_enter(struct rumpuser_mtx *mtx) 103 { 104 105 mtx->v++; 106 mtx->o = curlwp; 107 } 108 109 void 110 rumpuser_mutex_enter_nowrap(struct rumpuser_mtx *mtx) 111 { 112 113 rumpuser_mutex_enter(mtx); 114 } 115 116 int 117 rumpuser_mutex_tryenter(struct rumpuser_mtx *mtx) 118 { 119 120 mtx->v++; 121 return 0; 122 } 123 124 void 125 rumpuser_mutex_exit(struct rumpuser_mtx *mtx) 126 { 127 128 assert(mtx->v > 0); 129 if (--mtx->v == 0) 130 mtx->o = NULL; 131 } 132 133 void 134 rumpuser_mutex_destroy(struct rumpuser_mtx *mtx) 135 { 136 137 free(mtx); 138 } 139 140 void 141 rumpuser_mutex_owner(struct rumpuser_mtx *mtx, struct lwp **lp) 142 { 143 144 *lp = mtx->o; 145 } 146 147 void 148 rumpuser_rw_init(struct rumpuser_rw **rw) 149 { 150 151 *rw = calloc(1, sizeof(struct rumpuser_rw)); 152 } 153 154 void 155 rumpuser_rw_enter(int enum_rumprwlock, struct rumpuser_rw *rw) 156 { 157 enum rumprwlock lk = enum_rumprwlock; 158 159 switch (lk) { 160 case RUMPUSER_RW_WRITER: 161 rw->v++; 162 assert(rw->v == 1); 163 break; 164 case RUMPUSER_RW_READER: 165 assert(rw->v <= 0); 166 rw->v--; 167 break; 168 } 169 } 170 171 int 172 rumpuser_rw_tryenter(int enum_rumprwlock, struct rumpuser_rw *rw) 173 { 174 175 rumpuser_rw_enter(enum_rumprwlock, rw); 176 return 0; 177 } 178 179 void 180 rumpuser_rw_exit(struct rumpuser_rw *rw) 181 { 182 183 if (rw->v > 0) { 184 assert(rw->v == 1); 185 rw->v--; 186 } else { 187 rw->v++; 188 } 189 } 190 191 void 192 rumpuser_rw_destroy(struct rumpuser_rw *rw) 193 { 194 195 free(rw); 196 } 197 198 void 199 rumpuser_rw_held(int enum_rumprwlock, struct rumpuser_rw *rw, int *rvp) 200 { 201 enum rumprwlock lk = enum_rumprwlock; 202 203 switch (lk) { 204 case RUMPUSER_RW_WRITER: 205 *rvp = rw->v > 0; 206 break; 207 case RUMPUSER_RW_READER: 208 *rvp = rw->v < 0; 209 break; 210 } 211 } 212 213 void 214 rumpuser_rw_downgrade(struct rumpuser_rw *rw) 215 { 216 217 assert(rw->v == 1); 218 rw->v = -1; 219 } 220 221 int 222 rumpuser_rw_tryupgrade(struct rumpuser_rw *rw) 223 { 224 225 if (rw->v == -1) { 226 rw->v = 1; 227 return 0; 228 } 229 230 return EBUSY; 231 } 232 233 /*ARGSUSED*/ 234 void 235 rumpuser_cv_init(struct rumpuser_cv **cv) 236 { 237 238 } 239 240 /*ARGSUSED*/ 241 void 242 rumpuser_cv_destroy(struct rumpuser_cv *cv) 243 { 244 245 } 246 247 /*ARGSUSED*/ 248 void 249 rumpuser_cv_wait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx) 250 { 251 252 } 253 254 /*ARGSUSED*/ 255 void 256 rumpuser_cv_wait_nowrap(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx) 257 { 258 259 } 260 261 /*ARGSUSED*/ 262 int 263 rumpuser_cv_timedwait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx, 264 int64_t sec, int64_t nsec) 265 { 266 struct timespec ts; 267 268 /*LINTED*/ 269 ts.tv_sec = sec; 270 /*LINTED*/ 271 ts.tv_nsec = nsec; 272 273 nanosleep(&ts, NULL); 274 return 0; 275 } 276 277 /*ARGSUSED*/ 278 void 279 rumpuser_cv_signal(struct rumpuser_cv *cv) 280 { 281 282 } 283 284 /*ARGSUSED*/ 285 void 286 rumpuser_cv_broadcast(struct rumpuser_cv *cv) 287 { 288 289 } 290 291 /*ARGSUSED*/ 292 void 293 rumpuser_cv_has_waiters(struct rumpuser_cv *cv, int *rvp) 294 { 295 296 *rvp = 0; 297 } 298 299 /* 300 * curlwp 301 */ 302 303 void 304 rumpuser_curlwpop(int enum_rumplwpop, struct lwp *l) 305 { 306 enum rumplwpop op = enum_rumplwpop; 307 308 switch (op) { 309 case RUMPUSER_LWP_CREATE: 310 case RUMPUSER_LWP_DESTROY: 311 break; 312 case RUMPUSER_LWP_SET: 313 curlwp = l; 314 break; 315 case RUMPUSER_LWP_CLEAR: 316 assert(curlwp == l); 317 curlwp = NULL; 318 break; 319 } 320 } 321 322 struct lwp * 323 rumpuser_curlwp(void) 324 { 325 326 return curlwp; 327 } 328