Home | History | Annotate | Line # | Download | only in kern
kern_condvar.c revision 1.30.12.3
      1       1.30  uebayasi /*	$NetBSD: kern_condvar.c,v 1.30.12.3 2017/12/03 11:38:44 jdolecek 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.30  uebayasi __KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.30.12.3 2017/12/03 11:38:44 jdolecek Exp $");
     38        1.2        ad 
     39        1.2        ad #include <sys/param.h>
     40        1.2        ad #include <sys/systm.h>
     41  1.30.12.3  jdolecek #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.30.12.3  jdolecek #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.26   thorpej  *	cv_opaque[0]	sleepq...
     52       1.26   thorpej  *	cv_opaque[1]	...pointers
     53       1.26   thorpej  *	cv_opaque[2]	description for ps(1)
     54       1.26   thorpej  *
     55       1.26   thorpej  * cv_opaque[0..1] is protected by the interlock passed to cv_wait() (enqueue
     56       1.26   thorpej  * only), and the sleep queue lock acquired with sleeptab_lookup() (enqueue
     57       1.26   thorpej  * and dequeue).
     58       1.26   thorpej  *
     59       1.26   thorpej  * cv_opaque[2] (the wmesg) is static and does not change throughout the life
     60       1.26   thorpej  * of the CV.
     61       1.26   thorpej  */
     62       1.26   thorpej #define	CV_SLEEPQ(cv)		((sleepq_t *)(cv)->cv_opaque)
     63       1.26   thorpej #define	CV_WMESG(cv)		((const char *)(cv)->cv_opaque[2])
     64       1.26   thorpej #define	CV_SET_WMESG(cv, v) 	(cv)->cv_opaque[2] = __UNCONST(v)
     65       1.26   thorpej 
     66       1.26   thorpej #define	CV_DEBUG_P(cv)	(CV_WMESG(cv) != nodebug)
     67       1.20        ad #define	CV_RA		((uintptr_t)__builtin_return_address(0))
     68        1.2        ad 
     69  1.30.12.3  jdolecek static void		cv_unsleep(lwp_t *, bool);
     70  1.30.12.3  jdolecek static inline void	cv_wakeup_one(kcondvar_t *);
     71  1.30.12.3  jdolecek static inline void	cv_wakeup_all(kcondvar_t *);
     72        1.2        ad 
     73       1.10        ad static syncobj_t cv_syncobj = {
     74        1.2        ad 	SOBJ_SLEEPQ_SORTED,
     75        1.2        ad 	cv_unsleep,
     76       1.14        ad 	sleepq_changepri,
     77        1.4      yamt 	sleepq_lendpri,
     78        1.4      yamt 	syncobj_noowner,
     79        1.2        ad };
     80        1.2        ad 
     81       1.20        ad lockops_t cv_lockops = {
     82       1.20        ad 	"Condition variable",
     83       1.20        ad 	LOCKOPS_CV,
     84       1.20        ad 	NULL
     85       1.20        ad };
     86       1.20        ad 
     87       1.10        ad static const char deadcv[] = "deadcv";
     88  1.30.12.2       tls #ifdef LOCKDEBUG
     89       1.20        ad static const char nodebug[] = "nodebug";
     90  1.30.12.3  jdolecek 
     91  1.30.12.3  jdolecek #define CV_LOCKDEBUG_HANDOFF(l, cv) cv_lockdebug_handoff(l, cv)
     92  1.30.12.3  jdolecek #define CV_LOCKDEBUG_PROCESS(l, cv) cv_lockdebug_process(l, cv)
     93  1.30.12.3  jdolecek 
     94  1.30.12.3  jdolecek static inline void
     95  1.30.12.3  jdolecek cv_lockdebug_handoff(lwp_t *l, kcondvar_t *cv)
     96  1.30.12.3  jdolecek {
     97  1.30.12.3  jdolecek 
     98  1.30.12.3  jdolecek 	if (CV_DEBUG_P(cv))
     99  1.30.12.3  jdolecek 		l->l_flag |= LW_CVLOCKDEBUG;
    100  1.30.12.3  jdolecek }
    101  1.30.12.3  jdolecek 
    102  1.30.12.3  jdolecek static inline void
    103  1.30.12.3  jdolecek cv_lockdebug_process(lwp_t *l, kcondvar_t *cv)
    104  1.30.12.3  jdolecek {
    105  1.30.12.3  jdolecek 
    106  1.30.12.3  jdolecek 	if ((l->l_flag & LW_CVLOCKDEBUG) == 0)
    107  1.30.12.3  jdolecek 		return;
    108  1.30.12.3  jdolecek 
    109  1.30.12.3  jdolecek 	l->l_flag &= ~LW_CVLOCKDEBUG;
    110  1.30.12.3  jdolecek 	LOCKDEBUG_UNLOCKED(true, cv, CV_RA, 0);
    111  1.30.12.3  jdolecek }
    112  1.30.12.3  jdolecek #else
    113  1.30.12.3  jdolecek #define CV_LOCKDEBUG_HANDOFF(l, cv) __nothing
    114  1.30.12.3  jdolecek #define CV_LOCKDEBUG_PROCESS(l, cv) __nothing
    115  1.30.12.2       tls #endif
    116       1.10        ad 
    117        1.2        ad /*
    118        1.2        ad  * cv_init:
    119        1.2        ad  *
    120        1.2        ad  *	Initialize a condition variable for use.
    121        1.2        ad  */
    122        1.2        ad void
    123        1.2        ad cv_init(kcondvar_t *cv, const char *wmesg)
    124        1.2        ad {
    125       1.21        ad #ifdef LOCKDEBUG
    126       1.20        ad 	bool dodebug;
    127        1.2        ad 
    128       1.20        ad 	dodebug = LOCKDEBUG_ALLOC(cv, &cv_lockops,
    129       1.20        ad 	    (uintptr_t)__builtin_return_address(0));
    130       1.21        ad 	if (!dodebug) {
    131       1.20        ad 		/* XXX This will break vfs_lockf. */
    132       1.21        ad 		wmesg = nodebug;
    133       1.20        ad 	}
    134       1.21        ad #endif
    135       1.21        ad 	KASSERT(wmesg != NULL);
    136       1.26   thorpej 	CV_SET_WMESG(cv, wmesg);
    137       1.20        ad 	sleepq_init(CV_SLEEPQ(cv));
    138        1.2        ad }
    139        1.2        ad 
    140        1.2        ad /*
    141        1.2        ad  * cv_destroy:
    142        1.2        ad  *
    143        1.2        ad  *	Tear down a condition variable.
    144        1.2        ad  */
    145        1.2        ad void
    146        1.2        ad cv_destroy(kcondvar_t *cv)
    147        1.2        ad {
    148        1.2        ad 
    149       1.20        ad 	LOCKDEBUG_FREE(CV_DEBUG_P(cv), cv);
    150        1.2        ad #ifdef DIAGNOSTIC
    151       1.15        ad 	KASSERT(cv_is_valid(cv));
    152       1.26   thorpej 	CV_SET_WMESG(cv, deadcv);
    153        1.2        ad #endif
    154        1.2        ad }
    155        1.2        ad 
    156        1.2        ad /*
    157        1.2        ad  * cv_enter:
    158        1.2        ad  *
    159        1.2        ad  *	Look up and lock the sleep queue corresponding to the given
    160        1.2        ad  *	condition variable, and increment the number of waiters.
    161        1.2        ad  */
    162       1.20        ad static inline void
    163        1.6        ad cv_enter(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l)
    164        1.2        ad {
    165        1.2        ad 	sleepq_t *sq;
    166       1.18        ad 	kmutex_t *mp;
    167        1.2        ad 
    168       1.15        ad 	KASSERT(cv_is_valid(cv));
    169       1.24        ad 	KASSERT(!cpu_intr_p());
    170       1.14        ad 	KASSERT((l->l_pflag & LP_INTR) == 0 || panicstr != NULL);
    171        1.2        ad 
    172       1.20        ad 	LOCKDEBUG_LOCKED(CV_DEBUG_P(cv), cv, mtx, CV_RA, 0);
    173       1.20        ad 
    174       1.14        ad 	l->l_kpriority = true;
    175       1.24        ad 	mp = sleepq_hashlock(cv);
    176       1.20        ad 	sq = CV_SLEEPQ(cv);
    177       1.18        ad 	sleepq_enter(sq, l, mp);
    178       1.26   thorpej 	sleepq_enqueue(sq, cv, CV_WMESG(cv), &cv_syncobj);
    179        1.2        ad 	mutex_exit(mtx);
    180       1.24        ad 	KASSERT(cv_has_waiters(cv));
    181        1.2        ad }
    182        1.2        ad 
    183        1.2        ad /*
    184        1.6        ad  * cv_exit:
    185        1.6        ad  *
    186        1.6        ad  *	After resuming execution, check to see if we have been restarted
    187        1.6        ad  *	as a result of cv_signal().  If we have, but cannot take the
    188        1.6        ad  *	wakeup (because of eg a pending Unix signal or timeout) then try
    189        1.6        ad  *	to ensure that another LWP sees it.  This is necessary because
    190        1.6        ad  *	there may be multiple waiters, and at least one should take the
    191        1.6        ad  *	wakeup if possible.
    192        1.6        ad  */
    193        1.6        ad static inline int
    194        1.6        ad cv_exit(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l, const int error)
    195        1.6        ad {
    196        1.6        ad 
    197        1.6        ad 	mutex_enter(mtx);
    198       1.20        ad 	if (__predict_false(error != 0))
    199        1.6        ad 		cv_signal(cv);
    200        1.6        ad 
    201       1.20        ad 	LOCKDEBUG_UNLOCKED(CV_DEBUG_P(cv), cv, CV_RA, 0);
    202       1.15        ad 	KASSERT(cv_is_valid(cv));
    203       1.10        ad 
    204        1.6        ad 	return error;
    205        1.6        ad }
    206        1.6        ad 
    207        1.6        ad /*
    208        1.2        ad  * cv_unsleep:
    209        1.2        ad  *
    210        1.2        ad  *	Remove an LWP from the condition variable and sleep queue.  This
    211        1.2        ad  *	is called when the LWP has not been awoken normally but instead
    212        1.2        ad  *	interrupted: for example, when a signal is received.  Must be
    213        1.2        ad  *	called with the LWP locked, and must return it unlocked.
    214        1.2        ad  */
    215       1.27     rmind static void
    216       1.16        ad cv_unsleep(lwp_t *l, bool cleanup)
    217        1.2        ad {
    218  1.30.12.2       tls 	kcondvar_t *cv __diagused;
    219        1.2        ad 
    220       1.15        ad 	cv = (kcondvar_t *)(uintptr_t)l->l_wchan;
    221       1.15        ad 
    222       1.20        ad 	KASSERT(l->l_wchan == (wchan_t)cv);
    223       1.20        ad 	KASSERT(l->l_sleepq == CV_SLEEPQ(cv));
    224       1.15        ad 	KASSERT(cv_is_valid(cv));
    225       1.24        ad 	KASSERT(cv_has_waiters(cv));
    226        1.2        ad 
    227       1.27     rmind 	sleepq_unsleep(l, cleanup);
    228        1.2        ad }
    229        1.2        ad 
    230        1.2        ad /*
    231        1.2        ad  * cv_wait:
    232        1.2        ad  *
    233        1.2        ad  *	Wait non-interruptably on a condition variable until awoken.
    234        1.2        ad  */
    235        1.2        ad void
    236        1.2        ad cv_wait(kcondvar_t *cv, kmutex_t *mtx)
    237        1.2        ad {
    238        1.6        ad 	lwp_t *l = curlwp;
    239        1.2        ad 
    240        1.8      yamt 	KASSERT(mutex_owned(mtx));
    241        1.2        ad 
    242       1.20        ad 	cv_enter(cv, mtx, l);
    243  1.30.12.3  jdolecek 
    244  1.30.12.3  jdolecek 	/*
    245  1.30.12.3  jdolecek 	 * We can't use cv_exit() here since the cv might be destroyed before
    246  1.30.12.3  jdolecek 	 * this thread gets a chance to run.  Instead, hand off the lockdebug
    247  1.30.12.3  jdolecek 	 * responsibility to the thread that wakes us up.
    248  1.30.12.3  jdolecek 	 */
    249  1.30.12.3  jdolecek 
    250  1.30.12.3  jdolecek 	CV_LOCKDEBUG_HANDOFF(l, cv);
    251        1.8      yamt 	(void)sleepq_block(0, false);
    252  1.30.12.3  jdolecek 	mutex_enter(mtx);
    253        1.2        ad }
    254        1.2        ad 
    255        1.2        ad /*
    256        1.2        ad  * cv_wait_sig:
    257        1.2        ad  *
    258        1.2        ad  *	Wait on a condition variable until a awoken or a signal is received.
    259        1.2        ad  *	Will also return early if the process is exiting.  Returns zero if
    260       1.29       jym  *	awoken normally, ERESTART if a signal was received and the system
    261        1.2        ad  *	call is restartable, or EINTR otherwise.
    262        1.2        ad  */
    263        1.2        ad int
    264        1.2        ad cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
    265        1.2        ad {
    266        1.6        ad 	lwp_t *l = curlwp;
    267        1.2        ad 	int error;
    268        1.2        ad 
    269        1.8      yamt 	KASSERT(mutex_owned(mtx));
    270        1.2        ad 
    271       1.20        ad 	cv_enter(cv, mtx, l);
    272        1.8      yamt 	error = sleepq_block(0, true);
    273        1.6        ad 	return cv_exit(cv, mtx, l, error);
    274        1.2        ad }
    275        1.2        ad 
    276        1.2        ad /*
    277        1.2        ad  * cv_timedwait:
    278        1.2        ad  *
    279        1.2        ad  *	Wait on a condition variable until awoken or the specified timeout
    280        1.2        ad  *	expires.  Returns zero if awoken normally or EWOULDBLOCK if the
    281        1.2        ad  *	timeout expired.
    282  1.30.12.1       tls  *
    283  1.30.12.1       tls  *	timo is a timeout in ticks.  timo = 0 specifies an infinite timeout.
    284        1.2        ad  */
    285        1.2        ad int
    286        1.2        ad cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int timo)
    287        1.2        ad {
    288        1.6        ad 	lwp_t *l = curlwp;
    289        1.2        ad 	int error;
    290        1.2        ad 
    291        1.8      yamt 	KASSERT(mutex_owned(mtx));
    292        1.2        ad 
    293       1.20        ad 	cv_enter(cv, mtx, l);
    294        1.8      yamt 	error = sleepq_block(timo, false);
    295        1.6        ad 	return cv_exit(cv, mtx, l, error);
    296        1.2        ad }
    297        1.2        ad 
    298        1.2        ad /*
    299        1.2        ad  * cv_timedwait_sig:
    300        1.2        ad  *
    301        1.2        ad  *	Wait on a condition variable until a timeout expires, awoken or a
    302        1.2        ad  *	signal is received.  Will also return early if the process is
    303       1.29       jym  *	exiting.  Returns zero if awoken normally, EWOULDBLOCK if the
    304        1.2        ad  *	timeout expires, ERESTART if a signal was received and the system
    305        1.2        ad  *	call is restartable, or EINTR otherwise.
    306  1.30.12.1       tls  *
    307  1.30.12.1       tls  *	timo is a timeout in ticks.  timo = 0 specifies an infinite timeout.
    308        1.2        ad  */
    309        1.2        ad int
    310        1.2        ad cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int timo)
    311        1.2        ad {
    312        1.6        ad 	lwp_t *l = curlwp;
    313        1.2        ad 	int error;
    314        1.2        ad 
    315        1.8      yamt 	KASSERT(mutex_owned(mtx));
    316        1.2        ad 
    317       1.20        ad 	cv_enter(cv, mtx, l);
    318        1.8      yamt 	error = sleepq_block(timo, true);
    319        1.6        ad 	return cv_exit(cv, mtx, l, error);
    320        1.2        ad }
    321        1.2        ad 
    322        1.2        ad /*
    323  1.30.12.3  jdolecek  * Given a number of seconds, sec, and 2^64ths of a second, frac, we
    324  1.30.12.3  jdolecek  * want a number of ticks for a timeout:
    325  1.30.12.3  jdolecek  *
    326  1.30.12.3  jdolecek  *	timo = hz*(sec + frac/2^64)
    327  1.30.12.3  jdolecek  *	     = hz*sec + hz*frac/2^64
    328  1.30.12.3  jdolecek  *	     = hz*sec + hz*(frachi*2^32 + fraclo)/2^64
    329  1.30.12.3  jdolecek  *	     = hz*sec + hz*frachi/2^32 + hz*fraclo/2^64,
    330  1.30.12.3  jdolecek  *
    331  1.30.12.3  jdolecek  * where frachi is the high 32 bits of frac and fraclo is the
    332  1.30.12.3  jdolecek  * low 32 bits.
    333  1.30.12.3  jdolecek  *
    334  1.30.12.3  jdolecek  * We assume hz < INT_MAX/2 < UINT32_MAX, so
    335  1.30.12.3  jdolecek  *
    336  1.30.12.3  jdolecek  *	hz*fraclo/2^64 < fraclo*2^32/2^64 <= 1,
    337  1.30.12.3  jdolecek  *
    338  1.30.12.3  jdolecek  * since fraclo < 2^32.
    339  1.30.12.3  jdolecek  *
    340  1.30.12.3  jdolecek  * We clamp the result at INT_MAX/2 for a timeout in ticks, since we
    341  1.30.12.3  jdolecek  * can't represent timeouts higher than INT_MAX in cv_timedwait, and
    342  1.30.12.3  jdolecek  * spurious wakeup is OK.  Moreover, we don't want to wrap around,
    343  1.30.12.3  jdolecek  * because we compute end - start in ticks in order to compute the
    344  1.30.12.3  jdolecek  * remaining timeout, and that difference cannot wrap around, so we use
    345  1.30.12.3  jdolecek  * a timeout less than INT_MAX.  Using INT_MAX/2 provides plenty of
    346  1.30.12.3  jdolecek  * margin for paranoia and will exceed most waits in practice by far.
    347  1.30.12.3  jdolecek  */
    348  1.30.12.3  jdolecek static unsigned
    349  1.30.12.3  jdolecek bintime2timo(const struct bintime *bt)
    350  1.30.12.3  jdolecek {
    351  1.30.12.3  jdolecek 
    352  1.30.12.3  jdolecek 	KASSERT(hz < INT_MAX/2);
    353  1.30.12.3  jdolecek 	CTASSERT(INT_MAX/2 < UINT32_MAX);
    354  1.30.12.3  jdolecek 	if (bt->sec > ((INT_MAX/2)/hz))
    355  1.30.12.3  jdolecek 		return INT_MAX/2;
    356  1.30.12.3  jdolecek 	if ((hz*(bt->frac >> 32) >> 32) > (INT_MAX/2 - hz*bt->sec))
    357  1.30.12.3  jdolecek 		return INT_MAX/2;
    358  1.30.12.3  jdolecek 
    359  1.30.12.3  jdolecek 	return hz*bt->sec + (hz*(bt->frac >> 32) >> 32);
    360  1.30.12.3  jdolecek }
    361  1.30.12.3  jdolecek 
    362  1.30.12.3  jdolecek /*
    363  1.30.12.3  jdolecek  * timo is in units of ticks.  We want units of seconds and 2^64ths of
    364  1.30.12.3  jdolecek  * a second.  We know hz = 1 sec/tick, and 2^64 = 1 sec/(2^64th of a
    365  1.30.12.3  jdolecek  * second), from which we can conclude 2^64 / hz = 1 (2^64th of a
    366  1.30.12.3  jdolecek  * second)/tick.  So for the fractional part, we compute
    367  1.30.12.3  jdolecek  *
    368  1.30.12.3  jdolecek  *	frac = rem * 2^64 / hz
    369  1.30.12.3  jdolecek  *	     = ((rem * 2^32) / hz) * 2^32
    370  1.30.12.3  jdolecek  *
    371  1.30.12.3  jdolecek  * Using truncating integer division instead of real division will
    372  1.30.12.3  jdolecek  * leave us with only about 32 bits of precision, which means about
    373  1.30.12.3  jdolecek  * 1/4-nanosecond resolution, which is good enough for our purposes.
    374  1.30.12.3  jdolecek  */
    375  1.30.12.3  jdolecek static struct bintime
    376  1.30.12.3  jdolecek timo2bintime(unsigned timo)
    377  1.30.12.3  jdolecek {
    378  1.30.12.3  jdolecek 
    379  1.30.12.3  jdolecek 	return (struct bintime) {
    380  1.30.12.3  jdolecek 		.sec = timo / hz,
    381  1.30.12.3  jdolecek 		.frac = (((uint64_t)(timo % hz) << 32)/hz << 32),
    382  1.30.12.3  jdolecek 	};
    383  1.30.12.3  jdolecek }
    384  1.30.12.3  jdolecek 
    385  1.30.12.3  jdolecek /*
    386  1.30.12.3  jdolecek  * cv_timedwaitbt:
    387  1.30.12.3  jdolecek  *
    388  1.30.12.3  jdolecek  *	Wait on a condition variable until awoken or the specified
    389  1.30.12.3  jdolecek  *	timeout expires.  Returns zero if awoken normally or
    390  1.30.12.3  jdolecek  *	EWOULDBLOCK if the timeout expires.
    391  1.30.12.3  jdolecek  *
    392  1.30.12.3  jdolecek  *	On entry, bt is a timeout in bintime.  cv_timedwaitbt subtracts
    393  1.30.12.3  jdolecek  *	the time slept, so on exit, bt is the time remaining after
    394  1.30.12.3  jdolecek  *	sleeping, possibly negative if the complete time has elapsed.
    395  1.30.12.3  jdolecek  *	No infinite timeout; use cv_wait_sig instead.
    396  1.30.12.3  jdolecek  *
    397  1.30.12.3  jdolecek  *	epsilon is a requested maximum error in timeout (excluding
    398  1.30.12.3  jdolecek  *	spurious wakeups).  Currently not used, will be used in the
    399  1.30.12.3  jdolecek  *	future to choose between low- and high-resolution timers.
    400  1.30.12.3  jdolecek  *	Actual wakeup time will be somewhere in [t, t + max(e, r) + s)
    401  1.30.12.3  jdolecek  *	where r is the finest resolution of clock available and s is
    402  1.30.12.3  jdolecek  *	scheduling delays for scheduler overhead and competing threads.
    403  1.30.12.3  jdolecek  *	Time is measured by the interrupt source implementing the
    404  1.30.12.3  jdolecek  *	timeout, not by another timecounter.
    405  1.30.12.3  jdolecek  */
    406  1.30.12.3  jdolecek int
    407  1.30.12.3  jdolecek cv_timedwaitbt(kcondvar_t *cv, kmutex_t *mtx, struct bintime *bt,
    408  1.30.12.3  jdolecek     const struct bintime *epsilon __diagused)
    409  1.30.12.3  jdolecek {
    410  1.30.12.3  jdolecek 	struct bintime slept;
    411  1.30.12.3  jdolecek 	unsigned start, end;
    412  1.30.12.3  jdolecek 	int error;
    413  1.30.12.3  jdolecek 
    414  1.30.12.3  jdolecek 	KASSERTMSG(bt->sec >= 0, "negative timeout");
    415  1.30.12.3  jdolecek 	KASSERTMSG(epsilon != NULL, "specify maximum requested delay");
    416  1.30.12.3  jdolecek 
    417  1.30.12.3  jdolecek 	/*
    418  1.30.12.3  jdolecek 	 * hardclock_ticks is technically int, but nothing special
    419  1.30.12.3  jdolecek 	 * happens instead of overflow, so we assume two's-complement
    420  1.30.12.3  jdolecek 	 * wraparound and just treat it as unsigned.
    421  1.30.12.3  jdolecek 	 */
    422  1.30.12.3  jdolecek 	start = hardclock_ticks;
    423  1.30.12.3  jdolecek 	error = cv_timedwait(cv, mtx, bintime2timo(bt));
    424  1.30.12.3  jdolecek 	end = hardclock_ticks;
    425  1.30.12.3  jdolecek 
    426  1.30.12.3  jdolecek 	slept = timo2bintime(end - start);
    427  1.30.12.3  jdolecek 	/* bt := bt - slept */
    428  1.30.12.3  jdolecek 	bintime_sub(bt, &slept);
    429  1.30.12.3  jdolecek 
    430  1.30.12.3  jdolecek 	return error;
    431  1.30.12.3  jdolecek }
    432  1.30.12.3  jdolecek 
    433  1.30.12.3  jdolecek /*
    434  1.30.12.3  jdolecek  * cv_timedwaitbt_sig:
    435  1.30.12.3  jdolecek  *
    436  1.30.12.3  jdolecek  *	Wait on a condition variable until awoken, the specified
    437  1.30.12.3  jdolecek  *	timeout expires, or interrupted by a signal.  Returns zero if
    438  1.30.12.3  jdolecek  *	awoken normally, EWOULDBLOCK if the timeout expires, or
    439  1.30.12.3  jdolecek  *	EINTR/ERESTART if interrupted by a signal.
    440  1.30.12.3  jdolecek  *
    441  1.30.12.3  jdolecek  *	On entry, bt is a timeout in bintime.  cv_timedwaitbt_sig
    442  1.30.12.3  jdolecek  *	subtracts the time slept, so on exit, bt is the time remaining
    443  1.30.12.3  jdolecek  *	after sleeping.  No infinite timeout; use cv_wait instead.
    444  1.30.12.3  jdolecek  *
    445  1.30.12.3  jdolecek  *	epsilon is a requested maximum error in timeout (excluding
    446  1.30.12.3  jdolecek  *	spurious wakeups).  Currently not used, will be used in the
    447  1.30.12.3  jdolecek  *	future to choose between low- and high-resolution timers.
    448  1.30.12.3  jdolecek  */
    449  1.30.12.3  jdolecek int
    450  1.30.12.3  jdolecek cv_timedwaitbt_sig(kcondvar_t *cv, kmutex_t *mtx, struct bintime *bt,
    451  1.30.12.3  jdolecek     const struct bintime *epsilon __diagused)
    452  1.30.12.3  jdolecek {
    453  1.30.12.3  jdolecek 	struct bintime slept;
    454  1.30.12.3  jdolecek 	unsigned start, end;
    455  1.30.12.3  jdolecek 	int error;
    456  1.30.12.3  jdolecek 
    457  1.30.12.3  jdolecek 	KASSERTMSG(bt->sec >= 0, "negative timeout");
    458  1.30.12.3  jdolecek 	KASSERTMSG(epsilon != NULL, "specify maximum requested delay");
    459  1.30.12.3  jdolecek 
    460  1.30.12.3  jdolecek 	/*
    461  1.30.12.3  jdolecek 	 * hardclock_ticks is technically int, but nothing special
    462  1.30.12.3  jdolecek 	 * happens instead of overflow, so we assume two's-complement
    463  1.30.12.3  jdolecek 	 * wraparound and just treat it as unsigned.
    464  1.30.12.3  jdolecek 	 */
    465  1.30.12.3  jdolecek 	start = hardclock_ticks;
    466  1.30.12.3  jdolecek 	error = cv_timedwait_sig(cv, mtx, bintime2timo(bt));
    467  1.30.12.3  jdolecek 	end = hardclock_ticks;
    468  1.30.12.3  jdolecek 
    469  1.30.12.3  jdolecek 	slept = timo2bintime(end - start);
    470  1.30.12.3  jdolecek 	/* bt := bt - slept */
    471  1.30.12.3  jdolecek 	bintime_sub(bt, &slept);
    472  1.30.12.3  jdolecek 
    473  1.30.12.3  jdolecek 	return error;
    474  1.30.12.3  jdolecek }
    475  1.30.12.3  jdolecek 
    476  1.30.12.3  jdolecek /*
    477        1.2        ad  * cv_signal:
    478        1.2        ad  *
    479        1.2        ad  *	Wake the highest priority LWP waiting on a condition variable.
    480        1.2        ad  *	Must be called with the interlocking mutex held.
    481        1.2        ad  */
    482        1.2        ad void
    483        1.2        ad cv_signal(kcondvar_t *cv)
    484        1.2        ad {
    485       1.20        ad 
    486       1.22        ad 	/* LOCKDEBUG_WAKEUP(CV_DEBUG_P(cv), cv, CV_RA); */
    487       1.20        ad 	KASSERT(cv_is_valid(cv));
    488       1.20        ad 
    489       1.24        ad 	if (__predict_false(!TAILQ_EMPTY(CV_SLEEPQ(cv))))
    490       1.24        ad 		cv_wakeup_one(cv);
    491       1.20        ad }
    492       1.20        ad 
    493  1.30.12.3  jdolecek static inline void
    494       1.20        ad cv_wakeup_one(kcondvar_t *cv)
    495       1.20        ad {
    496        1.2        ad 	sleepq_t *sq;
    497       1.18        ad 	kmutex_t *mp;
    498       1.20        ad 	lwp_t *l;
    499        1.2        ad 
    500       1.15        ad 	KASSERT(cv_is_valid(cv));
    501       1.15        ad 
    502       1.24        ad 	mp = sleepq_hashlock(cv);
    503       1.20        ad 	sq = CV_SLEEPQ(cv);
    504       1.20        ad 	l = TAILQ_FIRST(sq);
    505       1.20        ad 	if (l == NULL) {
    506       1.20        ad 		mutex_spin_exit(mp);
    507        1.2        ad 		return;
    508       1.20        ad 	}
    509       1.20        ad 	KASSERT(l->l_sleepq == sq);
    510       1.20        ad 	KASSERT(l->l_mutex == mp);
    511       1.20        ad 	KASSERT(l->l_wchan == cv);
    512  1.30.12.3  jdolecek 	CV_LOCKDEBUG_PROCESS(l, cv);
    513       1.27     rmind 	sleepq_remove(sq, l);
    514       1.20        ad 	mutex_spin_exit(mp);
    515        1.2        ad 
    516       1.15        ad 	KASSERT(cv_is_valid(cv));
    517        1.2        ad }
    518        1.2        ad 
    519        1.2        ad /*
    520        1.2        ad  * cv_broadcast:
    521        1.2        ad  *
    522        1.2        ad  *	Wake all LWPs waiting on a condition variable.  Must be called
    523        1.2        ad  *	with the interlocking mutex held.
    524        1.2        ad  */
    525        1.2        ad void
    526        1.2        ad cv_broadcast(kcondvar_t *cv)
    527        1.2        ad {
    528       1.20        ad 
    529       1.22        ad 	/* LOCKDEBUG_WAKEUP(CV_DEBUG_P(cv), cv, CV_RA); */
    530       1.20        ad 	KASSERT(cv_is_valid(cv));
    531       1.20        ad 
    532       1.24        ad 	if (__predict_false(!TAILQ_EMPTY(CV_SLEEPQ(cv))))
    533       1.24        ad 		cv_wakeup_all(cv);
    534       1.20        ad }
    535       1.20        ad 
    536  1.30.12.3  jdolecek static inline void
    537       1.20        ad cv_wakeup_all(kcondvar_t *cv)
    538       1.20        ad {
    539        1.2        ad 	sleepq_t *sq;
    540       1.18        ad 	kmutex_t *mp;
    541       1.20        ad 	lwp_t *l, *next;
    542        1.2        ad 
    543       1.15        ad 	KASSERT(cv_is_valid(cv));
    544       1.15        ad 
    545       1.24        ad 	mp = sleepq_hashlock(cv);
    546       1.20        ad 	sq = CV_SLEEPQ(cv);
    547       1.20        ad 	for (l = TAILQ_FIRST(sq); l != NULL; l = next) {
    548       1.20        ad 		KASSERT(l->l_sleepq == sq);
    549       1.20        ad 		KASSERT(l->l_mutex == mp);
    550       1.20        ad 		KASSERT(l->l_wchan == cv);
    551       1.20        ad 		next = TAILQ_NEXT(l, l_sleepchain);
    552  1.30.12.3  jdolecek 		CV_LOCKDEBUG_PROCESS(l, cv);
    553       1.27     rmind 		sleepq_remove(sq, l);
    554       1.20        ad 	}
    555       1.20        ad 	mutex_spin_exit(mp);
    556        1.2        ad 
    557       1.15        ad 	KASSERT(cv_is_valid(cv));
    558        1.2        ad }
    559        1.2        ad 
    560        1.2        ad /*
    561        1.2        ad  * cv_has_waiters:
    562        1.2        ad  *
    563        1.2        ad  *	For diagnostic assertions: return non-zero if a condition
    564        1.2        ad  *	variable has waiters.
    565        1.2        ad  */
    566        1.7        ad bool
    567        1.2        ad cv_has_waiters(kcondvar_t *cv)
    568        1.2        ad {
    569       1.23     chris 
    570       1.25        ad 	return !TAILQ_EMPTY(CV_SLEEPQ(cv));
    571        1.2        ad }
    572       1.15        ad 
    573       1.15        ad /*
    574       1.15        ad  * cv_is_valid:
    575       1.15        ad  *
    576       1.15        ad  *	For diagnostic assertions: return non-zero if a condition
    577       1.15        ad  *	variable appears to be valid.  No locks need be held.
    578       1.15        ad  */
    579       1.15        ad bool
    580       1.15        ad cv_is_valid(kcondvar_t *cv)
    581       1.15        ad {
    582       1.15        ad 
    583       1.26   thorpej 	return CV_WMESG(cv) != deadcv && CV_WMESG(cv) != NULL;
    584       1.15        ad }
    585