Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_pth.c revision 1.19
      1 /*	$NetBSD: rumpuser_pth.c,v 1.19 2013/04/30 00:03:52 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.19 2013/04/30 00:03:52 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 struct rumpuser_mtx {
     51 	pthread_mutex_t pthmtx;
     52 	struct lwp *owner;
     53 	int flags;
     54 };
     55 
     56 #define RURW_AMWRITER(rw) (rw->writer == rumpuser_get_curlwp()		\
     57 				&& rw->readers == -1)
     58 #define RURW_HASREAD(rw)  (rw->readers > 0)
     59 
     60 #define RURW_SETWRITE(rw)						\
     61 do {									\
     62 	assert(rw->readers == 0);					\
     63 	rw->writer = rumpuser_get_curlwp();				\
     64 	rw->readers = -1;						\
     65 } while (/*CONSTCOND*/0)
     66 #define RURW_CLRWRITE(rw)						\
     67 do {									\
     68 	assert(rw->readers == -1 && RURW_AMWRITER(rw));			\
     69 	rw->readers = 0;						\
     70 } while (/*CONSTCOND*/0)
     71 #define RURW_INCREAD(rw)						\
     72 do {									\
     73 	pthread_spin_lock(&rw->spin);					\
     74 	assert(rw->readers >= 0);					\
     75 	++(rw)->readers;						\
     76 	pthread_spin_unlock(&rw->spin);					\
     77 } while (/*CONSTCOND*/0)
     78 #define RURW_DECREAD(rw)						\
     79 do {									\
     80 	pthread_spin_lock(&rw->spin);					\
     81 	assert(rw->readers > 0);					\
     82 	--(rw)->readers;						\
     83 	pthread_spin_unlock(&rw->spin);					\
     84 } while (/*CONSTCOND*/0)
     85 
     86 struct rumpuser_rw {
     87 	pthread_rwlock_t pthrw;
     88 	pthread_spinlock_t spin;
     89 	int readers;
     90 	struct lwp *writer;
     91 };
     92 
     93 struct rumpuser_cv {
     94 	pthread_cond_t pthcv;
     95 	int nwaiters;
     96 };
     97 
     98 void
     99 rumpuser__thrinit(void)
    100 {
    101 
    102 	pthread_key_create(&curlwpkey, NULL);
    103 }
    104 
    105 int
    106 rumpuser_thread_create(void *(*f)(void *), void *arg, const char *thrname,
    107 	int joinable, void **ptcookie)
    108 {
    109 	pthread_t ptid;
    110 	pthread_t *ptidp;
    111 	pthread_attr_t pattr;
    112 	int rv;
    113 
    114 	if ((rv = pthread_attr_init(&pattr)) != 0)
    115 		return rv;
    116 
    117 	if (joinable) {
    118 		NOFAIL(ptidp = malloc(sizeof(*ptidp)));
    119 		pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE);
    120 	} else {
    121 		ptidp = &ptid;
    122 		pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED);
    123 	}
    124 
    125 	rv = pthread_create(ptidp, &pattr, f, arg);
    126 #if defined(__NetBSD__)
    127 	if (rv == 0 && thrname)
    128 		pthread_setname_np(ptid, thrname, NULL);
    129 #elif defined(__linux__)
    130 	/*
    131 	 * The pthread_setname_np() call varies from one Linux distro to
    132 	 * another.  Comment out the call pending autoconf support.
    133 	 */
    134 #if 0
    135 	if (rv == 0 && thrname)
    136 		pthread_setname_np(ptid, thrname);
    137 #endif
    138 #endif
    139 
    140 	if (joinable) {
    141 		assert(ptcookie);
    142 		*ptcookie = ptidp;
    143 	}
    144 
    145 	pthread_attr_destroy(&pattr);
    146 
    147 	return rv;
    148 }
    149 
    150 __dead void
    151 rumpuser_thread_exit(void)
    152 {
    153 
    154 	pthread_exit(NULL);
    155 }
    156 
    157 int
    158 rumpuser_thread_join(void *ptcookie)
    159 {
    160 	pthread_t *pt = ptcookie;
    161 	int rv;
    162 
    163 	KLOCK_WRAP((rv = pthread_join(*pt, NULL)));
    164 	if (rv == 0)
    165 		free(pt);
    166 
    167 	return rv;
    168 }
    169 
    170 void
    171 rumpuser_mutex_init(struct rumpuser_mtx **mtx, int flags)
    172 {
    173 	pthread_mutexattr_t att;
    174 
    175 	NOFAIL(*mtx = malloc(sizeof(struct rumpuser_mtx)));
    176 
    177 	pthread_mutexattr_init(&att);
    178 	pthread_mutexattr_settype(&att, PTHREAD_MUTEX_ERRORCHECK);
    179 	NOFAIL_ERRNO(pthread_mutex_init(&((*mtx)->pthmtx), &att));
    180 	pthread_mutexattr_destroy(&att);
    181 
    182 	(*mtx)->owner = NULL;
    183 	assert(flags != 0);
    184 	(*mtx)->flags = flags;
    185 }
    186 
    187 static void
    188 mtxenter(struct rumpuser_mtx *mtx)
    189 {
    190 
    191 	if (!(mtx->flags & RUMPUSER_MTX_KMUTEX))
    192 		return;
    193 
    194 	assert(mtx->owner == NULL);
    195 	mtx->owner = rumpuser_get_curlwp();
    196 }
    197 
    198 static void
    199 mtxexit(struct rumpuser_mtx *mtx)
    200 {
    201 
    202 	if (!(mtx->flags & RUMPUSER_MTX_KMUTEX))
    203 		return;
    204 
    205 	assert(mtx->owner != NULL);
    206 	mtx->owner = NULL;
    207 }
    208 
    209 void
    210 rumpuser_mutex_enter(struct rumpuser_mtx *mtx)
    211 {
    212 
    213 	if (mtx->flags & RUMPUSER_MTX_SPIN) {
    214 		rumpuser_mutex_enter_nowrap(mtx);
    215 		return;
    216 	}
    217 
    218 	assert(mtx->flags & RUMPUSER_MTX_KMUTEX);
    219 	if (pthread_mutex_trylock(&mtx->pthmtx) != 0)
    220 		KLOCK_WRAP(NOFAIL_ERRNO(pthread_mutex_lock(&mtx->pthmtx)));
    221 	mtxenter(mtx);
    222 }
    223 
    224 void
    225 rumpuser_mutex_enter_nowrap(struct rumpuser_mtx *mtx)
    226 {
    227 
    228 	assert(mtx->flags & RUMPUSER_MTX_SPIN);
    229 	NOFAIL_ERRNO(pthread_mutex_lock(&mtx->pthmtx));
    230 	mtxenter(mtx);
    231 }
    232 
    233 int
    234 rumpuser_mutex_tryenter(struct rumpuser_mtx *mtx)
    235 {
    236 	int rv;
    237 
    238 	rv = pthread_mutex_trylock(&mtx->pthmtx);
    239 	if (rv == 0) {
    240 		mtxenter(mtx);
    241 	}
    242 
    243 	return rv;
    244 }
    245 
    246 void
    247 rumpuser_mutex_exit(struct rumpuser_mtx *mtx)
    248 {
    249 
    250 	mtxexit(mtx);
    251 	NOFAIL_ERRNO(pthread_mutex_unlock(&mtx->pthmtx));
    252 }
    253 
    254 void
    255 rumpuser_mutex_destroy(struct rumpuser_mtx *mtx)
    256 {
    257 
    258 	NOFAIL_ERRNO(pthread_mutex_destroy(&mtx->pthmtx));
    259 	free(mtx);
    260 }
    261 
    262 void
    263 rumpuser_mutex_owner(struct rumpuser_mtx *mtx, struct lwp **lp)
    264 {
    265 
    266 	if (__predict_false(!(mtx->flags & RUMPUSER_MTX_KMUTEX))) {
    267 		printf("panic: rumpuser_mutex_held unsupported on non-kmtx\n");
    268 		abort();
    269 	}
    270 
    271 	*lp = mtx->owner;
    272 }
    273 
    274 void
    275 rumpuser_rw_init(struct rumpuser_rw **rw)
    276 {
    277 
    278 	NOFAIL(*rw = malloc(sizeof(struct rumpuser_rw)));
    279 	NOFAIL_ERRNO(pthread_rwlock_init(&((*rw)->pthrw), NULL));
    280 	NOFAIL_ERRNO(pthread_spin_init(&((*rw)->spin),PTHREAD_PROCESS_PRIVATE));
    281 	(*rw)->readers = 0;
    282 	(*rw)->writer = NULL;
    283 }
    284 
    285 void
    286 rumpuser_rw_enter(struct rumpuser_rw *rw, int iswrite)
    287 {
    288 
    289 	if (iswrite) {
    290 		if (pthread_rwlock_trywrlock(&rw->pthrw) != 0)
    291 			KLOCK_WRAP(NOFAIL_ERRNO(
    292 			    pthread_rwlock_wrlock(&rw->pthrw)));
    293 		RURW_SETWRITE(rw);
    294 	} else {
    295 		if (pthread_rwlock_tryrdlock(&rw->pthrw) != 0)
    296 			KLOCK_WRAP(NOFAIL_ERRNO(
    297 			    pthread_rwlock_rdlock(&rw->pthrw)));
    298 		RURW_INCREAD(rw);
    299 	}
    300 }
    301 
    302 int
    303 rumpuser_rw_tryenter(struct rumpuser_rw *rw, int iswrite)
    304 {
    305 	int rv;
    306 
    307 	if (iswrite) {
    308 		rv = pthread_rwlock_trywrlock(&rw->pthrw);
    309 		if (rv == 0)
    310 			RURW_SETWRITE(rw);
    311 	} else {
    312 		rv = pthread_rwlock_tryrdlock(&rw->pthrw);
    313 		if (rv == 0)
    314 			RURW_INCREAD(rw);
    315 	}
    316 
    317 	return rv;
    318 }
    319 
    320 void
    321 rumpuser_rw_exit(struct rumpuser_rw *rw)
    322 {
    323 
    324 	if (RURW_HASREAD(rw))
    325 		RURW_DECREAD(rw);
    326 	else
    327 		RURW_CLRWRITE(rw);
    328 	NOFAIL_ERRNO(pthread_rwlock_unlock(&rw->pthrw));
    329 }
    330 
    331 void
    332 rumpuser_rw_destroy(struct rumpuser_rw *rw)
    333 {
    334 
    335 	NOFAIL_ERRNO(pthread_rwlock_destroy(&rw->pthrw));
    336 	NOFAIL_ERRNO(pthread_spin_destroy(&rw->spin));
    337 	free(rw);
    338 }
    339 
    340 void
    341 rumpuser_rw_held(struct rumpuser_rw *rw, int *rv)
    342 {
    343 
    344 	*rv = rw->readers != 0;
    345 }
    346 
    347 void
    348 rumpuser_rw_rdheld(struct rumpuser_rw *rw, int *rv)
    349 {
    350 
    351 	*rv = RURW_HASREAD(rw);
    352 }
    353 
    354 void
    355 rumpuser_rw_wrheld(struct rumpuser_rw *rw, int *rv)
    356 {
    357 
    358 	*rv = RURW_AMWRITER(rw);
    359 }
    360 
    361 void
    362 rumpuser_cv_init(struct rumpuser_cv **cv)
    363 {
    364 
    365 	NOFAIL(*cv = malloc(sizeof(struct rumpuser_cv)));
    366 	NOFAIL_ERRNO(pthread_cond_init(&((*cv)->pthcv), NULL));
    367 	(*cv)->nwaiters = 0;
    368 }
    369 
    370 void
    371 rumpuser_cv_destroy(struct rumpuser_cv *cv)
    372 {
    373 
    374 	NOFAIL_ERRNO(pthread_cond_destroy(&cv->pthcv));
    375 	free(cv);
    376 }
    377 
    378 void
    379 rumpuser_cv_wait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx)
    380 {
    381 	int nlocks;
    382 
    383 	cv->nwaiters++;
    384 	rumpkern_unsched(&nlocks, mtx);
    385 	mtxexit(mtx);
    386 	NOFAIL_ERRNO(pthread_cond_wait(&cv->pthcv, &mtx->pthmtx));
    387 	mtxenter(mtx);
    388 	rumpkern_sched(nlocks, mtx);
    389 	cv->nwaiters--;
    390 }
    391 
    392 void
    393 rumpuser_cv_wait_nowrap(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx)
    394 {
    395 
    396 	cv->nwaiters++;
    397 	mtxexit(mtx);
    398 	NOFAIL_ERRNO(pthread_cond_wait(&cv->pthcv, &mtx->pthmtx));
    399 	mtxenter(mtx);
    400 	cv->nwaiters--;
    401 }
    402 
    403 int
    404 rumpuser_cv_timedwait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx,
    405 	int64_t sec, int64_t nsec)
    406 {
    407 	struct timespec ts;
    408 	int rv, nlocks;
    409 
    410 	/*
    411 	 * Get clock already here, just in case we will be put to sleep
    412 	 * after releasing the kernel context.
    413 	 *
    414 	 * The condition variables should use CLOCK_MONOTONIC, but since
    415 	 * that's not available everywhere, leave it for another day.
    416 	 */
    417 	clock_gettime(CLOCK_REALTIME, &ts);
    418 
    419 	cv->nwaiters++;
    420 	rumpkern_unsched(&nlocks, mtx);
    421 	mtxexit(mtx);
    422 
    423 	ts.tv_sec += sec;
    424 	ts.tv_nsec += nsec;
    425 	if (ts.tv_nsec >= 1000*1000*1000) {
    426 		ts.tv_sec++;
    427 		ts.tv_nsec -= 1000*1000*1000;
    428 	}
    429 	rv = pthread_cond_timedwait(&cv->pthcv, &mtx->pthmtx, &ts);
    430 	mtxenter(mtx);
    431 	rumpkern_sched(nlocks, mtx);
    432 	cv->nwaiters--;
    433 
    434 	return rv;
    435 }
    436 
    437 void
    438 rumpuser_cv_signal(struct rumpuser_cv *cv)
    439 {
    440 
    441 	NOFAIL_ERRNO(pthread_cond_signal(&cv->pthcv));
    442 }
    443 
    444 void
    445 rumpuser_cv_broadcast(struct rumpuser_cv *cv)
    446 {
    447 
    448 	NOFAIL_ERRNO(pthread_cond_broadcast(&cv->pthcv));
    449 }
    450 
    451 void
    452 rumpuser_cv_has_waiters(struct rumpuser_cv *cv, int *nwaiters)
    453 {
    454 
    455 	*nwaiters = cv->nwaiters;
    456 }
    457 
    458 /*
    459  * curlwp
    460  */
    461 
    462 void
    463 rumpuser_set_curlwp(struct lwp *l)
    464 {
    465 
    466 	assert(pthread_getspecific(curlwpkey) == NULL || l == NULL);
    467 	pthread_setspecific(curlwpkey, l);
    468 }
    469 
    470 struct lwp *
    471 rumpuser_get_curlwp(void)
    472 {
    473 
    474 	return pthread_getspecific(curlwpkey);
    475 }
    476