Home | History | Annotate | Line # | Download | only in rumpkern
ltsleep.c revision 1.8
      1 /*	$NetBSD: ltsleep.c,v 1.8 2008/10/09 13:39:59 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/param.h>
     32 #include <sys/proc.h>
     33 #include <sys/queue.h>
     34 #include <sys/simplelock.h>
     35 
     36 #include <rump/rumpuser.h>
     37 
     38 #include "rump_private.h"
     39 
     40 struct ltsleeper {
     41 	wchan_t id;
     42 	kcondvar_t cv;
     43 	LIST_ENTRY(ltsleeper) entries;
     44 };
     45 
     46 static LIST_HEAD(, ltsleeper) sleepers = LIST_HEAD_INITIALIZER(sleepers);
     47 static kmutex_t sleepermtx;
     48 
     49 kcondvar_t lbolt; /* Oh Kath Ra */
     50 
     51 int
     52 ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
     53 	volatile struct simplelock *slock)
     54 {
     55 	struct ltsleeper lts;
     56 	int iplrecurse;
     57 
     58 	if (__predict_false(slock))
     59 		panic("simplelock not supported by rump, convert code");
     60 
     61 	lts.id = ident;
     62 	cv_init(&lts.cv, NULL);
     63 
     64 	mutex_enter(&sleepermtx);
     65 	LIST_INSERT_HEAD(&sleepers, &lts, entries);
     66 
     67 	/* release spl */
     68 	iplrecurse = rumpuser_whatis_ipl();
     69 	while (iplrecurse--)
     70 		rumpuser_rw_exit(&rumpspl);
     71 
     72 	/* protected by sleepermtx */
     73 	cv_wait(&lts.cv, &sleepermtx);
     74 
     75 	/* retake ipl */
     76 	iplrecurse = rumpuser_whatis_ipl();
     77 	while (iplrecurse--)
     78 		rumpuser_rw_enter(&rumpspl, 0);
     79 
     80 	LIST_REMOVE(&lts, entries);
     81 	mutex_exit(&sleepermtx);
     82 
     83 	cv_destroy(&lts.cv);
     84 
     85 	return 0;
     86 }
     87 
     88 int
     89 mtsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
     90 	kmutex_t *lock)
     91 {
     92 	struct ltsleeper lts;
     93 	int iplrecurse;
     94 
     95 	lts.id = ident;
     96 	cv_init(&lts.cv, NULL);
     97 
     98 	mutex_enter(&sleepermtx);
     99 	LIST_INSERT_HEAD(&sleepers, &lts, entries);
    100 
    101 	/* release spl */
    102 	iplrecurse = rumpuser_whatis_ipl();
    103 	while (iplrecurse--)
    104 		rumpuser_rw_exit(&rumpspl);
    105 
    106 	/* protected by sleepermtx */
    107 	mutex_exit(lock);
    108 	cv_wait(&lts.cv, &sleepermtx);
    109 
    110 	/* retake ipl */
    111 	iplrecurse = rumpuser_whatis_ipl();
    112 	while (iplrecurse--)
    113 		rumpuser_rw_enter(&rumpspl, 0);
    114 
    115 	LIST_REMOVE(&lts, entries);
    116 	mutex_exit(&sleepermtx);
    117 
    118 	cv_destroy(&lts.cv);
    119 
    120 	if ((prio & PNORELOCK) == 0)
    121 		mutex_enter(lock);
    122 
    123 	return 0;
    124 }
    125 
    126 void
    127 wakeup(wchan_t ident)
    128 {
    129 	struct ltsleeper *ltsp;
    130 
    131 	mutex_enter(&sleepermtx);
    132 	LIST_FOREACH(ltsp, &sleepers, entries)
    133 		if (ltsp->id == ident)
    134 			cv_signal(&ltsp->cv);
    135 	mutex_exit(&sleepermtx);
    136 }
    137 
    138 void
    139 rump_sleepers_init()
    140 {
    141 
    142 	mutex_init(&sleepermtx, MUTEX_DEFAULT, 0);
    143 }
    144