ltsleep.c revision 1.7 1 /* $NetBSD: ltsleep.c,v 1.7 2008/07/29 13:17:47 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 lts.id = ident;
59 cv_init(<s.cv, NULL);
60
61 mutex_enter(&sleepermtx);
62 LIST_INSERT_HEAD(&sleepers, <s, entries);
63
64 /* release spl */
65 iplrecurse = rumpuser_whatis_ipl();
66 while (iplrecurse--)
67 rumpuser_rw_exit(&rumpspl);
68
69 /* protected by sleepermtx */
70 if (slock)
71 simple_unlock(slock);
72 cv_wait(<s.cv, &sleepermtx);
73
74 /* retake ipl */
75 iplrecurse = rumpuser_whatis_ipl();
76 while (iplrecurse--)
77 rumpuser_rw_enter(&rumpspl, 0);
78
79 LIST_REMOVE(<s, entries);
80 mutex_exit(&sleepermtx);
81
82 cv_destroy(<s.cv);
83
84 if (slock && (prio & PNORELOCK) == 0)
85 simple_lock(slock);
86
87 return 0;
88 }
89
90 int
91 mtsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
92 kmutex_t *lock)
93 {
94 struct ltsleeper lts;
95 int iplrecurse;
96
97 lts.id = ident;
98 cv_init(<s.cv, NULL);
99
100 mutex_enter(&sleepermtx);
101 LIST_INSERT_HEAD(&sleepers, <s, entries);
102
103 /* release spl */
104 iplrecurse = rumpuser_whatis_ipl();
105 while (iplrecurse--)
106 rumpuser_rw_exit(&rumpspl);
107
108 /* protected by sleepermtx */
109 mutex_exit(lock);
110 cv_wait(<s.cv, &sleepermtx);
111
112 /* retake ipl */
113 iplrecurse = rumpuser_whatis_ipl();
114 while (iplrecurse--)
115 rumpuser_rw_enter(&rumpspl, 0);
116
117 LIST_REMOVE(<s, entries);
118 mutex_exit(&sleepermtx);
119
120 cv_destroy(<s.cv);
121
122 if ((prio & PNORELOCK) == 0)
123 mutex_enter(lock);
124
125 return 0;
126 }
127
128 void
129 wakeup(wchan_t ident)
130 {
131 struct ltsleeper *ltsp;
132
133 mutex_enter(&sleepermtx);
134 LIST_FOREACH(ltsp, &sleepers, entries)
135 if (ltsp->id == ident)
136 cv_signal(<sp->cv);
137 mutex_exit(&sleepermtx);
138 }
139
140 void
141 rump_sleepers_init()
142 {
143
144 mutex_init(&sleepermtx, MUTEX_DEFAULT, 0);
145 }
146