kern_condvar.c revision 1.49 1 /* $NetBSD: kern_condvar.c,v 1.49 2020/05/03 01:24:37 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2006, 2007, 2008, 2019, 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Kernel condition variable implementation.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.49 2020/05/03 01:24:37 riastradh Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/lwp.h>
42 #include <sys/condvar.h>
43 #include <sys/sleepq.h>
44 #include <sys/lockdebug.h>
45 #include <sys/cpu.h>
46 #include <sys/kernel.h>
47
48 /*
49 * Accessors for the private contents of the kcondvar_t data type.
50 *
51 * cv_opaque[0] sleepq_t
52 * cv_opaque[1] description for ps(1)
53 *
54 * cv_opaque[0] is protected by the interlock passed to cv_wait() (enqueue
55 * only), and the sleep queue lock acquired with sleepq_hashlock() (enqueue
56 * and dequeue).
57 *
58 * cv_opaque[1] (the wmesg) is static and does not change throughout the life
59 * of the CV.
60 */
61 #define CV_SLEEPQ(cv) ((sleepq_t *)(cv)->cv_opaque)
62 #define CV_WMESG(cv) ((const char *)(cv)->cv_opaque[1])
63 #define CV_SET_WMESG(cv, v) (cv)->cv_opaque[1] = __UNCONST(v)
64
65 #define CV_DEBUG_P(cv) (CV_WMESG(cv) != nodebug)
66 #define CV_RA ((uintptr_t)__builtin_return_address(0))
67
68 static void cv_unsleep(lwp_t *, bool);
69 static inline void cv_wakeup_one(kcondvar_t *);
70 static inline void cv_wakeup_all(kcondvar_t *);
71
72 syncobj_t cv_syncobj = {
73 .sobj_flag = SOBJ_SLEEPQ_SORTED,
74 .sobj_unsleep = cv_unsleep,
75 .sobj_changepri = sleepq_changepri,
76 .sobj_lendpri = sleepq_lendpri,
77 .sobj_owner = syncobj_noowner,
78 };
79
80 static const char deadcv[] = "deadcv";
81
82 /*
83 * cv_init:
84 *
85 * Initialize a condition variable for use.
86 */
87 void
88 cv_init(kcondvar_t *cv, const char *wmesg)
89 {
90
91 KASSERT(wmesg != NULL);
92 CV_SET_WMESG(cv, wmesg);
93 sleepq_init(CV_SLEEPQ(cv));
94 }
95
96 /*
97 * cv_destroy:
98 *
99 * Tear down a condition variable.
100 */
101 void
102 cv_destroy(kcondvar_t *cv)
103 {
104
105 #ifdef DIAGNOSTIC
106 KASSERT(cv_is_valid(cv));
107 KASSERT(!cv_has_waiters(cv));
108 CV_SET_WMESG(cv, deadcv);
109 #endif
110 }
111
112 /*
113 * cv_enter:
114 *
115 * Look up and lock the sleep queue corresponding to the given
116 * condition variable, and increment the number of waiters.
117 */
118 static inline void
119 cv_enter(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l, bool catch_p)
120 {
121 sleepq_t *sq;
122 kmutex_t *mp;
123
124 KASSERT(cv_is_valid(cv));
125 KASSERT(!cpu_intr_p());
126 KASSERT((l->l_pflag & LP_INTR) == 0 || panicstr != NULL);
127
128 l->l_kpriority = true;
129 mp = sleepq_hashlock(cv);
130 sq = CV_SLEEPQ(cv);
131 sleepq_enter(sq, l, mp);
132 sleepq_enqueue(sq, cv, CV_WMESG(cv), &cv_syncobj, catch_p);
133 mutex_exit(mtx);
134 KASSERT(cv_has_waiters(cv));
135 }
136
137 /*
138 * cv_unsleep:
139 *
140 * Remove an LWP from the condition variable and sleep queue. This
141 * is called when the LWP has not been awoken normally but instead
142 * interrupted: for example, when a signal is received. Must be
143 * called with the LWP locked. Will unlock if "unlock" is true.
144 */
145 static void
146 cv_unsleep(lwp_t *l, bool unlock)
147 {
148 kcondvar_t *cv __diagused;
149
150 cv = (kcondvar_t *)(uintptr_t)l->l_wchan;
151
152 KASSERT(l->l_wchan == (wchan_t)cv);
153 KASSERT(l->l_sleepq == CV_SLEEPQ(cv));
154 KASSERT(cv_is_valid(cv));
155 KASSERT(cv_has_waiters(cv));
156
157 sleepq_unsleep(l, unlock);
158 }
159
160 /*
161 * cv_wait:
162 *
163 * Wait non-interruptably on a condition variable until awoken.
164 */
165 void
166 cv_wait(kcondvar_t *cv, kmutex_t *mtx)
167 {
168 lwp_t *l = curlwp;
169
170 KASSERT(mutex_owned(mtx));
171
172 cv_enter(cv, mtx, l, false);
173 (void)sleepq_block(0, false);
174 mutex_enter(mtx);
175 }
176
177 /*
178 * cv_wait_sig:
179 *
180 * Wait on a condition variable until a awoken or a signal is received.
181 * Will also return early if the process is exiting. Returns zero if
182 * awoken normally, ERESTART if a signal was received and the system
183 * call is restartable, or EINTR otherwise.
184 */
185 int
186 cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
187 {
188 lwp_t *l = curlwp;
189 int error;
190
191 KASSERT(mutex_owned(mtx));
192
193 cv_enter(cv, mtx, l, true);
194 error = sleepq_block(0, true);
195 mutex_enter(mtx);
196 return error;
197 }
198
199 /*
200 * cv_timedwait:
201 *
202 * Wait on a condition variable until awoken or the specified timeout
203 * expires. Returns zero if awoken normally or EWOULDBLOCK if the
204 * timeout expired.
205 *
206 * timo is a timeout in ticks. timo = 0 specifies an infinite timeout.
207 */
208 int
209 cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int timo)
210 {
211 lwp_t *l = curlwp;
212 int error;
213
214 KASSERT(mutex_owned(mtx));
215
216 cv_enter(cv, mtx, l, false);
217 error = sleepq_block(timo, false);
218 mutex_enter(mtx);
219 return error;
220 }
221
222 /*
223 * cv_timedwait_sig:
224 *
225 * Wait on a condition variable until a timeout expires, awoken or a
226 * signal is received. Will also return early if the process is
227 * exiting. Returns zero if awoken normally, EWOULDBLOCK if the
228 * timeout expires, ERESTART if a signal was received and the system
229 * call is restartable, or EINTR otherwise.
230 *
231 * timo is a timeout in ticks. timo = 0 specifies an infinite timeout.
232 */
233 int
234 cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int timo)
235 {
236 lwp_t *l = curlwp;
237 int error;
238
239 KASSERT(mutex_owned(mtx));
240
241 cv_enter(cv, mtx, l, true);
242 error = sleepq_block(timo, true);
243 mutex_enter(mtx);
244 return error;
245 }
246
247 struct timedwaitclock {
248 struct timespec *timeout;
249 clockid_t clockid;
250 int flags;
251 const struct bintime *epsilon;
252 struct timespec starttime;
253 };
254
255 static int
256 cv_timedwaitclock_begin(struct timedwaitclock *T, int *timo)
257 {
258 struct timespec delta;
259 const struct timespec *deltap;
260 int error;
261
262 /* Sanity-check timeout -- may have come from userland. */
263 if (T->timeout->tv_nsec < 0 || T->timeout->tv_nsec >= 1000000000L)
264 return EINVAL;
265
266 /*
267 * Compute the time delta.
268 */
269 if ((T->flags & TIMER_ABSTIME) == TIMER_ABSTIME) {
270 /* Check our watch. */
271 error = clock_gettime1(T->clockid, &T->starttime);
272 if (error)
273 return error;
274
275 /* If the deadline has passed, we're done. */
276 if (timespeccmp(T->timeout, &T->starttime, <=))
277 return ETIMEDOUT;
278
279 /* Count how much time is left. */
280 timespecsub(T->timeout, &T->starttime, &delta);
281 deltap = δ
282 } else {
283 /* The user specified how much time is left. */
284 deltap = T->timeout;
285
286 /* If there's none left, we've timed out. */
287 if (deltap->tv_sec == 0 && deltap->tv_nsec == 0)
288 return ETIMEDOUT;
289 }
290
291 /*
292 * Convert to ticks, but clamp to be >=1.
293 *
294 * XXX In the tickless future, use a high-resolution timer if
295 * timo would round to zero.
296 */
297 *timo = tstohz(deltap);
298 KASSERTMSG(*timo >= 0, "negative ticks: %d", *timo);
299 if (*timo == 0)
300 *timo = 1;
301
302 /* Success! */
303 return 0;
304 }
305
306 static void
307 cv_timedwaitclock_end(struct timedwaitclock *T)
308 {
309 struct timespec endtime, delta;
310
311 /* If the timeout is absolute, nothing to do. */
312 if ((T->flags & TIMER_ABSTIME) == TIMER_ABSTIME)
313 return;
314
315 /*
316 * Check our watch. If anything goes wrong with it, make sure
317 * that the next time we immediately time out rather than fail
318 * to deduct the time elapsed.
319 */
320 if (clock_gettime1(T->clockid, &endtime)) {
321 T->timeout->tv_sec = 0;
322 T->timeout->tv_nsec = 0;
323 return;
324 }
325
326 /* Find how much time elapsed while we waited. */
327 timespecsub(&endtime, &T->starttime, &delta);
328
329 /*
330 * Paranoia: If the clock went backwards, treat it as if no
331 * time elapsed at all rather than adding anything.
332 */
333 if (delta.tv_sec < 0 ||
334 (delta.tv_sec == 0 && delta.tv_nsec < 0)) {
335 delta.tv_sec = 0;
336 delta.tv_nsec = 0;
337 }
338
339 /*
340 * Set it to the time left, or zero, whichever is larger. We
341 * do not fail with EWOULDBLOCK here because this may have been
342 * an explicit wakeup, so the caller needs to check before they
343 * give up or else cv_signal would be lost.
344 */
345 if (timespeccmp(T->timeout, &delta, <=)) {
346 T->timeout->tv_sec = 0;
347 T->timeout->tv_nsec = 0;
348 } else {
349 timespecsub(T->timeout, &delta, T->timeout);
350 }
351 }
352
353 /*
354 * cv_timedwaitclock:
355 *
356 * Wait on a condition variable until awoken normally, or the
357 * specified timeout expires according to the provided clock.
358 * Returns zero if awoken normally or EWOULDBLOCK if the timeout
359 * expired. For relative timeouts ((flags & TIMER_ABSTIME) == 0),
360 * updates timeout with the time left.
361 *
362 * timeout == NULL specifies an infinite timeout. epsilon is a
363 * requested maximum error in timeout (excluding spurious
364 * wakeups).
365 */
366 int
367 cv_timedwaitclock(kcondvar_t *cv, kmutex_t *mtx, struct timespec *timeout,
368 clockid_t clockid, int flags, const struct bintime *epsilon)
369 {
370 struct timedwaitclock T = {
371 .timeout = timeout,
372 .clockid = clockid,
373 .flags = flags,
374 .epsilon = epsilon,
375 };
376 int timo;
377 int error;
378
379 if (timeout == NULL) {
380 cv_wait(cv, mtx);
381 return 0;
382 }
383
384 error = cv_timedwaitclock_begin(&T, &timo);
385 if (error)
386 return error;
387 error = cv_timedwait(cv, mtx, timo);
388 cv_timedwaitclock_end(&T);
389 return error;
390 }
391
392 /*
393 * cv_timedwaitclock_sig:
394 *
395 * Wait on a condition variable until awoken normally, interrupted
396 * by a signal, or the specified timeout expires according to the
397 * provided clock. Returns zero if awoken normally,
398 * EINTR/ERESTART if interrupted by a signal, or EWOULDBLOCK if
399 * the timeout expired. For relative timeouts ((flags &
400 * TIMER_ABSTIME) == 0), updates timeout with the time left.
401 *
402 * timeout == NULL specifies an infinite timeout. epsilon is a
403 * requested maximum error in timeout (excluding spurious
404 * wakeups).
405 */
406 int
407 cv_timedwaitclock_sig(kcondvar_t *cv, kmutex_t *mtx, struct timespec *timeout,
408 clockid_t clockid, int flags, const struct bintime *epsilon)
409 {
410 struct timedwaitclock T = {
411 .timeout = timeout,
412 .clockid = clockid,
413 .flags = flags,
414 .epsilon = epsilon,
415 };
416 int timo;
417 int error;
418
419 if (timeout == NULL)
420 return cv_wait_sig(cv, mtx);
421
422 error = cv_timedwaitclock_begin(&T, &timo);
423 if (error)
424 return error;
425 error = cv_timedwait_sig(cv, mtx, timo);
426 cv_timedwaitclock_end(&T);
427 return error;
428 }
429
430 /*
431 * Given a number of seconds, sec, and 2^64ths of a second, frac, we
432 * want a number of ticks for a timeout:
433 *
434 * timo = hz*(sec + frac/2^64)
435 * = hz*sec + hz*frac/2^64
436 * = hz*sec + hz*(frachi*2^32 + fraclo)/2^64
437 * = hz*sec + hz*frachi/2^32 + hz*fraclo/2^64,
438 *
439 * where frachi is the high 32 bits of frac and fraclo is the
440 * low 32 bits.
441 *
442 * We assume hz < INT_MAX/2 < UINT32_MAX, so
443 *
444 * hz*fraclo/2^64 < fraclo*2^32/2^64 <= 1,
445 *
446 * since fraclo < 2^32.
447 *
448 * We clamp the result at INT_MAX/2 for a timeout in ticks, since we
449 * can't represent timeouts higher than INT_MAX in cv_timedwait, and
450 * spurious wakeup is OK. Moreover, we don't want to wrap around,
451 * because we compute end - start in ticks in order to compute the
452 * remaining timeout, and that difference cannot wrap around, so we use
453 * a timeout less than INT_MAX. Using INT_MAX/2 provides plenty of
454 * margin for paranoia and will exceed most waits in practice by far.
455 */
456 static unsigned
457 bintime2timo(const struct bintime *bt)
458 {
459
460 KASSERT(hz < INT_MAX/2);
461 CTASSERT(INT_MAX/2 < UINT32_MAX);
462 if (bt->sec > ((INT_MAX/2)/hz))
463 return INT_MAX/2;
464 if ((hz*(bt->frac >> 32) >> 32) > (INT_MAX/2 - hz*bt->sec))
465 return INT_MAX/2;
466
467 return hz*bt->sec + (hz*(bt->frac >> 32) >> 32);
468 }
469
470 /*
471 * timo is in units of ticks. We want units of seconds and 2^64ths of
472 * a second. We know hz = 1 sec/tick, and 2^64 = 1 sec/(2^64th of a
473 * second), from which we can conclude 2^64 / hz = 1 (2^64th of a
474 * second)/tick. So for the fractional part, we compute
475 *
476 * frac = rem * 2^64 / hz
477 * = ((rem * 2^32) / hz) * 2^32
478 *
479 * Using truncating integer division instead of real division will
480 * leave us with only about 32 bits of precision, which means about
481 * 1/4-nanosecond resolution, which is good enough for our purposes.
482 */
483 static struct bintime
484 timo2bintime(unsigned timo)
485 {
486
487 return (struct bintime) {
488 .sec = timo / hz,
489 .frac = (((uint64_t)(timo % hz) << 32)/hz << 32),
490 };
491 }
492
493 /*
494 * cv_timedwaitbt:
495 *
496 * Wait on a condition variable until awoken or the specified
497 * timeout expires. Returns zero if awoken normally or
498 * EWOULDBLOCK if the timeout expires.
499 *
500 * On entry, bt is a timeout in bintime. cv_timedwaitbt subtracts
501 * the time slept, so on exit, bt is the time remaining after
502 * sleeping, possibly negative if the complete time has elapsed.
503 * No infinite timeout; use cv_wait_sig instead.
504 *
505 * epsilon is a requested maximum error in timeout (excluding
506 * spurious wakeups). Currently not used, will be used in the
507 * future to choose between low- and high-resolution timers.
508 * Actual wakeup time will be somewhere in [t, t + max(e, r) + s)
509 * where r is the finest resolution of clock available and s is
510 * scheduling delays for scheduler overhead and competing threads.
511 * Time is measured by the interrupt source implementing the
512 * timeout, not by another timecounter.
513 */
514 int
515 cv_timedwaitbt(kcondvar_t *cv, kmutex_t *mtx, struct bintime *bt,
516 const struct bintime *epsilon __diagused)
517 {
518 struct bintime slept;
519 unsigned start, end;
520 int timo;
521 int error;
522
523 KASSERTMSG(bt->sec >= 0, "negative timeout");
524 KASSERTMSG(epsilon != NULL, "specify maximum requested delay");
525
526 /* If there's nothing left to wait, time out. */
527 if (bt->sec == 0 && bt->frac == 0)
528 return EWOULDBLOCK;
529
530 /* Convert to ticks, but clamp to be >=1. */
531 timo = bintime2timo(bt);
532 KASSERTMSG(timo >= 0, "negative ticks: %d", timo);
533 if (timo == 0)
534 timo = 1;
535
536 /*
537 * getticks() is technically int, but nothing special
538 * happens instead of overflow, so we assume two's-complement
539 * wraparound and just treat it as unsigned.
540 */
541 start = getticks();
542 error = cv_timedwait(cv, mtx, timo);
543 end = getticks();
544
545 /*
546 * Set it to the time left, or zero, whichever is larger. We
547 * do not fail with EWOULDBLOCK here because this may have been
548 * an explicit wakeup, so the caller needs to check before they
549 * give up or else cv_signal would be lost.
550 */
551 slept = timo2bintime(end - start);
552 if (bintimecmp(bt, &slept, <=)) {
553 bt->sec = 0;
554 bt->frac = 0;
555 } else {
556 /* bt := bt - slept */
557 bintime_sub(bt, &slept);
558 }
559
560 return error;
561 }
562
563 /*
564 * cv_timedwaitbt_sig:
565 *
566 * Wait on a condition variable until awoken, the specified
567 * timeout expires, or interrupted by a signal. Returns zero if
568 * awoken normally, EWOULDBLOCK if the timeout expires, or
569 * EINTR/ERESTART if interrupted by a signal.
570 *
571 * On entry, bt is a timeout in bintime. cv_timedwaitbt_sig
572 * subtracts the time slept, so on exit, bt is the time remaining
573 * after sleeping. No infinite timeout; use cv_wait instead.
574 *
575 * epsilon is a requested maximum error in timeout (excluding
576 * spurious wakeups). Currently not used, will be used in the
577 * future to choose between low- and high-resolution timers.
578 */
579 int
580 cv_timedwaitbt_sig(kcondvar_t *cv, kmutex_t *mtx, struct bintime *bt,
581 const struct bintime *epsilon __diagused)
582 {
583 struct bintime slept;
584 unsigned start, end;
585 int timo;
586 int error;
587
588 KASSERTMSG(bt->sec >= 0, "negative timeout");
589 KASSERTMSG(epsilon != NULL, "specify maximum requested delay");
590
591 /* If there's nothing left to wait, time out. */
592 if (bt->sec == 0 && bt->frac == 0)
593 return EWOULDBLOCK;
594
595 /* Convert to ticks, but clamp to be >=1. */
596 timo = bintime2timo(bt);
597 KASSERTMSG(timo >= 0, "negative ticks: %d", timo);
598 if (timo == 0)
599 timo = 1;
600
601 /*
602 * getticks() is technically int, but nothing special
603 * happens instead of overflow, so we assume two's-complement
604 * wraparound and just treat it as unsigned.
605 */
606 start = getticks();
607 error = cv_timedwait_sig(cv, mtx, timo);
608 end = getticks();
609
610 /*
611 * Set it to the time left, or zero, whichever is larger. We
612 * do not fail with EWOULDBLOCK here because this may have been
613 * an explicit wakeup, so the caller needs to check before they
614 * give up or else cv_signal would be lost.
615 */
616 slept = timo2bintime(end - start);
617 if (bintimecmp(bt, &slept, <=)) {
618 bt->sec = 0;
619 bt->frac = 0;
620 } else {
621 /* bt := bt - slept */
622 bintime_sub(bt, &slept);
623 }
624
625 return error;
626 }
627
628 /*
629 * cv_signal:
630 *
631 * Wake the highest priority LWP waiting on a condition variable.
632 * Must be called with the interlocking mutex held.
633 */
634 void
635 cv_signal(kcondvar_t *cv)
636 {
637
638 KASSERT(cv_is_valid(cv));
639
640 if (__predict_false(!LIST_EMPTY(CV_SLEEPQ(cv))))
641 cv_wakeup_one(cv);
642 }
643
644 /*
645 * cv_wakeup_one:
646 *
647 * Slow path for cv_signal(). Deliberately marked __noinline to
648 * prevent the compiler pulling it in to cv_signal(), which adds
649 * extra prologue and epilogue code.
650 */
651 static __noinline void
652 cv_wakeup_one(kcondvar_t *cv)
653 {
654 sleepq_t *sq;
655 kmutex_t *mp;
656 lwp_t *l;
657
658 /*
659 * Keep waking LWPs until a non-interruptable waiter is found. An
660 * interruptable waiter could fail to do something useful with the
661 * wakeup due to an error return from cv_[timed]wait_sig(), and the
662 * caller of cv_signal() may not expect such a scenario.
663 *
664 * This isn't a problem for non-interruptable waits (untimed and
665 * timed), because if such a waiter is woken here it will not return
666 * an error.
667 */
668 mp = sleepq_hashlock(cv);
669 sq = CV_SLEEPQ(cv);
670 while ((l = LIST_FIRST(sq)) != NULL) {
671 KASSERT(l->l_sleepq == sq);
672 KASSERT(l->l_mutex == mp);
673 KASSERT(l->l_wchan == cv);
674 if ((l->l_flag & LW_SINTR) == 0) {
675 sleepq_remove(sq, l);
676 break;
677 } else
678 sleepq_remove(sq, l);
679 }
680 mutex_spin_exit(mp);
681 }
682
683 /*
684 * cv_broadcast:
685 *
686 * Wake all LWPs waiting on a condition variable. Must be called
687 * with the interlocking mutex held.
688 */
689 void
690 cv_broadcast(kcondvar_t *cv)
691 {
692
693 KASSERT(cv_is_valid(cv));
694
695 if (__predict_false(!LIST_EMPTY(CV_SLEEPQ(cv))))
696 cv_wakeup_all(cv);
697 }
698
699 /*
700 * cv_wakeup_all:
701 *
702 * Slow path for cv_broadcast(). Deliberately marked __noinline to
703 * prevent the compiler pulling it in to cv_broadcast(), which adds
704 * extra prologue and epilogue code.
705 */
706 static __noinline void
707 cv_wakeup_all(kcondvar_t *cv)
708 {
709 sleepq_t *sq;
710 kmutex_t *mp;
711 lwp_t *l;
712
713 mp = sleepq_hashlock(cv);
714 sq = CV_SLEEPQ(cv);
715 while ((l = LIST_FIRST(sq)) != NULL) {
716 KASSERT(l->l_sleepq == sq);
717 KASSERT(l->l_mutex == mp);
718 KASSERT(l->l_wchan == cv);
719 sleepq_remove(sq, l);
720 }
721 mutex_spin_exit(mp);
722 }
723
724 /*
725 * cv_has_waiters:
726 *
727 * For diagnostic assertions: return non-zero if a condition
728 * variable has waiters.
729 */
730 bool
731 cv_has_waiters(kcondvar_t *cv)
732 {
733
734 return !LIST_EMPTY(CV_SLEEPQ(cv));
735 }
736
737 /*
738 * cv_is_valid:
739 *
740 * For diagnostic assertions: return non-zero if a condition
741 * variable appears to be valid. No locks need be held.
742 */
743 bool
744 cv_is_valid(kcondvar_t *cv)
745 {
746
747 return CV_WMESG(cv) != deadcv && CV_WMESG(cv) != NULL;
748 }
749