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