ltsleep.c revision 1.24 1 /* $NetBSD: ltsleep.c,v 1.24 2009/12/20 13:49:36 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 /*
32 * Emulate the prehistoric tsleep-style kernel interfaces. We assume
33 * only code under the biglock will be using this type of synchronization
34 * and use the biglock as the cv interlock.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ltsleep.c,v 1.24 2009/12/20 13:49:36 pooka Exp $");
39
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/proc.h>
43 #include <sys/queue.h>
44 #include <sys/simplelock.h>
45
46 #include <rump/rumpuser.h>
47
48 #include "rump_private.h"
49
50 struct ltsleeper {
51 wchan_t id;
52 struct rumpuser_cv *cv;
53 LIST_ENTRY(ltsleeper) entries;
54 };
55
56 static LIST_HEAD(, ltsleeper) sleepers = LIST_HEAD_INITIALIZER(sleepers);
57
58 static int
59 sleeper(struct ltsleeper *ltsp, int timo)
60 {
61 struct timespec ts, ticks;
62 int rv, nlocks;
63
64 LIST_INSERT_HEAD(&sleepers, ltsp, entries);
65 kernel_unlock_allbutone(&nlocks);
66
67 /* protected by biglock */
68 if (timo) {
69 ticks.tv_sec = timo / hz;
70 ticks.tv_nsec = (timo % hz) * (1000000000/hz);
71 nanotime(&ts);
72 timespecadd(&ts, &ticks, &ts);
73 if (rumpuser_cv_timedwait(ltsp->cv, rump_giantlock,
74 ts.tv_sec, ts.tv_nsec) == 0)
75 rv = 0;
76 else
77 rv = EWOULDBLOCK;
78 } else {
79 rumpuser_cv_wait(ltsp->cv, rump_giantlock);
80 rv = 0;
81 }
82
83 LIST_REMOVE(ltsp, entries);
84 rumpuser_cv_destroy(ltsp->cv);
85 kernel_ununlock_allbutone(nlocks);
86
87 return rv;
88 }
89
90 int
91 ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
92 volatile struct simplelock *slock)
93 {
94 struct ltsleeper lts;
95 int rv;
96
97 lts.id = ident;
98 rumpuser_cv_init(<s.cv);
99
100 if (slock)
101 simple_unlock(slock);
102
103 rv = sleeper(<s, timo);
104
105 if (slock && (prio & PNORELOCK) == 0)
106 simple_lock(slock);
107
108 return rv;
109 }
110
111 int
112 mtsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
113 kmutex_t *lock)
114 {
115 struct ltsleeper lts;
116 int rv;
117
118 lts.id = ident;
119 rumpuser_cv_init(<s.cv);
120
121 mutex_exit(lock);
122
123 rv = sleeper(<s, timo);
124
125 if ((prio & PNORELOCK) == 0)
126 mutex_enter(lock);
127
128 return rv;
129 }
130
131 static void
132 do_wakeup(wchan_t ident, void (*wakeupfn)(struct rumpuser_cv *))
133 {
134 struct ltsleeper *ltsp;
135
136 KASSERT(kernel_biglocked());
137 LIST_FOREACH(ltsp, &sleepers, entries) {
138 if (ltsp->id == ident) {
139 wakeupfn(ltsp->cv);
140 }
141 }
142 }
143
144 void
145 wakeup(wchan_t ident)
146 {
147
148 do_wakeup(ident, rumpuser_cv_broadcast);
149 }
150
151 void
152 wakeup_one(wchan_t ident)
153 {
154
155 do_wakeup(ident, rumpuser_cv_signal);
156 }
157