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