Home | History | Annotate | Line # | Download | only in rumpkern
ltsleep.c revision 1.1
      1 /*	$NetBSD: ltsleep.c,v 1.1 2007/10/31 15:57:21 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 
     35 #include "rump_private.h"
     36 
     37 struct ltsleeper {
     38 	wchan_t id;
     39 	kcondvar_t cv;
     40 	LIST_ENTRY(ltsleeper) entries;
     41 };
     42 
     43 static LIST_HEAD(, ltsleeper) sleepers = LIST_HEAD_INITIALIZER(sleepers);
     44 static kmutex_t sleepermtx;
     45 
     46 int
     47 ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
     48 	volatile struct simplelock *slock)
     49 {
     50 	struct ltsleeper lts;
     51 
     52 	lts.id = ident;
     53 	cv_init(&lts.cv, NULL);
     54 
     55 	mutex_enter(&sleepermtx);
     56 	LIST_INSERT_HEAD(&sleepers, &lts, entries);
     57 	/*
     58 	 * XXX: this is SOOOO WRONG, but I don't have any bright ideas.
     59 	 * ltsleep() is going away in any case, so considering this as
     60 	 * a punishment for your wrongdoing for still using it ....
     61 	 * or something
     62 	 */
     63 	if (slock)
     64 		simple_unlock(slock);
     65 	cv_wait(&lts.cv, &sleepermtx);
     66 	LIST_REMOVE(&lts, entries);
     67 	mutex_exit(&sleepermtx);
     68 
     69 	if (slock && (prio & PNORELOCK) == 0)
     70 		simple_lock(slock);
     71 
     72 	cv_destroy(&lts.cv);
     73 
     74 	return 0;
     75 }
     76 
     77 void
     78 wakeup(wchan_t ident)
     79 {
     80 	struct ltsleeper *ltsp;
     81 
     82 	mutex_enter(&sleepermtx);
     83 	LIST_FOREACH(ltsp, &sleepers, entries)
     84 		if (ltsp->id == ident)
     85 			cv_signal(&ltsp->cv);
     86 	mutex_exit(&sleepermtx);
     87 }
     88 
     89 void
     90 rump_sleepers_init()
     91 {
     92 
     93 	mutex_init(&sleepermtx, MUTEX_DEFAULT, 0);
     94 }
     95