kern_condvar.c revision 1.3.4.4 1 1.3.4.4 yamt /* $NetBSD: kern_condvar.c,v 1.3.4.4 2007/10/27 11:35:20 yamt Exp $ */
2 1.3.4.2 yamt
3 1.3.4.2 yamt /*-
4 1.3.4.2 yamt * Copyright (c) 2006, 2007 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.4 yamt __KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.3.4.4 2007/10/27 11:35:20 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.3 yamt static void cv_unsleep(lwp_t *);
58 1.3.4.3 yamt static void cv_changepri(lwp_t *, pri_t);
59 1.3.4.2 yamt
60 1.3.4.3 yamt static syncobj_t cv_syncobj = {
61 1.3.4.2 yamt SOBJ_SLEEPQ_SORTED,
62 1.3.4.2 yamt cv_unsleep,
63 1.3.4.2 yamt cv_changepri,
64 1.3.4.3 yamt sleepq_lendpri,
65 1.3.4.3 yamt syncobj_noowner,
66 1.3.4.2 yamt };
67 1.3.4.2 yamt
68 1.3.4.3 yamt static const char deadcv[] = "deadcv";
69 1.3.4.3 yamt
70 1.3.4.2 yamt /*
71 1.3.4.2 yamt * cv_init:
72 1.3.4.2 yamt *
73 1.3.4.2 yamt * Initialize a condition variable for use.
74 1.3.4.2 yamt */
75 1.3.4.2 yamt void
76 1.3.4.2 yamt cv_init(kcondvar_t *cv, const char *wmesg)
77 1.3.4.2 yamt {
78 1.3.4.2 yamt
79 1.3.4.2 yamt KASSERT(wmesg != NULL);
80 1.3.4.2 yamt
81 1.3.4.2 yamt cv->cv_wmesg = wmesg;
82 1.3.4.2 yamt cv->cv_waiters = 0;
83 1.3.4.2 yamt }
84 1.3.4.2 yamt
85 1.3.4.2 yamt /*
86 1.3.4.2 yamt * cv_destroy:
87 1.3.4.2 yamt *
88 1.3.4.2 yamt * Tear down a condition variable.
89 1.3.4.2 yamt */
90 1.3.4.2 yamt void
91 1.3.4.2 yamt cv_destroy(kcondvar_t *cv)
92 1.3.4.2 yamt {
93 1.3.4.2 yamt
94 1.3.4.2 yamt #ifdef DIAGNOSTIC
95 1.3.4.3 yamt KASSERT(cv->cv_wmesg != deadcv && cv->cv_wmesg != NULL);
96 1.3.4.3 yamt KASSERT(cv->cv_waiters == 0);
97 1.3.4.3 yamt cv->cv_wmesg = deadcv;
98 1.3.4.2 yamt #endif
99 1.3.4.2 yamt }
100 1.3.4.2 yamt
101 1.3.4.2 yamt /*
102 1.3.4.2 yamt * cv_enter:
103 1.3.4.2 yamt *
104 1.3.4.2 yamt * Look up and lock the sleep queue corresponding to the given
105 1.3.4.2 yamt * condition variable, and increment the number of waiters.
106 1.3.4.2 yamt */
107 1.3.4.2 yamt static inline sleepq_t *
108 1.3.4.3 yamt cv_enter(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l)
109 1.3.4.2 yamt {
110 1.3.4.2 yamt sleepq_t *sq;
111 1.3.4.2 yamt
112 1.3.4.3 yamt KASSERT(cv->cv_wmesg != deadcv && cv->cv_wmesg != NULL);
113 1.3.4.4 yamt KASSERT((l->l_flag & LW_INTR) == 0 || panicstr != NULL);
114 1.3.4.2 yamt
115 1.3.4.3 yamt l->l_cv_signalled = 0;
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.3 yamt sleepq_enqueue(sq, sched_kpri(l), 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.3 yamt KASSERT(cv->cv_wmesg != deadcv && cv->cv_wmesg != NULL);
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.2 yamt static void
157 1.3.4.3 yamt cv_unsleep(lwp_t *l)
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.2 yamt KASSERT(l->l_wchan != NULL);
162 1.3.4.3 yamt KASSERT(lwp_locked(l, l->l_sleepq->sq_mutex));
163 1.3.4.2 yamt
164 1.3.4.3 yamt cv = (kcondvar_t *)(uintptr_t)l->l_wchan;
165 1.3.4.3 yamt KASSERT(cv->cv_wmesg != deadcv && cv->cv_wmesg != NULL);
166 1.3.4.3 yamt cv->cv_waiters--;
167 1.3.4.2 yamt
168 1.3.4.2 yamt sleepq_unsleep(l);
169 1.3.4.2 yamt }
170 1.3.4.2 yamt
171 1.3.4.2 yamt /*
172 1.3.4.2 yamt * cv_changepri:
173 1.3.4.2 yamt *
174 1.3.4.2 yamt * Adjust the real (user) priority of an LWP blocked on a CV.
175 1.3.4.2 yamt */
176 1.3.4.2 yamt static void
177 1.3.4.3 yamt cv_changepri(lwp_t *l, pri_t pri)
178 1.3.4.2 yamt {
179 1.3.4.2 yamt sleepq_t *sq = l->l_sleepq;
180 1.3.4.3 yamt pri_t opri;
181 1.3.4.2 yamt
182 1.3.4.2 yamt KASSERT(lwp_locked(l, sq->sq_mutex));
183 1.3.4.2 yamt
184 1.3.4.3 yamt opri = lwp_eprio(l);
185 1.3.4.2 yamt l->l_usrpri = pri;
186 1.3.4.2 yamt l->l_priority = sched_kpri(l);
187 1.3.4.2 yamt
188 1.3.4.3 yamt if (lwp_eprio(l) != opri) {
189 1.3.4.2 yamt TAILQ_REMOVE(&sq->sq_queue, l, l_sleepchain);
190 1.3.4.3 yamt sleepq_insert(sq, l, l->l_syncobj);
191 1.3.4.2 yamt }
192 1.3.4.2 yamt }
193 1.3.4.2 yamt
194 1.3.4.2 yamt /*
195 1.3.4.2 yamt * cv_wait:
196 1.3.4.2 yamt *
197 1.3.4.2 yamt * Wait non-interruptably on a condition variable until awoken.
198 1.3.4.2 yamt */
199 1.3.4.2 yamt void
200 1.3.4.2 yamt cv_wait(kcondvar_t *cv, kmutex_t *mtx)
201 1.3.4.2 yamt {
202 1.3.4.3 yamt lwp_t *l = curlwp;
203 1.3.4.2 yamt sleepq_t *sq;
204 1.3.4.2 yamt
205 1.3.4.3 yamt KASSERT(mutex_owned(mtx));
206 1.3.4.2 yamt
207 1.3.4.2 yamt if (sleepq_dontsleep(l)) {
208 1.3.4.2 yamt (void)sleepq_abort(mtx, 0);
209 1.3.4.2 yamt return;
210 1.3.4.2 yamt }
211 1.3.4.2 yamt
212 1.3.4.2 yamt sq = cv_enter(cv, mtx, l);
213 1.3.4.3 yamt (void)sleepq_block(0, false);
214 1.3.4.3 yamt (void)cv_exit(cv, mtx, l, 0);
215 1.3.4.2 yamt }
216 1.3.4.2 yamt
217 1.3.4.2 yamt /*
218 1.3.4.2 yamt * cv_wait_sig:
219 1.3.4.2 yamt *
220 1.3.4.2 yamt * Wait on a condition variable until a awoken or a signal is received.
221 1.3.4.2 yamt * Will also return early if the process is exiting. Returns zero if
222 1.3.4.2 yamt * awoken normallly, ERESTART if a signal was received and the system
223 1.3.4.2 yamt * call is restartable, or EINTR otherwise.
224 1.3.4.2 yamt */
225 1.3.4.2 yamt int
226 1.3.4.2 yamt cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
227 1.3.4.2 yamt {
228 1.3.4.3 yamt lwp_t *l = curlwp;
229 1.3.4.2 yamt sleepq_t *sq;
230 1.3.4.2 yamt int error;
231 1.3.4.2 yamt
232 1.3.4.3 yamt KASSERT(mutex_owned(mtx));
233 1.3.4.2 yamt
234 1.3.4.2 yamt if (sleepq_dontsleep(l))
235 1.3.4.2 yamt return sleepq_abort(mtx, 0);
236 1.3.4.2 yamt
237 1.3.4.2 yamt sq = cv_enter(cv, mtx, l);
238 1.3.4.3 yamt error = sleepq_block(0, true);
239 1.3.4.3 yamt return cv_exit(cv, mtx, l, error);
240 1.3.4.2 yamt }
241 1.3.4.2 yamt
242 1.3.4.2 yamt /*
243 1.3.4.2 yamt * cv_timedwait:
244 1.3.4.2 yamt *
245 1.3.4.2 yamt * Wait on a condition variable until awoken or the specified timeout
246 1.3.4.2 yamt * expires. Returns zero if awoken normally or EWOULDBLOCK if the
247 1.3.4.2 yamt * timeout expired.
248 1.3.4.2 yamt */
249 1.3.4.2 yamt int
250 1.3.4.2 yamt cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int timo)
251 1.3.4.2 yamt {
252 1.3.4.3 yamt lwp_t *l = curlwp;
253 1.3.4.2 yamt sleepq_t *sq;
254 1.3.4.2 yamt int error;
255 1.3.4.2 yamt
256 1.3.4.3 yamt KASSERT(mutex_owned(mtx));
257 1.3.4.2 yamt
258 1.3.4.2 yamt if (sleepq_dontsleep(l))
259 1.3.4.2 yamt return sleepq_abort(mtx, 0);
260 1.3.4.2 yamt
261 1.3.4.2 yamt sq = cv_enter(cv, mtx, l);
262 1.3.4.3 yamt error = sleepq_block(timo, false);
263 1.3.4.3 yamt return cv_exit(cv, mtx, l, error);
264 1.3.4.2 yamt }
265 1.3.4.2 yamt
266 1.3.4.2 yamt /*
267 1.3.4.2 yamt * cv_timedwait_sig:
268 1.3.4.2 yamt *
269 1.3.4.2 yamt * Wait on a condition variable until a timeout expires, awoken or a
270 1.3.4.2 yamt * signal is received. Will also return early if the process is
271 1.3.4.2 yamt * exiting. Returns zero if awoken normallly, EWOULDBLOCK if the
272 1.3.4.2 yamt * timeout expires, ERESTART if a signal was received and the system
273 1.3.4.2 yamt * call is restartable, or EINTR otherwise.
274 1.3.4.2 yamt */
275 1.3.4.2 yamt int
276 1.3.4.2 yamt cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int timo)
277 1.3.4.2 yamt {
278 1.3.4.3 yamt lwp_t *l = curlwp;
279 1.3.4.2 yamt sleepq_t *sq;
280 1.3.4.2 yamt int error;
281 1.3.4.2 yamt
282 1.3.4.3 yamt KASSERT(mutex_owned(mtx));
283 1.3.4.2 yamt
284 1.3.4.2 yamt if (sleepq_dontsleep(l))
285 1.3.4.2 yamt return sleepq_abort(mtx, 0);
286 1.3.4.2 yamt
287 1.3.4.2 yamt sq = cv_enter(cv, mtx, l);
288 1.3.4.3 yamt error = sleepq_block(timo, true);
289 1.3.4.3 yamt return cv_exit(cv, mtx, l, error);
290 1.3.4.2 yamt }
291 1.3.4.2 yamt
292 1.3.4.2 yamt /*
293 1.3.4.2 yamt * cv_signal:
294 1.3.4.2 yamt *
295 1.3.4.2 yamt * Wake the highest priority LWP waiting on a condition variable.
296 1.3.4.2 yamt * Must be called with the interlocking mutex held.
297 1.3.4.2 yamt */
298 1.3.4.2 yamt void
299 1.3.4.2 yamt cv_signal(kcondvar_t *cv)
300 1.3.4.2 yamt {
301 1.3.4.3 yamt lwp_t *l;
302 1.3.4.2 yamt sleepq_t *sq;
303 1.3.4.2 yamt
304 1.3.4.2 yamt if (cv->cv_waiters == 0)
305 1.3.4.2 yamt return;
306 1.3.4.2 yamt
307 1.3.4.2 yamt /*
308 1.3.4.2 yamt * cv->cv_waiters may be stale and have dropped to zero, but
309 1.3.4.2 yamt * while holding the interlock (the mutex passed to cv_wait()
310 1.3.4.2 yamt * and similar) we will see non-zero values when it matters.
311 1.3.4.2 yamt */
312 1.3.4.2 yamt
313 1.3.4.2 yamt sq = sleeptab_lookup(&sleeptab, cv);
314 1.3.4.2 yamt if (cv->cv_waiters != 0) {
315 1.3.4.2 yamt cv->cv_waiters--;
316 1.3.4.3 yamt l = sleepq_wake(sq, cv, 1);
317 1.3.4.3 yamt l->l_cv_signalled = 1;
318 1.3.4.2 yamt } else
319 1.3.4.2 yamt sleepq_unlock(sq);
320 1.3.4.2 yamt }
321 1.3.4.2 yamt
322 1.3.4.2 yamt /*
323 1.3.4.2 yamt * cv_broadcast:
324 1.3.4.2 yamt *
325 1.3.4.2 yamt * Wake all LWPs waiting on a condition variable. Must be called
326 1.3.4.2 yamt * with the interlocking mutex held.
327 1.3.4.2 yamt */
328 1.3.4.2 yamt void
329 1.3.4.2 yamt cv_broadcast(kcondvar_t *cv)
330 1.3.4.2 yamt {
331 1.3.4.2 yamt sleepq_t *sq;
332 1.3.4.2 yamt u_int cnt;
333 1.3.4.2 yamt
334 1.3.4.2 yamt if (cv->cv_waiters == 0)
335 1.3.4.2 yamt return;
336 1.3.4.2 yamt
337 1.3.4.2 yamt sq = sleeptab_lookup(&sleeptab, cv);
338 1.3.4.2 yamt if ((cnt = cv->cv_waiters) != 0) {
339 1.3.4.2 yamt cv->cv_waiters = 0;
340 1.3.4.2 yamt sleepq_wake(sq, cv, cnt);
341 1.3.4.2 yamt } else
342 1.3.4.2 yamt sleepq_unlock(sq);
343 1.3.4.2 yamt }
344 1.3.4.2 yamt
345 1.3.4.2 yamt /*
346 1.3.4.2 yamt * cv_wakeup:
347 1.3.4.2 yamt *
348 1.3.4.3 yamt * Wake all LWPs waiting on a condition variable. For cases
349 1.3.4.3 yamt * where the address may be waited on by mtsleep()/tsleep().
350 1.3.4.3 yamt * Not a documented call.
351 1.3.4.2 yamt */
352 1.3.4.2 yamt void
353 1.3.4.2 yamt cv_wakeup(kcondvar_t *cv)
354 1.3.4.2 yamt {
355 1.3.4.2 yamt sleepq_t *sq;
356 1.3.4.2 yamt
357 1.3.4.2 yamt sq = sleeptab_lookup(&sleeptab, cv);
358 1.3.4.3 yamt cv->cv_waiters = 0;
359 1.3.4.3 yamt sleepq_wake(sq, cv, (u_int)-1);
360 1.3.4.2 yamt }
361 1.3.4.2 yamt
362 1.3.4.2 yamt /*
363 1.3.4.2 yamt * cv_has_waiters:
364 1.3.4.2 yamt *
365 1.3.4.2 yamt * For diagnostic assertions: return non-zero if a condition
366 1.3.4.2 yamt * variable has waiters.
367 1.3.4.2 yamt */
368 1.3.4.3 yamt bool
369 1.3.4.2 yamt cv_has_waiters(kcondvar_t *cv)
370 1.3.4.2 yamt {
371 1.3.4.2 yamt
372 1.3.4.2 yamt /* No need to interlock here */
373 1.3.4.3 yamt return cv->cv_waiters != 0;
374 1.3.4.2 yamt }
375