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