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