Home | History | Annotate | Line # | Download | only in rumpkern
locks.c revision 1.2.4.6
      1 /*	$NetBSD: locks.c,v 1.2.4.6 2008/03/17 09:15:46 yamt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by the
      7  * Finnish Cultural Foundation.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/param.h>
     32 #include <sys/mutex.h>
     33 #include <sys/rwlock.h>
     34 
     35 #include "rump_private.h"
     36 
     37 #include "rumpuser.h"
     38 
     39 void
     40 mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl)
     41 {
     42 
     43 	rumpuser_mutex_init(&mtx->kmtx_mtx);
     44 }
     45 
     46 void
     47 mutex_destroy(kmutex_t *mtx)
     48 {
     49 
     50 	rumpuser_mutex_destroy(mtx->kmtx_mtx);
     51 }
     52 
     53 void
     54 mutex_enter(kmutex_t *mtx)
     55 {
     56 
     57 	rumpuser_mutex_enter(mtx->kmtx_mtx);
     58 }
     59 
     60 void
     61 mutex_spin_enter(kmutex_t *mtx)
     62 {
     63 
     64 	mutex_enter(mtx);
     65 }
     66 
     67 int
     68 mutex_tryenter(kmutex_t *mtx)
     69 {
     70 
     71 	return rumpuser_mutex_tryenter(mtx->kmtx_mtx);
     72 }
     73 
     74 void
     75 mutex_exit(kmutex_t *mtx)
     76 {
     77 
     78 	rumpuser_mutex_exit(mtx->kmtx_mtx);
     79 }
     80 
     81 void
     82 mutex_spin_exit(kmutex_t *mtx)
     83 {
     84 
     85 	mutex_exit(mtx);
     86 }
     87 
     88 int
     89 mutex_owned(kmutex_t *mtx)
     90 {
     91 
     92 	return rumpuser_mutex_held(mtx->kmtx_mtx);
     93 }
     94 
     95 /* reader/writer locks */
     96 
     97 void
     98 rw_init(krwlock_t *rw)
     99 {
    100 
    101 	rumpuser_rw_init(&rw->krw_pthlock);
    102 }
    103 
    104 void
    105 rw_destroy(krwlock_t *rw)
    106 {
    107 
    108 	rumpuser_rw_destroy(rw->krw_pthlock);
    109 }
    110 
    111 void
    112 rw_enter(krwlock_t *rw, const krw_t op)
    113 {
    114 
    115 	rumpuser_rw_enter(rw->krw_pthlock, op == RW_WRITER);
    116 }
    117 
    118 int
    119 rw_tryenter(krwlock_t *rw, const krw_t op)
    120 {
    121 
    122 	return rumpuser_rw_tryenter(rw->krw_pthlock, op == RW_WRITER);
    123 }
    124 
    125 void
    126 rw_exit(krwlock_t *rw)
    127 {
    128 
    129 	rumpuser_rw_exit(rw->krw_pthlock);
    130 }
    131 
    132 /* always fails */
    133 int
    134 rw_tryupgrade(krwlock_t *rw)
    135 {
    136 
    137 	return 0;
    138 }
    139 
    140 int
    141 rw_write_held(krwlock_t *rw)
    142 {
    143 
    144 	return rumpuser_rw_wrheld(rw->krw_pthlock);
    145 }
    146 
    147 int
    148 rw_read_held(krwlock_t *rw)
    149 {
    150 
    151 	return rumpuser_rw_rdheld(rw->krw_pthlock);
    152 }
    153 
    154 int
    155 rw_lock_held(krwlock_t *rw)
    156 {
    157 
    158 	return rumpuser_rw_held(rw->krw_pthlock);
    159 }
    160 
    161 /* curriculum vitaes */
    162 
    163 /* forgive me for I have sinned */
    164 #define RUMPCV(a) ((struct rumpuser_cv *)(__UNCONST((a)->cv_wmesg)))
    165 
    166 void
    167 cv_init(kcondvar_t *cv, const char *msg)
    168 {
    169 
    170 	rumpuser_cv_init((struct rumpuser_cv **)__UNCONST(&cv->cv_wmesg));
    171 }
    172 
    173 void
    174 cv_destroy(kcondvar_t *cv)
    175 {
    176 
    177 	rumpuser_cv_destroy(RUMPCV(cv));
    178 }
    179 
    180 void
    181 cv_wait(kcondvar_t *cv, kmutex_t *mtx)
    182 {
    183 
    184 	rumpuser_cv_wait(RUMPCV(cv), mtx->kmtx_mtx);
    185 }
    186 
    187 int
    188 cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
    189 {
    190 
    191 	rumpuser_cv_wait(RUMPCV(cv), mtx->kmtx_mtx);
    192 	return 0;
    193 }
    194 
    195 int
    196 cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int ticks)
    197 {
    198 	extern int hz;
    199 
    200 	if (ticks == 0) {
    201 		cv_wait(cv, mtx);
    202 		return 0;
    203 	} else {
    204 		KASSERT(hz == 100);
    205 		return rumpuser_cv_timedwait(RUMPCV(cv), mtx->kmtx_mtx, ticks);
    206 	}
    207 }
    208 
    209 int
    210 cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int ticks)
    211 {
    212 
    213 	return cv_timedwait(cv, mtx, ticks);
    214 }
    215 
    216 void
    217 cv_signal(kcondvar_t *cv)
    218 {
    219 
    220 	rumpuser_cv_signal(RUMPCV(cv));
    221 }
    222 
    223 void
    224 cv_broadcast(kcondvar_t *cv)
    225 {
    226 
    227 	rumpuser_cv_broadcast(RUMPCV(cv));
    228 }
    229 
    230 /* kernel biglock, only for vnode_if */
    231 
    232 void
    233 _kernel_lock(int nlocks, struct lwp *l)
    234 {
    235 
    236 	KASSERT(nlocks == 1);
    237 	mutex_enter(&rump_giantlock);
    238 }
    239 
    240 void
    241 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
    242 {
    243 
    244 	KASSERT(nlocks == 1);
    245 	mutex_exit(&rump_giantlock);
    246 	if (countp)
    247 		*countp = 1;
    248 }
    249