Home | History | Annotate | Line # | Download | only in rumpkern
ltsleep.c revision 1.32
      1 /*	$NetBSD: ltsleep.c,v 1.32 2013/05/15 12:49:33 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2009, 2010 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * Implementation of the tsleep/mtsleep kernel sleep interface.  There
     30  * are two sides to our implementation.  For historic spinlocks we
     31  * assume the kernel is giantlocked and use kernel giantlock as the
     32  * wait interlock.  For mtsleep, we use the interlock supplied by
     33  * the caller.  This duality leads to some if/else messiness in the code ...
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: ltsleep.c,v 1.32 2013/05/15 12:49:33 pooka Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/kernel.h>
     41 #include <sys/proc.h>
     42 #include <sys/queue.h>
     43 
     44 #include <rump/rumpuser.h>
     45 
     46 #include "rump_private.h"
     47 
     48 struct ltsleeper {
     49 	wchan_t id;
     50 	union {
     51 		struct rumpuser_cv *user;
     52 		kcondvar_t kern;
     53 	} u;
     54 	bool iskwait;
     55 	LIST_ENTRY(ltsleeper) entries;
     56 };
     57 #define ucv u.user
     58 #define kcv u.kern
     59 
     60 static LIST_HEAD(, ltsleeper) sleepers = LIST_HEAD_INITIALIZER(sleepers);
     61 static kmutex_t *qlock;
     62 
     63 static int
     64 sleeper(wchan_t ident, int timo, kmutex_t *kinterlock)
     65 {
     66 	struct ltsleeper lts;
     67 	struct timespec ts;
     68 	int rv;
     69 
     70 	lts.id = ident;
     71 	if (kinterlock) {
     72 		lts.iskwait = true;
     73 		cv_init(&lts.kcv, "mtsleep");
     74 	} else {
     75 		lts.iskwait = false;
     76 		rumpuser_cv_init(&lts.ucv);
     77 	}
     78 
     79 	mutex_spin_enter(&qlock);
     80 	LIST_INSERT_HEAD(&sleepers, &lts, entries);
     81 	mutex_exit(&qlock);
     82 
     83 	if (timo) {
     84 		if (kinterlock) {
     85 			rv = cv_timedwait(&lts.kcv, kinterlock, timo);
     86 		} else {
     87 			/*
     88 			 * Calculate wakeup-time.
     89 			 */
     90 			ts.tv_sec = timo / hz;
     91 			ts.tv_nsec = (timo % hz) * (1000000000/hz);
     92 			rv = rumpuser_cv_timedwait(lts.ucv, rump_giantlock,
     93 			    ts.tv_sec, ts.tv_nsec);
     94 		}
     95 
     96 		if (rv != 0)
     97 			rv = EWOULDBLOCK;
     98 	} else {
     99 		if (kinterlock) {
    100 			cv_wait(&lts.kcv, kinterlock);
    101 		} else {
    102 			rumpuser_cv_wait(lts.ucv, rump_giantlock);
    103 		}
    104 		rv = 0;
    105 	}
    106 
    107 	mutex_spin_enter(&qlock);
    108 	LIST_REMOVE(&lts, entries);
    109 	mutex_exit(&qlock);
    110 
    111 	if (kinterlock)
    112 		cv_destroy(&lts.kcv);
    113 	else
    114 		rumpuser_cv_destroy(lts.ucv);
    115 
    116 	return rv;
    117 }
    118 
    119 int
    120 tsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo)
    121 {
    122 	int rv, nlocks;
    123 
    124 	/*
    125 	 * Since we cannot use slock as the rumpuser interlock,
    126 	 * require that everyone using this prehistoric interface
    127 	 * is biglocked.  Wrap around the biglock and drop lockcnt,
    128 	 * but retain the rumpuser mutex so that we can use it as an
    129 	 * interlock to rumpuser_cv_wait().
    130 	 */
    131 	rump_kernel_bigwrap(&nlocks);
    132 	rv = sleeper(ident, timo, NULL);
    133 	rump_kernel_bigunwrap(nlocks);
    134 
    135 	return rv;
    136 }
    137 
    138 int
    139 mtsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo, kmutex_t *lock)
    140 {
    141 	int rv;
    142 
    143 	rv = sleeper(ident, timo, lock);
    144 	if (prio & PNORELOCK)
    145 		mutex_exit(lock);
    146 
    147 	return rv;
    148 }
    149 
    150 void
    151 wakeup(wchan_t ident)
    152 {
    153 	struct ltsleeper *ltsp;
    154 
    155 	mutex_spin_enter(&qlock);
    156 	LIST_FOREACH(ltsp, &sleepers, entries) {
    157 		if (ltsp->id == ident) {
    158 			if (ltsp->iskwait) {
    159 				cv_broadcast(&ltsp->kcv);
    160 			} else {
    161 				rumpuser_cv_broadcast(ltsp->ucv);
    162 			}
    163 		}
    164 	}
    165 	mutex_exit(&qlock);
    166 }
    167 
    168 void
    169 rump_tsleep_init()
    170 {
    171 
    172 	mutex_init(&qlock, MUTEX_SPIN, IPL_NONE);
    173 }
    174