Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_pth.c revision 1.31
      1  1.31  pooka /*	$NetBSD: rumpuser_pth.c,v 1.31 2013/09/23 10:35:20 pooka Exp $	*/
      2   1.1  pooka 
      3   1.1  pooka /*
      4   1.1  pooka  * Copyright (c) 2007-2010 Antti Kantee.  All Rights Reserved.
      5   1.1  pooka  *
      6   1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7   1.1  pooka  * modification, are permitted provided that the following conditions
      8   1.1  pooka  * are met:
      9   1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10   1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11   1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13   1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14   1.1  pooka  *
     15   1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16   1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17   1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18   1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19   1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20   1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21   1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22   1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23   1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24   1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25   1.1  pooka  * SUCH DAMAGE.
     26   1.1  pooka  */
     27   1.1  pooka 
     28   1.8  pooka #include "rumpuser_port.h"
     29   1.8  pooka 
     30   1.1  pooka #if !defined(lint)
     31  1.31  pooka __RCSID("$NetBSD: rumpuser_pth.c,v 1.31 2013/09/23 10:35:20 pooka Exp $");
     32   1.1  pooka #endif /* !lint */
     33   1.1  pooka 
     34  1.23  pooka #include <sys/queue.h>
     35  1.23  pooka 
     36   1.1  pooka #include <assert.h>
     37   1.1  pooka #include <errno.h>
     38   1.8  pooka #include <fcntl.h>
     39   1.1  pooka #include <pthread.h>
     40   1.1  pooka #include <stdlib.h>
     41   1.1  pooka #include <stdio.h>
     42   1.1  pooka #include <string.h>
     43   1.1  pooka #include <stdint.h>
     44   1.1  pooka #include <unistd.h>
     45   1.1  pooka 
     46   1.1  pooka #include <rump/rumpuser.h>
     47   1.1  pooka 
     48   1.1  pooka #include "rumpuser_int.h"
     49   1.1  pooka 
     50   1.1  pooka int
     51   1.3  pooka rumpuser_thread_create(void *(*f)(void *), void *arg, const char *thrname,
     52  1.21  pooka 	int joinable, int priority, int cpuidx, void **ptcookie)
     53   1.1  pooka {
     54   1.1  pooka 	pthread_t ptid;
     55   1.3  pooka 	pthread_t *ptidp;
     56   1.3  pooka 	pthread_attr_t pattr;
     57  1.31  pooka 	int rv, i;
     58   1.1  pooka 
     59   1.3  pooka 	if ((rv = pthread_attr_init(&pattr)) != 0)
     60   1.3  pooka 		return rv;
     61   1.3  pooka 
     62   1.3  pooka 	if (joinable) {
     63   1.3  pooka 		NOFAIL(ptidp = malloc(sizeof(*ptidp)));
     64   1.3  pooka 		pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE);
     65   1.3  pooka 	} else {
     66   1.3  pooka 		ptidp = &ptid;
     67   1.3  pooka 		pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED);
     68   1.3  pooka 	}
     69   1.3  pooka 
     70  1.31  pooka 	for (i = 0; i < 10; i++) {
     71  1.31  pooka 		const struct timespec ts = {0, 10*1000*1000};
     72  1.31  pooka 
     73  1.31  pooka 		rv = pthread_create(ptidp, &pattr, f, arg);
     74  1.31  pooka 		if (rv != EAGAIN)
     75  1.31  pooka 			break;
     76  1.31  pooka 		nanosleep(&ts, NULL);
     77  1.31  pooka 	}
     78  1.31  pooka 
     79   1.9  pooka #if defined(__NetBSD__)
     80   1.1  pooka 	if (rv == 0 && thrname)
     81   1.1  pooka 		pthread_setname_np(ptid, thrname, NULL);
     82   1.9  pooka #elif defined(__linux__)
     83  1.12  pooka 	/*
     84  1.12  pooka 	 * The pthread_setname_np() call varies from one Linux distro to
     85  1.12  pooka 	 * another.  Comment out the call pending autoconf support.
     86  1.12  pooka 	 */
     87  1.12  pooka #if 0
     88   1.9  pooka 	if (rv == 0 && thrname)
     89   1.9  pooka 		pthread_setname_np(ptid, thrname);
     90   1.1  pooka #endif
     91  1.12  pooka #endif
     92   1.1  pooka 
     93   1.3  pooka 	if (joinable) {
     94   1.3  pooka 		assert(ptcookie);
     95   1.3  pooka 		*ptcookie = ptidp;
     96   1.3  pooka 	}
     97   1.3  pooka 
     98   1.3  pooka 	pthread_attr_destroy(&pattr);
     99   1.3  pooka 
    100  1.20  pooka 	ET(rv);
    101   1.1  pooka }
    102   1.1  pooka 
    103   1.1  pooka __dead void
    104   1.1  pooka rumpuser_thread_exit(void)
    105   1.1  pooka {
    106   1.1  pooka 
    107   1.1  pooka 	pthread_exit(NULL);
    108   1.1  pooka }
    109   1.1  pooka 
    110   1.3  pooka int
    111   1.3  pooka rumpuser_thread_join(void *ptcookie)
    112   1.3  pooka {
    113   1.3  pooka 	pthread_t *pt = ptcookie;
    114   1.3  pooka 	int rv;
    115   1.3  pooka 
    116   1.3  pooka 	KLOCK_WRAP((rv = pthread_join(*pt, NULL)));
    117   1.3  pooka 	if (rv == 0)
    118   1.3  pooka 		free(pt);
    119   1.3  pooka 
    120  1.20  pooka 	ET(rv);
    121   1.3  pooka }
    122   1.3  pooka 
    123  1.26  pooka struct rumpuser_mtx {
    124  1.26  pooka 	pthread_mutex_t pthmtx;
    125  1.26  pooka 	struct lwp *owner;
    126  1.26  pooka 	int flags;
    127  1.26  pooka };
    128  1.26  pooka 
    129   1.1  pooka void
    130  1.15  pooka rumpuser_mutex_init(struct rumpuser_mtx **mtx, int flags)
    131   1.1  pooka {
    132   1.1  pooka 	pthread_mutexattr_t att;
    133   1.1  pooka 
    134   1.1  pooka 	NOFAIL(*mtx = malloc(sizeof(struct rumpuser_mtx)));
    135   1.1  pooka 
    136   1.1  pooka 	pthread_mutexattr_init(&att);
    137   1.1  pooka 	pthread_mutexattr_settype(&att, PTHREAD_MUTEX_ERRORCHECK);
    138   1.1  pooka 	NOFAIL_ERRNO(pthread_mutex_init(&((*mtx)->pthmtx), &att));
    139   1.1  pooka 	pthread_mutexattr_destroy(&att);
    140   1.1  pooka 
    141   1.1  pooka 	(*mtx)->owner = NULL;
    142  1.15  pooka 	assert(flags != 0);
    143  1.15  pooka 	(*mtx)->flags = flags;
    144   1.4  pooka }
    145   1.4  pooka 
    146   1.1  pooka static void
    147   1.1  pooka mtxenter(struct rumpuser_mtx *mtx)
    148   1.1  pooka {
    149   1.1  pooka 
    150  1.15  pooka 	if (!(mtx->flags & RUMPUSER_MTX_KMUTEX))
    151   1.4  pooka 		return;
    152   1.4  pooka 
    153   1.4  pooka 	assert(mtx->owner == NULL);
    154  1.23  pooka 	mtx->owner = rumpuser_curlwp();
    155   1.1  pooka }
    156   1.1  pooka 
    157   1.1  pooka static void
    158   1.1  pooka mtxexit(struct rumpuser_mtx *mtx)
    159   1.1  pooka {
    160   1.1  pooka 
    161  1.15  pooka 	if (!(mtx->flags & RUMPUSER_MTX_KMUTEX))
    162   1.4  pooka 		return;
    163   1.4  pooka 
    164   1.1  pooka 	assert(mtx->owner != NULL);
    165   1.4  pooka 	mtx->owner = NULL;
    166   1.1  pooka }
    167   1.1  pooka 
    168   1.1  pooka void
    169   1.1  pooka rumpuser_mutex_enter(struct rumpuser_mtx *mtx)
    170   1.1  pooka {
    171   1.1  pooka 
    172  1.15  pooka 	if (mtx->flags & RUMPUSER_MTX_SPIN) {
    173  1.13  pooka 		rumpuser_mutex_enter_nowrap(mtx);
    174  1.13  pooka 		return;
    175  1.13  pooka 	}
    176  1.13  pooka 
    177  1.15  pooka 	assert(mtx->flags & RUMPUSER_MTX_KMUTEX);
    178   1.1  pooka 	if (pthread_mutex_trylock(&mtx->pthmtx) != 0)
    179   1.1  pooka 		KLOCK_WRAP(NOFAIL_ERRNO(pthread_mutex_lock(&mtx->pthmtx)));
    180   1.1  pooka 	mtxenter(mtx);
    181   1.1  pooka }
    182   1.1  pooka 
    183   1.1  pooka void
    184   1.1  pooka rumpuser_mutex_enter_nowrap(struct rumpuser_mtx *mtx)
    185   1.1  pooka {
    186   1.1  pooka 
    187  1.15  pooka 	assert(mtx->flags & RUMPUSER_MTX_SPIN);
    188   1.1  pooka 	NOFAIL_ERRNO(pthread_mutex_lock(&mtx->pthmtx));
    189   1.1  pooka 	mtxenter(mtx);
    190   1.1  pooka }
    191   1.1  pooka 
    192   1.1  pooka int
    193   1.1  pooka rumpuser_mutex_tryenter(struct rumpuser_mtx *mtx)
    194   1.1  pooka {
    195   1.1  pooka 	int rv;
    196   1.1  pooka 
    197   1.1  pooka 	rv = pthread_mutex_trylock(&mtx->pthmtx);
    198   1.1  pooka 	if (rv == 0) {
    199   1.1  pooka 		mtxenter(mtx);
    200   1.1  pooka 	}
    201   1.1  pooka 
    202  1.20  pooka 	ET(rv);
    203   1.1  pooka }
    204   1.1  pooka 
    205   1.1  pooka void
    206   1.1  pooka rumpuser_mutex_exit(struct rumpuser_mtx *mtx)
    207   1.1  pooka {
    208   1.1  pooka 
    209   1.1  pooka 	mtxexit(mtx);
    210   1.1  pooka 	NOFAIL_ERRNO(pthread_mutex_unlock(&mtx->pthmtx));
    211   1.1  pooka }
    212   1.1  pooka 
    213   1.1  pooka void
    214   1.1  pooka rumpuser_mutex_destroy(struct rumpuser_mtx *mtx)
    215   1.1  pooka {
    216   1.1  pooka 
    217   1.1  pooka 	NOFAIL_ERRNO(pthread_mutex_destroy(&mtx->pthmtx));
    218   1.1  pooka 	free(mtx);
    219   1.1  pooka }
    220   1.1  pooka 
    221  1.19  pooka void
    222  1.19  pooka rumpuser_mutex_owner(struct rumpuser_mtx *mtx, struct lwp **lp)
    223   1.1  pooka {
    224   1.1  pooka 
    225  1.15  pooka 	if (__predict_false(!(mtx->flags & RUMPUSER_MTX_KMUTEX))) {
    226   1.4  pooka 		printf("panic: rumpuser_mutex_held unsupported on non-kmtx\n");
    227   1.4  pooka 		abort();
    228   1.4  pooka 	}
    229   1.4  pooka 
    230  1.19  pooka 	*lp = mtx->owner;
    231   1.1  pooka }
    232   1.1  pooka 
    233  1.26  pooka /*
    234  1.27  pooka  * rwlocks.  these are mostly simple, except that NetBSD wants to
    235  1.27  pooka  * support something called downgrade, which means we need to swap
    236  1.27  pooka  * our exclusive lock for a shared lock.  to accommodate this,
    237  1.27  pooka  * we need to check *after* acquiring a lock in case someone was
    238  1.27  pooka  * downgrading it.  if so, we couldn't actually have it and maybe
    239  1.27  pooka  * need to retry later.
    240  1.26  pooka  */
    241  1.26  pooka 
    242  1.26  pooka struct rumpuser_rw {
    243  1.26  pooka 	pthread_rwlock_t pthrw;
    244  1.26  pooka 	pthread_spinlock_t spin;
    245  1.26  pooka 	int readers;
    246  1.26  pooka 	struct lwp *writer;
    247  1.27  pooka 	int downgrade; /* someone is downgrading (hopefully lock holder ;) */
    248  1.26  pooka };
    249  1.26  pooka 
    250  1.27  pooka static int
    251  1.27  pooka rw_amwriter(struct rumpuser_rw *rw)
    252  1.27  pooka {
    253  1.27  pooka 
    254  1.27  pooka 	return rw->writer == rumpuser_curlwp() && rw->readers == -1;
    255  1.27  pooka }
    256  1.27  pooka 
    257  1.27  pooka static int
    258  1.27  pooka rw_nreaders(struct rumpuser_rw *rw)
    259  1.27  pooka {
    260  1.27  pooka 
    261  1.27  pooka 	return rw->readers > 0 ? rw->readers : 0;
    262  1.27  pooka }
    263  1.27  pooka 
    264  1.27  pooka static int
    265  1.27  pooka rw_setwriter(struct rumpuser_rw *rw, int retry)
    266  1.27  pooka {
    267  1.27  pooka 
    268  1.27  pooka 	/*
    269  1.27  pooka 	 * Don't need the spinlock here, we already have an
    270  1.27  pooka 	 * exclusive lock and "downgrade" is stable until complete.
    271  1.27  pooka 	 */
    272  1.27  pooka 	if (rw->downgrade) {
    273  1.27  pooka 		pthread_rwlock_unlock(&rw->pthrw);
    274  1.27  pooka 		if (retry) {
    275  1.27  pooka 			struct timespec ts;
    276  1.27  pooka 
    277  1.27  pooka 			/* portable yield, essentially */
    278  1.27  pooka 			ts.tv_sec = 0;
    279  1.27  pooka 			ts.tv_nsec = 1;
    280  1.27  pooka 			KLOCK_WRAP(nanosleep(&ts, NULL));
    281  1.27  pooka 		}
    282  1.27  pooka 		return EBUSY;
    283  1.27  pooka 	}
    284  1.27  pooka 	assert(rw->readers == 0);
    285  1.27  pooka 	rw->writer = rumpuser_curlwp();
    286  1.27  pooka 	rw->readers = -1;
    287  1.27  pooka 	return 0;
    288  1.27  pooka }
    289  1.27  pooka 
    290  1.27  pooka static void
    291  1.27  pooka rw_clearwriter(struct rumpuser_rw *rw)
    292  1.27  pooka {
    293  1.27  pooka 
    294  1.27  pooka 	assert(rw_amwriter(rw));
    295  1.27  pooka 	rw->readers = 0;
    296  1.27  pooka 	rw->writer = NULL;
    297  1.27  pooka }
    298  1.27  pooka 
    299  1.27  pooka static void
    300  1.27  pooka rw_readup(struct rumpuser_rw *rw)
    301  1.27  pooka {
    302  1.27  pooka 
    303  1.27  pooka 	pthread_spin_lock(&rw->spin);
    304  1.27  pooka 	assert(rw->readers >= 0);
    305  1.27  pooka 	++rw->readers;
    306  1.27  pooka 	pthread_spin_unlock(&rw->spin);
    307  1.27  pooka }
    308  1.27  pooka 
    309  1.27  pooka static void
    310  1.27  pooka rw_readdown(struct rumpuser_rw *rw)
    311  1.27  pooka {
    312  1.27  pooka 
    313  1.27  pooka 	pthread_spin_lock(&rw->spin);
    314  1.27  pooka 	assert(rw->readers > 0);
    315  1.27  pooka 	--rw->readers;
    316  1.27  pooka 	pthread_spin_unlock(&rw->spin);
    317  1.27  pooka }
    318  1.26  pooka 
    319   1.1  pooka void
    320   1.1  pooka rumpuser_rw_init(struct rumpuser_rw **rw)
    321   1.1  pooka {
    322   1.1  pooka 
    323   1.1  pooka 	NOFAIL(*rw = malloc(sizeof(struct rumpuser_rw)));
    324   1.1  pooka 	NOFAIL_ERRNO(pthread_rwlock_init(&((*rw)->pthrw), NULL));
    325  1.10  pooka 	NOFAIL_ERRNO(pthread_spin_init(&((*rw)->spin),PTHREAD_PROCESS_PRIVATE));
    326   1.1  pooka 	(*rw)->readers = 0;
    327   1.1  pooka 	(*rw)->writer = NULL;
    328  1.28  pooka 	(*rw)->downgrade = 0;
    329   1.1  pooka }
    330   1.1  pooka 
    331   1.1  pooka void
    332  1.30  pooka rumpuser_rw_enter(int enum_rumprwlock, struct rumpuser_rw *rw)
    333   1.1  pooka {
    334  1.30  pooka 	enum rumprwlock lk = enum_rumprwlock;
    335   1.1  pooka 
    336  1.25  pooka 	switch (lk) {
    337  1.25  pooka 	case RUMPUSER_RW_WRITER:
    338  1.27  pooka 		do {
    339  1.27  pooka 			if (pthread_rwlock_trywrlock(&rw->pthrw) != 0)
    340  1.27  pooka 				KLOCK_WRAP(NOFAIL_ERRNO(
    341  1.27  pooka 				    pthread_rwlock_wrlock(&rw->pthrw)));
    342  1.27  pooka 		} while (rw_setwriter(rw, 1) != 0);
    343  1.25  pooka 		break;
    344  1.25  pooka 	case RUMPUSER_RW_READER:
    345   1.1  pooka 		if (pthread_rwlock_tryrdlock(&rw->pthrw) != 0)
    346   1.1  pooka 			KLOCK_WRAP(NOFAIL_ERRNO(
    347   1.1  pooka 			    pthread_rwlock_rdlock(&rw->pthrw)));
    348  1.27  pooka 		rw_readup(rw);
    349  1.25  pooka 		break;
    350   1.1  pooka 	}
    351   1.1  pooka }
    352   1.1  pooka 
    353   1.1  pooka int
    354  1.30  pooka rumpuser_rw_tryenter(int enum_rumprwlock, struct rumpuser_rw *rw)
    355   1.1  pooka {
    356  1.30  pooka 	enum rumprwlock lk = enum_rumprwlock;
    357   1.1  pooka 	int rv;
    358   1.1  pooka 
    359  1.25  pooka 	switch (lk) {
    360  1.25  pooka 	case RUMPUSER_RW_WRITER:
    361   1.1  pooka 		rv = pthread_rwlock_trywrlock(&rw->pthrw);
    362   1.1  pooka 		if (rv == 0)
    363  1.27  pooka 			rv = rw_setwriter(rw, 0);
    364  1.25  pooka 		break;
    365  1.25  pooka 	case RUMPUSER_RW_READER:
    366   1.1  pooka 		rv = pthread_rwlock_tryrdlock(&rw->pthrw);
    367   1.1  pooka 		if (rv == 0)
    368  1.27  pooka 			rw_readup(rw);
    369  1.25  pooka 		break;
    370  1.25  pooka 	default:
    371  1.25  pooka 		rv = EINVAL;
    372  1.25  pooka 		break;
    373   1.1  pooka 	}
    374   1.1  pooka 
    375  1.20  pooka 	ET(rv);
    376   1.1  pooka }
    377   1.1  pooka 
    378  1.25  pooka int
    379  1.25  pooka rumpuser_rw_tryupgrade(struct rumpuser_rw *rw)
    380  1.25  pooka {
    381  1.25  pooka 
    382  1.27  pooka 	/*
    383  1.27  pooka 	 * Not supported by pthreads.  Since the caller needs to
    384  1.27  pooka 	 * back off anyway to avoid deadlock, always failing
    385  1.27  pooka 	 * is correct.
    386  1.27  pooka 	 */
    387  1.25  pooka 	ET(EBUSY);
    388  1.25  pooka }
    389  1.25  pooka 
    390  1.27  pooka /*
    391  1.27  pooka  * convert from exclusive to shared lock without allowing anyone to
    392  1.27  pooka  * obtain an exclusive lock in between.  actually, might allow
    393  1.27  pooka  * someone to obtain the lock, we just don't allow that thread to
    394  1.27  pooka  * return from the hypercall with it.
    395  1.27  pooka  */
    396  1.25  pooka void
    397  1.25  pooka rumpuser_rw_downgrade(struct rumpuser_rw *rw)
    398  1.25  pooka {
    399  1.25  pooka 
    400  1.27  pooka 	assert(rw->downgrade == 0);
    401  1.27  pooka 	rw->downgrade = 1;
    402  1.27  pooka 	rumpuser_rw_exit(rw);
    403  1.25  pooka 	/*
    404  1.27  pooka 	 * though the competition can't get out of the hypervisor, it
    405  1.27  pooka 	 * might have rescheduled itself after we released the lock.
    406  1.27  pooka 	 * so need a wrap here.
    407  1.25  pooka 	 */
    408  1.27  pooka 	KLOCK_WRAP(NOFAIL_ERRNO(pthread_rwlock_rdlock(&rw->pthrw)));
    409  1.27  pooka 	rw->downgrade = 0;
    410  1.27  pooka 	rw_readup(rw);
    411  1.25  pooka }
    412  1.25  pooka 
    413   1.1  pooka void
    414   1.1  pooka rumpuser_rw_exit(struct rumpuser_rw *rw)
    415   1.1  pooka {
    416   1.1  pooka 
    417  1.27  pooka 	if (rw_nreaders(rw))
    418  1.27  pooka 		rw_readdown(rw);
    419   1.1  pooka 	else
    420  1.27  pooka 		rw_clearwriter(rw);
    421   1.1  pooka 	NOFAIL_ERRNO(pthread_rwlock_unlock(&rw->pthrw));
    422   1.1  pooka }
    423   1.1  pooka 
    424   1.1  pooka void
    425   1.1  pooka rumpuser_rw_destroy(struct rumpuser_rw *rw)
    426   1.1  pooka {
    427   1.1  pooka 
    428   1.1  pooka 	NOFAIL_ERRNO(pthread_rwlock_destroy(&rw->pthrw));
    429   1.1  pooka 	NOFAIL_ERRNO(pthread_spin_destroy(&rw->spin));
    430   1.1  pooka 	free(rw);
    431   1.1  pooka }
    432   1.1  pooka 
    433  1.19  pooka void
    434  1.30  pooka rumpuser_rw_held(int enum_rumprwlock, struct rumpuser_rw *rw, int *rv)
    435   1.1  pooka {
    436  1.30  pooka 	enum rumprwlock lk = enum_rumprwlock;
    437   1.1  pooka 
    438  1.25  pooka 	switch (lk) {
    439  1.25  pooka 	case RUMPUSER_RW_WRITER:
    440  1.27  pooka 		*rv = rw_amwriter(rw);
    441  1.25  pooka 		break;
    442  1.25  pooka 	case RUMPUSER_RW_READER:
    443  1.27  pooka 		*rv = rw_nreaders(rw);
    444  1.25  pooka 		break;
    445  1.25  pooka 	}
    446   1.1  pooka }
    447   1.1  pooka 
    448  1.26  pooka /*
    449  1.26  pooka  * condvar
    450  1.26  pooka  */
    451  1.26  pooka 
    452  1.26  pooka struct rumpuser_cv {
    453  1.26  pooka 	pthread_cond_t pthcv;
    454  1.26  pooka 	int nwaiters;
    455  1.26  pooka };
    456  1.26  pooka 
    457   1.1  pooka void
    458   1.1  pooka rumpuser_cv_init(struct rumpuser_cv **cv)
    459   1.1  pooka {
    460   1.1  pooka 
    461   1.1  pooka 	NOFAIL(*cv = malloc(sizeof(struct rumpuser_cv)));
    462   1.1  pooka 	NOFAIL_ERRNO(pthread_cond_init(&((*cv)->pthcv), NULL));
    463   1.1  pooka 	(*cv)->nwaiters = 0;
    464   1.1  pooka }
    465   1.1  pooka 
    466   1.1  pooka void
    467   1.1  pooka rumpuser_cv_destroy(struct rumpuser_cv *cv)
    468   1.1  pooka {
    469   1.1  pooka 
    470   1.1  pooka 	NOFAIL_ERRNO(pthread_cond_destroy(&cv->pthcv));
    471   1.1  pooka 	free(cv);
    472   1.1  pooka }
    473   1.1  pooka 
    474  1.24  pooka static void
    475  1.24  pooka cv_unschedule(struct rumpuser_mtx *mtx, int *nlocks)
    476  1.24  pooka {
    477  1.24  pooka 
    478  1.24  pooka 	rumpkern_unsched(nlocks, mtx);
    479  1.24  pooka 	mtxexit(mtx);
    480  1.24  pooka }
    481  1.24  pooka 
    482  1.24  pooka static void
    483  1.24  pooka cv_reschedule(struct rumpuser_mtx *mtx, int nlocks)
    484  1.24  pooka {
    485  1.24  pooka 
    486  1.24  pooka 	/*
    487  1.24  pooka 	 * If the cv interlock is a spin mutex, we must first release
    488  1.24  pooka 	 * the mutex that was reacquired by pthread_cond_wait(),
    489  1.24  pooka 	 * acquire the CPU context and only then relock the mutex.
    490  1.24  pooka 	 * This is to preserve resource allocation order so that
    491  1.24  pooka 	 * we don't deadlock.  Non-spinning mutexes don't have this
    492  1.24  pooka 	 * problem since they don't use a hold-and-wait approach
    493  1.24  pooka 	 * to acquiring the mutex wrt the rump kernel CPU context.
    494  1.24  pooka 	 *
    495  1.24  pooka 	 * The more optimal solution would be to rework rumpkern_sched()
    496  1.24  pooka 	 * so that it's possible to tell the scheduler
    497  1.24  pooka 	 * "if you need to block, drop this lock first", but I'm not
    498  1.24  pooka 	 * going poking there without some numbers on how often this
    499  1.24  pooka 	 * path is taken for spin mutexes.
    500  1.24  pooka 	 */
    501  1.24  pooka 	if ((mtx->flags & (RUMPUSER_MTX_SPIN | RUMPUSER_MTX_KMUTEX)) ==
    502  1.24  pooka 	    (RUMPUSER_MTX_SPIN | RUMPUSER_MTX_KMUTEX)) {
    503  1.24  pooka 		NOFAIL_ERRNO(pthread_mutex_unlock(&mtx->pthmtx));
    504  1.24  pooka 		rumpkern_sched(nlocks, mtx);
    505  1.24  pooka 		rumpuser_mutex_enter_nowrap(mtx);
    506  1.24  pooka 	} else {
    507  1.24  pooka 		mtxenter(mtx);
    508  1.24  pooka 		rumpkern_sched(nlocks, mtx);
    509  1.24  pooka 	}
    510  1.24  pooka }
    511  1.24  pooka 
    512   1.1  pooka void
    513   1.1  pooka rumpuser_cv_wait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx)
    514   1.1  pooka {
    515   1.2  pooka 	int nlocks;
    516   1.1  pooka 
    517   1.1  pooka 	cv->nwaiters++;
    518  1.24  pooka 	cv_unschedule(mtx, &nlocks);
    519   1.2  pooka 	NOFAIL_ERRNO(pthread_cond_wait(&cv->pthcv, &mtx->pthmtx));
    520  1.24  pooka 	cv_reschedule(mtx, nlocks);
    521   1.1  pooka 	cv->nwaiters--;
    522   1.1  pooka }
    523   1.1  pooka 
    524   1.1  pooka void
    525   1.1  pooka rumpuser_cv_wait_nowrap(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx)
    526   1.1  pooka {
    527   1.1  pooka 
    528   1.1  pooka 	cv->nwaiters++;
    529   1.1  pooka 	mtxexit(mtx);
    530   1.1  pooka 	NOFAIL_ERRNO(pthread_cond_wait(&cv->pthcv, &mtx->pthmtx));
    531   1.1  pooka 	mtxenter(mtx);
    532   1.1  pooka 	cv->nwaiters--;
    533   1.1  pooka }
    534   1.1  pooka 
    535   1.1  pooka int
    536   1.1  pooka rumpuser_cv_timedwait(struct rumpuser_cv *cv, struct rumpuser_mtx *mtx,
    537   1.1  pooka 	int64_t sec, int64_t nsec)
    538   1.1  pooka {
    539   1.1  pooka 	struct timespec ts;
    540   1.2  pooka 	int rv, nlocks;
    541   1.1  pooka 
    542  1.16  pooka 	/*
    543  1.16  pooka 	 * Get clock already here, just in case we will be put to sleep
    544  1.16  pooka 	 * after releasing the kernel context.
    545  1.16  pooka 	 *
    546  1.16  pooka 	 * The condition variables should use CLOCK_MONOTONIC, but since
    547  1.16  pooka 	 * that's not available everywhere, leave it for another day.
    548  1.16  pooka 	 */
    549  1.16  pooka 	clock_gettime(CLOCK_REALTIME, &ts);
    550   1.1  pooka 
    551   1.1  pooka 	cv->nwaiters++;
    552  1.24  pooka 	cv_unschedule(mtx, &nlocks);
    553  1.16  pooka 
    554  1.16  pooka 	ts.tv_sec += sec;
    555  1.16  pooka 	ts.tv_nsec += nsec;
    556  1.16  pooka 	if (ts.tv_nsec >= 1000*1000*1000) {
    557  1.16  pooka 		ts.tv_sec++;
    558  1.16  pooka 		ts.tv_nsec -= 1000*1000*1000;
    559  1.16  pooka 	}
    560   1.2  pooka 	rv = pthread_cond_timedwait(&cv->pthcv, &mtx->pthmtx, &ts);
    561  1.24  pooka 
    562  1.24  pooka 	cv_reschedule(mtx, nlocks);
    563   1.1  pooka 	cv->nwaiters--;
    564   1.1  pooka 
    565  1.20  pooka 	ET(rv);
    566   1.1  pooka }
    567   1.1  pooka 
    568   1.1  pooka void
    569   1.1  pooka rumpuser_cv_signal(struct rumpuser_cv *cv)
    570   1.1  pooka {
    571   1.1  pooka 
    572   1.1  pooka 	NOFAIL_ERRNO(pthread_cond_signal(&cv->pthcv));
    573   1.1  pooka }
    574   1.1  pooka 
    575   1.1  pooka void
    576   1.1  pooka rumpuser_cv_broadcast(struct rumpuser_cv *cv)
    577   1.1  pooka {
    578   1.1  pooka 
    579   1.1  pooka 	NOFAIL_ERRNO(pthread_cond_broadcast(&cv->pthcv));
    580   1.1  pooka }
    581   1.1  pooka 
    582  1.19  pooka void
    583  1.19  pooka rumpuser_cv_has_waiters(struct rumpuser_cv *cv, int *nwaiters)
    584   1.1  pooka {
    585   1.1  pooka 
    586  1.19  pooka 	*nwaiters = cv->nwaiters;
    587   1.1  pooka }
    588   1.1  pooka 
    589   1.1  pooka /*
    590   1.1  pooka  * curlwp
    591   1.1  pooka  */
    592   1.1  pooka 
    593  1.26  pooka static pthread_key_t curlwpkey;
    594  1.26  pooka 
    595  1.23  pooka /*
    596  1.23  pooka  * the if0'd curlwp implementation is not used by this hypervisor,
    597  1.23  pooka  * but serves as test code to check that the intended usage works.
    598  1.23  pooka  */
    599  1.23  pooka #if 0
    600  1.23  pooka struct rumpuser_lwp {
    601  1.23  pooka 	struct lwp *l;
    602  1.23  pooka 	LIST_ENTRY(rumpuser_lwp) l_entries;
    603  1.23  pooka };
    604  1.23  pooka static LIST_HEAD(, rumpuser_lwp) lwps = LIST_HEAD_INITIALIZER(lwps);
    605  1.23  pooka static pthread_mutex_t lwplock = PTHREAD_MUTEX_INITIALIZER;
    606  1.23  pooka 
    607   1.1  pooka void
    608  1.23  pooka rumpuser_curlwpop(enum rumplwpop op, struct lwp *l)
    609   1.1  pooka {
    610  1.23  pooka 	struct rumpuser_lwp *rl, *rliter;
    611   1.1  pooka 
    612  1.23  pooka 	switch (op) {
    613  1.23  pooka 	case RUMPUSER_LWP_CREATE:
    614  1.23  pooka 		rl = malloc(sizeof(*rl));
    615  1.23  pooka 		rl->l = l;
    616  1.23  pooka 		pthread_mutex_lock(&lwplock);
    617  1.23  pooka 		LIST_FOREACH(rliter, &lwps, l_entries) {
    618  1.23  pooka 			if (rliter->l == l) {
    619  1.23  pooka 				fprintf(stderr, "LWP_CREATE: %p exists\n", l);
    620  1.23  pooka 				abort();
    621  1.23  pooka 			}
    622  1.23  pooka 		}
    623  1.23  pooka 		LIST_INSERT_HEAD(&lwps, rl, l_entries);
    624  1.23  pooka 		pthread_mutex_unlock(&lwplock);
    625  1.23  pooka 		break;
    626  1.23  pooka 	case RUMPUSER_LWP_DESTROY:
    627  1.23  pooka 		pthread_mutex_lock(&lwplock);
    628  1.23  pooka 		LIST_FOREACH(rl, &lwps, l_entries) {
    629  1.23  pooka 			if (rl->l == l)
    630  1.23  pooka 				break;
    631  1.23  pooka 		}
    632  1.23  pooka 		if (!rl) {
    633  1.23  pooka 			fprintf(stderr, "LWP_DESTROY: %p does not exist\n", l);
    634  1.23  pooka 			abort();
    635  1.23  pooka 		}
    636  1.23  pooka 		LIST_REMOVE(rl, l_entries);
    637  1.23  pooka 		pthread_mutex_unlock(&lwplock);
    638  1.23  pooka 		free(rl);
    639  1.23  pooka 		break;
    640  1.23  pooka 	case RUMPUSER_LWP_SET:
    641  1.29  pooka 		assert(pthread_getspecific(curlwpkey) == NULL && l != NULL);
    642  1.23  pooka 
    643  1.29  pooka 		pthread_mutex_lock(&lwplock);
    644  1.29  pooka 		LIST_FOREACH(rl, &lwps, l_entries) {
    645  1.29  pooka 			if (rl->l == l)
    646  1.29  pooka 				break;
    647  1.29  pooka 		}
    648  1.29  pooka 		if (!rl) {
    649  1.29  pooka 			fprintf(stderr,
    650  1.29  pooka 			    "LWP_SET: %p does not exist\n", l);
    651  1.29  pooka 			abort();
    652  1.23  pooka 		}
    653  1.29  pooka 		pthread_mutex_unlock(&lwplock);
    654  1.23  pooka 
    655  1.23  pooka 		pthread_setspecific(curlwpkey, rl);
    656  1.23  pooka 		break;
    657  1.29  pooka 	case RUMPUSER_LWP_CLEAR:
    658  1.29  pooka 		assert(((struct rumpuser_lwp *)
    659  1.29  pooka 		    pthread_getspecific(curlwpkey))->l == l);
    660  1.29  pooka 		pthread_setspecific(curlwpkey, NULL);
    661  1.29  pooka 		break;
    662  1.23  pooka 	}
    663   1.1  pooka }
    664   1.1  pooka 
    665   1.1  pooka struct lwp *
    666  1.23  pooka rumpuser_curlwp(void)
    667  1.23  pooka {
    668  1.23  pooka 	struct rumpuser_lwp *rl;
    669  1.23  pooka 
    670  1.23  pooka 	rl = pthread_getspecific(curlwpkey);
    671  1.23  pooka 	return rl ? rl->l : NULL;
    672  1.23  pooka }
    673  1.23  pooka 
    674  1.23  pooka #else
    675  1.23  pooka 
    676  1.23  pooka void
    677  1.30  pooka rumpuser_curlwpop(int enum_rumplwpop, struct lwp *l)
    678  1.23  pooka {
    679  1.30  pooka 	enum rumplwpop op = enum_rumplwpop;
    680  1.23  pooka 
    681  1.23  pooka 	switch (op) {
    682  1.23  pooka 	case RUMPUSER_LWP_CREATE:
    683  1.23  pooka 		break;
    684  1.23  pooka 	case RUMPUSER_LWP_DESTROY:
    685  1.23  pooka 		break;
    686  1.23  pooka 	case RUMPUSER_LWP_SET:
    687  1.29  pooka 		assert(pthread_getspecific(curlwpkey) == NULL);
    688  1.23  pooka 		pthread_setspecific(curlwpkey, l);
    689  1.23  pooka 		break;
    690  1.29  pooka 	case RUMPUSER_LWP_CLEAR:
    691  1.29  pooka 		assert(pthread_getspecific(curlwpkey) == l);
    692  1.29  pooka 		pthread_setspecific(curlwpkey, NULL);
    693  1.29  pooka 		break;
    694  1.23  pooka 	}
    695  1.23  pooka }
    696  1.23  pooka 
    697  1.23  pooka struct lwp *
    698  1.23  pooka rumpuser_curlwp(void)
    699   1.1  pooka {
    700   1.1  pooka 
    701   1.1  pooka 	return pthread_getspecific(curlwpkey);
    702   1.1  pooka }
    703  1.23  pooka #endif
    704  1.26  pooka 
    705  1.26  pooka 
    706  1.26  pooka void
    707  1.26  pooka rumpuser__thrinit(void)
    708  1.26  pooka {
    709  1.26  pooka 	pthread_key_create(&curlwpkey, NULL);
    710  1.26  pooka }
    711