xref: /netbsd-src/sys/rump/librump/rumpkern/ltsleep.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: ltsleep.c,v 1.3 2007/11/07 18:59:18 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 #include "rumpuser.h"
37 
38 struct ltsleeper {
39 	wchan_t id;
40 	kcondvar_t cv;
41 	LIST_ENTRY(ltsleeper) entries;
42 };
43 
44 static LIST_HEAD(, ltsleeper) sleepers = LIST_HEAD_INITIALIZER(sleepers);
45 static kmutex_t sleepermtx;
46 
47 int
48 ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
49 	volatile struct simplelock *slock)
50 {
51 	struct ltsleeper lts;
52 	int iplrecurse;
53 
54 	lts.id = ident;
55 	cv_init(&lts.cv, NULL);
56 
57 	mutex_enter(&sleepermtx);
58 	LIST_INSERT_HEAD(&sleepers, &lts, entries);
59 
60 	/* release spl */
61 	iplrecurse = rumpuser_whatis_ipl();
62 	while (iplrecurse--)
63 		rumpuser_rw_exit(&rumpspl);
64 
65 	/* protected by sleepermtx */
66 	if (slock)
67 		simple_unlock(slock);
68 	cv_wait(&lts.cv, &sleepermtx);
69 
70 	/* retake ipl */
71 	iplrecurse = rumpuser_whatis_ipl();
72 	while (iplrecurse--)
73 		rumpuser_rw_enter(&rumpspl, 0);
74 
75 	LIST_REMOVE(&lts, entries);
76 	mutex_exit(&sleepermtx);
77 
78 	cv_destroy(&lts.cv);
79 
80 	if (slock && (prio & PNORELOCK) == 0)
81 		simple_lock(slock);
82 
83 	return 0;
84 }
85 
86 void
87 wakeup(wchan_t ident)
88 {
89 	struct ltsleeper *ltsp;
90 
91 	mutex_enter(&sleepermtx);
92 	LIST_FOREACH(ltsp, &sleepers, entries)
93 		if (ltsp->id == ident)
94 			cv_signal(&ltsp->cv);
95 	mutex_exit(&sleepermtx);
96 }
97 
98 void
99 rump_sleepers_init()
100 {
101 
102 	mutex_init(&sleepermtx, MUTEX_DEFAULT, 0);
103 }
104