xref: /illumos-gate/usr/src/uts/common/os/condvar.c (revision 6a0b1217)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52db6c79fSstevel  * Common Development and Distribution License (the "License").
62db6c79fSstevel  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21a574db85Sraf 
227c478bd9Sstevel@tonic-gate /*
231c4c388cSRafael Vanoni  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27cd1c8b85SMatthew Ahrens /*
28cd1c8b85SMatthew Ahrens  * Copyright (c) 2012 by Delphix. All rights reserved.
29*6a0b1217SPatrick Mooney  * Copyright 2019 Joyent, Inc.
30cd1c8b85SMatthew Ahrens  */
31fe234e7cSMatt Amdur 
327c478bd9Sstevel@tonic-gate #include <sys/thread.h>
337c478bd9Sstevel@tonic-gate #include <sys/proc.h>
347c478bd9Sstevel@tonic-gate #include <sys/debug.h>
357c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/sobject.h>
387c478bd9Sstevel@tonic-gate #include <sys/sleepq.h>
397c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
407c478bd9Sstevel@tonic-gate #include <sys/condvar.h>
417c478bd9Sstevel@tonic-gate #include <sys/condvar_impl.h>
427c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
437c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
447c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
4587a18d3fSMadhavan Venkataraman #include <sys/callo.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * CV_MAX_WAITERS is the maximum number of waiters we track; once
497c478bd9Sstevel@tonic-gate  * the number becomes higher than that, we look at the sleepq to
507c478bd9Sstevel@tonic-gate  * see whether there are *really* any waiters.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate #define	CV_MAX_WAITERS		1024		/* must be power of 2 */
537c478bd9Sstevel@tonic-gate #define	CV_WAITERS_MASK		(CV_MAX_WAITERS - 1)
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Threads don't "own" condition variables.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate /* ARGSUSED */
597c478bd9Sstevel@tonic-gate static kthread_t *
cv_owner(void * cvp)607c478bd9Sstevel@tonic-gate cv_owner(void *cvp)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	return (NULL);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Unsleep a thread that's blocked on a condition variable.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate static void
cv_unsleep(kthread_t * t)697c478bd9Sstevel@tonic-gate cv_unsleep(kthread_t *t)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
727c478bd9Sstevel@tonic-gate 	sleepq_head_t *sqh = SQHASH(cvp);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (cvp == NULL)
77903a11ebSrh 		panic("cv_unsleep: thread %p not on sleepq %p",
78903a11ebSrh 		    (void *)t, (void *)sqh);
792db6c79fSstevel 	DTRACE_SCHED1(wakeup, kthread_t *, t);
807c478bd9Sstevel@tonic-gate 	sleepq_unsleep(t);
817c478bd9Sstevel@tonic-gate 	if (cvp->cv_waiters != CV_MAX_WAITERS)
827c478bd9Sstevel@tonic-gate 		cvp->cv_waiters--;
837c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&sqh->sq_lock);
847c478bd9Sstevel@tonic-gate 	CL_SETRUN(t);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Change the priority of a thread that's blocked on a condition variable.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate static void
cv_change_pri(kthread_t * t,pri_t pri,pri_t * t_prip)917c478bd9Sstevel@tonic-gate cv_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
947c478bd9Sstevel@tonic-gate 	sleepq_t *sqp = t->t_sleepq;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
977c478bd9Sstevel@tonic-gate 	ASSERT(&SQHASH(cvp)->sq_queue == sqp);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (cvp == NULL)
100903a11ebSrh 		panic("cv_change_pri: %p not on sleep queue", (void *)t);
1017c478bd9Sstevel@tonic-gate 	sleepq_dequeue(t);
1027c478bd9Sstevel@tonic-gate 	*t_prip = pri;
1037c478bd9Sstevel@tonic-gate 	sleepq_insert(sqp, t);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * The sobj_ops vector exports a set of functions needed when a thread
1087c478bd9Sstevel@tonic-gate  * is asleep on a synchronization object of this type.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate static sobj_ops_t cv_sobj_ops = {
1117c478bd9Sstevel@tonic-gate 	SOBJ_CV, cv_owner, cv_unsleep, cv_change_pri
1127c478bd9Sstevel@tonic-gate };
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /* ARGSUSED */
1157c478bd9Sstevel@tonic-gate void
cv_init(kcondvar_t * cvp,char * name,kcv_type_t type,void * arg)1167c478bd9Sstevel@tonic-gate cv_init(kcondvar_t *cvp, char *name, kcv_type_t type, void *arg)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	((condvar_impl_t *)cvp)->cv_waiters = 0;
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * cv_destroy is not currently needed, but is part of the DDI.
1237c478bd9Sstevel@tonic-gate  * This is in case cv_init ever needs to allocate something for a cv.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate /* ARGSUSED */
1267c478bd9Sstevel@tonic-gate void
cv_destroy(kcondvar_t * cvp)1277c478bd9Sstevel@tonic-gate cv_destroy(kcondvar_t *cvp)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	ASSERT((((condvar_impl_t *)cvp)->cv_waiters & CV_WAITERS_MASK) == 0);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * The cv_block() function blocks a thread on a condition variable
1347c478bd9Sstevel@tonic-gate  * by putting it in a hashed sleep queue associated with the
1357c478bd9Sstevel@tonic-gate  * synchronization object.
1367c478bd9Sstevel@tonic-gate  *
1377c478bd9Sstevel@tonic-gate  * Threads are taken off the hashed sleep queues via calls to
1387c478bd9Sstevel@tonic-gate  * cv_signal(), cv_broadcast(), or cv_unsleep().
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate static void
cv_block(condvar_impl_t * cvp)1417c478bd9Sstevel@tonic-gate cv_block(condvar_impl_t *cvp)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
1447c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
1457c478bd9Sstevel@tonic-gate 	sleepq_head_t *sqh;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1487c478bd9Sstevel@tonic-gate 	ASSERT(t != CPU->cpu_idle_thread);
1497c478bd9Sstevel@tonic-gate 	ASSERT(CPU_ON_INTR(CPU) == 0);
1507c478bd9Sstevel@tonic-gate 	ASSERT(t->t_wchan0 == NULL && t->t_wchan == NULL);
1517c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	t->t_schedflag &= ~TS_SIGNALLED;
1547c478bd9Sstevel@tonic-gate 	CL_SLEEP(t);			/* assign kernel priority */
1557c478bd9Sstevel@tonic-gate 	t->t_wchan = (caddr_t)cvp;
1567c478bd9Sstevel@tonic-gate 	t->t_sobj_ops = &cv_sobj_ops;
1577c478bd9Sstevel@tonic-gate 	DTRACE_SCHED(sleep);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/*
1607c478bd9Sstevel@tonic-gate 	 * The check for t_intr is to avoid doing the
1617c478bd9Sstevel@tonic-gate 	 * account for an interrupt thread on the still-pinned
1627c478bd9Sstevel@tonic-gate 	 * lwp's statistics.
1637c478bd9Sstevel@tonic-gate 	 */
1647c478bd9Sstevel@tonic-gate 	if (lwp != NULL && t->t_intr == NULL) {
1657c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.nvcsw++;
1667c478bd9Sstevel@tonic-gate 		(void) new_mstate(t, LMS_SLEEP);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	sqh = SQHASH(cvp);
1707c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&sqh->sq_lock);
1717c478bd9Sstevel@tonic-gate 	if (cvp->cv_waiters < CV_MAX_WAITERS)
1727c478bd9Sstevel@tonic-gate 		cvp->cv_waiters++;
1737c478bd9Sstevel@tonic-gate 	ASSERT(cvp->cv_waiters <= CV_MAX_WAITERS);
1747c478bd9Sstevel@tonic-gate 	THREAD_SLEEP(t, &sqh->sq_lock);
1757c478bd9Sstevel@tonic-gate 	sleepq_insert(&sqh->sq_queue, t);
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * THREAD_SLEEP() moves curthread->t_lockp to point to the
1787c478bd9Sstevel@tonic-gate 	 * lock sqh->sq_lock. This lock is later released by the caller
1797c478bd9Sstevel@tonic-gate 	 * when it calls thread_unlock() on curthread.
1807c478bd9Sstevel@tonic-gate 	 */
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate #define	cv_block_sig(t, cvp)	\
1847c478bd9Sstevel@tonic-gate 	{ (t)->t_flag |= T_WAKEABLE; cv_block(cvp); }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * Block on the indicated condition variable and release the
1887c478bd9Sstevel@tonic-gate  * associated kmutex while blocked.
1897c478bd9Sstevel@tonic-gate  */
1907c478bd9Sstevel@tonic-gate void
cv_wait(kcondvar_t * cvp,kmutex_t * mp)1917c478bd9Sstevel@tonic-gate cv_wait(kcondvar_t *cvp, kmutex_t *mp)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	if (panicstr)
1947c478bd9Sstevel@tonic-gate 		return;
1958a3feaaaSJerry Gilliam 	ASSERT(!quiesce_active);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
1987c478bd9Sstevel@tonic-gate 	thread_lock(curthread);			/* lock the thread */
1997c478bd9Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
2007c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(curthread);	/* unlock the waiters field */
2017c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
2027c478bd9Sstevel@tonic-gate 	swtch();
2037c478bd9Sstevel@tonic-gate 	mutex_enter(mp);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
20687a18d3fSMadhavan Venkataraman static void
cv_wakeup(void * arg)20787a18d3fSMadhavan Venkataraman cv_wakeup(void *arg)
20887a18d3fSMadhavan Venkataraman {
20987a18d3fSMadhavan Venkataraman 	kthread_t *t = arg;
21087a18d3fSMadhavan Venkataraman 
21187a18d3fSMadhavan Venkataraman 	/*
21287a18d3fSMadhavan Venkataraman 	 * This mutex is acquired and released in order to make sure that
21387a18d3fSMadhavan Venkataraman 	 * the wakeup does not happen before the block itself happens.
21487a18d3fSMadhavan Venkataraman 	 */
215454ab202SMadhavan Venkataraman 	mutex_enter(&t->t_wait_mutex);
216454ab202SMadhavan Venkataraman 	mutex_exit(&t->t_wait_mutex);
21787a18d3fSMadhavan Venkataraman 	setrun(t);
21887a18d3fSMadhavan Venkataraman }
21987a18d3fSMadhavan Venkataraman 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * Same as cv_wait except the thread will unblock at 'tim'
2227c478bd9Sstevel@tonic-gate  * (an absolute time) if it hasn't already unblocked.
2237c478bd9Sstevel@tonic-gate  *
2247c478bd9Sstevel@tonic-gate  * Returns the amount of time left from the original 'tim' value
2257c478bd9Sstevel@tonic-gate  * when it was unblocked.
2267c478bd9Sstevel@tonic-gate  */
2277c478bd9Sstevel@tonic-gate clock_t
cv_timedwait(kcondvar_t * cvp,kmutex_t * mp,clock_t tim)2287c478bd9Sstevel@tonic-gate cv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
22951b32bddSMadhavan Venkataraman {
23051b32bddSMadhavan Venkataraman 	hrtime_t hrtim;
231d3d50737SRafael Vanoni 	clock_t now = ddi_get_lbolt();
23251b32bddSMadhavan Venkataraman 
233d3d50737SRafael Vanoni 	if (tim <= now)
23451b32bddSMadhavan Venkataraman 		return (-1);
23551b32bddSMadhavan Venkataraman 
236d3d50737SRafael Vanoni 	hrtim = TICK_TO_NSEC(tim - now);
23751b32bddSMadhavan Venkataraman 	return (cv_timedwait_hires(cvp, mp, hrtim, nsec_per_tick, 0));
23851b32bddSMadhavan Venkataraman }
23951b32bddSMadhavan Venkataraman 
240d3d50737SRafael Vanoni /*
241d3d50737SRafael Vanoni  * Same as cv_timedwait() except that the third argument is a relative
242d3d50737SRafael Vanoni  * timeout value, as opposed to an absolute one. There is also a fourth
243d3d50737SRafael Vanoni  * argument that specifies how accurately the timeout must be implemented.
244d3d50737SRafael Vanoni  */
245d3d50737SRafael Vanoni clock_t
cv_reltimedwait(kcondvar_t * cvp,kmutex_t * mp,clock_t delta,time_res_t res)246d3d50737SRafael Vanoni cv_reltimedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t delta, time_res_t res)
247d3d50737SRafael Vanoni {
248d3d50737SRafael Vanoni 	hrtime_t exp;
249d3d50737SRafael Vanoni 
250d3d50737SRafael Vanoni 	ASSERT(TIME_RES_VALID(res));
251d3d50737SRafael Vanoni 
252d3d50737SRafael Vanoni 	if (delta <= 0)
253d3d50737SRafael Vanoni 		return (-1);
254d3d50737SRafael Vanoni 
255d3d50737SRafael Vanoni 	if ((exp = TICK_TO_NSEC(delta)) < 0)
256d3d50737SRafael Vanoni 		exp = CY_INFINITY;
257d3d50737SRafael Vanoni 
258d3d50737SRafael Vanoni 	return (cv_timedwait_hires(cvp, mp, exp, time_res[res], 0));
259d3d50737SRafael Vanoni }
260d3d50737SRafael Vanoni 
26151b32bddSMadhavan Venkataraman clock_t
cv_timedwait_hires(kcondvar_t * cvp,kmutex_t * mp,hrtime_t tim,hrtime_t res,int flag)26251b32bddSMadhavan Venkataraman cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
26351b32bddSMadhavan Venkataraman     hrtime_t res, int flag)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
26687a18d3fSMadhavan Venkataraman 	callout_id_t id;
2677c478bd9Sstevel@tonic-gate 	clock_t timeleft;
26851b32bddSMadhavan Venkataraman 	hrtime_t limit;
2697c478bd9Sstevel@tonic-gate 	int signalled;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if (panicstr)
2727c478bd9Sstevel@tonic-gate 		return (-1);
2738a3feaaaSJerry Gilliam 	ASSERT(!quiesce_active);
2747c478bd9Sstevel@tonic-gate 
27551b32bddSMadhavan Venkataraman 	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
27651b32bddSMadhavan Venkataraman 	if (tim <= limit)
2777c478bd9Sstevel@tonic-gate 		return (-1);
278454ab202SMadhavan Venkataraman 	mutex_enter(&t->t_wait_mutex);
27951b32bddSMadhavan Venkataraman 	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
28051b32bddSMadhavan Venkataraman 	    tim, res, flag);
2817c478bd9Sstevel@tonic-gate 	thread_lock(t);		/* lock the thread */
2827c478bd9Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
2837c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
284454ab202SMadhavan Venkataraman 	mutex_exit(&t->t_wait_mutex);
2857c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
2867c478bd9Sstevel@tonic-gate 	swtch();
2877c478bd9Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
2887c478bd9Sstevel@tonic-gate 	/*
2897c478bd9Sstevel@tonic-gate 	 * Get the time left. untimeout() returns -1 if the timeout has
2907c478bd9Sstevel@tonic-gate 	 * occured or the time remaining.  If the time remaining is zero,
2917c478bd9Sstevel@tonic-gate 	 * the timeout has occured between when we were awoken and
2927c478bd9Sstevel@tonic-gate 	 * we called untimeout.  We will treat this as if the timeout
2937c478bd9Sstevel@tonic-gate 	 * has occured and set timeleft to -1.
2947c478bd9Sstevel@tonic-gate 	 */
295454ab202SMadhavan Venkataraman 	timeleft = untimeout_default(id, 0);
2967c478bd9Sstevel@tonic-gate 	mutex_enter(mp);
2977c478bd9Sstevel@tonic-gate 	if (timeleft <= 0) {
2987c478bd9Sstevel@tonic-gate 		timeleft = -1;
2997c478bd9Sstevel@tonic-gate 		if (signalled)	/* avoid consuming the cv_signal() */
3007c478bd9Sstevel@tonic-gate 			cv_signal(cvp);
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 	return (timeleft);
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate int
cv_wait_sig(kcondvar_t * cvp,kmutex_t * mp)3067c478bd9Sstevel@tonic-gate cv_wait_sig(kcondvar_t *cvp, kmutex_t *mp)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
3097c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
3107c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
311a574db85Sraf 	int cancel_pending;
3127c478bd9Sstevel@tonic-gate 	int rval = 1;
3137c478bd9Sstevel@tonic-gate 	int signalled = 0;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if (panicstr)
3167c478bd9Sstevel@tonic-gate 		return (rval);
3178a3feaaaSJerry Gilliam 	ASSERT(!quiesce_active);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/*
32035a5a358SJonathan Adams 	 * Threads in system processes don't process signals.  This is
32135a5a358SJonathan Adams 	 * true both for standard threads of system processes and for
32235a5a358SJonathan Adams 	 * interrupt threads which have borrowed their pinned thread's LWP.
3237c478bd9Sstevel@tonic-gate 	 */
32435a5a358SJonathan Adams 	if (lwp == NULL || (p->p_flag & SSYS)) {
3257c478bd9Sstevel@tonic-gate 		cv_wait(cvp, mp);
3267c478bd9Sstevel@tonic-gate 		return (rval);
3277c478bd9Sstevel@tonic-gate 	}
32835a5a358SJonathan Adams 	ASSERT(t->t_intr == NULL);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
331a574db85Sraf 	cancel_pending = schedctl_cancel_pending();
3327c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
3337c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
3347c478bd9Sstevel@tonic-gate 	thread_lock(t);
3357c478bd9Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
3367c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
3377c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
338a574db85Sraf 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
3397c478bd9Sstevel@tonic-gate 		setrun(t);
3407c478bd9Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
3417c478bd9Sstevel@tonic-gate 	swtch();
3427c478bd9Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
3437c478bd9Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
3447c478bd9Sstevel@tonic-gate 	mutex_enter(mp);
3457c478bd9Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
3467c478bd9Sstevel@tonic-gate 		mutex_exit(mp);
3477c478bd9Sstevel@tonic-gate 		if (issig(FORREAL))
3487c478bd9Sstevel@tonic-gate 			rval = 0;
3497c478bd9Sstevel@tonic-gate 		mutex_enter(mp);
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
3527c478bd9Sstevel@tonic-gate 		rval = 0;
353a574db85Sraf 	if (rval != 0 && cancel_pending) {
354a574db85Sraf 		schedctl_cancel_eintr();
355a574db85Sraf 		rval = 0;
356a574db85Sraf 	}
3577c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
3587c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
3597c478bd9Sstevel@tonic-gate 	if (rval == 0 && signalled)	/* avoid consuming the cv_signal() */
3607c478bd9Sstevel@tonic-gate 		cv_signal(cvp);
3617c478bd9Sstevel@tonic-gate 	return (rval);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
36487a18d3fSMadhavan Venkataraman static clock_t
cv_timedwait_sig_hires(kcondvar_t * cvp,kmutex_t * mp,hrtime_t tim,hrtime_t res,int flag)36551b32bddSMadhavan Venkataraman cv_timedwait_sig_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
36651b32bddSMadhavan Venkataraman     hrtime_t res, int flag)
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
3697c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
3707c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
371a574db85Sraf 	int cancel_pending = 0;
37287a18d3fSMadhavan Venkataraman 	callout_id_t id;
3737c478bd9Sstevel@tonic-gate 	clock_t rval = 1;
37451b32bddSMadhavan Venkataraman 	hrtime_t limit;
3757c478bd9Sstevel@tonic-gate 	int signalled = 0;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if (panicstr)
3787c478bd9Sstevel@tonic-gate 		return (rval);
3798a3feaaaSJerry Gilliam 	ASSERT(!quiesce_active);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	/*
38235a5a358SJonathan Adams 	 * Threads in system processes don't process signals.  This is
38335a5a358SJonathan Adams 	 * true both for standard threads of system processes and for
38435a5a358SJonathan Adams 	 * interrupt threads which have borrowed their pinned thread's LWP.
3857c478bd9Sstevel@tonic-gate 	 */
38635a5a358SJonathan Adams 	if (lwp == NULL || (p->p_flag & SSYS))
38751b32bddSMadhavan Venkataraman 		return (cv_timedwait_hires(cvp, mp, tim, res, flag));
38835a5a358SJonathan Adams 	ASSERT(t->t_intr == NULL);
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	/*
39151b32bddSMadhavan Venkataraman 	 * If tim is less than or equal to current hrtime, then the timeout
3927c478bd9Sstevel@tonic-gate 	 * has already occured.  So just check to see if there is a signal
3937c478bd9Sstevel@tonic-gate 	 * pending.  If so return 0 indicating that there is a signal pending.
3947c478bd9Sstevel@tonic-gate 	 * Else return -1 indicating that the timeout occured. No need to
3957c478bd9Sstevel@tonic-gate 	 * wait on anything.
3967c478bd9Sstevel@tonic-gate 	 */
39751b32bddSMadhavan Venkataraman 	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
39851b32bddSMadhavan Venkataraman 	if (tim <= limit) {
3997c478bd9Sstevel@tonic-gate 		lwp->lwp_asleep = 1;
4007c478bd9Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;
4017c478bd9Sstevel@tonic-gate 		rval = -1;
4027c478bd9Sstevel@tonic-gate 		goto out;
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/*
4067c478bd9Sstevel@tonic-gate 	 * Set the timeout and wait.
4077c478bd9Sstevel@tonic-gate 	 */
408a574db85Sraf 	cancel_pending = schedctl_cancel_pending();
409454ab202SMadhavan Venkataraman 	mutex_enter(&t->t_wait_mutex);
41087a18d3fSMadhavan Venkataraman 	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
41151b32bddSMadhavan Venkataraman 	    tim, res, flag);
4127c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
4137c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
4147c478bd9Sstevel@tonic-gate 	thread_lock(t);
4157c478bd9Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
4167c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
417454ab202SMadhavan Venkataraman 	mutex_exit(&t->t_wait_mutex);
4187c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
41987a18d3fSMadhavan Venkataraman 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
4207c478bd9Sstevel@tonic-gate 		setrun(t);
4217c478bd9Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
4227c478bd9Sstevel@tonic-gate 	swtch();
4237c478bd9Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
4247c478bd9Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * Untimeout the thread.  untimeout() returns -1 if the timeout has
4287c478bd9Sstevel@tonic-gate 	 * occured or the time remaining.  If the time remaining is zero,
4297c478bd9Sstevel@tonic-gate 	 * the timeout has occured between when we were awoken and
4307c478bd9Sstevel@tonic-gate 	 * we called untimeout.  We will treat this as if the timeout
4317c478bd9Sstevel@tonic-gate 	 * has occured and set rval to -1.
4327c478bd9Sstevel@tonic-gate 	 */
433454ab202SMadhavan Venkataraman 	rval = untimeout_default(id, 0);
43487a18d3fSMadhavan Venkataraman 	mutex_enter(mp);
4357c478bd9Sstevel@tonic-gate 	if (rval <= 0)
4367c478bd9Sstevel@tonic-gate 		rval = -1;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	/*
4397c478bd9Sstevel@tonic-gate 	 * Check to see if a signal is pending.  If so, regardless of whether
4407c478bd9Sstevel@tonic-gate 	 * or not we were awoken due to the signal, the signal is now pending
4417c478bd9Sstevel@tonic-gate 	 * and a return of 0 has the highest priority.
4427c478bd9Sstevel@tonic-gate 	 */
4437c478bd9Sstevel@tonic-gate out:
4447c478bd9Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
4457c478bd9Sstevel@tonic-gate 		mutex_exit(mp);
4467c478bd9Sstevel@tonic-gate 		if (issig(FORREAL))
4477c478bd9Sstevel@tonic-gate 			rval = 0;
4487c478bd9Sstevel@tonic-gate 		mutex_enter(mp);
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
4517c478bd9Sstevel@tonic-gate 		rval = 0;
452a574db85Sraf 	if (rval != 0 && cancel_pending) {
453a574db85Sraf 		schedctl_cancel_eintr();
454a574db85Sraf 		rval = 0;
455a574db85Sraf 	}
4567c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
4577c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
4587c478bd9Sstevel@tonic-gate 	if (rval <= 0 && signalled)	/* avoid consuming the cv_signal() */
4597c478bd9Sstevel@tonic-gate 		cv_signal(cvp);
4607c478bd9Sstevel@tonic-gate 	return (rval);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
46387a18d3fSMadhavan Venkataraman /*
46487a18d3fSMadhavan Venkataraman  * Returns:
465*6a0b1217SPatrick Mooney  *	Function result in order of precedence:
46687a18d3fSMadhavan Venkataraman  *		 0 if a signal was received
46787a18d3fSMadhavan Venkataraman  *		-1 if timeout occured
46887a18d3fSMadhavan Venkataraman  *		>0 if awakened via cv_signal() or cv_broadcast().
46987a18d3fSMadhavan Venkataraman  *		   (returns time remaining)
47087a18d3fSMadhavan Venkataraman  *
47187a18d3fSMadhavan Venkataraman  * cv_timedwait_sig() is now part of the DDI.
47287a18d3fSMadhavan Venkataraman  *
47351b32bddSMadhavan Venkataraman  * This function is now just a wrapper for cv_timedwait_sig_hires().
47487a18d3fSMadhavan Venkataraman  */
47587a18d3fSMadhavan Venkataraman clock_t
cv_timedwait_sig(kcondvar_t * cvp,kmutex_t * mp,clock_t tim)47687a18d3fSMadhavan Venkataraman cv_timedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
47787a18d3fSMadhavan Venkataraman {
47851b32bddSMadhavan Venkataraman 	hrtime_t hrtim;
47951b32bddSMadhavan Venkataraman 
480d3d50737SRafael Vanoni 	hrtim = TICK_TO_NSEC(tim - ddi_get_lbolt());
48151b32bddSMadhavan Venkataraman 	return (cv_timedwait_sig_hires(cvp, mp, hrtim, nsec_per_tick, 0));
48287a18d3fSMadhavan Venkataraman }
48387a18d3fSMadhavan Venkataraman 
484cd1c8b85SMatthew Ahrens /*
485cd1c8b85SMatthew Ahrens  * Wait until the specified time.
486cd1c8b85SMatthew Ahrens  * If tim == -1, waits without timeout using cv_wait_sig_swap().
487cd1c8b85SMatthew Ahrens  */
488cd1c8b85SMatthew Ahrens int
cv_timedwait_sig_hrtime(kcondvar_t * cvp,kmutex_t * mp,hrtime_t tim)489cd1c8b85SMatthew Ahrens cv_timedwait_sig_hrtime(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim)
490cd1c8b85SMatthew Ahrens {
491cd1c8b85SMatthew Ahrens 	if (tim == -1) {
492cd1c8b85SMatthew Ahrens 		return (cv_wait_sig_swap(cvp, mp));
493cd1c8b85SMatthew Ahrens 	} else {
494cd1c8b85SMatthew Ahrens 		return (cv_timedwait_sig_hires(cvp, mp, tim, 1,
495cd1c8b85SMatthew Ahrens 		    CALLOUT_FLAG_ABSOLUTE | CALLOUT_FLAG_ROUNDUP));
496cd1c8b85SMatthew Ahrens 	}
497cd1c8b85SMatthew Ahrens }
498cd1c8b85SMatthew Ahrens 
499d3d50737SRafael Vanoni /*
500d3d50737SRafael Vanoni  * Same as cv_timedwait_sig() except that the third argument is a relative
501d3d50737SRafael Vanoni  * timeout value, as opposed to an absolute one. There is also a fourth
502d3d50737SRafael Vanoni  * argument that specifies how accurately the timeout must be implemented.
503d3d50737SRafael Vanoni  */
504d3d50737SRafael Vanoni clock_t
cv_reltimedwait_sig(kcondvar_t * cvp,kmutex_t * mp,clock_t delta,time_res_t res)505d3d50737SRafael Vanoni cv_reltimedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t delta,
506d3d50737SRafael Vanoni     time_res_t res)
507d3d50737SRafael Vanoni {
5081c4c388cSRafael Vanoni 	hrtime_t exp = 0;
509d3d50737SRafael Vanoni 
510d3d50737SRafael Vanoni 	ASSERT(TIME_RES_VALID(res));
511d3d50737SRafael Vanoni 
5121c4c388cSRafael Vanoni 	if (delta > 0) {
5131c4c388cSRafael Vanoni 		if ((exp = TICK_TO_NSEC(delta)) < 0)
5141c4c388cSRafael Vanoni 			exp = CY_INFINITY;
5151c4c388cSRafael Vanoni 	}
516d3d50737SRafael Vanoni 
517d3d50737SRafael Vanoni 	return (cv_timedwait_sig_hires(cvp, mp, exp, time_res[res], 0));
518d3d50737SRafael Vanoni }
519d3d50737SRafael Vanoni 
5207c478bd9Sstevel@tonic-gate /*
5217c478bd9Sstevel@tonic-gate  * Like cv_wait_sig_swap but allows the caller to indicate (with a
5227c478bd9Sstevel@tonic-gate  * non-NULL sigret) that they will take care of signalling the cv
5237c478bd9Sstevel@tonic-gate  * after wakeup, if necessary.  This is a vile hack that should only
5247c478bd9Sstevel@tonic-gate  * be used when no other option is available; almost all callers
5257c478bd9Sstevel@tonic-gate  * should just use cv_wait_sig_swap (which takes care of the cv_signal
5267c478bd9Sstevel@tonic-gate  * stuff automatically) instead.
5277c478bd9Sstevel@tonic-gate  */
5287c478bd9Sstevel@tonic-gate int
cv_wait_sig_swap_core(kcondvar_t * cvp,kmutex_t * mp,int * sigret)5297c478bd9Sstevel@tonic-gate cv_wait_sig_swap_core(kcondvar_t *cvp, kmutex_t *mp, int *sigret)
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
5327c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
5337c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
534a574db85Sraf 	int cancel_pending;
5357c478bd9Sstevel@tonic-gate 	int rval = 1;
5367c478bd9Sstevel@tonic-gate 	int signalled = 0;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	if (panicstr)
5397c478bd9Sstevel@tonic-gate 		return (rval);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	/*
54235a5a358SJonathan Adams 	 * Threads in system processes don't process signals.  This is
54335a5a358SJonathan Adams 	 * true both for standard threads of system processes and for
54435a5a358SJonathan Adams 	 * interrupt threads which have borrowed their pinned thread's LWP.
5457c478bd9Sstevel@tonic-gate 	 */
54635a5a358SJonathan Adams 	if (lwp == NULL || (p->p_flag & SSYS)) {
5477c478bd9Sstevel@tonic-gate 		cv_wait(cvp, mp);
5487c478bd9Sstevel@tonic-gate 		return (rval);
5497c478bd9Sstevel@tonic-gate 	}
55035a5a358SJonathan Adams 	ASSERT(t->t_intr == NULL);
5517c478bd9Sstevel@tonic-gate 
552a574db85Sraf 	cancel_pending = schedctl_cancel_pending();
5537c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
5547c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
5557c478bd9Sstevel@tonic-gate 	thread_lock(t);
5567c478bd9Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
5577c478bd9Sstevel@tonic-gate 	/* I can be swapped now */
5587c478bd9Sstevel@tonic-gate 	curthread->t_schedflag &= ~TS_DONT_SWAP;
5597c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
5607c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
561a574db85Sraf 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
5627c478bd9Sstevel@tonic-gate 		setrun(t);
5637c478bd9Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
5647c478bd9Sstevel@tonic-gate 	swtch();
5657c478bd9Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
5667c478bd9Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
5677c478bd9Sstevel@tonic-gate 	/* TS_DONT_SWAP set by disp() */
5687c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
5697c478bd9Sstevel@tonic-gate 	mutex_enter(mp);
5707c478bd9Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
5717c478bd9Sstevel@tonic-gate 		mutex_exit(mp);
5727c478bd9Sstevel@tonic-gate 		if (issig(FORREAL))
5737c478bd9Sstevel@tonic-gate 			rval = 0;
5747c478bd9Sstevel@tonic-gate 		mutex_enter(mp);
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
5777c478bd9Sstevel@tonic-gate 		rval = 0;
578a574db85Sraf 	if (rval != 0 && cancel_pending) {
579a574db85Sraf 		schedctl_cancel_eintr();
580a574db85Sraf 		rval = 0;
581a574db85Sraf 	}
5827c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
5837c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
5847c478bd9Sstevel@tonic-gate 	if (rval == 0) {
5857c478bd9Sstevel@tonic-gate 		if (sigret != NULL)
5867c478bd9Sstevel@tonic-gate 			*sigret = signalled;	/* just tell the caller */
5877c478bd9Sstevel@tonic-gate 		else if (signalled)
5887c478bd9Sstevel@tonic-gate 			cv_signal(cvp);	/* avoid consuming the cv_signal() */
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 	return (rval);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate /*
5947c478bd9Sstevel@tonic-gate  * Same as cv_wait_sig but the thread can be swapped out while waiting.
5957c478bd9Sstevel@tonic-gate  * This should only be used when we know we aren't holding any locks.
5967c478bd9Sstevel@tonic-gate  */
5977c478bd9Sstevel@tonic-gate int
cv_wait_sig_swap(kcondvar_t * cvp,kmutex_t * mp)5987c478bd9Sstevel@tonic-gate cv_wait_sig_swap(kcondvar_t *cvp, kmutex_t *mp)
5997c478bd9Sstevel@tonic-gate {
6007c478bd9Sstevel@tonic-gate 	return (cv_wait_sig_swap_core(cvp, mp, NULL));
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate void
cv_signal(kcondvar_t * cvp)6047c478bd9Sstevel@tonic-gate cv_signal(kcondvar_t *cvp)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/* make sure the cv_waiters field looks sane */
6097c478bd9Sstevel@tonic-gate 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
6107c478bd9Sstevel@tonic-gate 	if (cp->cv_waiters > 0) {
6117c478bd9Sstevel@tonic-gate 		sleepq_head_t *sqh = SQHASH(cp);
6127c478bd9Sstevel@tonic-gate 		disp_lock_enter(&sqh->sq_lock);
6137c478bd9Sstevel@tonic-gate 		ASSERT(CPU_ON_INTR(CPU) == 0);
6147c478bd9Sstevel@tonic-gate 		if (cp->cv_waiters & CV_WAITERS_MASK) {
6157c478bd9Sstevel@tonic-gate 			kthread_t *t;
6167c478bd9Sstevel@tonic-gate 			cp->cv_waiters--;
6177c478bd9Sstevel@tonic-gate 			t = sleepq_wakeone_chan(&sqh->sq_queue, cp);
6187c478bd9Sstevel@tonic-gate 			/*
6197c478bd9Sstevel@tonic-gate 			 * If cv_waiters is non-zero (and less than
6207c478bd9Sstevel@tonic-gate 			 * CV_MAX_WAITERS) there should be a thread
6217c478bd9Sstevel@tonic-gate 			 * in the queue.
6227c478bd9Sstevel@tonic-gate 			 */
6237c478bd9Sstevel@tonic-gate 			ASSERT(t != NULL);
6247c478bd9Sstevel@tonic-gate 		} else if (sleepq_wakeone_chan(&sqh->sq_queue, cp) == NULL) {
6257c478bd9Sstevel@tonic-gate 			cp->cv_waiters = 0;
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 		disp_lock_exit(&sqh->sq_lock);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate void
cv_broadcast(kcondvar_t * cvp)6327c478bd9Sstevel@tonic-gate cv_broadcast(kcondvar_t *cvp)
6337c478bd9Sstevel@tonic-gate {
6347c478bd9Sstevel@tonic-gate 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	/* make sure the cv_waiters field looks sane */
6377c478bd9Sstevel@tonic-gate 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
6387c478bd9Sstevel@tonic-gate 	if (cp->cv_waiters > 0) {
6397c478bd9Sstevel@tonic-gate 		sleepq_head_t *sqh = SQHASH(cp);
6407c478bd9Sstevel@tonic-gate 		disp_lock_enter(&sqh->sq_lock);
6417c478bd9Sstevel@tonic-gate 		ASSERT(CPU_ON_INTR(CPU) == 0);
6427c478bd9Sstevel@tonic-gate 		sleepq_wakeall_chan(&sqh->sq_queue, cp);
6437c478bd9Sstevel@tonic-gate 		cp->cv_waiters = 0;
6447c478bd9Sstevel@tonic-gate 		disp_lock_exit(&sqh->sq_lock);
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate /*
6497c478bd9Sstevel@tonic-gate  * Same as cv_wait(), but wakes up (after wakeup_time milliseconds) to check
6507c478bd9Sstevel@tonic-gate  * for requests to stop, like cv_wait_sig() but without dealing with signals.
6517c478bd9Sstevel@tonic-gate  * This is a horrible kludge.  It is evil.  It is vile.  It is swill.
6527c478bd9Sstevel@tonic-gate  * If your code has to call this function then your code is the same.
6537c478bd9Sstevel@tonic-gate  */
6547c478bd9Sstevel@tonic-gate void
cv_wait_stop(kcondvar_t * cvp,kmutex_t * mp,int wakeup_time)6557c478bd9Sstevel@tonic-gate cv_wait_stop(kcondvar_t *cvp, kmutex_t *mp, int wakeup_time)
6567c478bd9Sstevel@tonic-gate {
6577c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
6587c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
6597c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
66087a18d3fSMadhavan Venkataraman 	callout_id_t id;
6617c478bd9Sstevel@tonic-gate 	clock_t tim;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	if (panicstr)
6647c478bd9Sstevel@tonic-gate 		return;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	/*
66735a5a358SJonathan Adams 	 * Threads in system processes don't process signals.  This is
66835a5a358SJonathan Adams 	 * true both for standard threads of system processes and for
66935a5a358SJonathan Adams 	 * interrupt threads which have borrowed their pinned thread's LWP.
6707c478bd9Sstevel@tonic-gate 	 */
67135a5a358SJonathan Adams 	if (lwp == NULL || (p->p_flag & SSYS)) {
6727c478bd9Sstevel@tonic-gate 		cv_wait(cvp, mp);
6737c478bd9Sstevel@tonic-gate 		return;
6747c478bd9Sstevel@tonic-gate 	}
67535a5a358SJonathan Adams 	ASSERT(t->t_intr == NULL);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	/*
6787c478bd9Sstevel@tonic-gate 	 * Wakeup in wakeup_time milliseconds, i.e., human time.
6797c478bd9Sstevel@tonic-gate 	 */
680d3d50737SRafael Vanoni 	tim = ddi_get_lbolt() + MSEC_TO_TICK(wakeup_time);
681454ab202SMadhavan Venkataraman 	mutex_enter(&t->t_wait_mutex);
68287a18d3fSMadhavan Venkataraman 	id = realtime_timeout_default((void (*)(void *))cv_wakeup, t,
683d3d50737SRafael Vanoni 	    tim - ddi_get_lbolt());
6847c478bd9Sstevel@tonic-gate 	thread_lock(t);			/* lock the thread */
6857c478bd9Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
6867c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
687454ab202SMadhavan Venkataraman 	mutex_exit(&t->t_wait_mutex);
6887c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
6897c478bd9Sstevel@tonic-gate 	/* ASSERT(no locks are held); */
6907c478bd9Sstevel@tonic-gate 	swtch();
691454ab202SMadhavan Venkataraman 	(void) untimeout_default(id, 0);
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	/*
6948548bf79Snr 	 * Check for reasons to stop, if lwp_nostop is not true.
6957c478bd9Sstevel@tonic-gate 	 * See issig_forreal() for explanations of the various stops.
6967c478bd9Sstevel@tonic-gate 	 */
6977c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
6988548bf79Snr 	while (lwp->lwp_nostop == 0 && !(p->p_flag & SEXITLWPS)) {
6997c478bd9Sstevel@tonic-gate 		/*
7007c478bd9Sstevel@tonic-gate 		 * Hold the lwp here for watchpoint manipulation.
7017c478bd9Sstevel@tonic-gate 		 */
7028548bf79Snr 		if (t->t_proc_flag & TP_PAUSE) {
7037c478bd9Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_PAUSE);
7047c478bd9Sstevel@tonic-gate 			continue;
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 		/*
7077c478bd9Sstevel@tonic-gate 		 * System checkpoint.
7087c478bd9Sstevel@tonic-gate 		 */
7098548bf79Snr 		if (t->t_proc_flag & TP_CHKPT) {
7107c478bd9Sstevel@tonic-gate 			stop(PR_CHECKPOINT, 0);
7117c478bd9Sstevel@tonic-gate 			continue;
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 		/*
7147c478bd9Sstevel@tonic-gate 		 * Honor fork1(), watchpoint activity (remapping a page),
7158548bf79Snr 		 * and lwp_suspend() requests.
7167c478bd9Sstevel@tonic-gate 		 */
7178548bf79Snr 		if ((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
7188548bf79Snr 		    (t->t_proc_flag & TP_HOLDLWP)) {
7197c478bd9Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_NORMAL);
7207c478bd9Sstevel@tonic-gate 			continue;
7217c478bd9Sstevel@tonic-gate 		}
7227c478bd9Sstevel@tonic-gate 		/*
7237c478bd9Sstevel@tonic-gate 		 * Honor /proc requested stop.
7247c478bd9Sstevel@tonic-gate 		 */
7258548bf79Snr 		if (t->t_proc_flag & TP_PRSTOP) {
7267c478bd9Sstevel@tonic-gate 			stop(PR_REQUESTED, 0);
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 		/*
7297c478bd9Sstevel@tonic-gate 		 * If some lwp in the process has already stopped
7307c478bd9Sstevel@tonic-gate 		 * showing PR_JOBCONTROL, stop in sympathy with it.
7317c478bd9Sstevel@tonic-gate 		 */
7328548bf79Snr 		if (p->p_stopsig && t != p->p_agenttp) {
7337c478bd9Sstevel@tonic-gate 			stop(PR_JOBCONTROL, p->p_stopsig);
7347c478bd9Sstevel@tonic-gate 			continue;
7357c478bd9Sstevel@tonic-gate 		}
7367c478bd9Sstevel@tonic-gate 		break;
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
7397c478bd9Sstevel@tonic-gate 	mutex_enter(mp);
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate /*
7437c478bd9Sstevel@tonic-gate  * Like cv_timedwait_sig(), but takes an absolute hires future time
7447c478bd9Sstevel@tonic-gate  * rather than a future time in clock ticks.  Will not return showing
7457c478bd9Sstevel@tonic-gate  * that a timeout occurred until the future time is passed.
7467c478bd9Sstevel@tonic-gate  * If 'when' is a NULL pointer, no timeout will occur.
7477c478bd9Sstevel@tonic-gate  * Returns:
748*6a0b1217SPatrick Mooney  *	Function result in order of precedence:
7497c478bd9Sstevel@tonic-gate  *		 0 if a signal was received
7507c478bd9Sstevel@tonic-gate  *		-1 if timeout occured
7517c478bd9Sstevel@tonic-gate  *	        >0 if awakened via cv_signal() or cv_broadcast()
7527c478bd9Sstevel@tonic-gate  *		   or by a spurious wakeup.
7537c478bd9Sstevel@tonic-gate  *		   (might return time remaining)
7543348528fSdm  * As a special test, if someone abruptly resets the system time
7553348528fSdm  * (but not through adjtime(2); drifting of the clock is allowed and
7563348528fSdm  * expected [see timespectohz_adj()]), then we force a return of -1
7573348528fSdm  * so the caller can return a premature timeout to the calling process
7583348528fSdm  * so it can reevaluate the situation in light of the new system time.
7593348528fSdm  * (The system clock has been reset if timecheck != timechanged.)
760cd1c8b85SMatthew Ahrens  *
761cd1c8b85SMatthew Ahrens  * Generally, cv_timedwait_sig_hrtime() should be used instead of this
762cd1c8b85SMatthew Ahrens  * routine.  It waits based on hrtime rather than wall-clock time and therefore
763cd1c8b85SMatthew Ahrens  * does not need to deal with the time changing.
7647c478bd9Sstevel@tonic-gate  */
7657c478bd9Sstevel@tonic-gate int
cv_waituntil_sig(kcondvar_t * cvp,kmutex_t * mp,timestruc_t * when,int timecheck)766*6a0b1217SPatrick Mooney cv_waituntil_sig(kcondvar_t *cvp, kmutex_t *mp, timestruc_t *when,
767*6a0b1217SPatrick Mooney     int timecheck)
7687c478bd9Sstevel@tonic-gate {
7697c478bd9Sstevel@tonic-gate 	timestruc_t now;
770b2a1c443Svb 	timestruc_t delta;
77151b32bddSMadhavan Venkataraman 	hrtime_t interval;
7727c478bd9Sstevel@tonic-gate 	int rval;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	if (when == NULL)
7757c478bd9Sstevel@tonic-gate 		return (cv_wait_sig_swap(cvp, mp));
7767c478bd9Sstevel@tonic-gate 
77744e59b5cSDonghai Qiao 	gethrestime(&now);
778b2a1c443Svb 	delta = *when;
779b2a1c443Svb 	timespecsub(&delta, &now);
780b2a1c443Svb 	if (delta.tv_sec < 0 || (delta.tv_sec == 0 && delta.tv_nsec == 0)) {
7817c478bd9Sstevel@tonic-gate 		/*
7827c478bd9Sstevel@tonic-gate 		 * We have already reached the absolute future time.
7837c478bd9Sstevel@tonic-gate 		 * Call cv_timedwait_sig() just to check for signals.
7847c478bd9Sstevel@tonic-gate 		 * We will return immediately with either 0 or -1.
7857c478bd9Sstevel@tonic-gate 		 */
78651b32bddSMadhavan Venkataraman 		rval = cv_timedwait_sig_hires(cvp, mp, 0, 1, 0);
7877c478bd9Sstevel@tonic-gate 	} else {
7883348528fSdm 		if (timecheck == timechanged) {
78951b32bddSMadhavan Venkataraman 			/*
79051b32bddSMadhavan Venkataraman 			 * Make sure that the interval is atleast one tick.
79151b32bddSMadhavan Venkataraman 			 * This is to prevent a user from flooding the system
79251b32bddSMadhavan Venkataraman 			 * with very small, high resolution timers.
79351b32bddSMadhavan Venkataraman 			 */
79451b32bddSMadhavan Venkataraman 			interval = ts2hrt(&delta);
79551b32bddSMadhavan Venkataraman 			if (interval < nsec_per_tick)
79651b32bddSMadhavan Venkataraman 				interval = nsec_per_tick;
79751b32bddSMadhavan Venkataraman 			rval = cv_timedwait_sig_hires(cvp, mp, interval, 1,
79887a18d3fSMadhavan Venkataraman 			    CALLOUT_FLAG_HRESTIME);
7993348528fSdm 		} else {
8003348528fSdm 			/*
8013348528fSdm 			 * Someone reset the system time;
8023348528fSdm 			 * just force an immediate timeout.
8033348528fSdm 			 */
8043348528fSdm 			rval = -1;
8053348528fSdm 		}
8063348528fSdm 		if (rval == -1 && timecheck == timechanged) {
8073348528fSdm 			/*
8083348528fSdm 			 * Even though cv_timedwait_sig() returned showing a
8093348528fSdm 			 * timeout, the future time may not have passed yet.
8103348528fSdm 			 * If not, change rval to indicate a normal wakeup.
8113348528fSdm 			 */
8123348528fSdm 			gethrestime(&now);
8133348528fSdm 			delta = *when;
8143348528fSdm 			timespecsub(&delta, &now);
8153348528fSdm 			if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
8163348528fSdm 			    delta.tv_nsec > 0))
8177c478bd9Sstevel@tonic-gate 				rval = 1;
8183348528fSdm 		}
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 	return (rval);
8217c478bd9Sstevel@tonic-gate }
822