Home | History | Annotate | Line # | Download | only in kern
kern_condvar.c revision 1.41.4.2
      1  1.41.4.1    martin /*	$NetBSD: kern_condvar.c,v 1.41.4.2 2020/04/13 08:05:03 martin Exp $	*/
      2       1.2        ad 
      3       1.2        ad /*-
      4  1.41.4.1    martin  * Copyright (c) 2006, 2007, 2008, 2019, 2020 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.41.4.1    martin __KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.41.4.2 2020/04/13 08:05:03 martin 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.37  riastrad #include <sys/kernel.h>
     47      1.20        ad 
     48      1.26   thorpej /*
     49      1.26   thorpej  * Accessors for the private contents of the kcondvar_t data type.
     50      1.26   thorpej  *
     51  1.41.4.1    martin  *	cv_opaque[0]	sleepq_t
     52  1.41.4.1    martin  *	cv_opaque[1]	description for ps(1)
     53      1.26   thorpej  *
     54  1.41.4.1    martin  * cv_opaque[0] is protected by the interlock passed to cv_wait() (enqueue
     55  1.41.4.1    martin  * only), and the sleep queue lock acquired with sleepq_hashlock() (enqueue
     56      1.26   thorpej  * and dequeue).
     57      1.26   thorpej  *
     58  1.41.4.1    martin  * cv_opaque[1] (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.41.4.1    martin #define	CV_WMESG(cv)		((const char *)(cv)->cv_opaque[1])
     63  1.41.4.1    martin #define	CV_SET_WMESG(cv, v) 	(cv)->cv_opaque[1] = __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.41.4.1    martin syncobj_t cv_syncobj = {
     73      1.41     ozaki 	.sobj_flag	= SOBJ_SLEEPQ_SORTED,
     74      1.41     ozaki 	.sobj_unsleep	= cv_unsleep,
     75      1.41     ozaki 	.sobj_changepri	= sleepq_changepri,
     76      1.41     ozaki 	.sobj_lendpri	= sleepq_lendpri,
     77      1.41     ozaki 	.sobj_owner	= syncobj_noowner,
     78       1.2        ad };
     79       1.2        ad 
     80      1.10        ad static const char deadcv[] = "deadcv";
     81      1.10        ad 
     82       1.2        ad /*
     83       1.2        ad  * cv_init:
     84       1.2        ad  *
     85       1.2        ad  *	Initialize a condition variable for use.
     86       1.2        ad  */
     87       1.2        ad void
     88       1.2        ad cv_init(kcondvar_t *cv, const char *wmesg)
     89       1.2        ad {
     90       1.2        ad 
     91      1.21        ad 	KASSERT(wmesg != NULL);
     92      1.26   thorpej 	CV_SET_WMESG(cv, wmesg);
     93      1.20        ad 	sleepq_init(CV_SLEEPQ(cv));
     94       1.2        ad }
     95       1.2        ad 
     96       1.2        ad /*
     97       1.2        ad  * cv_destroy:
     98       1.2        ad  *
     99       1.2        ad  *	Tear down a condition variable.
    100       1.2        ad  */
    101       1.2        ad void
    102       1.2        ad cv_destroy(kcondvar_t *cv)
    103       1.2        ad {
    104       1.2        ad 
    105       1.2        ad #ifdef DIAGNOSTIC
    106      1.15        ad 	KASSERT(cv_is_valid(cv));
    107  1.41.4.2    martin 	KASSERT(!cv_has_waiters(cv));
    108      1.26   thorpej 	CV_SET_WMESG(cv, deadcv);
    109       1.2        ad #endif
    110       1.2        ad }
    111       1.2        ad 
    112       1.2        ad /*
    113       1.2        ad  * cv_enter:
    114       1.2        ad  *
    115       1.2        ad  *	Look up and lock the sleep queue corresponding to the given
    116       1.2        ad  *	condition variable, and increment the number of waiters.
    117       1.2        ad  */
    118      1.20        ad static inline void
    119       1.6        ad cv_enter(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l)
    120       1.2        ad {
    121       1.2        ad 	sleepq_t *sq;
    122      1.18        ad 	kmutex_t *mp;
    123       1.2        ad 
    124      1.15        ad 	KASSERT(cv_is_valid(cv));
    125      1.24        ad 	KASSERT(!cpu_intr_p());
    126      1.14        ad 	KASSERT((l->l_pflag & LP_INTR) == 0 || panicstr != NULL);
    127       1.2        ad 
    128      1.14        ad 	l->l_kpriority = true;
    129      1.24        ad 	mp = sleepq_hashlock(cv);
    130      1.20        ad 	sq = CV_SLEEPQ(cv);
    131      1.18        ad 	sleepq_enter(sq, l, mp);
    132      1.26   thorpej 	sleepq_enqueue(sq, cv, CV_WMESG(cv), &cv_syncobj);
    133       1.2        ad 	mutex_exit(mtx);
    134      1.24        ad 	KASSERT(cv_has_waiters(cv));
    135       1.2        ad }
    136       1.2        ad 
    137       1.2        ad /*
    138       1.2        ad  * cv_unsleep:
    139       1.2        ad  *
    140       1.2        ad  *	Remove an LWP from the condition variable and sleep queue.  This
    141       1.2        ad  *	is called when the LWP has not been awoken normally but instead
    142       1.2        ad  *	interrupted: for example, when a signal is received.  Must be
    143  1.41.4.1    martin  *	called with the LWP locked.  Will unlock if "unlock" is true.
    144       1.2        ad  */
    145      1.27     rmind static void
    146  1.41.4.1    martin cv_unsleep(lwp_t *l, bool unlock)
    147       1.2        ad {
    148      1.34    martin 	kcondvar_t *cv __diagused;
    149       1.2        ad 
    150      1.15        ad 	cv = (kcondvar_t *)(uintptr_t)l->l_wchan;
    151      1.15        ad 
    152      1.20        ad 	KASSERT(l->l_wchan == (wchan_t)cv);
    153      1.20        ad 	KASSERT(l->l_sleepq == CV_SLEEPQ(cv));
    154      1.15        ad 	KASSERT(cv_is_valid(cv));
    155      1.24        ad 	KASSERT(cv_has_waiters(cv));
    156       1.2        ad 
    157  1.41.4.1    martin 	sleepq_unsleep(l, unlock);
    158       1.2        ad }
    159       1.2        ad 
    160       1.2        ad /*
    161       1.2        ad  * cv_wait:
    162       1.2        ad  *
    163       1.2        ad  *	Wait non-interruptably on a condition variable until awoken.
    164       1.2        ad  */
    165       1.2        ad void
    166       1.2        ad cv_wait(kcondvar_t *cv, kmutex_t *mtx)
    167       1.2        ad {
    168       1.6        ad 	lwp_t *l = curlwp;
    169       1.2        ad 
    170       1.8      yamt 	KASSERT(mutex_owned(mtx));
    171       1.2        ad 
    172      1.20        ad 	cv_enter(cv, mtx, l);
    173       1.8      yamt 	(void)sleepq_block(0, false);
    174      1.36       chs 	mutex_enter(mtx);
    175       1.2        ad }
    176       1.2        ad 
    177       1.2        ad /*
    178       1.2        ad  * cv_wait_sig:
    179       1.2        ad  *
    180       1.2        ad  *	Wait on a condition variable until a awoken or a signal is received.
    181       1.2        ad  *	Will also return early if the process is exiting.  Returns zero if
    182      1.29       jym  *	awoken normally, ERESTART if a signal was received and the system
    183       1.2        ad  *	call is restartable, or EINTR otherwise.
    184       1.2        ad  */
    185       1.2        ad int
    186       1.2        ad cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
    187       1.2        ad {
    188       1.6        ad 	lwp_t *l = curlwp;
    189       1.2        ad 	int error;
    190       1.2        ad 
    191       1.8      yamt 	KASSERT(mutex_owned(mtx));
    192       1.2        ad 
    193      1.20        ad 	cv_enter(cv, mtx, l);
    194       1.8      yamt 	error = sleepq_block(0, true);
    195  1.41.4.2    martin 	mutex_enter(mtx);
    196  1.41.4.2    martin 	return error;
    197       1.2        ad }
    198       1.2        ad 
    199       1.2        ad /*
    200       1.2        ad  * cv_timedwait:
    201       1.2        ad  *
    202       1.2        ad  *	Wait on a condition variable until awoken or the specified timeout
    203       1.2        ad  *	expires.  Returns zero if awoken normally or EWOULDBLOCK if the
    204       1.2        ad  *	timeout expired.
    205      1.31       apb  *
    206      1.31       apb  *	timo is a timeout in ticks.  timo = 0 specifies an infinite timeout.
    207       1.2        ad  */
    208       1.2        ad int
    209       1.2        ad cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int timo)
    210       1.2        ad {
    211       1.6        ad 	lwp_t *l = curlwp;
    212       1.2        ad 	int error;
    213       1.2        ad 
    214       1.8      yamt 	KASSERT(mutex_owned(mtx));
    215       1.2        ad 
    216      1.20        ad 	cv_enter(cv, mtx, l);
    217       1.8      yamt 	error = sleepq_block(timo, false);
    218  1.41.4.2    martin 	mutex_enter(mtx);
    219  1.41.4.2    martin 	return error;
    220       1.2        ad }
    221       1.2        ad 
    222       1.2        ad /*
    223       1.2        ad  * cv_timedwait_sig:
    224       1.2        ad  *
    225       1.2        ad  *	Wait on a condition variable until a timeout expires, awoken or a
    226       1.2        ad  *	signal is received.  Will also return early if the process is
    227      1.29       jym  *	exiting.  Returns zero if awoken normally, EWOULDBLOCK if the
    228       1.2        ad  *	timeout expires, ERESTART if a signal was received and the system
    229       1.2        ad  *	call is restartable, or EINTR otherwise.
    230      1.32       apb  *
    231      1.32       apb  *	timo is a timeout in ticks.  timo = 0 specifies an infinite timeout.
    232       1.2        ad  */
    233       1.2        ad int
    234       1.2        ad cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int timo)
    235       1.2        ad {
    236       1.6        ad 	lwp_t *l = curlwp;
    237       1.2        ad 	int error;
    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.8      yamt 	error = sleepq_block(timo, true);
    243  1.41.4.2    martin 	mutex_enter(mtx);
    244  1.41.4.2    martin 	return error;
    245       1.2        ad }
    246       1.2        ad 
    247       1.2        ad /*
    248      1.37  riastrad  * Given a number of seconds, sec, and 2^64ths of a second, frac, we
    249      1.37  riastrad  * want a number of ticks for a timeout:
    250      1.37  riastrad  *
    251      1.37  riastrad  *	timo = hz*(sec + frac/2^64)
    252      1.37  riastrad  *	     = hz*sec + hz*frac/2^64
    253      1.37  riastrad  *	     = hz*sec + hz*(frachi*2^32 + fraclo)/2^64
    254      1.37  riastrad  *	     = hz*sec + hz*frachi/2^32 + hz*fraclo/2^64,
    255      1.37  riastrad  *
    256      1.37  riastrad  * where frachi is the high 32 bits of frac and fraclo is the
    257      1.37  riastrad  * low 32 bits.
    258      1.37  riastrad  *
    259      1.37  riastrad  * We assume hz < INT_MAX/2 < UINT32_MAX, so
    260      1.37  riastrad  *
    261      1.37  riastrad  *	hz*fraclo/2^64 < fraclo*2^32/2^64 <= 1,
    262      1.37  riastrad  *
    263      1.37  riastrad  * since fraclo < 2^32.
    264      1.37  riastrad  *
    265      1.37  riastrad  * We clamp the result at INT_MAX/2 for a timeout in ticks, since we
    266      1.37  riastrad  * can't represent timeouts higher than INT_MAX in cv_timedwait, and
    267      1.37  riastrad  * spurious wakeup is OK.  Moreover, we don't want to wrap around,
    268      1.37  riastrad  * because we compute end - start in ticks in order to compute the
    269      1.37  riastrad  * remaining timeout, and that difference cannot wrap around, so we use
    270      1.37  riastrad  * a timeout less than INT_MAX.  Using INT_MAX/2 provides plenty of
    271      1.37  riastrad  * margin for paranoia and will exceed most waits in practice by far.
    272      1.37  riastrad  */
    273      1.37  riastrad static unsigned
    274      1.37  riastrad bintime2timo(const struct bintime *bt)
    275      1.37  riastrad {
    276      1.37  riastrad 
    277      1.37  riastrad 	KASSERT(hz < INT_MAX/2);
    278      1.37  riastrad 	CTASSERT(INT_MAX/2 < UINT32_MAX);
    279      1.37  riastrad 	if (bt->sec > ((INT_MAX/2)/hz))
    280      1.37  riastrad 		return INT_MAX/2;
    281      1.37  riastrad 	if ((hz*(bt->frac >> 32) >> 32) > (INT_MAX/2 - hz*bt->sec))
    282      1.37  riastrad 		return INT_MAX/2;
    283      1.37  riastrad 
    284      1.37  riastrad 	return hz*bt->sec + (hz*(bt->frac >> 32) >> 32);
    285      1.37  riastrad }
    286      1.37  riastrad 
    287      1.37  riastrad /*
    288      1.37  riastrad  * timo is in units of ticks.  We want units of seconds and 2^64ths of
    289      1.37  riastrad  * a second.  We know hz = 1 sec/tick, and 2^64 = 1 sec/(2^64th of a
    290      1.37  riastrad  * second), from which we can conclude 2^64 / hz = 1 (2^64th of a
    291      1.37  riastrad  * second)/tick.  So for the fractional part, we compute
    292      1.37  riastrad  *
    293      1.37  riastrad  *	frac = rem * 2^64 / hz
    294      1.37  riastrad  *	     = ((rem * 2^32) / hz) * 2^32
    295      1.37  riastrad  *
    296      1.37  riastrad  * Using truncating integer division instead of real division will
    297      1.37  riastrad  * leave us with only about 32 bits of precision, which means about
    298      1.37  riastrad  * 1/4-nanosecond resolution, which is good enough for our purposes.
    299      1.37  riastrad  */
    300      1.37  riastrad static struct bintime
    301      1.37  riastrad timo2bintime(unsigned timo)
    302      1.37  riastrad {
    303      1.37  riastrad 
    304      1.37  riastrad 	return (struct bintime) {
    305      1.37  riastrad 		.sec = timo / hz,
    306      1.37  riastrad 		.frac = (((uint64_t)(timo % hz) << 32)/hz << 32),
    307      1.37  riastrad 	};
    308      1.37  riastrad }
    309      1.37  riastrad 
    310      1.37  riastrad /*
    311      1.37  riastrad  * cv_timedwaitbt:
    312      1.37  riastrad  *
    313      1.37  riastrad  *	Wait on a condition variable until awoken or the specified
    314      1.37  riastrad  *	timeout expires.  Returns zero if awoken normally or
    315      1.37  riastrad  *	EWOULDBLOCK if the timeout expires.
    316      1.37  riastrad  *
    317      1.37  riastrad  *	On entry, bt is a timeout in bintime.  cv_timedwaitbt subtracts
    318      1.37  riastrad  *	the time slept, so on exit, bt is the time remaining after
    319      1.38  riastrad  *	sleeping, possibly negative if the complete time has elapsed.
    320      1.38  riastrad  *	No infinite timeout; use cv_wait_sig instead.
    321      1.37  riastrad  *
    322      1.37  riastrad  *	epsilon is a requested maximum error in timeout (excluding
    323      1.37  riastrad  *	spurious wakeups).  Currently not used, will be used in the
    324      1.37  riastrad  *	future to choose between low- and high-resolution timers.
    325      1.38  riastrad  *	Actual wakeup time will be somewhere in [t, t + max(e, r) + s)
    326      1.38  riastrad  *	where r is the finest resolution of clock available and s is
    327      1.38  riastrad  *	scheduling delays for scheduler overhead and competing threads.
    328      1.38  riastrad  *	Time is measured by the interrupt source implementing the
    329      1.38  riastrad  *	timeout, not by another timecounter.
    330      1.37  riastrad  */
    331      1.37  riastrad int
    332      1.37  riastrad cv_timedwaitbt(kcondvar_t *cv, kmutex_t *mtx, struct bintime *bt,
    333      1.38  riastrad     const struct bintime *epsilon __diagused)
    334      1.37  riastrad {
    335      1.37  riastrad 	struct bintime slept;
    336      1.37  riastrad 	unsigned start, end;
    337      1.37  riastrad 	int error;
    338      1.37  riastrad 
    339      1.38  riastrad 	KASSERTMSG(bt->sec >= 0, "negative timeout");
    340      1.38  riastrad 	KASSERTMSG(epsilon != NULL, "specify maximum requested delay");
    341      1.38  riastrad 
    342      1.37  riastrad 	/*
    343      1.37  riastrad 	 * hardclock_ticks is technically int, but nothing special
    344      1.37  riastrad 	 * happens instead of overflow, so we assume two's-complement
    345      1.37  riastrad 	 * wraparound and just treat it as unsigned.
    346      1.37  riastrad 	 */
    347      1.37  riastrad 	start = hardclock_ticks;
    348      1.37  riastrad 	error = cv_timedwait(cv, mtx, bintime2timo(bt));
    349      1.37  riastrad 	end = hardclock_ticks;
    350      1.37  riastrad 
    351      1.37  riastrad 	slept = timo2bintime(end - start);
    352      1.37  riastrad 	/* bt := bt - slept */
    353      1.37  riastrad 	bintime_sub(bt, &slept);
    354      1.37  riastrad 
    355      1.37  riastrad 	return error;
    356      1.37  riastrad }
    357      1.37  riastrad 
    358      1.37  riastrad /*
    359      1.37  riastrad  * cv_timedwaitbt_sig:
    360      1.37  riastrad  *
    361      1.37  riastrad  *	Wait on a condition variable until awoken, the specified
    362      1.37  riastrad  *	timeout expires, or interrupted by a signal.  Returns zero if
    363      1.37  riastrad  *	awoken normally, EWOULDBLOCK if the timeout expires, or
    364      1.37  riastrad  *	EINTR/ERESTART if interrupted by a signal.
    365      1.37  riastrad  *
    366      1.37  riastrad  *	On entry, bt is a timeout in bintime.  cv_timedwaitbt_sig
    367      1.37  riastrad  *	subtracts the time slept, so on exit, bt is the time remaining
    368      1.37  riastrad  *	after sleeping.  No infinite timeout; use cv_wait instead.
    369      1.37  riastrad  *
    370      1.37  riastrad  *	epsilon is a requested maximum error in timeout (excluding
    371      1.37  riastrad  *	spurious wakeups).  Currently not used, will be used in the
    372      1.37  riastrad  *	future to choose between low- and high-resolution timers.
    373      1.37  riastrad  */
    374      1.37  riastrad int
    375      1.37  riastrad cv_timedwaitbt_sig(kcondvar_t *cv, kmutex_t *mtx, struct bintime *bt,
    376      1.39  riastrad     const struct bintime *epsilon __diagused)
    377      1.37  riastrad {
    378      1.37  riastrad 	struct bintime slept;
    379      1.37  riastrad 	unsigned start, end;
    380      1.37  riastrad 	int error;
    381      1.37  riastrad 
    382      1.39  riastrad 	KASSERTMSG(bt->sec >= 0, "negative timeout");
    383      1.39  riastrad 	KASSERTMSG(epsilon != NULL, "specify maximum requested delay");
    384      1.39  riastrad 
    385      1.37  riastrad 	/*
    386      1.37  riastrad 	 * hardclock_ticks is technically int, but nothing special
    387      1.37  riastrad 	 * happens instead of overflow, so we assume two's-complement
    388      1.37  riastrad 	 * wraparound and just treat it as unsigned.
    389      1.37  riastrad 	 */
    390      1.37  riastrad 	start = hardclock_ticks;
    391      1.37  riastrad 	error = cv_timedwait_sig(cv, mtx, bintime2timo(bt));
    392      1.37  riastrad 	end = hardclock_ticks;
    393      1.37  riastrad 
    394      1.37  riastrad 	slept = timo2bintime(end - start);
    395      1.37  riastrad 	/* bt := bt - slept */
    396      1.37  riastrad 	bintime_sub(bt, &slept);
    397      1.37  riastrad 
    398      1.37  riastrad 	return error;
    399      1.37  riastrad }
    400      1.37  riastrad 
    401      1.37  riastrad /*
    402       1.2        ad  * cv_signal:
    403       1.2        ad  *
    404       1.2        ad  *	Wake the highest priority LWP waiting on a condition variable.
    405       1.2        ad  *	Must be called with the interlocking mutex held.
    406       1.2        ad  */
    407       1.2        ad void
    408       1.2        ad cv_signal(kcondvar_t *cv)
    409       1.2        ad {
    410      1.20        ad 
    411      1.20        ad 	KASSERT(cv_is_valid(cv));
    412      1.20        ad 
    413  1.41.4.1    martin 	if (__predict_false(!LIST_EMPTY(CV_SLEEPQ(cv))))
    414      1.24        ad 		cv_wakeup_one(cv);
    415      1.20        ad }
    416      1.20        ad 
    417  1.41.4.1    martin /*
    418  1.41.4.1    martin  * cv_wakeup_one:
    419  1.41.4.1    martin  *
    420  1.41.4.1    martin  *	Slow path for cv_signal().  Deliberately marked __noinline to
    421  1.41.4.1    martin  *	prevent the compiler pulling it in to cv_signal(), which adds
    422  1.41.4.1    martin  *	extra prologue and epilogue code.
    423  1.41.4.1    martin  */
    424  1.41.4.1    martin static __noinline void
    425      1.20        ad cv_wakeup_one(kcondvar_t *cv)
    426      1.20        ad {
    427       1.2        ad 	sleepq_t *sq;
    428      1.18        ad 	kmutex_t *mp;
    429      1.20        ad 	lwp_t *l;
    430       1.2        ad 
    431  1.41.4.2    martin 	/*
    432  1.41.4.2    martin 	 * Keep waking LWPs until a non-interruptable waiter is found.  An
    433  1.41.4.2    martin 	 * interruptable waiter could fail to do something useful with the
    434  1.41.4.2    martin 	 * wakeup due to an error return from cv_[timed]wait_sig(), and the
    435  1.41.4.2    martin 	 * caller of cv_signal() may not expect such a scenario.
    436  1.41.4.2    martin 	 *
    437  1.41.4.2    martin 	 * This isn't a problem for non-interruptable waits (untimed and
    438  1.41.4.2    martin 	 * timed), because if such a waiter is woken here it will not return
    439  1.41.4.2    martin 	 * an error.
    440  1.41.4.2    martin 	 */
    441      1.24        ad 	mp = sleepq_hashlock(cv);
    442      1.20        ad 	sq = CV_SLEEPQ(cv);
    443  1.41.4.2    martin 	while ((l = LIST_FIRST(sq)) != NULL) {
    444  1.41.4.2    martin 		KASSERT(l->l_sleepq == sq);
    445  1.41.4.2    martin 		KASSERT(l->l_mutex == mp);
    446  1.41.4.2    martin 		KASSERT(l->l_wchan == cv);
    447  1.41.4.2    martin 		if ((l->l_flag & LW_SINTR) == 0) {
    448  1.41.4.2    martin 			sleepq_remove(sq, l);
    449  1.41.4.2    martin 			break;
    450  1.41.4.2    martin 		} else
    451  1.41.4.2    martin 			sleepq_remove(sq, l);
    452      1.20        ad 	}
    453      1.20        ad 	mutex_spin_exit(mp);
    454       1.2        ad }
    455       1.2        ad 
    456       1.2        ad /*
    457       1.2        ad  * cv_broadcast:
    458       1.2        ad  *
    459       1.2        ad  *	Wake all LWPs waiting on a condition variable.  Must be called
    460       1.2        ad  *	with the interlocking mutex held.
    461       1.2        ad  */
    462       1.2        ad void
    463       1.2        ad cv_broadcast(kcondvar_t *cv)
    464       1.2        ad {
    465      1.20        ad 
    466      1.20        ad 	KASSERT(cv_is_valid(cv));
    467      1.20        ad 
    468  1.41.4.1    martin 	if (__predict_false(!LIST_EMPTY(CV_SLEEPQ(cv))))
    469      1.24        ad 		cv_wakeup_all(cv);
    470      1.20        ad }
    471      1.20        ad 
    472  1.41.4.1    martin /*
    473  1.41.4.1    martin  * cv_wakeup_all:
    474  1.41.4.1    martin  *
    475  1.41.4.1    martin  *	Slow path for cv_broadcast().  Deliberately marked __noinline to
    476  1.41.4.1    martin  *	prevent the compiler pulling it in to cv_broadcast(), which adds
    477  1.41.4.1    martin  *	extra prologue and epilogue code.
    478  1.41.4.1    martin  */
    479  1.41.4.1    martin static __noinline void
    480      1.20        ad cv_wakeup_all(kcondvar_t *cv)
    481      1.20        ad {
    482       1.2        ad 	sleepq_t *sq;
    483      1.18        ad 	kmutex_t *mp;
    484  1.41.4.2    martin 	lwp_t *l;
    485      1.15        ad 
    486      1.24        ad 	mp = sleepq_hashlock(cv);
    487      1.20        ad 	sq = CV_SLEEPQ(cv);
    488  1.41.4.2    martin 	while ((l = LIST_FIRST(sq)) != NULL) {
    489      1.20        ad 		KASSERT(l->l_sleepq == sq);
    490      1.20        ad 		KASSERT(l->l_mutex == mp);
    491      1.20        ad 		KASSERT(l->l_wchan == cv);
    492      1.27     rmind 		sleepq_remove(sq, l);
    493      1.20        ad 	}
    494      1.20        ad 	mutex_spin_exit(mp);
    495       1.2        ad }
    496       1.2        ad 
    497       1.2        ad /*
    498       1.2        ad  * cv_has_waiters:
    499       1.2        ad  *
    500       1.2        ad  *	For diagnostic assertions: return non-zero if a condition
    501       1.2        ad  *	variable has waiters.
    502       1.2        ad  */
    503       1.7        ad bool
    504       1.2        ad cv_has_waiters(kcondvar_t *cv)
    505       1.2        ad {
    506      1.23     chris 
    507  1.41.4.1    martin 	return !LIST_EMPTY(CV_SLEEPQ(cv));
    508       1.2        ad }
    509      1.15        ad 
    510      1.15        ad /*
    511      1.15        ad  * cv_is_valid:
    512      1.15        ad  *
    513      1.15        ad  *	For diagnostic assertions: return non-zero if a condition
    514      1.15        ad  *	variable appears to be valid.  No locks need be held.
    515      1.15        ad  */
    516      1.15        ad bool
    517      1.15        ad cv_is_valid(kcondvar_t *cv)
    518      1.15        ad {
    519      1.15        ad 
    520      1.26   thorpej 	return CV_WMESG(cv) != deadcv && CV_WMESG(cv) != NULL;
    521      1.15        ad }
    522