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