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