Home | History | Annotate | Download | only in os
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 /*
     23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 #include <sys/thread.h>
     28 #include <sys/proc.h>
     29 #include <sys/debug.h>
     30 #include <sys/cmn_err.h>
     31 #include <sys/systm.h>
     32 #include <sys/sobject.h>
     33 #include <sys/sleepq.h>
     34 #include <sys/cpuvar.h>
     35 #include <sys/condvar.h>
     36 #include <sys/condvar_impl.h>
     37 #include <sys/schedctl.h>
     38 #include <sys/procfs.h>
     39 #include <sys/sdt.h>
     40 #include <sys/callo.h>
     41 
     42 clock_t cv_timedwait_hires(kcondvar_t *, kmutex_t *, hrtime_t, hrtime_t, int);
     43 
     44 /*
     45  * CV_MAX_WAITERS is the maximum number of waiters we track; once
     46  * the number becomes higher than that, we look at the sleepq to
     47  * see whether there are *really* any waiters.
     48  */
     49 #define	CV_MAX_WAITERS		1024		/* must be power of 2 */
     50 #define	CV_WAITERS_MASK		(CV_MAX_WAITERS - 1)
     51 
     52 /*
     53  * Threads don't "own" condition variables.
     54  */
     55 /* ARGSUSED */
     56 static kthread_t *
     57 cv_owner(void *cvp)
     58 {
     59 	return (NULL);
     60 }
     61 
     62 /*
     63  * Unsleep a thread that's blocked on a condition variable.
     64  */
     65 static void
     66 cv_unsleep(kthread_t *t)
     67 {
     68 	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
     69 	sleepq_head_t *sqh = SQHASH(cvp);
     70 
     71 	ASSERT(THREAD_LOCK_HELD(t));
     72 
     73 	if (cvp == NULL)
     74 		panic("cv_unsleep: thread %p not on sleepq %p",
     75 		    (void *)t, (void *)sqh);
     76 	DTRACE_SCHED1(wakeup, kthread_t *, t);
     77 	sleepq_unsleep(t);
     78 	if (cvp->cv_waiters != CV_MAX_WAITERS)
     79 		cvp->cv_waiters--;
     80 	disp_lock_exit_high(&sqh->sq_lock);
     81 	CL_SETRUN(t);
     82 }
     83 
     84 /*
     85  * Change the priority of a thread that's blocked on a condition variable.
     86  */
     87 static void
     88 cv_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip)
     89 {
     90 	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
     91 	sleepq_t *sqp = t->t_sleepq;
     92 
     93 	ASSERT(THREAD_LOCK_HELD(t));
     94 	ASSERT(&SQHASH(cvp)->sq_queue == sqp);
     95 
     96 	if (cvp == NULL)
     97 		panic("cv_change_pri: %p not on sleep queue", (void *)t);
     98 	sleepq_dequeue(t);
     99 	*t_prip = pri;
    100 	sleepq_insert(sqp, t);
    101 }
    102 
    103 /*
    104  * The sobj_ops vector exports a set of functions needed when a thread
    105  * is asleep on a synchronization object of this type.
    106  */
    107 static sobj_ops_t cv_sobj_ops = {
    108 	SOBJ_CV, cv_owner, cv_unsleep, cv_change_pri
    109 };
    110 
    111 /* ARGSUSED */
    112 void
    113 cv_init(kcondvar_t *cvp, char *name, kcv_type_t type, void *arg)
    114 {
    115 	((condvar_impl_t *)cvp)->cv_waiters = 0;
    116 }
    117 
    118 /*
    119  * cv_destroy is not currently needed, but is part of the DDI.
    120  * This is in case cv_init ever needs to allocate something for a cv.
    121  */
    122 /* ARGSUSED */
    123 void
    124 cv_destroy(kcondvar_t *cvp)
    125 {
    126 	ASSERT((((condvar_impl_t *)cvp)->cv_waiters & CV_WAITERS_MASK) == 0);
    127 }
    128 
    129 /*
    130  * The cv_block() function blocks a thread on a condition variable
    131  * by putting it in a hashed sleep queue associated with the
    132  * synchronization object.
    133  *
    134  * Threads are taken off the hashed sleep queues via calls to
    135  * cv_signal(), cv_broadcast(), or cv_unsleep().
    136  */
    137 static void
    138 cv_block(condvar_impl_t *cvp)
    139 {
    140 	kthread_t *t = curthread;
    141 	klwp_t *lwp = ttolwp(t);
    142 	sleepq_head_t *sqh;
    143 
    144 	ASSERT(THREAD_LOCK_HELD(t));
    145 	ASSERT(t != CPU->cpu_idle_thread);
    146 	ASSERT(CPU_ON_INTR(CPU) == 0);
    147 	ASSERT(t->t_wchan0 == NULL && t->t_wchan == NULL);
    148 	ASSERT(t->t_state == TS_ONPROC);
    149 
    150 	t->t_schedflag &= ~TS_SIGNALLED;
    151 	CL_SLEEP(t);			/* assign kernel priority */
    152 	t->t_wchan = (caddr_t)cvp;
    153 	t->t_sobj_ops = &cv_sobj_ops;
    154 	DTRACE_SCHED(sleep);
    155 
    156 	/*
    157 	 * The check for t_intr is to avoid doing the
    158 	 * account for an interrupt thread on the still-pinned
    159 	 * lwp's statistics.
    160 	 */
    161 	if (lwp != NULL && t->t_intr == NULL) {
    162 		lwp->lwp_ru.nvcsw++;
    163 		(void) new_mstate(t, LMS_SLEEP);
    164 	}
    165 
    166 	sqh = SQHASH(cvp);
    167 	disp_lock_enter_high(&sqh->sq_lock);
    168 	if (cvp->cv_waiters < CV_MAX_WAITERS)
    169 		cvp->cv_waiters++;
    170 	ASSERT(cvp->cv_waiters <= CV_MAX_WAITERS);
    171 	THREAD_SLEEP(t, &sqh->sq_lock);
    172 	sleepq_insert(&sqh->sq_queue, t);
    173 	/*
    174 	 * THREAD_SLEEP() moves curthread->t_lockp to point to the
    175 	 * lock sqh->sq_lock. This lock is later released by the caller
    176 	 * when it calls thread_unlock() on curthread.
    177 	 */
    178 }
    179 
    180 #define	cv_block_sig(t, cvp)	\
    181 	{ (t)->t_flag |= T_WAKEABLE; cv_block(cvp); }
    182 
    183 /*
    184  * Block on the indicated condition variable and release the
    185  * associated kmutex while blocked.
    186  */
    187 void
    188 cv_wait(kcondvar_t *cvp, kmutex_t *mp)
    189 {
    190 	if (panicstr)
    191 		return;
    192 	ASSERT(!quiesce_active);
    193 
    194 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
    195 	thread_lock(curthread);			/* lock the thread */
    196 	cv_block((condvar_impl_t *)cvp);
    197 	thread_unlock_nopreempt(curthread);	/* unlock the waiters field */
    198 	mutex_exit(mp);
    199 	swtch();
    200 	mutex_enter(mp);
    201 }
    202 
    203 static void
    204 cv_wakeup(void *arg)
    205 {
    206 	kthread_t *t = arg;
    207 
    208 	/*
    209 	 * This mutex is acquired and released in order to make sure that
    210 	 * the wakeup does not happen before the block itself happens.
    211 	 */
    212 	mutex_enter(&t->t_wait_mutex);
    213 	mutex_exit(&t->t_wait_mutex);
    214 	setrun(t);
    215 }
    216 
    217 /*
    218  * Same as cv_wait except the thread will unblock at 'tim'
    219  * (an absolute time) if it hasn't already unblocked.
    220  *
    221  * Returns the amount of time left from the original 'tim' value
    222  * when it was unblocked.
    223  */
    224 clock_t
    225 cv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
    226 {
    227 	hrtime_t hrtim;
    228 	clock_t now = ddi_get_lbolt();
    229 
    230 	if (tim <= now)
    231 		return (-1);
    232 
    233 	hrtim = TICK_TO_NSEC(tim - now);
    234 	return (cv_timedwait_hires(cvp, mp, hrtim, nsec_per_tick, 0));
    235 }
    236 
    237 /*
    238  * Same as cv_timedwait() except that the third argument is a relative
    239  * timeout value, as opposed to an absolute one. There is also a fourth
    240  * argument that specifies how accurately the timeout must be implemented.
    241  */
    242 clock_t
    243 cv_reltimedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t delta, time_res_t res)
    244 {
    245 	hrtime_t exp;
    246 
    247 	ASSERT(TIME_RES_VALID(res));
    248 
    249 	if (delta <= 0)
    250 		return (-1);
    251 
    252 	if ((exp = TICK_TO_NSEC(delta)) < 0)
    253 		exp = CY_INFINITY;
    254 
    255 	return (cv_timedwait_hires(cvp, mp, exp, time_res[res], 0));
    256 }
    257 
    258 clock_t
    259 cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
    260     hrtime_t res, int flag)
    261 {
    262 	kthread_t *t = curthread;
    263 	callout_id_t id;
    264 	clock_t timeleft;
    265 	hrtime_t limit;
    266 	int signalled;
    267 
    268 	if (panicstr)
    269 		return (-1);
    270 	ASSERT(!quiesce_active);
    271 
    272 	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
    273 	if (tim <= limit)
    274 		return (-1);
    275 	mutex_enter(&t->t_wait_mutex);
    276 	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
    277 	    tim, res, flag);
    278 	thread_lock(t);		/* lock the thread */
    279 	cv_block((condvar_impl_t *)cvp);
    280 	thread_unlock_nopreempt(t);
    281 	mutex_exit(&t->t_wait_mutex);
    282 	mutex_exit(mp);
    283 	swtch();
    284 	signalled = (t->t_schedflag & TS_SIGNALLED);
    285 	/*
    286 	 * Get the time left. untimeout() returns -1 if the timeout has
    287 	 * occured or the time remaining.  If the time remaining is zero,
    288 	 * the timeout has occured between when we were awoken and
    289 	 * we called untimeout.  We will treat this as if the timeout
    290 	 * has occured and set timeleft to -1.
    291 	 */
    292 	timeleft = untimeout_default(id, 0);
    293 	mutex_enter(mp);
    294 	if (timeleft <= 0) {
    295 		timeleft = -1;
    296 		if (signalled)	/* avoid consuming the cv_signal() */
    297 			cv_signal(cvp);
    298 	}
    299 	return (timeleft);
    300 }
    301 
    302 int
    303 cv_wait_sig(kcondvar_t *cvp, kmutex_t *mp)
    304 {
    305 	kthread_t *t = curthread;
    306 	proc_t *p = ttoproc(t);
    307 	klwp_t *lwp = ttolwp(t);
    308 	int cancel_pending;
    309 	int rval = 1;
    310 	int signalled = 0;
    311 
    312 	if (panicstr)
    313 		return (rval);
    314 	ASSERT(!quiesce_active);
    315 
    316 	/*
    317 	 * Threads in system processes don't process signals.  This is
    318 	 * true both for standard threads of system processes and for
    319 	 * interrupt threads which have borrowed their pinned thread's LWP.
    320 	 */
    321 	if (lwp == NULL || (p->p_flag & SSYS)) {
    322 		cv_wait(cvp, mp);
    323 		return (rval);
    324 	}
    325 	ASSERT(t->t_intr == NULL);
    326 
    327 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
    328 	cancel_pending = schedctl_cancel_pending();
    329 	lwp->lwp_asleep = 1;
    330 	lwp->lwp_sysabort = 0;
    331 	thread_lock(t);
    332 	cv_block_sig(t, (condvar_impl_t *)cvp);
    333 	thread_unlock_nopreempt(t);
    334 	mutex_exit(mp);
    335 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
    336 		setrun(t);
    337 	/* ASSERT(no locks are held) */
    338 	swtch();
    339 	signalled = (t->t_schedflag & TS_SIGNALLED);
    340 	t->t_flag &= ~T_WAKEABLE;
    341 	mutex_enter(mp);
    342 	if (ISSIG_PENDING(t, lwp, p)) {
    343 		mutex_exit(mp);
    344 		if (issig(FORREAL))
    345 			rval = 0;
    346 		mutex_enter(mp);
    347 	}
    348 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
    349 		rval = 0;
    350 	if (rval != 0 && cancel_pending) {
    351 		schedctl_cancel_eintr();
    352 		rval = 0;
    353 	}
    354 	lwp->lwp_asleep = 0;
    355 	lwp->lwp_sysabort = 0;
    356 	if (rval == 0 && signalled)	/* avoid consuming the cv_signal() */
    357 		cv_signal(cvp);
    358 	return (rval);
    359 }
    360 
    361 static clock_t
    362 cv_timedwait_sig_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
    363     hrtime_t res, int flag)
    364 {
    365 	kthread_t *t = curthread;
    366 	proc_t *p = ttoproc(t);
    367 	klwp_t *lwp = ttolwp(t);
    368 	int cancel_pending = 0;
    369 	callout_id_t id;
    370 	clock_t rval = 1;
    371 	hrtime_t limit;
    372 	int signalled = 0;
    373 
    374 	if (panicstr)
    375 		return (rval);
    376 	ASSERT(!quiesce_active);
    377 
    378 	/*
    379 	 * Threads in system processes don't process signals.  This is
    380 	 * true both for standard threads of system processes and for
    381 	 * interrupt threads which have borrowed their pinned thread's LWP.
    382 	 */
    383 	if (lwp == NULL || (p->p_flag & SSYS))
    384 		return (cv_timedwait_hires(cvp, mp, tim, res, flag));
    385 	ASSERT(t->t_intr == NULL);
    386 
    387 	/*
    388 	 * If tim is less than or equal to current hrtime, then the timeout
    389 	 * has already occured.  So just check to see if there is a signal
    390 	 * pending.  If so return 0 indicating that there is a signal pending.
    391 	 * Else return -1 indicating that the timeout occured. No need to
    392 	 * wait on anything.
    393 	 */
    394 	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
    395 	if (tim <= limit) {
    396 		lwp->lwp_asleep = 1;
    397 		lwp->lwp_sysabort = 0;
    398 		rval = -1;
    399 		goto out;
    400 	}
    401 
    402 	/*
    403 	 * Set the timeout and wait.
    404 	 */
    405 	cancel_pending = schedctl_cancel_pending();
    406 	mutex_enter(&t->t_wait_mutex);
    407 	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
    408 	    tim, res, flag);
    409 	lwp->lwp_asleep = 1;
    410 	lwp->lwp_sysabort = 0;
    411 	thread_lock(t);
    412 	cv_block_sig(t, (condvar_impl_t *)cvp);
    413 	thread_unlock_nopreempt(t);
    414 	mutex_exit(&t->t_wait_mutex);
    415 	mutex_exit(mp);
    416 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
    417 		setrun(t);
    418 	/* ASSERT(no locks are held) */
    419 	swtch();
    420 	signalled = (t->t_schedflag & TS_SIGNALLED);
    421 	t->t_flag &= ~T_WAKEABLE;
    422 
    423 	/*
    424 	 * Untimeout the thread.  untimeout() returns -1 if the timeout has
    425 	 * occured or the time remaining.  If the time remaining is zero,
    426 	 * the timeout has occured between when we were awoken and
    427 	 * we called untimeout.  We will treat this as if the timeout
    428 	 * has occured and set rval to -1.
    429 	 */
    430 	rval = untimeout_default(id, 0);
    431 	mutex_enter(mp);
    432 	if (rval <= 0)
    433 		rval = -1;
    434 
    435 	/*
    436 	 * Check to see if a signal is pending.  If so, regardless of whether
    437 	 * or not we were awoken due to the signal, the signal is now pending
    438 	 * and a return of 0 has the highest priority.
    439 	 */
    440 out:
    441 	if (ISSIG_PENDING(t, lwp, p)) {
    442 		mutex_exit(mp);
    443 		if (issig(FORREAL))
    444 			rval = 0;
    445 		mutex_enter(mp);
    446 	}
    447 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
    448 		rval = 0;
    449 	if (rval != 0 && cancel_pending) {
    450 		schedctl_cancel_eintr();
    451 		rval = 0;
    452 	}
    453 	lwp->lwp_asleep = 0;
    454 	lwp->lwp_sysabort = 0;
    455 	if (rval <= 0 && signalled)	/* avoid consuming the cv_signal() */
    456 		cv_signal(cvp);
    457 	return (rval);
    458 }
    459 
    460 /*
    461  * Returns:
    462  * 	Function result in order of precedence:
    463  *		 0 if a signal was received
    464  *		-1 if timeout occured
    465  *		>0 if awakened via cv_signal() or cv_broadcast().
    466  *		   (returns time remaining)
    467  *
    468  * cv_timedwait_sig() is now part of the DDI.
    469  *
    470  * This function is now just a wrapper for cv_timedwait_sig_hires().
    471  */
    472 clock_t
    473 cv_timedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
    474 {
    475 	hrtime_t hrtim;
    476 
    477 	hrtim = TICK_TO_NSEC(tim - ddi_get_lbolt());
    478 	return (cv_timedwait_sig_hires(cvp, mp, hrtim, nsec_per_tick, 0));
    479 }
    480 
    481 /*
    482  * Same as cv_timedwait_sig() except that the third argument is a relative
    483  * timeout value, as opposed to an absolute one. There is also a fourth
    484  * argument that specifies how accurately the timeout must be implemented.
    485  */
    486 clock_t
    487 cv_reltimedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t delta,
    488     time_res_t res)
    489 {
    490 	hrtime_t exp;
    491 
    492 	ASSERT(TIME_RES_VALID(res));
    493 
    494 	if ((exp = TICK_TO_NSEC(delta)) < 0)
    495 		exp = CY_INFINITY;
    496 
    497 	return (cv_timedwait_sig_hires(cvp, mp, exp, time_res[res], 0));
    498 }
    499 
    500 /*
    501  * Like cv_wait_sig_swap but allows the caller to indicate (with a
    502  * non-NULL sigret) that they will take care of signalling the cv
    503  * after wakeup, if necessary.  This is a vile hack that should only
    504  * be used when no other option is available; almost all callers
    505  * should just use cv_wait_sig_swap (which takes care of the cv_signal
    506  * stuff automatically) instead.
    507  */
    508 int
    509 cv_wait_sig_swap_core(kcondvar_t *cvp, kmutex_t *mp, int *sigret)
    510 {
    511 	kthread_t *t = curthread;
    512 	proc_t *p = ttoproc(t);
    513 	klwp_t *lwp = ttolwp(t);
    514 	int cancel_pending;
    515 	int rval = 1;
    516 	int signalled = 0;
    517 
    518 	if (panicstr)
    519 		return (rval);
    520 
    521 	/*
    522 	 * Threads in system processes don't process signals.  This is
    523 	 * true both for standard threads of system processes and for
    524 	 * interrupt threads which have borrowed their pinned thread's LWP.
    525 	 */
    526 	if (lwp == NULL || (p->p_flag & SSYS)) {
    527 		cv_wait(cvp, mp);
    528 		return (rval);
    529 	}
    530 	ASSERT(t->t_intr == NULL);
    531 
    532 	cancel_pending = schedctl_cancel_pending();
    533 	lwp->lwp_asleep = 1;
    534 	lwp->lwp_sysabort = 0;
    535 	thread_lock(t);
    536 	t->t_kpri_req = 0;	/* don't need kernel priority */
    537 	cv_block_sig(t, (condvar_impl_t *)cvp);
    538 	/* I can be swapped now */
    539 	curthread->t_schedflag &= ~TS_DONT_SWAP;
    540 	thread_unlock_nopreempt(t);
    541 	mutex_exit(mp);
    542 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
    543 		setrun(t);
    544 	/* ASSERT(no locks are held) */
    545 	swtch();
    546 	signalled = (t->t_schedflag & TS_SIGNALLED);
    547 	t->t_flag &= ~T_WAKEABLE;
    548 	/* TS_DONT_SWAP set by disp() */
    549 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
    550 	mutex_enter(mp);
    551 	if (ISSIG_PENDING(t, lwp, p)) {
    552 		mutex_exit(mp);
    553 		if (issig(FORREAL))
    554 			rval = 0;
    555 		mutex_enter(mp);
    556 	}
    557 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
    558 		rval = 0;
    559 	if (rval != 0 && cancel_pending) {
    560 		schedctl_cancel_eintr();
    561 		rval = 0;
    562 	}
    563 	lwp->lwp_asleep = 0;
    564 	lwp->lwp_sysabort = 0;
    565 	if (rval == 0) {
    566 		if (sigret != NULL)
    567 			*sigret = signalled;	/* just tell the caller */
    568 		else if (signalled)
    569 			cv_signal(cvp);	/* avoid consuming the cv_signal() */
    570 	}
    571 	return (rval);
    572 }
    573 
    574 /*
    575  * Same as cv_wait_sig but the thread can be swapped out while waiting.
    576  * This should only be used when we know we aren't holding any locks.
    577  */
    578 int
    579 cv_wait_sig_swap(kcondvar_t *cvp, kmutex_t *mp)
    580 {
    581 	return (cv_wait_sig_swap_core(cvp, mp, NULL));
    582 }
    583 
    584 void
    585 cv_signal(kcondvar_t *cvp)
    586 {
    587 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
    588 
    589 	/* make sure the cv_waiters field looks sane */
    590 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
    591 	if (cp->cv_waiters > 0) {
    592 		sleepq_head_t *sqh = SQHASH(cp);
    593 		disp_lock_enter(&sqh->sq_lock);
    594 		ASSERT(CPU_ON_INTR(CPU) == 0);
    595 		if (cp->cv_waiters & CV_WAITERS_MASK) {
    596 			kthread_t *t;
    597 			cp->cv_waiters--;
    598 			t = sleepq_wakeone_chan(&sqh->sq_queue, cp);
    599 			/*
    600 			 * If cv_waiters is non-zero (and less than
    601 			 * CV_MAX_WAITERS) there should be a thread
    602 			 * in the queue.
    603 			 */
    604 			ASSERT(t != NULL);
    605 		} else if (sleepq_wakeone_chan(&sqh->sq_queue, cp) == NULL) {
    606 			cp->cv_waiters = 0;
    607 		}
    608 		disp_lock_exit(&sqh->sq_lock);
    609 	}
    610 }
    611 
    612 void
    613 cv_broadcast(kcondvar_t *cvp)
    614 {
    615 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
    616 
    617 	/* make sure the cv_waiters field looks sane */
    618 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
    619 	if (cp->cv_waiters > 0) {
    620 		sleepq_head_t *sqh = SQHASH(cp);
    621 		disp_lock_enter(&sqh->sq_lock);
    622 		ASSERT(CPU_ON_INTR(CPU) == 0);
    623 		sleepq_wakeall_chan(&sqh->sq_queue, cp);
    624 		cp->cv_waiters = 0;
    625 		disp_lock_exit(&sqh->sq_lock);
    626 	}
    627 }
    628 
    629 /*
    630  * Same as cv_wait(), but wakes up (after wakeup_time milliseconds) to check
    631  * for requests to stop, like cv_wait_sig() but without dealing with signals.
    632  * This is a horrible kludge.  It is evil.  It is vile.  It is swill.
    633  * If your code has to call this function then your code is the same.
    634  */
    635 void
    636 cv_wait_stop(kcondvar_t *cvp, kmutex_t *mp, int wakeup_time)
    637 {
    638 	kthread_t *t = curthread;
    639 	klwp_t *lwp = ttolwp(t);
    640 	proc_t *p = ttoproc(t);
    641 	callout_id_t id;
    642 	clock_t tim;
    643 
    644 	if (panicstr)
    645 		return;
    646 
    647 	/*
    648 	 * Threads in system processes don't process signals.  This is
    649 	 * true both for standard threads of system processes and for
    650 	 * interrupt threads which have borrowed their pinned thread's LWP.
    651 	 */
    652 	if (lwp == NULL || (p->p_flag & SSYS)) {
    653 		cv_wait(cvp, mp);
    654 		return;
    655 	}
    656 	ASSERT(t->t_intr == NULL);
    657 
    658 	/*
    659 	 * Wakeup in wakeup_time milliseconds, i.e., human time.
    660 	 */
    661 	tim = ddi_get_lbolt() + MSEC_TO_TICK(wakeup_time);
    662 	mutex_enter(&t->t_wait_mutex);
    663 	id = realtime_timeout_default((void (*)(void *))cv_wakeup, t,
    664 	    tim - ddi_get_lbolt());
    665 	thread_lock(t);			/* lock the thread */
    666 	cv_block((condvar_impl_t *)cvp);
    667 	thread_unlock_nopreempt(t);
    668 	mutex_exit(&t->t_wait_mutex);
    669 	mutex_exit(mp);
    670 	/* ASSERT(no locks are held); */
    671 	swtch();
    672 	(void) untimeout_default(id, 0);
    673 
    674 	/*
    675 	 * Check for reasons to stop, if lwp_nostop is not true.
    676 	 * See issig_forreal() for explanations of the various stops.
    677 	 */
    678 	mutex_enter(&p->p_lock);
    679 	while (lwp->lwp_nostop == 0 && !(p->p_flag & SEXITLWPS)) {
    680 		/*
    681 		 * Hold the lwp here for watchpoint manipulation.
    682 		 */
    683 		if (t->t_proc_flag & TP_PAUSE) {
    684 			stop(PR_SUSPENDED, SUSPEND_PAUSE);
    685 			continue;
    686 		}
    687 		/*
    688 		 * System checkpoint.
    689 		 */
    690 		if (t->t_proc_flag & TP_CHKPT) {
    691 			stop(PR_CHECKPOINT, 0);
    692 			continue;
    693 		}
    694 		/*
    695 		 * Honor fork1(), watchpoint activity (remapping a page),
    696 		 * and lwp_suspend() requests.
    697 		 */
    698 		if ((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
    699 		    (t->t_proc_flag & TP_HOLDLWP)) {
    700 			stop(PR_SUSPENDED, SUSPEND_NORMAL);
    701 			continue;
    702 		}
    703 		/*
    704 		 * Honor /proc requested stop.
    705 		 */
    706 		if (t->t_proc_flag & TP_PRSTOP) {
    707 			stop(PR_REQUESTED, 0);
    708 		}
    709 		/*
    710 		 * If some lwp in the process has already stopped
    711 		 * showing PR_JOBCONTROL, stop in sympathy with it.
    712 		 */
    713 		if (p->p_stopsig && t != p->p_agenttp) {
    714 			stop(PR_JOBCONTROL, p->p_stopsig);
    715 			continue;
    716 		}
    717 		break;
    718 	}
    719 	mutex_exit(&p->p_lock);
    720 	mutex_enter(mp);
    721 }
    722 
    723 /*
    724  * Like cv_timedwait_sig(), but takes an absolute hires future time
    725  * rather than a future time in clock ticks.  Will not return showing
    726  * that a timeout occurred until the future time is passed.
    727  * If 'when' is a NULL pointer, no timeout will occur.
    728  * Returns:
    729  * 	Function result in order of precedence:
    730  *		 0 if a signal was received
    731  *		-1 if timeout occured
    732  *	        >0 if awakened via cv_signal() or cv_broadcast()
    733  *		   or by a spurious wakeup.
    734  *		   (might return time remaining)
    735  * As a special test, if someone abruptly resets the system time
    736  * (but not through adjtime(2); drifting of the clock is allowed and
    737  * expected [see timespectohz_adj()]), then we force a return of -1
    738  * so the caller can return a premature timeout to the calling process
    739  * so it can reevaluate the situation in light of the new system time.
    740  * (The system clock has been reset if timecheck != timechanged.)
    741  */
    742 int
    743 cv_waituntil_sig(kcondvar_t *cvp, kmutex_t *mp,
    744 	timestruc_t *when, int timecheck)
    745 {
    746 	timestruc_t now;
    747 	timestruc_t delta;
    748 	hrtime_t interval;
    749 	int rval;
    750 
    751 	if (when == NULL)
    752 		return (cv_wait_sig_swap(cvp, mp));
    753 
    754 	gethrestime(&now);
    755 	delta = *when;
    756 	timespecsub(&delta, &now);
    757 	if (delta.tv_sec < 0 || (delta.tv_sec == 0 && delta.tv_nsec == 0)) {
    758 		/*
    759 		 * We have already reached the absolute future time.
    760 		 * Call cv_timedwait_sig() just to check for signals.
    761 		 * We will return immediately with either 0 or -1.
    762 		 */
    763 		rval = cv_timedwait_sig_hires(cvp, mp, 0, 1, 0);
    764 	} else {
    765 		if (timecheck == timechanged) {
    766 			/*
    767 			 * Make sure that the interval is atleast one tick.
    768 			 * This is to prevent a user from flooding the system
    769 			 * with very small, high resolution timers.
    770 			 */
    771 			interval = ts2hrt(&delta);
    772 			if (interval < nsec_per_tick)
    773 				interval = nsec_per_tick;
    774 			rval = cv_timedwait_sig_hires(cvp, mp, interval, 1,
    775 			    CALLOUT_FLAG_HRESTIME);
    776 		} else {
    777 			/*
    778 			 * Someone reset the system time;
    779 			 * just force an immediate timeout.
    780 			 */
    781 			rval = -1;
    782 		}
    783 		if (rval == -1 && timecheck == timechanged) {
    784 			/*
    785 			 * Even though cv_timedwait_sig() returned showing a
    786 			 * timeout, the future time may not have passed yet.
    787 			 * If not, change rval to indicate a normal wakeup.
    788 			 */
    789 			gethrestime(&now);
    790 			delta = *when;
    791 			timespecsub(&delta, &now);
    792 			if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
    793 			    delta.tv_nsec > 0))
    794 				rval = 1;
    795 		}
    796 	}
    797 	return (rval);
    798 }
    799