kern_condvar.c revision 1.36 1 1.36 chs /* $NetBSD: kern_condvar.c,v 1.36 2017/06/08 01:09:52 chs 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 *
19 1.2 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.2 ad */
31 1.2 ad
32 1.2 ad /*
33 1.24 ad * Kernel condition variable implementation.
34 1.2 ad */
35 1.2 ad
36 1.2 ad #include <sys/cdefs.h>
37 1.36 chs __KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.36 2017/06/08 01:09:52 chs Exp $");
38 1.2 ad
39 1.2 ad #include <sys/param.h>
40 1.2 ad #include <sys/systm.h>
41 1.35 uebayasi #include <sys/lwp.h>
42 1.2 ad #include <sys/condvar.h>
43 1.2 ad #include <sys/sleepq.h>
44 1.20 ad #include <sys/lockdebug.h>
45 1.24 ad #include <sys/cpu.h>
46 1.20 ad
47 1.26 thorpej /*
48 1.26 thorpej * Accessors for the private contents of the kcondvar_t data type.
49 1.26 thorpej *
50 1.26 thorpej * cv_opaque[0] sleepq...
51 1.26 thorpej * cv_opaque[1] ...pointers
52 1.26 thorpej * cv_opaque[2] description for ps(1)
53 1.26 thorpej *
54 1.26 thorpej * cv_opaque[0..1] is protected by the interlock passed to cv_wait() (enqueue
55 1.26 thorpej * only), and the sleep queue lock acquired with sleeptab_lookup() (enqueue
56 1.26 thorpej * and dequeue).
57 1.26 thorpej *
58 1.26 thorpej * cv_opaque[2] (the wmesg) is static and does not change throughout the life
59 1.26 thorpej * of the CV.
60 1.26 thorpej */
61 1.26 thorpej #define CV_SLEEPQ(cv) ((sleepq_t *)(cv)->cv_opaque)
62 1.26 thorpej #define CV_WMESG(cv) ((const char *)(cv)->cv_opaque[2])
63 1.26 thorpej #define CV_SET_WMESG(cv, v) (cv)->cv_opaque[2] = __UNCONST(v)
64 1.26 thorpej
65 1.26 thorpej #define CV_DEBUG_P(cv) (CV_WMESG(cv) != nodebug)
66 1.20 ad #define CV_RA ((uintptr_t)__builtin_return_address(0))
67 1.2 ad
68 1.36 chs static void cv_unsleep(lwp_t *, bool);
69 1.36 chs static inline void cv_wakeup_one(kcondvar_t *);
70 1.36 chs static inline void cv_wakeup_all(kcondvar_t *);
71 1.2 ad
72 1.10 ad static syncobj_t cv_syncobj = {
73 1.2 ad SOBJ_SLEEPQ_SORTED,
74 1.2 ad cv_unsleep,
75 1.14 ad sleepq_changepri,
76 1.4 yamt sleepq_lendpri,
77 1.4 yamt syncobj_noowner,
78 1.2 ad };
79 1.2 ad
80 1.20 ad lockops_t cv_lockops = {
81 1.20 ad "Condition variable",
82 1.20 ad LOCKOPS_CV,
83 1.20 ad NULL
84 1.20 ad };
85 1.20 ad
86 1.10 ad static const char deadcv[] = "deadcv";
87 1.33 joerg #ifdef LOCKDEBUG
88 1.20 ad static const char nodebug[] = "nodebug";
89 1.36 chs
90 1.36 chs #define CV_LOCKDEBUG_HANDOFF(l, cv) cv_lockdebug_handoff(l, cv)
91 1.36 chs #define CV_LOCKDEBUG_PROCESS(l, cv) cv_lockdebug_process(l, cv)
92 1.36 chs
93 1.36 chs static inline void
94 1.36 chs cv_lockdebug_handoff(lwp_t *l, kcondvar_t *cv)
95 1.36 chs {
96 1.36 chs
97 1.36 chs if (CV_DEBUG_P(cv))
98 1.36 chs l->l_flag |= LW_CVLOCKDEBUG;
99 1.36 chs }
100 1.36 chs
101 1.36 chs static inline void
102 1.36 chs cv_lockdebug_process(lwp_t *l, kcondvar_t *cv)
103 1.36 chs {
104 1.36 chs
105 1.36 chs if ((l->l_flag & LW_CVLOCKDEBUG) == 0)
106 1.36 chs return;
107 1.36 chs
108 1.36 chs l->l_flag &= ~LW_CVLOCKDEBUG;
109 1.36 chs LOCKDEBUG_UNLOCKED(true, cv, CV_RA, 0);
110 1.36 chs }
111 1.36 chs #else
112 1.36 chs #define CV_LOCKDEBUG_HANDOFF(l, cv) __nothing
113 1.36 chs #define CV_LOCKDEBUG_PROCESS(l, cv) __nothing
114 1.33 joerg #endif
115 1.10 ad
116 1.2 ad /*
117 1.2 ad * cv_init:
118 1.2 ad *
119 1.2 ad * Initialize a condition variable for use.
120 1.2 ad */
121 1.2 ad void
122 1.2 ad cv_init(kcondvar_t *cv, const char *wmesg)
123 1.2 ad {
124 1.21 ad #ifdef LOCKDEBUG
125 1.20 ad bool dodebug;
126 1.2 ad
127 1.20 ad dodebug = LOCKDEBUG_ALLOC(cv, &cv_lockops,
128 1.20 ad (uintptr_t)__builtin_return_address(0));
129 1.21 ad if (!dodebug) {
130 1.20 ad /* XXX This will break vfs_lockf. */
131 1.21 ad wmesg = nodebug;
132 1.20 ad }
133 1.21 ad #endif
134 1.21 ad KASSERT(wmesg != NULL);
135 1.26 thorpej CV_SET_WMESG(cv, wmesg);
136 1.20 ad sleepq_init(CV_SLEEPQ(cv));
137 1.2 ad }
138 1.2 ad
139 1.2 ad /*
140 1.2 ad * cv_destroy:
141 1.2 ad *
142 1.2 ad * Tear down a condition variable.
143 1.2 ad */
144 1.2 ad void
145 1.2 ad cv_destroy(kcondvar_t *cv)
146 1.2 ad {
147 1.2 ad
148 1.20 ad LOCKDEBUG_FREE(CV_DEBUG_P(cv), cv);
149 1.2 ad #ifdef DIAGNOSTIC
150 1.15 ad KASSERT(cv_is_valid(cv));
151 1.26 thorpej CV_SET_WMESG(cv, deadcv);
152 1.2 ad #endif
153 1.2 ad }
154 1.2 ad
155 1.2 ad /*
156 1.2 ad * cv_enter:
157 1.2 ad *
158 1.2 ad * Look up and lock the sleep queue corresponding to the given
159 1.2 ad * condition variable, and increment the number of waiters.
160 1.2 ad */
161 1.20 ad static inline void
162 1.6 ad cv_enter(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l)
163 1.2 ad {
164 1.2 ad sleepq_t *sq;
165 1.18 ad kmutex_t *mp;
166 1.2 ad
167 1.15 ad KASSERT(cv_is_valid(cv));
168 1.24 ad KASSERT(!cpu_intr_p());
169 1.14 ad KASSERT((l->l_pflag & LP_INTR) == 0 || panicstr != NULL);
170 1.2 ad
171 1.20 ad LOCKDEBUG_LOCKED(CV_DEBUG_P(cv), cv, mtx, CV_RA, 0);
172 1.20 ad
173 1.14 ad l->l_kpriority = true;
174 1.24 ad mp = sleepq_hashlock(cv);
175 1.20 ad sq = CV_SLEEPQ(cv);
176 1.18 ad sleepq_enter(sq, l, mp);
177 1.26 thorpej sleepq_enqueue(sq, cv, CV_WMESG(cv), &cv_syncobj);
178 1.2 ad mutex_exit(mtx);
179 1.24 ad KASSERT(cv_has_waiters(cv));
180 1.2 ad }
181 1.2 ad
182 1.2 ad /*
183 1.6 ad * cv_exit:
184 1.6 ad *
185 1.6 ad * After resuming execution, check to see if we have been restarted
186 1.6 ad * as a result of cv_signal(). If we have, but cannot take the
187 1.6 ad * wakeup (because of eg a pending Unix signal or timeout) then try
188 1.6 ad * to ensure that another LWP sees it. This is necessary because
189 1.6 ad * there may be multiple waiters, and at least one should take the
190 1.6 ad * wakeup if possible.
191 1.6 ad */
192 1.6 ad static inline int
193 1.6 ad cv_exit(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l, const int error)
194 1.6 ad {
195 1.6 ad
196 1.6 ad mutex_enter(mtx);
197 1.20 ad if (__predict_false(error != 0))
198 1.6 ad cv_signal(cv);
199 1.6 ad
200 1.20 ad LOCKDEBUG_UNLOCKED(CV_DEBUG_P(cv), cv, CV_RA, 0);
201 1.15 ad KASSERT(cv_is_valid(cv));
202 1.10 ad
203 1.6 ad return error;
204 1.6 ad }
205 1.6 ad
206 1.6 ad /*
207 1.2 ad * cv_unsleep:
208 1.2 ad *
209 1.2 ad * Remove an LWP from the condition variable and sleep queue. This
210 1.2 ad * is called when the LWP has not been awoken normally but instead
211 1.2 ad * interrupted: for example, when a signal is received. Must be
212 1.2 ad * called with the LWP locked, and must return it unlocked.
213 1.2 ad */
214 1.27 rmind static void
215 1.16 ad cv_unsleep(lwp_t *l, bool cleanup)
216 1.2 ad {
217 1.34 martin kcondvar_t *cv __diagused;
218 1.2 ad
219 1.15 ad cv = (kcondvar_t *)(uintptr_t)l->l_wchan;
220 1.15 ad
221 1.20 ad KASSERT(l->l_wchan == (wchan_t)cv);
222 1.20 ad KASSERT(l->l_sleepq == CV_SLEEPQ(cv));
223 1.15 ad KASSERT(cv_is_valid(cv));
224 1.24 ad KASSERT(cv_has_waiters(cv));
225 1.2 ad
226 1.27 rmind sleepq_unsleep(l, cleanup);
227 1.2 ad }
228 1.2 ad
229 1.2 ad /*
230 1.2 ad * cv_wait:
231 1.2 ad *
232 1.2 ad * Wait non-interruptably on a condition variable until awoken.
233 1.2 ad */
234 1.2 ad void
235 1.2 ad cv_wait(kcondvar_t *cv, kmutex_t *mtx)
236 1.2 ad {
237 1.6 ad lwp_t *l = curlwp;
238 1.2 ad
239 1.8 yamt KASSERT(mutex_owned(mtx));
240 1.2 ad
241 1.20 ad cv_enter(cv, mtx, l);
242 1.36 chs
243 1.36 chs /*
244 1.36 chs * We can't use cv_exit() here since the cv might be destroyed before
245 1.36 chs * this thread gets a chance to run. Instead, hand off the lockdebug
246 1.36 chs * responsibility to the thread that wakes us up.
247 1.36 chs */
248 1.36 chs
249 1.36 chs CV_LOCKDEBUG_HANDOFF(l, cv);
250 1.8 yamt (void)sleepq_block(0, false);
251 1.36 chs mutex_enter(mtx);
252 1.2 ad }
253 1.2 ad
254 1.2 ad /*
255 1.2 ad * cv_wait_sig:
256 1.2 ad *
257 1.2 ad * Wait on a condition variable until a awoken or a signal is received.
258 1.2 ad * Will also return early if the process is exiting. Returns zero if
259 1.29 jym * awoken normally, ERESTART if a signal was received and the system
260 1.2 ad * call is restartable, or EINTR otherwise.
261 1.2 ad */
262 1.2 ad int
263 1.2 ad cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
264 1.2 ad {
265 1.6 ad lwp_t *l = curlwp;
266 1.2 ad int error;
267 1.2 ad
268 1.8 yamt KASSERT(mutex_owned(mtx));
269 1.2 ad
270 1.20 ad cv_enter(cv, mtx, l);
271 1.8 yamt error = sleepq_block(0, true);
272 1.6 ad return cv_exit(cv, mtx, l, error);
273 1.2 ad }
274 1.2 ad
275 1.2 ad /*
276 1.2 ad * cv_timedwait:
277 1.2 ad *
278 1.2 ad * Wait on a condition variable until awoken or the specified timeout
279 1.2 ad * expires. Returns zero if awoken normally or EWOULDBLOCK if the
280 1.2 ad * timeout expired.
281 1.31 apb *
282 1.31 apb * timo is a timeout in ticks. timo = 0 specifies an infinite timeout.
283 1.2 ad */
284 1.2 ad int
285 1.2 ad cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int timo)
286 1.2 ad {
287 1.6 ad lwp_t *l = curlwp;
288 1.2 ad int error;
289 1.2 ad
290 1.8 yamt KASSERT(mutex_owned(mtx));
291 1.2 ad
292 1.20 ad cv_enter(cv, mtx, l);
293 1.8 yamt error = sleepq_block(timo, false);
294 1.6 ad return cv_exit(cv, mtx, l, error);
295 1.2 ad }
296 1.2 ad
297 1.2 ad /*
298 1.2 ad * cv_timedwait_sig:
299 1.2 ad *
300 1.2 ad * Wait on a condition variable until a timeout expires, awoken or a
301 1.2 ad * signal is received. Will also return early if the process is
302 1.29 jym * exiting. Returns zero if awoken normally, EWOULDBLOCK if the
303 1.2 ad * timeout expires, ERESTART if a signal was received and the system
304 1.2 ad * call is restartable, or EINTR otherwise.
305 1.32 apb *
306 1.32 apb * timo is a timeout in ticks. timo = 0 specifies an infinite timeout.
307 1.2 ad */
308 1.2 ad int
309 1.2 ad cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int timo)
310 1.2 ad {
311 1.6 ad lwp_t *l = curlwp;
312 1.2 ad int error;
313 1.2 ad
314 1.8 yamt KASSERT(mutex_owned(mtx));
315 1.2 ad
316 1.20 ad cv_enter(cv, mtx, l);
317 1.8 yamt error = sleepq_block(timo, true);
318 1.6 ad return cv_exit(cv, mtx, l, error);
319 1.2 ad }
320 1.2 ad
321 1.2 ad /*
322 1.2 ad * cv_signal:
323 1.2 ad *
324 1.2 ad * Wake the highest priority LWP waiting on a condition variable.
325 1.2 ad * Must be called with the interlocking mutex held.
326 1.2 ad */
327 1.2 ad void
328 1.2 ad cv_signal(kcondvar_t *cv)
329 1.2 ad {
330 1.20 ad
331 1.22 ad /* LOCKDEBUG_WAKEUP(CV_DEBUG_P(cv), cv, CV_RA); */
332 1.20 ad KASSERT(cv_is_valid(cv));
333 1.20 ad
334 1.24 ad if (__predict_false(!TAILQ_EMPTY(CV_SLEEPQ(cv))))
335 1.24 ad cv_wakeup_one(cv);
336 1.20 ad }
337 1.20 ad
338 1.36 chs static inline void
339 1.20 ad cv_wakeup_one(kcondvar_t *cv)
340 1.20 ad {
341 1.2 ad sleepq_t *sq;
342 1.18 ad kmutex_t *mp;
343 1.20 ad lwp_t *l;
344 1.2 ad
345 1.15 ad KASSERT(cv_is_valid(cv));
346 1.15 ad
347 1.24 ad mp = sleepq_hashlock(cv);
348 1.20 ad sq = CV_SLEEPQ(cv);
349 1.20 ad l = TAILQ_FIRST(sq);
350 1.20 ad if (l == NULL) {
351 1.20 ad mutex_spin_exit(mp);
352 1.2 ad return;
353 1.20 ad }
354 1.20 ad KASSERT(l->l_sleepq == sq);
355 1.20 ad KASSERT(l->l_mutex == mp);
356 1.20 ad KASSERT(l->l_wchan == cv);
357 1.36 chs CV_LOCKDEBUG_PROCESS(l, cv);
358 1.27 rmind sleepq_remove(sq, l);
359 1.20 ad mutex_spin_exit(mp);
360 1.2 ad
361 1.15 ad KASSERT(cv_is_valid(cv));
362 1.2 ad }
363 1.2 ad
364 1.2 ad /*
365 1.2 ad * cv_broadcast:
366 1.2 ad *
367 1.2 ad * Wake all LWPs waiting on a condition variable. Must be called
368 1.2 ad * with the interlocking mutex held.
369 1.2 ad */
370 1.2 ad void
371 1.2 ad cv_broadcast(kcondvar_t *cv)
372 1.2 ad {
373 1.20 ad
374 1.22 ad /* LOCKDEBUG_WAKEUP(CV_DEBUG_P(cv), cv, CV_RA); */
375 1.20 ad KASSERT(cv_is_valid(cv));
376 1.20 ad
377 1.24 ad if (__predict_false(!TAILQ_EMPTY(CV_SLEEPQ(cv))))
378 1.24 ad cv_wakeup_all(cv);
379 1.20 ad }
380 1.20 ad
381 1.36 chs static inline void
382 1.20 ad cv_wakeup_all(kcondvar_t *cv)
383 1.20 ad {
384 1.2 ad sleepq_t *sq;
385 1.18 ad kmutex_t *mp;
386 1.20 ad lwp_t *l, *next;
387 1.2 ad
388 1.15 ad KASSERT(cv_is_valid(cv));
389 1.15 ad
390 1.24 ad mp = sleepq_hashlock(cv);
391 1.20 ad sq = CV_SLEEPQ(cv);
392 1.20 ad for (l = TAILQ_FIRST(sq); l != NULL; l = next) {
393 1.20 ad KASSERT(l->l_sleepq == sq);
394 1.20 ad KASSERT(l->l_mutex == mp);
395 1.20 ad KASSERT(l->l_wchan == cv);
396 1.20 ad next = TAILQ_NEXT(l, l_sleepchain);
397 1.36 chs CV_LOCKDEBUG_PROCESS(l, cv);
398 1.27 rmind sleepq_remove(sq, l);
399 1.20 ad }
400 1.20 ad mutex_spin_exit(mp);
401 1.2 ad
402 1.15 ad KASSERT(cv_is_valid(cv));
403 1.2 ad }
404 1.2 ad
405 1.2 ad /*
406 1.2 ad * cv_has_waiters:
407 1.2 ad *
408 1.2 ad * For diagnostic assertions: return non-zero if a condition
409 1.2 ad * variable has waiters.
410 1.2 ad */
411 1.7 ad bool
412 1.2 ad cv_has_waiters(kcondvar_t *cv)
413 1.2 ad {
414 1.23 chris
415 1.25 ad return !TAILQ_EMPTY(CV_SLEEPQ(cv));
416 1.2 ad }
417 1.15 ad
418 1.15 ad /*
419 1.15 ad * cv_is_valid:
420 1.15 ad *
421 1.15 ad * For diagnostic assertions: return non-zero if a condition
422 1.15 ad * variable appears to be valid. No locks need be held.
423 1.15 ad */
424 1.15 ad bool
425 1.15 ad cv_is_valid(kcondvar_t *cv)
426 1.15 ad {
427 1.15 ad
428 1.26 thorpej return CV_WMESG(cv) != deadcv && CV_WMESG(cv) != NULL;
429 1.15 ad }
430