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