Home | History | Annotate | Line # | Download | only in rumpkern
locks.c revision 1.38.4.2
      1  1.38.4.2  rmind /*	$NetBSD: locks.c,v 1.38.4.2 2010/07/03 01:20:02 rmind 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.38.4.2  rmind __KERNEL_RCSID(0, "$NetBSD: locks.c,v 1.38.4.2 2010/07/03 01:20:02 rmind 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.22  pooka  * We map locks to pthread routines.  The difference between kernel
     45      1.22  pooka  * and rumpuser routines is that while the kernel uses static
     46      1.22  pooka  * storage, rumpuser allocates the object from the heap.  This
     47      1.22  pooka  * indirection is necessary because we don't know the size of
     48      1.38    snj  * pthread objects here.  It is also beneficial, since we can
     49      1.22  pooka  * be easily compatible with the kernel ABI because all kernel
     50      1.22  pooka  * objects regardless of machine architecture are always at least
     51      1.22  pooka  * the size of a pointer.  The downside, of course, is a performance
     52      1.22  pooka  * penalty.
     53      1.22  pooka  */
     54      1.22  pooka 
     55      1.22  pooka #define RUMPMTX(mtx) (*(struct rumpuser_mtx **)(mtx))
     56      1.22  pooka 
     57       1.1  pooka void
     58       1.1  pooka mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl)
     59       1.1  pooka {
     60       1.1  pooka 
     61      1.22  pooka 	CTASSERT(sizeof(kmutex_t) >= sizeof(void *));
     62      1.22  pooka 
     63      1.22  pooka 	rumpuser_mutex_init((struct rumpuser_mtx **)mtx);
     64       1.1  pooka }
     65       1.1  pooka 
     66       1.1  pooka void
     67       1.1  pooka mutex_destroy(kmutex_t *mtx)
     68       1.1  pooka {
     69       1.1  pooka 
     70      1.22  pooka 	rumpuser_mutex_destroy(RUMPMTX(mtx));
     71       1.1  pooka }
     72       1.1  pooka 
     73       1.1  pooka void
     74       1.1  pooka mutex_enter(kmutex_t *mtx)
     75       1.1  pooka {
     76       1.1  pooka 
     77      1.22  pooka 	rumpuser_mutex_enter(RUMPMTX(mtx));
     78       1.1  pooka }
     79       1.1  pooka 
     80       1.6  pooka void
     81       1.6  pooka mutex_spin_enter(kmutex_t *mtx)
     82       1.6  pooka {
     83       1.6  pooka 
     84      1.37  pooka 	mutex_enter(mtx);
     85       1.6  pooka }
     86       1.6  pooka 
     87       1.1  pooka int
     88       1.1  pooka mutex_tryenter(kmutex_t *mtx)
     89       1.1  pooka {
     90       1.1  pooka 
     91      1.22  pooka 	return rumpuser_mutex_tryenter(RUMPMTX(mtx));
     92       1.1  pooka }
     93       1.1  pooka 
     94       1.1  pooka void
     95       1.1  pooka mutex_exit(kmutex_t *mtx)
     96       1.1  pooka {
     97       1.1  pooka 
     98      1.22  pooka 	rumpuser_mutex_exit(RUMPMTX(mtx));
     99       1.1  pooka }
    100       1.1  pooka 
    101       1.6  pooka void
    102       1.6  pooka mutex_spin_exit(kmutex_t *mtx)
    103       1.6  pooka {
    104       1.6  pooka 
    105      1.37  pooka 	mutex_exit(mtx);
    106       1.6  pooka }
    107       1.6  pooka 
    108       1.1  pooka int
    109       1.1  pooka mutex_owned(kmutex_t *mtx)
    110       1.1  pooka {
    111       1.1  pooka 
    112      1.22  pooka 	return rumpuser_mutex_held(RUMPMTX(mtx));
    113       1.1  pooka }
    114       1.1  pooka 
    115      1.22  pooka #define RUMPRW(rw) (*(struct rumpuser_rw **)(rw))
    116      1.22  pooka 
    117       1.1  pooka /* reader/writer locks */
    118       1.1  pooka 
    119       1.1  pooka void
    120       1.1  pooka rw_init(krwlock_t *rw)
    121       1.1  pooka {
    122       1.1  pooka 
    123      1.22  pooka 	CTASSERT(sizeof(krwlock_t) >= sizeof(void *));
    124      1.22  pooka 
    125      1.22  pooka 	rumpuser_rw_init((struct rumpuser_rw **)rw);
    126       1.1  pooka }
    127       1.1  pooka 
    128       1.1  pooka void
    129       1.1  pooka rw_destroy(krwlock_t *rw)
    130       1.1  pooka {
    131       1.1  pooka 
    132      1.22  pooka 	rumpuser_rw_destroy(RUMPRW(rw));
    133       1.1  pooka }
    134       1.1  pooka 
    135       1.1  pooka void
    136       1.1  pooka rw_enter(krwlock_t *rw, const krw_t op)
    137       1.1  pooka {
    138       1.1  pooka 
    139      1.22  pooka 	rumpuser_rw_enter(RUMPRW(rw), op == RW_WRITER);
    140       1.1  pooka }
    141       1.1  pooka 
    142       1.1  pooka int
    143       1.1  pooka rw_tryenter(krwlock_t *rw, const krw_t op)
    144       1.1  pooka {
    145       1.1  pooka 
    146      1.22  pooka 	return rumpuser_rw_tryenter(RUMPRW(rw), op == RW_WRITER);
    147       1.1  pooka }
    148       1.1  pooka 
    149       1.1  pooka void
    150       1.1  pooka rw_exit(krwlock_t *rw)
    151       1.1  pooka {
    152       1.1  pooka 
    153      1.22  pooka 	rumpuser_rw_exit(RUMPRW(rw));
    154       1.1  pooka }
    155       1.1  pooka 
    156       1.1  pooka /* always fails */
    157       1.1  pooka int
    158       1.1  pooka rw_tryupgrade(krwlock_t *rw)
    159       1.1  pooka {
    160       1.1  pooka 
    161       1.1  pooka 	return 0;
    162       1.1  pooka }
    163       1.1  pooka 
    164       1.6  pooka int
    165       1.6  pooka rw_write_held(krwlock_t *rw)
    166       1.6  pooka {
    167       1.6  pooka 
    168      1.22  pooka 	return rumpuser_rw_wrheld(RUMPRW(rw));
    169      1.10     ad }
    170      1.10     ad 
    171      1.10     ad int
    172      1.10     ad rw_read_held(krwlock_t *rw)
    173      1.10     ad {
    174      1.10     ad 
    175      1.22  pooka 	return rumpuser_rw_rdheld(RUMPRW(rw));
    176      1.10     ad }
    177      1.10     ad 
    178      1.10     ad int
    179      1.10     ad rw_lock_held(krwlock_t *rw)
    180      1.10     ad {
    181      1.10     ad 
    182      1.22  pooka 	return rumpuser_rw_held(RUMPRW(rw));
    183       1.6  pooka }
    184       1.6  pooka 
    185       1.1  pooka /* curriculum vitaes */
    186       1.1  pooka 
    187      1.24  pooka #define RUMPCV(cv) (*(struct rumpuser_cv **)(cv))
    188       1.1  pooka 
    189       1.1  pooka void
    190       1.1  pooka cv_init(kcondvar_t *cv, const char *msg)
    191       1.1  pooka {
    192       1.1  pooka 
    193      1.25  pooka 	CTASSERT(sizeof(kcondvar_t) >= sizeof(void *));
    194      1.25  pooka 
    195      1.24  pooka 	rumpuser_cv_init((struct rumpuser_cv **)cv);
    196       1.1  pooka }
    197       1.1  pooka 
    198       1.1  pooka void
    199       1.1  pooka cv_destroy(kcondvar_t *cv)
    200       1.1  pooka {
    201       1.1  pooka 
    202       1.1  pooka 	rumpuser_cv_destroy(RUMPCV(cv));
    203       1.1  pooka }
    204       1.1  pooka 
    205       1.1  pooka void
    206       1.1  pooka cv_wait(kcondvar_t *cv, kmutex_t *mtx)
    207       1.1  pooka {
    208       1.1  pooka 
    209  1.38.4.2  rmind 	if (__predict_false(rump_threads == 0))
    210      1.28  pooka 		panic("cv_wait without threads");
    211      1.22  pooka 	rumpuser_cv_wait(RUMPCV(cv), RUMPMTX(mtx));
    212       1.1  pooka }
    213       1.1  pooka 
    214       1.3  pooka int
    215       1.5  pooka cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
    216       1.5  pooka {
    217       1.5  pooka 
    218  1.38.4.2  rmind 	if (__predict_false(rump_threads == 0))
    219  1.38.4.2  rmind 		panic("cv_wait without threads");
    220      1.22  pooka 	rumpuser_cv_wait(RUMPCV(cv), RUMPMTX(mtx));
    221       1.5  pooka 	return 0;
    222       1.5  pooka }
    223       1.5  pooka 
    224       1.5  pooka int
    225       1.3  pooka cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int ticks)
    226       1.3  pooka {
    227      1.27  pooka 	struct timespec ts, tick;
    228       1.3  pooka 	extern int hz;
    229      1.27  pooka 
    230       1.9  pooka 	if (ticks == 0) {
    231       1.9  pooka 		cv_wait(cv, mtx);
    232       1.9  pooka 		return 0;
    233       1.9  pooka 	} else {
    234  1.38.4.2  rmind 		/*
    235  1.38.4.2  rmind 		 * XXX: this fetches rump kernel time, but
    236  1.38.4.2  rmind 		 * rumpuser_cv_timedwait uses host time.
    237  1.38.4.2  rmind 		 */
    238  1.38.4.2  rmind 		nanotime(&ts);
    239  1.38.4.2  rmind 		tick.tv_sec = ticks / hz;
    240  1.38.4.2  rmind 		tick.tv_nsec = (ticks % hz) * (1000000000/hz);
    241  1.38.4.2  rmind 		timespecadd(&ts, &tick, &ts);
    242  1.38.4.2  rmind 
    243      1.34  pooka 		if (rumpuser_cv_timedwait(RUMPCV(cv), RUMPMTX(mtx),
    244      1.34  pooka 		    ts.tv_sec, ts.tv_nsec))
    245      1.34  pooka 			return EWOULDBLOCK;
    246      1.34  pooka 		else
    247      1.34  pooka 			return 0;
    248       1.9  pooka 	}
    249       1.3  pooka }
    250       1.3  pooka 
    251       1.5  pooka int
    252       1.5  pooka cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int ticks)
    253       1.5  pooka {
    254       1.5  pooka 
    255       1.9  pooka 	return cv_timedwait(cv, mtx, ticks);
    256       1.5  pooka }
    257       1.5  pooka 
    258       1.1  pooka void
    259       1.1  pooka cv_signal(kcondvar_t *cv)
    260       1.1  pooka {
    261       1.1  pooka 
    262       1.1  pooka 	rumpuser_cv_signal(RUMPCV(cv));
    263       1.1  pooka }
    264       1.2  pooka 
    265       1.4  pooka void
    266       1.4  pooka cv_broadcast(kcondvar_t *cv)
    267       1.4  pooka {
    268       1.4  pooka 
    269       1.4  pooka 	rumpuser_cv_broadcast(RUMPCV(cv));
    270       1.4  pooka }
    271       1.4  pooka 
    272      1.17  pooka bool
    273      1.17  pooka cv_has_waiters(kcondvar_t *cv)
    274      1.17  pooka {
    275      1.17  pooka 
    276      1.17  pooka 	return rumpuser_cv_has_waiters(RUMPCV(cv));
    277      1.17  pooka }
    278      1.17  pooka 
    279      1.35  pooka /* this is not much of an attempt, but ... */
    280      1.35  pooka bool
    281      1.35  pooka cv_is_valid(kcondvar_t *cv)
    282      1.35  pooka {
    283      1.35  pooka 
    284      1.35  pooka 	return RUMPCV(cv) != NULL;
    285      1.35  pooka }
    286