Home | History | Annotate | Line # | Download | only in rumpkern
locks.c revision 1.48.2.1
      1  1.48.2.1  bouyer /*	$NetBSD: locks.c,v 1.48.2.1 2011/02/08 16:20:04 bouyer Exp $	*/
      2       1.1   pooka 
      3       1.1   pooka /*
      4      1.22   pooka  * Copyright (c) 2007, 2008 Antti Kantee.  All Rights Reserved.
      5       1.1   pooka  *
      6       1.1   pooka  * Development of this software was supported by the
      7       1.1   pooka  * Finnish Cultural Foundation.
      8       1.1   pooka  *
      9       1.1   pooka  * Redistribution and use in source and binary forms, with or without
     10       1.1   pooka  * modification, are permitted provided that the following conditions
     11       1.1   pooka  * are met:
     12       1.1   pooka  * 1. Redistributions of source code must retain the above copyright
     13       1.1   pooka  *    notice, this list of conditions and the following disclaimer.
     14       1.1   pooka  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1   pooka  *    notice, this list of conditions and the following disclaimer in the
     16       1.1   pooka  *    documentation and/or other materials provided with the distribution.
     17       1.1   pooka  *
     18       1.1   pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19       1.1   pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20       1.1   pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21       1.1   pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22       1.1   pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23       1.1   pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24       1.1   pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25       1.1   pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26       1.1   pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27       1.1   pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28       1.1   pooka  * SUCH DAMAGE.
     29       1.1   pooka  */
     30       1.1   pooka 
     31      1.23   pooka #include <sys/cdefs.h>
     32  1.48.2.1  bouyer __KERNEL_RCSID(0, "$NetBSD: locks.c,v 1.48.2.1 2011/02/08 16:20:04 bouyer Exp $");
     33      1.23   pooka 
     34       1.1   pooka #include <sys/param.h>
     35      1.26   pooka #include <sys/kmem.h>
     36       1.1   pooka #include <sys/mutex.h>
     37       1.1   pooka #include <sys/rwlock.h>
     38       1.1   pooka 
     39      1.18   pooka #include <rump/rumpuser.h>
     40      1.18   pooka 
     41       1.2   pooka #include "rump_private.h"
     42       1.2   pooka 
     43      1.22   pooka /*
     44      1.45   pooka  * Simple lockdebug.  If it's compiled in, it's always active.
     45      1.45   pooka  * Currently available only for mtx/rwlock.
     46      1.45   pooka  */
     47      1.45   pooka #ifdef LOCKDEBUG
     48      1.45   pooka #include <sys/lockdebug.h>
     49      1.45   pooka 
     50      1.45   pooka static lockops_t mutex_lockops = {
     51      1.45   pooka 	"mutex",
     52      1.45   pooka 	LOCKOPS_SLEEP,
     53      1.45   pooka 	NULL
     54      1.45   pooka };
     55      1.45   pooka static lockops_t rw_lockops = {
     56      1.46   pooka 	"rwlock",
     57      1.45   pooka 	LOCKOPS_SLEEP,
     58      1.45   pooka 	NULL
     59      1.45   pooka };
     60      1.45   pooka 
     61      1.45   pooka #define ALLOCK(lock, ops)		\
     62      1.45   pooka     lockdebug_alloc(lock, ops, (uintptr_t)__builtin_return_address(0))
     63      1.45   pooka #define FREELOCK(lock)			\
     64      1.45   pooka     lockdebug_free(lock)
     65      1.45   pooka #define WANTLOCK(lock, shar, try)	\
     66      1.45   pooka     lockdebug_wantlock(lock, (uintptr_t)__builtin_return_address(0), shar, try)
     67      1.45   pooka #define LOCKED(lock, shar)		\
     68      1.45   pooka     lockdebug_locked(lock, NULL, (uintptr_t)__builtin_return_address(0), shar)
     69      1.45   pooka #define UNLOCKED(lock, shar)		\
     70      1.45   pooka     lockdebug_unlocked(lock, (uintptr_t)__builtin_return_address(0), shar)
     71      1.45   pooka #else
     72      1.45   pooka #define ALLOCK(a, b)
     73      1.45   pooka #define FREELOCK(a)
     74      1.45   pooka #define WANTLOCK(a, b, c)
     75      1.45   pooka #define LOCKED(a, b)
     76      1.45   pooka #define UNLOCKED(a, b)
     77      1.45   pooka #endif
     78      1.45   pooka 
     79      1.45   pooka /*
     80      1.22   pooka  * We map locks to pthread routines.  The difference between kernel
     81      1.22   pooka  * and rumpuser routines is that while the kernel uses static
     82      1.22   pooka  * storage, rumpuser allocates the object from the heap.  This
     83      1.22   pooka  * indirection is necessary because we don't know the size of
     84      1.38     snj  * pthread objects here.  It is also beneficial, since we can
     85      1.22   pooka  * be easily compatible with the kernel ABI because all kernel
     86      1.22   pooka  * objects regardless of machine architecture are always at least
     87      1.22   pooka  * the size of a pointer.  The downside, of course, is a performance
     88      1.22   pooka  * penalty.
     89      1.22   pooka  */
     90      1.22   pooka 
     91      1.22   pooka #define RUMPMTX(mtx) (*(struct rumpuser_mtx **)(mtx))
     92      1.22   pooka 
     93       1.1   pooka void
     94       1.1   pooka mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl)
     95       1.1   pooka {
     96       1.1   pooka 
     97      1.22   pooka 	CTASSERT(sizeof(kmutex_t) >= sizeof(void *));
     98      1.22   pooka 
     99      1.43   pooka 	rumpuser_mutex_init_kmutex((struct rumpuser_mtx **)mtx);
    100      1.45   pooka 	ALLOCK(mtx, &mutex_lockops);
    101       1.1   pooka }
    102       1.1   pooka 
    103       1.1   pooka void
    104       1.1   pooka mutex_destroy(kmutex_t *mtx)
    105       1.1   pooka {
    106       1.1   pooka 
    107      1.45   pooka 	FREELOCK(mtx);
    108      1.22   pooka 	rumpuser_mutex_destroy(RUMPMTX(mtx));
    109       1.1   pooka }
    110       1.1   pooka 
    111       1.1   pooka void
    112       1.1   pooka mutex_enter(kmutex_t *mtx)
    113       1.1   pooka {
    114       1.1   pooka 
    115      1.45   pooka 	WANTLOCK(mtx, false, false);
    116      1.22   pooka 	rumpuser_mutex_enter(RUMPMTX(mtx));
    117      1.45   pooka 	LOCKED(mtx, false);
    118       1.1   pooka }
    119      1.45   pooka __strong_alias(mutex_spin_enter,mutex_enter);
    120       1.6   pooka 
    121       1.1   pooka int
    122       1.1   pooka mutex_tryenter(kmutex_t *mtx)
    123       1.1   pooka {
    124      1.45   pooka 	int rv;
    125       1.1   pooka 
    126      1.45   pooka 	rv = rumpuser_mutex_tryenter(RUMPMTX(mtx));
    127      1.45   pooka 	if (rv) {
    128      1.45   pooka 		WANTLOCK(mtx, false, true);
    129      1.45   pooka 		LOCKED(mtx, false);
    130      1.45   pooka 	}
    131      1.45   pooka 	return rv;
    132       1.1   pooka }
    133       1.1   pooka 
    134       1.1   pooka void
    135       1.1   pooka mutex_exit(kmutex_t *mtx)
    136       1.1   pooka {
    137       1.1   pooka 
    138      1.45   pooka 	UNLOCKED(mtx, false);
    139      1.22   pooka 	rumpuser_mutex_exit(RUMPMTX(mtx));
    140       1.1   pooka }
    141      1.45   pooka __strong_alias(mutex_spin_exit,mutex_exit);
    142       1.6   pooka 
    143       1.1   pooka int
    144       1.1   pooka mutex_owned(kmutex_t *mtx)
    145       1.1   pooka {
    146       1.1   pooka 
    147      1.44   pooka 	return mutex_owner(mtx) == curlwp;
    148      1.44   pooka }
    149      1.44   pooka 
    150      1.44   pooka struct lwp *
    151      1.44   pooka mutex_owner(kmutex_t *mtx)
    152      1.44   pooka {
    153      1.44   pooka 
    154      1.44   pooka 	return rumpuser_mutex_owner(RUMPMTX(mtx));
    155       1.1   pooka }
    156       1.1   pooka 
    157      1.22   pooka #define RUMPRW(rw) (*(struct rumpuser_rw **)(rw))
    158      1.22   pooka 
    159       1.1   pooka /* reader/writer locks */
    160       1.1   pooka 
    161       1.1   pooka void
    162       1.1   pooka rw_init(krwlock_t *rw)
    163       1.1   pooka {
    164       1.1   pooka 
    165      1.22   pooka 	CTASSERT(sizeof(krwlock_t) >= sizeof(void *));
    166      1.22   pooka 
    167      1.22   pooka 	rumpuser_rw_init((struct rumpuser_rw **)rw);
    168      1.45   pooka 	ALLOCK(rw, &rw_lockops);
    169       1.1   pooka }
    170       1.1   pooka 
    171       1.1   pooka void
    172       1.1   pooka rw_destroy(krwlock_t *rw)
    173       1.1   pooka {
    174       1.1   pooka 
    175      1.45   pooka 	FREELOCK(rw);
    176      1.22   pooka 	rumpuser_rw_destroy(RUMPRW(rw));
    177       1.1   pooka }
    178       1.1   pooka 
    179       1.1   pooka void
    180       1.1   pooka rw_enter(krwlock_t *rw, const krw_t op)
    181       1.1   pooka {
    182       1.1   pooka 
    183      1.45   pooka 
    184      1.45   pooka 	WANTLOCK(rw, op == RW_READER, false);
    185      1.22   pooka 	rumpuser_rw_enter(RUMPRW(rw), op == RW_WRITER);
    186      1.45   pooka 	LOCKED(rw, op == RW_READER);
    187       1.1   pooka }
    188       1.1   pooka 
    189       1.1   pooka int
    190       1.1   pooka rw_tryenter(krwlock_t *rw, const krw_t op)
    191       1.1   pooka {
    192      1.45   pooka 	int rv;
    193       1.1   pooka 
    194      1.45   pooka 	rv = rumpuser_rw_tryenter(RUMPRW(rw), op == RW_WRITER);
    195      1.45   pooka 	if (rv) {
    196      1.45   pooka 		WANTLOCK(rw, op == RW_READER, true);
    197      1.45   pooka 		LOCKED(rw, op == RW_READER);
    198      1.45   pooka 	}
    199      1.45   pooka 	return rv;
    200       1.1   pooka }
    201       1.1   pooka 
    202       1.1   pooka void
    203       1.1   pooka rw_exit(krwlock_t *rw)
    204       1.1   pooka {
    205       1.1   pooka 
    206      1.45   pooka #ifdef LOCKDEBUG
    207      1.45   pooka 	bool shared = !rw_write_held(rw);
    208      1.45   pooka 
    209      1.45   pooka 	if (shared)
    210      1.45   pooka 		KASSERT(rw_read_held(rw));
    211      1.45   pooka 	UNLOCKED(rw, shared);
    212      1.45   pooka #endif
    213      1.22   pooka 	rumpuser_rw_exit(RUMPRW(rw));
    214       1.1   pooka }
    215       1.1   pooka 
    216       1.1   pooka /* always fails */
    217       1.1   pooka int
    218       1.1   pooka rw_tryupgrade(krwlock_t *rw)
    219       1.1   pooka {
    220       1.1   pooka 
    221       1.1   pooka 	return 0;
    222       1.1   pooka }
    223       1.1   pooka 
    224      1.48    haad void
    225      1.48    haad rw_downgrade(krwlock_t *rw)
    226      1.48    haad {
    227      1.48    haad 
    228      1.48    haad #ifdef LOCKDEBUG
    229      1.48    haad 	KASSERT(!rw_write_held(rw));
    230      1.48    haad #endif
    231      1.48    haad 	/*
    232      1.48    haad 	 * XXX HACK: How we can downgrade re lock in rump properly.
    233      1.48    haad 	 */
    234      1.48    haad 	rw_exit(rw);
    235      1.48    haad 	rw_enter(rw, RW_READER);
    236      1.48    haad 	return;
    237      1.48    haad }
    238      1.48    haad 
    239       1.6   pooka int
    240       1.6   pooka rw_write_held(krwlock_t *rw)
    241       1.6   pooka {
    242       1.6   pooka 
    243      1.22   pooka 	return rumpuser_rw_wrheld(RUMPRW(rw));
    244      1.10      ad }
    245      1.10      ad 
    246      1.10      ad int
    247      1.10      ad rw_read_held(krwlock_t *rw)
    248      1.10      ad {
    249      1.10      ad 
    250      1.22   pooka 	return rumpuser_rw_rdheld(RUMPRW(rw));
    251      1.10      ad }
    252      1.10      ad 
    253      1.10      ad int
    254      1.10      ad rw_lock_held(krwlock_t *rw)
    255      1.10      ad {
    256      1.10      ad 
    257      1.22   pooka 	return rumpuser_rw_held(RUMPRW(rw));
    258       1.6   pooka }
    259       1.6   pooka 
    260       1.1   pooka /* curriculum vitaes */
    261       1.1   pooka 
    262      1.24   pooka #define RUMPCV(cv) (*(struct rumpuser_cv **)(cv))
    263       1.1   pooka 
    264       1.1   pooka void
    265       1.1   pooka cv_init(kcondvar_t *cv, const char *msg)
    266       1.1   pooka {
    267       1.1   pooka 
    268      1.25   pooka 	CTASSERT(sizeof(kcondvar_t) >= sizeof(void *));
    269      1.25   pooka 
    270      1.24   pooka 	rumpuser_cv_init((struct rumpuser_cv **)cv);
    271       1.1   pooka }
    272       1.1   pooka 
    273       1.1   pooka void
    274       1.1   pooka cv_destroy(kcondvar_t *cv)
    275       1.1   pooka {
    276       1.1   pooka 
    277       1.1   pooka 	rumpuser_cv_destroy(RUMPCV(cv));
    278       1.1   pooka }
    279       1.1   pooka 
    280      1.47   pooka static int
    281      1.47   pooka docvwait(kcondvar_t *cv, kmutex_t *mtx, struct timespec *ts)
    282      1.47   pooka {
    283      1.47   pooka 	struct lwp *l = curlwp;
    284      1.47   pooka 	int rv;
    285      1.47   pooka 
    286  1.48.2.1  bouyer 	if (__predict_false(l->l_flag & LW_RUMP_DYING)) {
    287      1.47   pooka 		/*
    288  1.48.2.1  bouyer 		 * yield() here, someone might want the cpu
    289  1.48.2.1  bouyer 		 * to set a condition.  otherwise we'll just
    290  1.48.2.1  bouyer 		 * loop forever.
    291      1.47   pooka 		 */
    292  1.48.2.1  bouyer 		yield();
    293      1.47   pooka 		return EINTR;
    294      1.47   pooka 	}
    295      1.47   pooka 
    296      1.47   pooka 	UNLOCKED(mtx, false);
    297      1.47   pooka 
    298      1.47   pooka 	l->l_private = cv;
    299      1.47   pooka 	rv = 0;
    300      1.47   pooka 	if (ts) {
    301      1.47   pooka 		if (rumpuser_cv_timedwait(RUMPCV(cv), RUMPMTX(mtx),
    302      1.47   pooka 		    ts->tv_sec, ts->tv_nsec))
    303      1.47   pooka 			rv = EWOULDBLOCK;
    304      1.47   pooka 	} else {
    305      1.47   pooka 		rumpuser_cv_wait(RUMPCV(cv), RUMPMTX(mtx));
    306      1.47   pooka 	}
    307      1.47   pooka 
    308      1.47   pooka 	/*
    309  1.48.2.1  bouyer 	 * Check for DYING.  if so, we need to wait here until we
    310      1.47   pooka 	 * are allowed to exit.
    311      1.47   pooka 	 */
    312  1.48.2.1  bouyer 	if (__predict_false(l->l_flag & LW_RUMP_DYING)) {
    313      1.47   pooka 		struct proc *p = l->l_proc;
    314      1.47   pooka 
    315      1.47   pooka 		mutex_exit(mtx); /* drop and retake later */
    316      1.47   pooka 
    317      1.47   pooka 		mutex_enter(p->p_lock);
    318  1.48.2.1  bouyer 		while (p->p_stat != SDYING) {
    319      1.47   pooka 			/* avoid recursion */
    320      1.47   pooka 			rumpuser_cv_wait(RUMPCV(&p->p_waitcv),
    321      1.47   pooka 			    RUMPMTX(p->p_lock));
    322      1.47   pooka 		}
    323  1.48.2.1  bouyer 		KASSERT(p->p_stat == SDYING);
    324      1.47   pooka 		mutex_exit(p->p_lock);
    325      1.47   pooka 
    326      1.47   pooka 		/* ok, we can exit and remove "reference" to l->private */
    327      1.47   pooka 
    328      1.47   pooka 		mutex_enter(mtx);
    329      1.47   pooka 		rv = EINTR;
    330      1.47   pooka 	}
    331      1.47   pooka 	l->l_private = NULL;
    332      1.47   pooka 
    333      1.47   pooka 	LOCKED(mtx, false);
    334      1.47   pooka 
    335      1.47   pooka 	return rv;
    336      1.47   pooka }
    337      1.47   pooka 
    338       1.1   pooka void
    339       1.1   pooka cv_wait(kcondvar_t *cv, kmutex_t *mtx)
    340       1.1   pooka {
    341       1.1   pooka 
    342      1.42   pooka 	if (__predict_false(rump_threads == 0))
    343      1.28   pooka 		panic("cv_wait without threads");
    344      1.47   pooka 	(void) docvwait(cv, mtx, NULL);
    345       1.1   pooka }
    346       1.1   pooka 
    347       1.3   pooka int
    348       1.5   pooka cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
    349       1.5   pooka {
    350       1.5   pooka 
    351      1.42   pooka 	if (__predict_false(rump_threads == 0))
    352      1.42   pooka 		panic("cv_wait without threads");
    353      1.47   pooka 	return docvwait(cv, mtx, NULL);
    354       1.5   pooka }
    355       1.5   pooka 
    356       1.5   pooka int
    357       1.3   pooka cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int ticks)
    358       1.3   pooka {
    359      1.27   pooka 	struct timespec ts, tick;
    360       1.3   pooka 	extern int hz;
    361      1.45   pooka 	int rv;
    362      1.27   pooka 
    363       1.9   pooka 	if (ticks == 0) {
    364      1.47   pooka 		rv = cv_wait_sig(cv, mtx);
    365       1.9   pooka 	} else {
    366      1.42   pooka 		/*
    367      1.42   pooka 		 * XXX: this fetches rump kernel time, but
    368      1.42   pooka 		 * rumpuser_cv_timedwait uses host time.
    369      1.42   pooka 		 */
    370      1.42   pooka 		nanotime(&ts);
    371      1.42   pooka 		tick.tv_sec = ticks / hz;
    372      1.42   pooka 		tick.tv_nsec = (ticks % hz) * (1000000000/hz);
    373      1.42   pooka 		timespecadd(&ts, &tick, &ts);
    374      1.42   pooka 
    375      1.47   pooka 		rv = docvwait(cv, mtx, &ts);
    376       1.9   pooka 	}
    377       1.5   pooka 
    378      1.45   pooka 	return rv;
    379       1.5   pooka }
    380      1.45   pooka __strong_alias(cv_timedwait_sig,cv_timedwait);
    381       1.5   pooka 
    382       1.1   pooka void
    383       1.1   pooka cv_signal(kcondvar_t *cv)
    384       1.1   pooka {
    385       1.1   pooka 
    386       1.1   pooka 	rumpuser_cv_signal(RUMPCV(cv));
    387       1.1   pooka }
    388       1.2   pooka 
    389       1.4   pooka void
    390       1.4   pooka cv_broadcast(kcondvar_t *cv)
    391       1.4   pooka {
    392       1.4   pooka 
    393       1.4   pooka 	rumpuser_cv_broadcast(RUMPCV(cv));
    394       1.4   pooka }
    395       1.4   pooka 
    396      1.17   pooka bool
    397      1.17   pooka cv_has_waiters(kcondvar_t *cv)
    398      1.17   pooka {
    399      1.17   pooka 
    400      1.17   pooka 	return rumpuser_cv_has_waiters(RUMPCV(cv));
    401      1.17   pooka }
    402      1.17   pooka 
    403      1.35   pooka /* this is not much of an attempt, but ... */
    404      1.35   pooka bool
    405      1.35   pooka cv_is_valid(kcondvar_t *cv)
    406      1.35   pooka {
    407      1.35   pooka 
    408      1.35   pooka 	return RUMPCV(cv) != NULL;
    409      1.35   pooka }
    410