Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_pth.c revision 1.9
      1 /*	$NetBSD: rumpuser_pth.c,v 1.9 2012/10/08 18:02:04 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007-2010 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.c,v 1.9 2012/10/08 18:02:04 pooka Exp $");
     32 #endif /* !lint */
     33 
     34 #include <assert.h>
     35 #include <errno.h>
     36 #include <fcntl.h>
     37 #include <pthread.h>
     38 #include <stdlib.h>
     39 #include <stdio.h>
     40 #include <string.h>
     41 #include <stdint.h>
     42 #include <unistd.h>
     43 
     44 #include <rump/rumpuser.h>
     45 
     46 #include "rumpuser_int.h"
     47 
     48 static pthread_key_t curlwpkey;
     49 
     50 #define NOFAIL(a) do {if (!(a)) abort();} while (/*CONSTCOND*/0)
     51 #define NOFAIL_ERRNO(a)							\
     52 do {									\
     53 	int fail_rv = (a);						\
     54 	if (fail_rv) {							\
     55 		printf("panic: rumpuser fatal failure %d (%s)\n",	\
     56 		    fail_rv, strerror(fail_rv));			\
     57 		abort();						\
     58 	}								\
     59 } while (/*CONSTCOND*/0)
     60 
     61 struct rumpuser_mtx {
     62 	pthread_mutex_t pthmtx;
     63 	struct lwp *owner;
     64 	int iskmutex;
     65 };
     66 
     67 #define RURW_AMWRITER(rw) (rw->writer == rumpuser_get_curlwp()		\
     68 				&& rw->readers == -1)
     69 #define RURW_HASREAD(rw)  (rw->readers > 0)
     70 
     71 #define RURW_SETWRITE(rw)						\
     72 do {									\
     73 	assert(rw->readers == 0);					\
     74 	rw->writer = rumpuser_get_curlwp();				\
     75 	rw->readers = -1;						\
     76 } while (/*CONSTCOND*/0)
     77 #define RURW_CLRWRITE(rw)						\
     78 do {									\
     79 	assert(rw->readers == -1 && RURW_AMWRITER(rw));			\
     80 	rw->readers = 0;						\
     81 } while (/*CONSTCOND*/0)
     82 #define RURW_INCREAD(rw)						\
     83 do {									\
     84 	pthread_spin_lock(&rw->spin);					\
     85 	assert(rw->readers >= 0);					\
     86 	++(rw)->readers;						\
     87 	pthread_spin_unlock(&rw->spin);					\
     88 } while (/*CONSTCOND*/0)
     89 #define RURW_DECREAD(rw)						\
     90 do {									\
     91 	pthread_spin_lock(&rw->spin);					\
     92 	assert(rw->readers > 0);					\
     93 	--(rw)->readers;						\
     94 	pthread_spin_unlock(&rw->spin);					\
     95 } while (/*CONSTCOND*/0)
     96 
     97 struct rumpuser_rw {
     98 	pthread_rwlock_t pthrw;
     99 	pthread_spinlock_t spin;
    100 	int readers;
    101 	struct lwp *writer;
    102 };
    103 
    104 struct rumpuser_cv {
    105 	pthread_cond_t pthcv;
    106 	int nwaiters;
    107 };
    108 
    109 struct rumpuser_mtx rumpuser_aio_mtx;
    110 struct rumpuser_cv rumpuser_aio_cv;
    111 int rumpuser_aio_head, rumpuser_aio_tail;
    112 struct rumpuser_aio rumpuser_aios[N_AIOS];
    113 
    114 kernel_lockfn	rumpuser__klock;
    115 kernel_unlockfn	rumpuser__kunlock;
    116 int		rumpuser__wantthreads;
    117 
    118 void
    119 /*ARGSUSED*/
    120 rumpuser_biothread(void *arg)
    121 {
    122 	struct rumpuser_aio *rua;
    123 	rump_biodone_fn biodone = arg;
    124 	ssize_t rv;
    125 	int error, dummy;
    126 
    127 	/* unschedule from CPU.  we reschedule before running the interrupt */
    128 	rumpuser__kunlock(0, &dummy, NULL);
    129 	assert(dummy == 0);
    130 
    131 	NOFAIL_ERRNO(pthread_mutex_lock(&rumpuser_aio_mtx.pthmtx));
    132 	for (;;) {
    133 		while (rumpuser_aio_head == rumpuser_aio_tail) {
    134 			NOFAIL_ERRNO(pthread_cond_wait(&rumpuser_aio_cv.pthcv,
    135 			    &rumpuser_aio_mtx.pthmtx));
    136 		}
    137 
    138 		rua = &rumpuser_aios[rumpuser_aio_tail];
    139 		assert(rua->rua_bp != NULL);
    140 		pthread_mutex_unlock(&rumpuser_aio_mtx.pthmtx);
    141 
    142 		if (rua->rua_op & RUA_OP_READ) {
    143 			error = 0;
    144 			rv = pread(rua->rua_fd, rua->rua_data,
    145 			    rua->rua_dlen, rua->rua_off);
    146 			if (rv < 0) {
    147 				rv = 0;
    148 				error = errno;
    149 			}
    150 		} else {
    151 			error = 0;
    152 			rv = pwrite(rua->rua_fd, rua->rua_data,
    153 			    rua->rua_dlen, rua->rua_off);
    154 			if (rv < 0) {
    155 				rv = 0;
    156 				error = errno;
    157 			} else if (rua->rua_op & RUA_OP_SYNC) {
    158 #ifdef __NetBSD__
    159 				fsync_range(rua->rua_fd, FDATASYNC,
    160 				    rua->rua_off, rua->rua_dlen);
    161 #else
    162 				fsync(rua->rua_fd);
    163 #endif
    164 			}
    165 		}
    166 		rumpuser__klock(0, NULL);
    167 		biodone(rua->rua_bp, (size_t)rv, error);
    168 		rumpuser__kunlock(0, &dummy, NULL);
    169 
    170 		rua->rua_bp = NULL;
    171 
    172 		NOFAIL_ERRNO(pthread_mutex_lock(&rumpuser_aio_mtx.pthmtx));
    173 		rumpuser_aio_tail = (rumpuser_aio_tail+1) % N_AIOS;
    174 		pthread_cond_signal(&rumpuser_aio_cv.pthcv);
    175 	}
    176 
    177 	/*NOTREACHED*/
    178 	fprintf(stderr, "error: rumpuser_biothread reached unreachable\n");
    179 	abort();
    180 }
    181 
    182 void
    183 rumpuser_thrinit(kernel_lockfn lockfn, kernel_unlockfn unlockfn, int threads)
    184 {
    185 #ifdef __linux__
    186 	/* XXX: there's no rumpuser_bootstrap, so do this here */
    187 	uint32_t rv;
    188 	int fd;
    189 
    190 	if ((fd = open("/dev/urandom", O_RDONLY)) == -1) {
    191 		srandom(time(NULL));
    192 	} else {
    193 		if (read(fd, &rv, sizeof(rv)) != sizeof(rv))
    194 			srandom(time(NULL));
    195 		else
    196 			srandom(rv);
    197 		close(fd);
    198 	}
    199 #endif
    200 
    201 	pthread_mutex_init(&rumpuser_aio_mtx.pthmtx, NULL);
    202 	pthread_cond_init(&rumpuser_aio_cv.pthcv, NULL);
    203 
    204 	pthread_key_create(&curlwpkey, NULL);
    205 
    206 	rumpuser__klock = lockfn;
    207 	rumpuser__kunlock = unlockfn;
    208 	rumpuser__wantthreads = threads;
    209 }
    210 
    211 #if 0
    212 void
    213 rumpuser__thrdestroy(void)
    214 {
    215 
    216 	pthread_key_delete(curlwpkey);
    217 }
    218 #endif
    219 
    220 int
    221 rumpuser_thread_create(void *(*f)(void *), void *arg, const char *thrname,
    222 	int joinable, void **ptcookie)
    223 {
    224 	pthread_t ptid;
    225 	pthread_t *ptidp;
    226 	pthread_attr_t pattr;
    227 	int rv;
    228 
    229 	if ((rv = pthread_attr_init(&pattr)) != 0)
    230 		return rv;
    231 
    232 	if (joinable) {
    233 		NOFAIL(ptidp = malloc(sizeof(*ptidp)));
    234 		pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE);
    235 	} else {
    236 		ptidp = &ptid;
    237 		pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED);
    238 	}
    239 
    240 	rv = pthread_create(ptidp, &pattr, f, arg);
    241 #if defined(__NetBSD__)
    242 	if (rv == 0 && thrname)
    243 		pthread_setname_np(ptid, thrname, NULL);
    244 #elif defined(__linux__)
    245 	if (rv == 0 && thrname)
    246 		pthread_setname_np(ptid, thrname);
    247 #endif
    248 
    249 	if (joinable) {
    250 		assert(ptcookie);
    251 		*ptcookie = ptidp;
    252 	}
    253 
    254 	pthread_attr_destroy(&pattr);
    255 
    256 	return rv;
    257 }
    258 
    259 __dead void
    260 rumpuser_thread_exit(void)
    261 {
    262 
    263 	pthread_exit(NULL);
    264 }
    265 
    266 int
    267 rumpuser_thread_join(void *ptcookie)
    268 {
    269 	pthread_t *pt = ptcookie;
    270 	int rv;
    271 
    272 	KLOCK_WRAP((rv = pthread_join(*pt, NULL)));
    273 	if (rv == 0)
    274 		free(pt);
    275 
    276 	return rv;
    277 }
    278 
    279 void
    280 rumpuser_mutex_init(struct rumpuser_mtx **mtx)
    281 {
    282 	pthread_mutexattr_t att;
    283 
    284 	NOFAIL(*mtx = malloc(sizeof(struct rumpuser_mtx)));
    285 
    286 	pthread_mutexattr_init(&att);
    287 	pthread_mutexattr_settype(&att, PTHREAD_MUTEX_ERRORCHECK);
    288 	NOFAIL_ERRNO(pthread_mutex_init(&((*mtx)->pthmtx), &att));
    289 	pthread_mutexattr_destroy(&att);
    290 
    291 	(*mtx)->owner = NULL;
    292 	(*mtx)->iskmutex = 0;
    293 }
    294 
    295 void
    296 rumpuser_mutex_init_kmutex(struct rumpuser_mtx **mtx)
    297 {
    298 
    299 	rumpuser_mutex_init(mtx);
    300 	(*mtx)->iskmutex = 1;
    301 }
    302 
    303 static void
    304 mtxenter(struct rumpuser_mtx *mtx)
    305 {
    306 
    307 	if (!mtx->iskmutex)
    308 		return;
    309 
    310 	assert(mtx->owner == NULL);
    311 	mtx->owner = rumpuser_get_curlwp();
    312 }
    313 
    314 static void
    315 mtxexit(struct rumpuser_mtx *mtx)
    316 {
    317 
    318 	if (!mtx->iskmutex)
    319 		return;
    320 
    321 	assert(mtx->owner != NULL);
    322 	mtx->owner = NULL;
    323 }
    324 
    325 void
    326 rumpuser_mutex_enter(struct rumpuser_mtx *mtx)
    327 {
    328 
    329 	if (pthread_mutex_trylock(&mtx->pthmtx) != 0)
    330 		KLOCK_WRAP(NOFAIL_ERRNO(pthread_mutex_lock(&mtx->pthmtx)));
    331 	mtxenter(mtx);
    332 }
    333 
    334 void
    335 rumpuser_mutex_enter_nowrap(struct rumpuser_mtx *mtx)
    336 {
    337 
    338 	NOFAIL_ERRNO(pthread_mutex_lock(&mtx->pthmtx));
    339 	mtxenter(mtx);
    340 }
    341 
    342 int
    343 rumpuser_mutex_tryenter(struct rumpuser_mtx *mtx)
    344 {
    345 	int rv;
    346 
    347 	rv = pthread_mutex_trylock(&mtx->pthmtx);
    348 	if (rv == 0) {
    349 		mtxenter(mtx);
    350 	}
    351 
    352 	return rv == 0;
    353 }
    354 
    355 void
    356 rumpuser_mutex_exit(struct rumpuser_mtx *mtx)
    357 {
    358 
    359 	mtxexit(mtx);
    360 	NOFAIL_ERRNO(pthread_mutex_unlock(&mtx->pthmtx));
    361 }
    362 
    363 void
    364 rumpuser_mutex_destroy(struct rumpuser_mtx *mtx)
    365 {
    366 
    367 	NOFAIL_ERRNO(pthread_mutex_destroy(&mtx->pthmtx));
    368 	free(mtx);
    369 }
    370 
    371 struct lwp *
    372 rumpuser_mutex_owner(struct rumpuser_mtx *mtx)
    373 {
    374 
    375 	if (__predict_false(!mtx->iskmutex)) {
    376 		printf("panic: rumpuser_mutex_held unsupported on non-kmtx\n");
    377 		abort();
    378 	}
    379 
    380 	return mtx->owner;
    381 }
    382 
    383 void
    384 rumpuser_rw_init(struct rumpuser_rw **rw)
    385 {
    386 
    387 	NOFAIL(*rw = malloc(sizeof(struct rumpuser_rw)));
    388 	NOFAIL_ERRNO(pthread_rwlock_init(&((*rw)->pthrw), NULL));
    389 	NOFAIL_ERRNO(pthread_spin_init(&((*rw)->spin), PTHREAD_PROCESS_SHARED));
    390 	(*rw)->readers = 0;
    391 	(*rw)->writer = NULL;
    392 }
    393 
    394 void
    395 rumpuser_rw_enter(struct rumpuser_rw *rw, int iswrite)
    396 {
    397 
    398 	if (iswrite) {
    399 		if (pthread_rwlock_trywrlock(&rw->pthrw) != 0)
    400 			KLOCK_WRAP(NOFAIL_ERRNO(
    401 			    pthread_rwlock_wrlock(&rw->pthrw)));
    402 		RURW_SETWRITE(rw);
    403 	} else {
    404 		if (pthread_rwlock_tryrdlock(&rw->pthrw) != 0)
    405 			KLOCK_WRAP(NOFAIL_ERRNO(
    406 			    pthread_rwlock_rdlock(&rw->pthrw)));
    407 		RURW_INCREAD(rw);
    408 	}
    409 }
    410 
    411 int
    412 rumpuser_rw_tryenter(struct rumpuser_rw *rw, int iswrite)
    413 {
    414 	int rv;
    415 
    416 	if (iswrite) {
    417 		rv = pthread_rwlock_trywrlock(&rw->pthrw);
    418 		if (rv == 0)
    419 			RURW_SETWRITE(rw);
    420 	} else {
    421 		rv = pthread_rwlock_tryrdlock(&rw->pthrw);
    422 		if (rv == 0)
    423 			RURW_INCREAD(rw);
    424 	}
    425 
    426 	return rv == 0;
    427 }
    428 
    429 void
    430 rumpuser_rw_exit(struct rumpuser_rw *rw)
    431 {
    432 
    433 	if (RURW_HASREAD(rw))
    434 		RURW_DECREAD(rw);
    435 	else
    436 		RURW_CLRWRITE(rw);
    437 	NOFAIL_ERRNO(pthread_rwlock_unlock(&rw->pthrw));
    438 }
    439 
    440 void
    441 rumpuser_rw_destroy(struct rumpuser_rw *rw)
    442 {
    443 
    444 	NOFAIL_ERRNO(pthread_rwlock_destroy(&rw->pthrw));
    445 	NOFAIL_ERRNO(pthread_spin_destroy(&rw->spin));
    446 	free(rw);
    447 }
    448 
    449 int
    450 rumpuser_rw_held(struct rumpuser_rw *rw)
    451 {
    452 
    453 	return rw->readers != 0;
    454 }
    455 
    456 int
    457 rumpuser_rw_rdheld(struct rumpuser_rw *rw)
    458 {
    459 
    460 	return RURW_HASREAD(rw);
    461 }
    462 
    463 int
    464 rumpuser_rw_wrheld(struct rumpuser_rw *rw)
    465 {
    466 
    467 	return RURW_AMWRITER(rw);
    468 }
    469 
    470 void
    471 rumpuser_cv_init(struct rumpuser_cv **cv)
    472 {
    473 
    474 	NOFAIL(*cv = malloc(sizeof(struct rumpuser_cv)));
    475 	NOFAIL_ERRNO(pthread_cond_init(&((*cv)->pthcv), NULL));
    476 	(*cv)->nwaiters = 0;
    477 }
    478 
    479 void
    480 rumpuser_cv_destroy(struct rumpuser_cv *cv)
    481 {
    482 
    483 	NOFAIL_ERRNO(pthread_cond_destroy(&cv->pthcv));
    484 	free(cv);
    485 }
    486 
    487 void
    488 rumpuser_cv_wait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx)
    489 {
    490 	int nlocks;
    491 
    492 	cv->nwaiters++;
    493 	rumpuser__kunlock(0, &nlocks, mtx);
    494 	mtxexit(mtx);
    495 	NOFAIL_ERRNO(pthread_cond_wait(&cv->pthcv, &mtx->pthmtx));
    496 	mtxenter(mtx);
    497 	rumpuser__klock(nlocks, mtx);
    498 	cv->nwaiters--;
    499 }
    500 
    501 void
    502 rumpuser_cv_wait_nowrap(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx)
    503 {
    504 
    505 	cv->nwaiters++;
    506 	mtxexit(mtx);
    507 	NOFAIL_ERRNO(pthread_cond_wait(&cv->pthcv, &mtx->pthmtx));
    508 	mtxenter(mtx);
    509 	cv->nwaiters--;
    510 }
    511 
    512 int
    513 rumpuser_cv_timedwait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx,
    514 	int64_t sec, int64_t nsec)
    515 {
    516 	struct timespec ts;
    517 	int rv, nlocks;
    518 
    519 	/* LINTED */
    520 	ts.tv_sec = sec; ts.tv_nsec = nsec;
    521 
    522 	cv->nwaiters++;
    523 	rumpuser__kunlock(0, &nlocks, mtx);
    524 	mtxexit(mtx);
    525 	rv = pthread_cond_timedwait(&cv->pthcv, &mtx->pthmtx, &ts);
    526 	mtxenter(mtx);
    527 	rumpuser__klock(nlocks, mtx);
    528 	cv->nwaiters--;
    529 	if (rv != 0 && rv != ETIMEDOUT)
    530 		abort();
    531 
    532 	return rv == ETIMEDOUT;
    533 }
    534 
    535 void
    536 rumpuser_cv_signal(struct rumpuser_cv *cv)
    537 {
    538 
    539 	NOFAIL_ERRNO(pthread_cond_signal(&cv->pthcv));
    540 }
    541 
    542 void
    543 rumpuser_cv_broadcast(struct rumpuser_cv *cv)
    544 {
    545 
    546 	NOFAIL_ERRNO(pthread_cond_broadcast(&cv->pthcv));
    547 }
    548 
    549 int
    550 rumpuser_cv_has_waiters(struct rumpuser_cv *cv)
    551 {
    552 
    553 	return cv->nwaiters;
    554 }
    555 
    556 /*
    557  * curlwp
    558  */
    559 
    560 void
    561 rumpuser_set_curlwp(struct lwp *l)
    562 {
    563 
    564 	assert(pthread_getspecific(curlwpkey) == NULL || l == NULL);
    565 	pthread_setspecific(curlwpkey, l);
    566 }
    567 
    568 struct lwp *
    569 rumpuser_get_curlwp(void)
    570 {
    571 
    572 	return pthread_getspecific(curlwpkey);
    573 }
    574