kern_condvar.c revision 1.1.2.6 1 1.1.2.6 ad /* $NetBSD: kern_condvar.c,v 1.1.2.6 2007/02/05 13:16:11 ad Exp $ */
2 1.1.2.1 ad
3 1.1.2.1 ad /*-
4 1.1.2.5 ad * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
5 1.1.2.1 ad * All rights reserved.
6 1.1.2.1 ad *
7 1.1.2.1 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 ad * by Andrew Doran.
9 1.1.2.1 ad *
10 1.1.2.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 ad * modification, are permitted provided that the following conditions
12 1.1.2.1 ad * are met:
13 1.1.2.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 ad * documentation and/or other materials provided with the distribution.
18 1.1.2.1 ad * 3. All advertising materials mentioning features or use of this software
19 1.1.2.1 ad * must display the following acknowledgement:
20 1.1.2.1 ad * This product includes software developed by the NetBSD
21 1.1.2.1 ad * Foundation, Inc. and its contributors.
22 1.1.2.1 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.2.1 ad * contributors may be used to endorse or promote products derived
24 1.1.2.1 ad * from this software without specific prior written permission.
25 1.1.2.1 ad *
26 1.1.2.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.2.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.2.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.2.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.2.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.2.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.2.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.2.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.2.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.2.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.2.1 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.1.2.1 ad */
38 1.1.2.1 ad
39 1.1.2.1 ad /*
40 1.1.2.2 ad * Kernel condition variable implementation, modeled after those found in
41 1.1.2.1 ad * Solaris, a description of which can be found in:
42 1.1.2.1 ad *
43 1.1.2.1 ad * Solaris Internals: Core Kernel Architecture, Jim Mauro and
44 1.1.2.1 ad * Richard McDougall.
45 1.1.2.1 ad */
46 1.1.2.1 ad
47 1.1.2.1 ad #include <sys/cdefs.h>
48 1.1.2.6 ad __KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.1.2.6 2007/02/05 13:16:11 ad Exp $");
49 1.1.2.1 ad
50 1.1.2.1 ad #include <sys/param.h>
51 1.1.2.1 ad #include <sys/proc.h>
52 1.1.2.1 ad #include <sys/sched.h>
53 1.1.2.1 ad #include <sys/systm.h>
54 1.1.2.1 ad #include <sys/condvar.h>
55 1.1.2.1 ad #include <sys/sleepq.h>
56 1.1.2.1 ad
57 1.1.2.2 ad void cv_unsleep(struct lwp *);
58 1.1.2.2 ad
59 1.1.2.2 ad syncobj_t cv_syncobj = {
60 1.1.2.2 ad SOBJ_SLEEPQ_SORTED,
61 1.1.2.2 ad cv_unsleep,
62 1.1.2.2 ad sleepq_changepri
63 1.1.2.2 ad };
64 1.1.2.2 ad
65 1.1.2.1 ad /*
66 1.1.2.1 ad * cv_init:
67 1.1.2.1 ad *
68 1.1.2.1 ad * Initialize a condition variable for use.
69 1.1.2.1 ad */
70 1.1.2.1 ad void
71 1.1.2.2 ad cv_init(kcondvar_t *cv, const char *wmesg)
72 1.1.2.1 ad {
73 1.1.2.1 ad
74 1.1.2.2 ad KASSERT(wmesg != NULL);
75 1.1.2.2 ad
76 1.1.2.1 ad cv->cv_wmesg = wmesg;
77 1.1.2.4 ad cv->cv_waiters = 0;
78 1.1.2.1 ad }
79 1.1.2.1 ad
80 1.1.2.1 ad /*
81 1.1.2.1 ad * cv_destroy:
82 1.1.2.1 ad *
83 1.1.2.1 ad * Tear down a condition variable.
84 1.1.2.1 ad */
85 1.1.2.1 ad void
86 1.1.2.1 ad cv_destroy(kcondvar_t *cv)
87 1.1.2.1 ad {
88 1.1.2.1 ad
89 1.1.2.4 ad #ifdef DIAGNOSTIC
90 1.1.2.2 ad KASSERT(cv->cv_waiters == 0 && cv->cv_wmesg != NULL);
91 1.1.2.4 ad cv->cv_wmesg = NULL;
92 1.1.2.4 ad #endif
93 1.1.2.2 ad }
94 1.1.2.2 ad
95 1.1.2.2 ad /*
96 1.1.2.2 ad * cv_enter:
97 1.1.2.2 ad *
98 1.1.2.2 ad * Look up and lock the sleep queue corresponding to the given
99 1.1.2.2 ad * condition variable, and increment the number of waiters.
100 1.1.2.2 ad */
101 1.1.2.2 ad static inline sleepq_t *
102 1.1.2.4 ad cv_enter(kcondvar_t *cv, kmutex_t *mtx, struct lwp *l)
103 1.1.2.2 ad {
104 1.1.2.2 ad sleepq_t *sq;
105 1.1.2.2 ad
106 1.1.2.2 ad KASSERT(cv->cv_wmesg != NULL);
107 1.1.2.2 ad
108 1.1.2.2 ad sq = sleeptab_lookup(&sleeptab, cv);
109 1.1.2.2 ad cv->cv_waiters++;
110 1.1.2.4 ad sleepq_enter(sq, l);
111 1.1.2.4 ad mutex_exit(mtx);
112 1.1.2.2 ad
113 1.1.2.2 ad return sq;
114 1.1.2.2 ad }
115 1.1.2.2 ad
116 1.1.2.2 ad /*
117 1.1.2.2 ad * cv_unsleep:
118 1.1.2.2 ad *
119 1.1.2.2 ad * Remove an LWP from the condition variable and sleep queue. This
120 1.1.2.2 ad * is called when the LWP has not been awoken normally but instead
121 1.1.2.5 ad * interrupted: for example, when a signal is received. Must be
122 1.1.2.5 ad * called with the LWP locked, and must return it unlocked.
123 1.1.2.2 ad */
124 1.1.2.2 ad void
125 1.1.2.2 ad cv_unsleep(struct lwp *l)
126 1.1.2.2 ad {
127 1.1.2.2 ad uintptr_t addr;
128 1.1.2.2 ad
129 1.1.2.2 ad KASSERT(l->l_wchan != NULL);
130 1.1.2.2 ad LOCK_ASSERT(lwp_locked(l, l->l_sleepq->sq_mutex));
131 1.1.2.2 ad
132 1.1.2.2 ad addr = (uintptr_t)l->l_wchan;
133 1.1.2.2 ad ((kcondvar_t *)addr)->cv_waiters--;
134 1.1.2.2 ad
135 1.1.2.2 ad sleepq_unsleep(l);
136 1.1.2.1 ad }
137 1.1.2.1 ad
138 1.1.2.1 ad /*
139 1.1.2.1 ad * cv_wait:
140 1.1.2.1 ad *
141 1.1.2.1 ad * Wait non-interruptably on a condition variable until awoken.
142 1.1.2.1 ad */
143 1.1.2.1 ad void
144 1.1.2.1 ad cv_wait(kcondvar_t *cv, kmutex_t *mtx)
145 1.1.2.1 ad {
146 1.1.2.1 ad struct lwp *l = curlwp;
147 1.1.2.1 ad sleepq_t *sq;
148 1.1.2.1 ad
149 1.1.2.1 ad LOCK_ASSERT(mutex_owned(mtx));
150 1.1.2.1 ad
151 1.1.2.1 ad if (sleepq_dontsleep(l)) {
152 1.1.2.1 ad (void)sleepq_abort(mtx, 0);
153 1.1.2.1 ad return;
154 1.1.2.1 ad }
155 1.1.2.1 ad
156 1.1.2.4 ad sq = cv_enter(cv, mtx, l);
157 1.1.2.4 ad sleepq_block(sq, sched_kpri(l), cv, cv->cv_wmesg, 0, 0,
158 1.1.2.2 ad &cv_syncobj);
159 1.1.2.4 ad (void)sleepq_unblock(0, 0);
160 1.1.2.1 ad mutex_enter(mtx);
161 1.1.2.1 ad }
162 1.1.2.1 ad
163 1.1.2.1 ad /*
164 1.1.2.1 ad * cv_wait_sig:
165 1.1.2.1 ad *
166 1.1.2.1 ad * Wait on a condition variable until a awoken or a signal is received.
167 1.1.2.1 ad * Will also return early if the process is exiting. Returns zero if
168 1.1.2.1 ad * awoken normallly, ERESTART if a signal was received and the system
169 1.1.2.1 ad * call is restartable, or EINTR otherwise.
170 1.1.2.1 ad */
171 1.1.2.1 ad int
172 1.1.2.1 ad cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
173 1.1.2.1 ad {
174 1.1.2.1 ad struct lwp *l = curlwp;
175 1.1.2.1 ad sleepq_t *sq;
176 1.1.2.1 ad int error;
177 1.1.2.1 ad
178 1.1.2.1 ad LOCK_ASSERT(mutex_owned(mtx));
179 1.1.2.1 ad
180 1.1.2.1 ad if (sleepq_dontsleep(l))
181 1.1.2.1 ad return sleepq_abort(mtx, 0);
182 1.1.2.1 ad
183 1.1.2.4 ad sq = cv_enter(cv, mtx, l);
184 1.1.2.4 ad sleepq_block(sq, sched_kpri(l), cv, cv->cv_wmesg, 0, 1,
185 1.1.2.2 ad &cv_syncobj);
186 1.1.2.4 ad error = sleepq_unblock(0, 1);
187 1.1.2.1 ad mutex_enter(mtx);
188 1.1.2.2 ad
189 1.1.2.1 ad return error;
190 1.1.2.1 ad }
191 1.1.2.1 ad
192 1.1.2.1 ad /*
193 1.1.2.1 ad * cv_timedwait:
194 1.1.2.1 ad *
195 1.1.2.1 ad * Wait on a condition variable until awoken or the specified timeout
196 1.1.2.1 ad * expires. Returns zero if awoken normally or EWOULDBLOCK if the
197 1.1.2.1 ad * timeout expired.
198 1.1.2.1 ad */
199 1.1.2.1 ad int
200 1.1.2.1 ad cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int timo)
201 1.1.2.1 ad {
202 1.1.2.1 ad struct lwp *l = curlwp;
203 1.1.2.1 ad sleepq_t *sq;
204 1.1.2.1 ad int error;
205 1.1.2.1 ad
206 1.1.2.1 ad LOCK_ASSERT(mutex_owned(mtx));
207 1.1.2.1 ad
208 1.1.2.1 ad if (sleepq_dontsleep(l))
209 1.1.2.1 ad return sleepq_abort(mtx, 0);
210 1.1.2.1 ad
211 1.1.2.4 ad sq = cv_enter(cv, mtx, l);
212 1.1.2.4 ad sleepq_block(sq, sched_kpri(l), cv, cv->cv_wmesg, timo, 0,
213 1.1.2.2 ad &cv_syncobj);
214 1.1.2.4 ad error = sleepq_unblock(timo, 0);
215 1.1.2.1 ad mutex_enter(mtx);
216 1.1.2.2 ad
217 1.1.2.2 ad return error;
218 1.1.2.1 ad }
219 1.1.2.1 ad
220 1.1.2.1 ad /*
221 1.1.2.1 ad * cv_timedwait_sig:
222 1.1.2.1 ad *
223 1.1.2.1 ad * Wait on a condition variable until a timeout expires, awoken or a
224 1.1.2.1 ad * signal is received. Will also return early if the process is
225 1.1.2.1 ad * exiting. Returns zero if awoken normallly, EWOULDBLOCK if the
226 1.1.2.1 ad * timeout expires, ERESTART if a signal was received and the system
227 1.1.2.1 ad * call is restartable, or EINTR otherwise.
228 1.1.2.1 ad */
229 1.1.2.1 ad int
230 1.1.2.1 ad cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int timo)
231 1.1.2.1 ad {
232 1.1.2.1 ad struct lwp *l = curlwp;
233 1.1.2.1 ad sleepq_t *sq;
234 1.1.2.1 ad int error;
235 1.1.2.1 ad
236 1.1.2.1 ad LOCK_ASSERT(mutex_owned(mtx));
237 1.1.2.1 ad
238 1.1.2.1 ad if (sleepq_dontsleep(l))
239 1.1.2.1 ad return sleepq_abort(mtx, 0);
240 1.1.2.1 ad
241 1.1.2.4 ad sq = cv_enter(cv, mtx, l);
242 1.1.2.4 ad sleepq_block(sq, sched_kpri(l), cv, cv->cv_wmesg, timo, 1,
243 1.1.2.2 ad &cv_syncobj);
244 1.1.2.4 ad error = sleepq_unblock(timo, 1);
245 1.1.2.1 ad mutex_enter(mtx);
246 1.1.2.2 ad
247 1.1.2.2 ad return error;
248 1.1.2.1 ad }
249 1.1.2.1 ad
250 1.1.2.1 ad /*
251 1.1.2.1 ad * cv_signal:
252 1.1.2.1 ad *
253 1.1.2.1 ad * Wake the highest priority LWP waiting on a condition variable.
254 1.1.2.5 ad * Must be called with the interlocking mutex held.
255 1.1.2.1 ad */
256 1.1.2.1 ad void
257 1.1.2.1 ad cv_signal(kcondvar_t *cv)
258 1.1.2.1 ad {
259 1.1.2.1 ad sleepq_t *sq;
260 1.1.2.1 ad
261 1.1.2.5 ad if (cv->cv_waiters == 0)
262 1.1.2.5 ad return;
263 1.1.2.5 ad
264 1.1.2.5 ad /*
265 1.1.2.5 ad * cv->cv_waiters may be stale and have dropped to zero, but
266 1.1.2.5 ad * while holding the interlock (the mutex passed to cv_wait()
267 1.1.2.5 ad * and similar) we will see non-zero values when it matters.
268 1.1.2.5 ad */
269 1.1.2.5 ad
270 1.1.2.2 ad sq = sleeptab_lookup(&sleeptab, cv);
271 1.1.2.1 ad if (cv->cv_waiters != 0) {
272 1.1.2.1 ad cv->cv_waiters--;
273 1.1.2.3 ad sleepq_wake(sq, cv, 1);
274 1.1.2.1 ad } else
275 1.1.2.1 ad sleepq_unlock(sq);
276 1.1.2.1 ad }
277 1.1.2.1 ad
278 1.1.2.1 ad /*
279 1.1.2.1 ad * cv_broadcast:
280 1.1.2.1 ad *
281 1.1.2.5 ad * Wake all LWPs waiting on a condition variable. Must be called
282 1.1.2.5 ad * with the interlocking mutex held.
283 1.1.2.1 ad */
284 1.1.2.1 ad void
285 1.1.2.1 ad cv_broadcast(kcondvar_t *cv)
286 1.1.2.1 ad {
287 1.1.2.1 ad sleepq_t *sq;
288 1.1.2.3 ad u_int cnt;
289 1.1.2.1 ad
290 1.1.2.5 ad if (cv->cv_waiters == 0)
291 1.1.2.5 ad return;
292 1.1.2.5 ad
293 1.1.2.5 ad sq = sleeptab_lookup(&sleeptab, cv);
294 1.1.2.5 ad if ((cnt = cv->cv_waiters) != 0) {
295 1.1.2.5 ad cv->cv_waiters = 0;
296 1.1.2.5 ad sleepq_wake(sq, cv, cnt);
297 1.1.2.5 ad } else
298 1.1.2.5 ad sleepq_unlock(sq);
299 1.1.2.5 ad }
300 1.1.2.5 ad
301 1.1.2.5 ad /*
302 1.1.2.6 ad * cv_wakeup:
303 1.1.2.5 ad *
304 1.1.2.6 ad * Wake all LWPs waiting on a condition variable. The interlock
305 1.1.2.6 ad * need not be held, but it is the caller's responsibility to
306 1.1.2.6 ad * ensure correct synchronization.
307 1.1.2.5 ad */
308 1.1.2.5 ad void
309 1.1.2.6 ad cv_wakeup(kcondvar_t *cv)
310 1.1.2.5 ad {
311 1.1.2.5 ad sleepq_t *sq;
312 1.1.2.5 ad u_int cnt;
313 1.1.2.5 ad
314 1.1.2.2 ad sq = sleeptab_lookup(&sleeptab, cv);
315 1.1.2.3 ad if ((cnt = cv->cv_waiters) != 0) {
316 1.1.2.1 ad cv->cv_waiters = 0;
317 1.1.2.3 ad sleepq_wake(sq, cv, cnt);
318 1.1.2.1 ad } else
319 1.1.2.1 ad sleepq_unlock(sq);
320 1.1.2.1 ad }
321 1.1.2.4 ad
322 1.1.2.4 ad /*
323 1.1.2.4 ad * cv_has_waiters:
324 1.1.2.4 ad *
325 1.1.2.4 ad * For diagnostic assertions: return non-zero if a condition
326 1.1.2.4 ad * variable has waiters.
327 1.1.2.4 ad */
328 1.1.2.4 ad int
329 1.1.2.4 ad cv_has_waiters(kcondvar_t *cv)
330 1.1.2.4 ad {
331 1.1.2.4 ad
332 1.1.2.4 ad /* No need to interlock here */
333 1.1.2.4 ad return (int)cv->cv_waiters;
334 1.1.2.4 ad }
335