Home | History | Annotate | Line # | Download | only in rumpkern
ltsleep.c revision 1.20
      1 /*	$NetBSD: ltsleep.c,v 1.20 2009/11/11 16:47:50 pooka 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/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: ltsleep.c,v 1.20 2009/11/11 16:47:50 pooka Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/kernel.h>
     36 #include <sys/proc.h>
     37 #include <sys/queue.h>
     38 #include <sys/simplelock.h>
     39 
     40 #include <rump/rumpuser.h>
     41 
     42 #include "rump_private.h"
     43 
     44 struct ltsleeper {
     45 	wchan_t id;
     46 	struct rumpuser_cv *cv;
     47 	LIST_ENTRY(ltsleeper) entries;
     48 };
     49 
     50 static LIST_HEAD(, ltsleeper) sleepers = LIST_HEAD_INITIALIZER(sleepers);
     51 
     52 kcondvar_t lbolt; /* Oh Kath Ra */
     53 
     54 static int
     55 sleeper(struct ltsleeper *ltsp, int timo)
     56 {
     57 	int rv, nlocks;
     58 
     59 	LIST_INSERT_HEAD(&sleepers, ltsp, entries);
     60 	kernel_unlock_allbutone(&nlocks);
     61 
     62 	/* protected by biglock */
     63 	if (timo) {
     64 		rv = rumpuser_cv_timedwait(ltsp->cv, rump_giantlock,
     65 		    timo / hz, (timo % hz) * (1000000000/hz));
     66 	} else {
     67 		rumpuser_cv_wait(ltsp->cv, rump_giantlock);
     68 		rv = 0;
     69 	}
     70 
     71 	LIST_REMOVE(ltsp, entries);
     72 	rumpuser_cv_destroy(ltsp->cv);
     73 	kernel_ununlock_allbutone(nlocks);
     74 
     75 	return rv;
     76 }
     77 
     78 int
     79 ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
     80 	volatile struct simplelock *slock)
     81 {
     82 	struct ltsleeper lts;
     83 	int rv;
     84 
     85 	lts.id = ident;
     86 	rumpuser_cv_init(&lts.cv);
     87 
     88 	if (slock)
     89 		simple_unlock(slock);
     90 
     91 	rv = sleeper(&lts, timo);
     92 
     93 	if (slock && (prio & PNORELOCK) == 0)
     94 		simple_lock(slock);
     95 
     96 	return rv;
     97 }
     98 
     99 int
    100 mtsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
    101 	kmutex_t *lock)
    102 {
    103 	struct ltsleeper lts;
    104 	int rv;
    105 
    106 	lts.id = ident;
    107 	rumpuser_cv_init(&lts.cv);
    108 
    109 	mutex_exit(lock);
    110 
    111 	rv = sleeper(&lts, timo);
    112 
    113 	if ((prio & PNORELOCK) == 0)
    114 		mutex_enter(lock);
    115 
    116 	return rv;
    117 }
    118 
    119 static void
    120 do_wakeup(wchan_t ident, void (*wakeupfn)(struct rumpuser_cv *))
    121 {
    122 	struct ltsleeper *ltsp;
    123 
    124 	KASSERT(kernel_biglocked());
    125 	LIST_FOREACH(ltsp, &sleepers, entries) {
    126 		if (ltsp->id == ident) {
    127 			wakeupfn(ltsp->cv);
    128 		}
    129 	}
    130 }
    131 
    132 void
    133 wakeup(wchan_t ident)
    134 {
    135 
    136 	do_wakeup(ident, rumpuser_cv_broadcast);
    137 }
    138 
    139 void
    140 wakeup_one(wchan_t ident)
    141 {
    142 
    143 	do_wakeup(ident, rumpuser_cv_signal);
    144 }
    145 
    146 void
    147 rump_sleepers_init(void)
    148 {
    149 
    150 }
    151