xref: /illumos-gate/usr/src/uts/common/os/condvar.c (revision cd1c8b85)
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 
27*cd1c8b85SMatthew Ahrens /*
28*cd1c8b85SMatthew Ahrens  * Copyright (c) 2012 by Delphix. All rights reserved.
29*cd1c8b85SMatthew Ahrens  */
30fe234e7cSMatt Amdur 
317c478bd9Sstevel@tonic-gate #include <sys/thread.h>
327c478bd9Sstevel@tonic-gate #include <sys/proc.h>
337c478bd9Sstevel@tonic-gate #include <sys/debug.h>
347c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate #include <sys/sobject.h>
377c478bd9Sstevel@tonic-gate #include <sys/sleepq.h>
387c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
397c478bd9Sstevel@tonic-gate #include <sys/condvar.h>
407c478bd9Sstevel@tonic-gate #include <sys/condvar_impl.h>
417c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
427c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
437c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
4487a18d3fSMadhavan Venkataraman #include <sys/callo.h>
457c478bd9Sstevel@tonic-gate 
4651b32bddSMadhavan Venkataraman clock_t cv_timedwait_hires(kcondvar_t *, kmutex_t *, hrtime_t, hrtime_t, int);
4751b32bddSMadhavan Venkataraman 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * CV_MAX_WAITERS is the maximum number of waiters we track; once
507c478bd9Sstevel@tonic-gate  * the number becomes higher than that, we look at the sleepq to
517c478bd9Sstevel@tonic-gate  * see whether there are *really* any waiters.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate #define	CV_MAX_WAITERS		1024		/* must be power of 2 */
547c478bd9Sstevel@tonic-gate #define	CV_WAITERS_MASK		(CV_MAX_WAITERS - 1)
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * Threads don't "own" condition variables.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate /* ARGSUSED */
607c478bd9Sstevel@tonic-gate static kthread_t *
617c478bd9Sstevel@tonic-gate cv_owner(void *cvp)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	return (NULL);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * Unsleep a thread that's blocked on a condition variable.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate static void
707c478bd9Sstevel@tonic-gate cv_unsleep(kthread_t *t)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
737c478bd9Sstevel@tonic-gate 	sleepq_head_t *sqh = SQHASH(cvp);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if (cvp == NULL)
78903a11ebSrh 		panic("cv_unsleep: thread %p not on sleepq %p",
79903a11ebSrh 		    (void *)t, (void *)sqh);
802db6c79fSstevel 	DTRACE_SCHED1(wakeup, kthread_t *, t);
817c478bd9Sstevel@tonic-gate 	sleepq_unsleep(t);
827c478bd9Sstevel@tonic-gate 	if (cvp->cv_waiters != CV_MAX_WAITERS)
837c478bd9Sstevel@tonic-gate 		cvp->cv_waiters--;
847c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&sqh->sq_lock);
857c478bd9Sstevel@tonic-gate 	CL_SETRUN(t);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * Change the priority of a thread that's blocked on a condition variable.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate static void
927c478bd9Sstevel@tonic-gate cv_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
957c478bd9Sstevel@tonic-gate 	sleepq_t *sqp = t->t_sleepq;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
987c478bd9Sstevel@tonic-gate 	ASSERT(&SQHASH(cvp)->sq_queue == sqp);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (cvp == NULL)
101903a11ebSrh 		panic("cv_change_pri: %p not on sleep queue", (void *)t);
1027c478bd9Sstevel@tonic-gate 	sleepq_dequeue(t);
1037c478bd9Sstevel@tonic-gate 	*t_prip = pri;
1047c478bd9Sstevel@tonic-gate 	sleepq_insert(sqp, t);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * The sobj_ops vector exports a set of functions needed when a thread
1097c478bd9Sstevel@tonic-gate  * is asleep on a synchronization object of this type.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate static sobj_ops_t cv_sobj_ops = {
1127c478bd9Sstevel@tonic-gate 	SOBJ_CV, cv_owner, cv_unsleep, cv_change_pri
1137c478bd9Sstevel@tonic-gate };
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /* ARGSUSED */
1167c478bd9Sstevel@tonic-gate void
1177c478bd9Sstevel@tonic-gate cv_init(kcondvar_t *cvp, char *name, kcv_type_t type, void *arg)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	((condvar_impl_t *)cvp)->cv_waiters = 0;
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * cv_destroy is not currently needed, but is part of the DDI.
1247c478bd9Sstevel@tonic-gate  * This is in case cv_init ever needs to allocate something for a cv.
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate /* ARGSUSED */
1277c478bd9Sstevel@tonic-gate void
1287c478bd9Sstevel@tonic-gate cv_destroy(kcondvar_t *cvp)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	ASSERT((((condvar_impl_t *)cvp)->cv_waiters & CV_WAITERS_MASK) == 0);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * The cv_block() function blocks a thread on a condition variable
1357c478bd9Sstevel@tonic-gate  * by putting it in a hashed sleep queue associated with the
1367c478bd9Sstevel@tonic-gate  * synchronization object.
1377c478bd9Sstevel@tonic-gate  *
1387c478bd9Sstevel@tonic-gate  * Threads are taken off the hashed sleep queues via calls to
1397c478bd9Sstevel@tonic-gate  * cv_signal(), cv_broadcast(), or cv_unsleep().
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate static void
1427c478bd9Sstevel@tonic-gate cv_block(condvar_impl_t *cvp)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
1457c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
1467c478bd9Sstevel@tonic-gate 	sleepq_head_t *sqh;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1497c478bd9Sstevel@tonic-gate 	ASSERT(t != CPU->cpu_idle_thread);
1507c478bd9Sstevel@tonic-gate 	ASSERT(CPU_ON_INTR(CPU) == 0);
1517c478bd9Sstevel@tonic-gate 	ASSERT(t->t_wchan0 == NULL && t->t_wchan == NULL);
1527c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	t->t_schedflag &= ~TS_SIGNALLED;
1557c478bd9Sstevel@tonic-gate 	CL_SLEEP(t);			/* assign kernel priority */
1567c478bd9Sstevel@tonic-gate 	t->t_wchan = (caddr_t)cvp;
1577c478bd9Sstevel@tonic-gate 	t->t_sobj_ops = &cv_sobj_ops;
1587c478bd9Sstevel@tonic-gate 	DTRACE_SCHED(sleep);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	/*
1617c478bd9Sstevel@tonic-gate 	 * The check for t_intr is to avoid doing the
1627c478bd9Sstevel@tonic-gate 	 * account for an interrupt thread on the still-pinned
1637c478bd9Sstevel@tonic-gate 	 * lwp's statistics.
1647c478bd9Sstevel@tonic-gate 	 */
1657c478bd9Sstevel@tonic-gate 	if (lwp != NULL && t->t_intr == NULL) {
1667c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.nvcsw++;
1677c478bd9Sstevel@tonic-gate 		(void) new_mstate(t, LMS_SLEEP);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	sqh = SQHASH(cvp);
1717c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&sqh->sq_lock);
1727c478bd9Sstevel@tonic-gate 	if (cvp->cv_waiters < CV_MAX_WAITERS)
1737c478bd9Sstevel@tonic-gate 		cvp->cv_waiters++;
1747c478bd9Sstevel@tonic-gate 	ASSERT(cvp->cv_waiters <= CV_MAX_WAITERS);
1757c478bd9Sstevel@tonic-gate 	THREAD_SLEEP(t, &sqh->sq_lock);
1767c478bd9Sstevel@tonic-gate 	sleepq_insert(&sqh->sq_queue, t);
1777c478bd9Sstevel@tonic-gate 	/*
1787c478bd9Sstevel@tonic-gate 	 * THREAD_SLEEP() moves curthread->t_lockp to point to the
1797c478bd9Sstevel@tonic-gate 	 * lock sqh->sq_lock. This lock is later released by the caller
1807c478bd9Sstevel@tonic-gate 	 * when it calls thread_unlock() on curthread.
1817c478bd9Sstevel@tonic-gate 	 */
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate #define	cv_block_sig(t, cvp)	\
1857c478bd9Sstevel@tonic-gate 	{ (t)->t_flag |= T_WAKEABLE; cv_block(cvp); }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * Block on the indicated condition variable and release the
1897c478bd9Sstevel@tonic-gate  * associated kmutex while blocked.
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate void
1927c478bd9Sstevel@tonic-gate cv_wait(kcondvar_t *cvp, kmutex_t *mp)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	if (panicstr)
1957c478bd9Sstevel@tonic-gate 		return;
1968a3feaaaSJerry Gilliam 	ASSERT(!quiesce_active);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
1997c478bd9Sstevel@tonic-gate 	thread_lock(curthread);			/* lock the thread */
2007c478bd9Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
2017c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(curthread);	/* unlock the waiters field */
2027c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
2037c478bd9Sstevel@tonic-gate 	swtch();
2047c478bd9Sstevel@tonic-gate 	mutex_enter(mp);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
20787a18d3fSMadhavan Venkataraman static void
20887a18d3fSMadhavan Venkataraman cv_wakeup(void *arg)
20987a18d3fSMadhavan Venkataraman {
21087a18d3fSMadhavan Venkataraman 	kthread_t *t = arg;
21187a18d3fSMadhavan Venkataraman 
21287a18d3fSMadhavan Venkataraman 	/*
21387a18d3fSMadhavan Venkataraman 	 * This mutex is acquired and released in order to make sure that
21487a18d3fSMadhavan Venkataraman 	 * the wakeup does not happen before the block itself happens.
21587a18d3fSMadhavan Venkataraman 	 */
216454ab202SMadhavan Venkataraman 	mutex_enter(&t->t_wait_mutex);
217454ab202SMadhavan Venkataraman 	mutex_exit(&t->t_wait_mutex);
21887a18d3fSMadhavan Venkataraman 	setrun(t);
21987a18d3fSMadhavan Venkataraman }
22087a18d3fSMadhavan Venkataraman 
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate  * Same as cv_wait except the thread will unblock at 'tim'
2237c478bd9Sstevel@tonic-gate  * (an absolute time) if it hasn't already unblocked.
2247c478bd9Sstevel@tonic-gate  *
2257c478bd9Sstevel@tonic-gate  * Returns the amount of time left from the original 'tim' value
2267c478bd9Sstevel@tonic-gate  * when it was unblocked.
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate clock_t
2297c478bd9Sstevel@tonic-gate cv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
23051b32bddSMadhavan Venkataraman {
23151b32bddSMadhavan Venkataraman 	hrtime_t hrtim;
232d3d50737SRafael Vanoni 	clock_t now = ddi_get_lbolt();
23351b32bddSMadhavan Venkataraman 
234d3d50737SRafael Vanoni 	if (tim <= now)
23551b32bddSMadhavan Venkataraman 		return (-1);
23651b32bddSMadhavan Venkataraman 
237d3d50737SRafael Vanoni 	hrtim = TICK_TO_NSEC(tim - now);
23851b32bddSMadhavan Venkataraman 	return (cv_timedwait_hires(cvp, mp, hrtim, nsec_per_tick, 0));
23951b32bddSMadhavan Venkataraman }
24051b32bddSMadhavan Venkataraman 
241d3d50737SRafael Vanoni /*
242d3d50737SRafael Vanoni  * Same as cv_timedwait() except that the third argument is a relative
243d3d50737SRafael Vanoni  * timeout value, as opposed to an absolute one. There is also a fourth
244d3d50737SRafael Vanoni  * argument that specifies how accurately the timeout must be implemented.
245d3d50737SRafael Vanoni  */
246d3d50737SRafael Vanoni clock_t
247d3d50737SRafael Vanoni cv_reltimedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t delta, time_res_t res)
248d3d50737SRafael Vanoni {
249d3d50737SRafael Vanoni 	hrtime_t exp;
250d3d50737SRafael Vanoni 
251d3d50737SRafael Vanoni 	ASSERT(TIME_RES_VALID(res));
252d3d50737SRafael Vanoni 
253d3d50737SRafael Vanoni 	if (delta <= 0)
254d3d50737SRafael Vanoni 		return (-1);
255d3d50737SRafael Vanoni 
256d3d50737SRafael Vanoni 	if ((exp = TICK_TO_NSEC(delta)) < 0)
257d3d50737SRafael Vanoni 		exp = CY_INFINITY;
258d3d50737SRafael Vanoni 
259d3d50737SRafael Vanoni 	return (cv_timedwait_hires(cvp, mp, exp, time_res[res], 0));
260d3d50737SRafael Vanoni }
261d3d50737SRafael Vanoni 
26251b32bddSMadhavan Venkataraman clock_t
26351b32bddSMadhavan Venkataraman cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
26451b32bddSMadhavan Venkataraman     hrtime_t res, int flag)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
26787a18d3fSMadhavan Venkataraman 	callout_id_t id;
2687c478bd9Sstevel@tonic-gate 	clock_t timeleft;
26951b32bddSMadhavan Venkataraman 	hrtime_t limit;
2707c478bd9Sstevel@tonic-gate 	int signalled;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if (panicstr)
2737c478bd9Sstevel@tonic-gate 		return (-1);
2748a3feaaaSJerry Gilliam 	ASSERT(!quiesce_active);
2757c478bd9Sstevel@tonic-gate 
27651b32bddSMadhavan Venkataraman 	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
27751b32bddSMadhavan Venkataraman 	if (tim <= limit)
2787c478bd9Sstevel@tonic-gate 		return (-1);
279454ab202SMadhavan Venkataraman 	mutex_enter(&t->t_wait_mutex);
28051b32bddSMadhavan Venkataraman 	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
28151b32bddSMadhavan Venkataraman 	    tim, res, flag);
2827c478bd9Sstevel@tonic-gate 	thread_lock(t);		/* lock the thread */
2837c478bd9Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
2847c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
285454ab202SMadhavan Venkataraman 	mutex_exit(&t->t_wait_mutex);
2867c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
2877c478bd9Sstevel@tonic-gate 	swtch();
2887c478bd9Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
2897c478bd9Sstevel@tonic-gate 	/*
2907c478bd9Sstevel@tonic-gate 	 * Get the time left. untimeout() returns -1 if the timeout has
2917c478bd9Sstevel@tonic-gate 	 * occured or the time remaining.  If the time remaining is zero,
2927c478bd9Sstevel@tonic-gate 	 * the timeout has occured between when we were awoken and
2937c478bd9Sstevel@tonic-gate 	 * we called untimeout.  We will treat this as if the timeout
2947c478bd9Sstevel@tonic-gate 	 * has occured and set timeleft to -1.
2957c478bd9Sstevel@tonic-gate 	 */
296454ab202SMadhavan Venkataraman 	timeleft = untimeout_default(id, 0);
2977c478bd9Sstevel@tonic-gate 	mutex_enter(mp);
2987c478bd9Sstevel@tonic-gate 	if (timeleft <= 0) {
2997c478bd9Sstevel@tonic-gate 		timeleft = -1;
3007c478bd9Sstevel@tonic-gate 		if (signalled)	/* avoid consuming the cv_signal() */
3017c478bd9Sstevel@tonic-gate 			cv_signal(cvp);
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 	return (timeleft);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate int
3077c478bd9Sstevel@tonic-gate cv_wait_sig(kcondvar_t *cvp, kmutex_t *mp)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
3107c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
3117c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
312a574db85Sraf 	int cancel_pending;
3137c478bd9Sstevel@tonic-gate 	int rval = 1;
3147c478bd9Sstevel@tonic-gate 	int signalled = 0;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	if (panicstr)
3177c478bd9Sstevel@tonic-gate 		return (rval);
3188a3feaaaSJerry Gilliam 	ASSERT(!quiesce_active);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/*
32135a5a358SJonathan Adams 	 * Threads in system processes don't process signals.  This is
32235a5a358SJonathan Adams 	 * true both for standard threads of system processes and for
32335a5a358SJonathan Adams 	 * interrupt threads which have borrowed their pinned thread's LWP.
3247c478bd9Sstevel@tonic-gate 	 */
32535a5a358SJonathan Adams 	if (lwp == NULL || (p->p_flag & SSYS)) {
3267c478bd9Sstevel@tonic-gate 		cv_wait(cvp, mp);
3277c478bd9Sstevel@tonic-gate 		return (rval);
3287c478bd9Sstevel@tonic-gate 	}
32935a5a358SJonathan Adams 	ASSERT(t->t_intr == NULL);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
332a574db85Sraf 	cancel_pending = schedctl_cancel_pending();
3337c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
3347c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
3357c478bd9Sstevel@tonic-gate 	thread_lock(t);
3367c478bd9Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
3377c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
3387c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
339a574db85Sraf 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
3407c478bd9Sstevel@tonic-gate 		setrun(t);
3417c478bd9Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
3427c478bd9Sstevel@tonic-gate 	swtch();
3437c478bd9Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
3447c478bd9Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
3457c478bd9Sstevel@tonic-gate 	mutex_enter(mp);
3467c478bd9Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
3477c478bd9Sstevel@tonic-gate 		mutex_exit(mp);
3487c478bd9Sstevel@tonic-gate 		if (issig(FORREAL))
3497c478bd9Sstevel@tonic-gate 			rval = 0;
3507c478bd9Sstevel@tonic-gate 		mutex_enter(mp);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
3537c478bd9Sstevel@tonic-gate 		rval = 0;
354a574db85Sraf 	if (rval != 0 && cancel_pending) {
355a574db85Sraf 		schedctl_cancel_eintr();
356a574db85Sraf 		rval = 0;
357a574db85Sraf 	}
3587c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
3597c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
3607c478bd9Sstevel@tonic-gate 	if (rval == 0 && signalled)	/* avoid consuming the cv_signal() */
3617c478bd9Sstevel@tonic-gate 		cv_signal(cvp);
3627c478bd9Sstevel@tonic-gate 	return (rval);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
36587a18d3fSMadhavan Venkataraman static clock_t
36651b32bddSMadhavan Venkataraman cv_timedwait_sig_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
36751b32bddSMadhavan Venkataraman     hrtime_t res, int flag)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
3707c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
3717c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
372a574db85Sraf 	int cancel_pending = 0;
37387a18d3fSMadhavan Venkataraman 	callout_id_t id;
3747c478bd9Sstevel@tonic-gate 	clock_t rval = 1;
37551b32bddSMadhavan Venkataraman 	hrtime_t limit;
3767c478bd9Sstevel@tonic-gate 	int signalled = 0;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	if (panicstr)
3797c478bd9Sstevel@tonic-gate 		return (rval);
3808a3feaaaSJerry Gilliam 	ASSERT(!quiesce_active);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/*
38335a5a358SJonathan Adams 	 * Threads in system processes don't process signals.  This is
38435a5a358SJonathan Adams 	 * true both for standard threads of system processes and for
38535a5a358SJonathan Adams 	 * interrupt threads which have borrowed their pinned thread's LWP.
3867c478bd9Sstevel@tonic-gate 	 */
38735a5a358SJonathan Adams 	if (lwp == NULL || (p->p_flag & SSYS))
38851b32bddSMadhavan Venkataraman 		return (cv_timedwait_hires(cvp, mp, tim, res, flag));
38935a5a358SJonathan Adams 	ASSERT(t->t_intr == NULL);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/*
39251b32bddSMadhavan Venkataraman 	 * If tim is less than or equal to current hrtime, then the timeout
3937c478bd9Sstevel@tonic-gate 	 * has already occured.  So just check to see if there is a signal
3947c478bd9Sstevel@tonic-gate 	 * pending.  If so return 0 indicating that there is a signal pending.
3957c478bd9Sstevel@tonic-gate 	 * Else return -1 indicating that the timeout occured. No need to
3967c478bd9Sstevel@tonic-gate 	 * wait on anything.
3977c478bd9Sstevel@tonic-gate 	 */
39851b32bddSMadhavan Venkataraman 	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
39951b32bddSMadhavan Venkataraman 	if (tim <= limit) {
4007c478bd9Sstevel@tonic-gate 		lwp->lwp_asleep = 1;
4017c478bd9Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;
4027c478bd9Sstevel@tonic-gate 		rval = -1;
4037c478bd9Sstevel@tonic-gate 		goto out;
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	/*
4077c478bd9Sstevel@tonic-gate 	 * Set the timeout and wait.
4087c478bd9Sstevel@tonic-gate 	 */
409a574db85Sraf 	cancel_pending = schedctl_cancel_pending();
410454ab202SMadhavan Venkataraman 	mutex_enter(&t->t_wait_mutex);
41187a18d3fSMadhavan Venkataraman 	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
41251b32bddSMadhavan Venkataraman 	    tim, res, flag);
4137c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
4147c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
4157c478bd9Sstevel@tonic-gate 	thread_lock(t);
4167c478bd9Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
4177c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
418454ab202SMadhavan Venkataraman 	mutex_exit(&t->t_wait_mutex);
4197c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
42087a18d3fSMadhavan Venkataraman 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
4217c478bd9Sstevel@tonic-gate 		setrun(t);
4227c478bd9Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
4237c478bd9Sstevel@tonic-gate 	swtch();
4247c478bd9Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
4257c478bd9Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	/*
4287c478bd9Sstevel@tonic-gate 	 * Untimeout the thread.  untimeout() returns -1 if the timeout has
4297c478bd9Sstevel@tonic-gate 	 * occured or the time remaining.  If the time remaining is zero,
4307c478bd9Sstevel@tonic-gate 	 * the timeout has occured between when we were awoken and
4317c478bd9Sstevel@tonic-gate 	 * we called untimeout.  We will treat this as if the timeout
4327c478bd9Sstevel@tonic-gate 	 * has occured and set rval to -1.
4337c478bd9Sstevel@tonic-gate 	 */
434454ab202SMadhavan Venkataraman 	rval = untimeout_default(id, 0);
43587a18d3fSMadhavan Venkataraman 	mutex_enter(mp);
4367c478bd9Sstevel@tonic-gate 	if (rval <= 0)
4377c478bd9Sstevel@tonic-gate 		rval = -1;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	/*
4407c478bd9Sstevel@tonic-gate 	 * Check to see if a signal is pending.  If so, regardless of whether
4417c478bd9Sstevel@tonic-gate 	 * or not we were awoken due to the signal, the signal is now pending
4427c478bd9Sstevel@tonic-gate 	 * and a return of 0 has the highest priority.
4437c478bd9Sstevel@tonic-gate 	 */
4447c478bd9Sstevel@tonic-gate out:
4457c478bd9Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
4467c478bd9Sstevel@tonic-gate 		mutex_exit(mp);
4477c478bd9Sstevel@tonic-gate 		if (issig(FORREAL))
4487c478bd9Sstevel@tonic-gate 			rval = 0;
4497c478bd9Sstevel@tonic-gate 		mutex_enter(mp);
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
4527c478bd9Sstevel@tonic-gate 		rval = 0;
453a574db85Sraf 	if (rval != 0 && cancel_pending) {
454a574db85Sraf 		schedctl_cancel_eintr();
455a574db85Sraf 		rval = 0;
456a574db85Sraf 	}
4577c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
4587c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
4597c478bd9Sstevel@tonic-gate 	if (rval <= 0 && signalled)	/* avoid consuming the cv_signal() */
4607c478bd9Sstevel@tonic-gate 		cv_signal(cvp);
4617c478bd9Sstevel@tonic-gate 	return (rval);
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate 
46487a18d3fSMadhavan Venkataraman /*
46587a18d3fSMadhavan Venkataraman  * Returns:
46687a18d3fSMadhavan Venkataraman  * 	Function result in order of precedence:
46787a18d3fSMadhavan Venkataraman  *		 0 if a signal was received
46887a18d3fSMadhavan Venkataraman  *		-1 if timeout occured
46987a18d3fSMadhavan Venkataraman  *		>0 if awakened via cv_signal() or cv_broadcast().
47087a18d3fSMadhavan Venkataraman  *		   (returns time remaining)
47187a18d3fSMadhavan Venkataraman  *
47287a18d3fSMadhavan Venkataraman  * cv_timedwait_sig() is now part of the DDI.
47387a18d3fSMadhavan Venkataraman  *
47451b32bddSMadhavan Venkataraman  * This function is now just a wrapper for cv_timedwait_sig_hires().
47587a18d3fSMadhavan Venkataraman  */
47687a18d3fSMadhavan Venkataraman clock_t
47787a18d3fSMadhavan Venkataraman cv_timedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
47887a18d3fSMadhavan Venkataraman {
47951b32bddSMadhavan Venkataraman 	hrtime_t hrtim;
48051b32bddSMadhavan Venkataraman 
481d3d50737SRafael Vanoni 	hrtim = TICK_TO_NSEC(tim - ddi_get_lbolt());
48251b32bddSMadhavan Venkataraman 	return (cv_timedwait_sig_hires(cvp, mp, hrtim, nsec_per_tick, 0));
48387a18d3fSMadhavan Venkataraman }
48487a18d3fSMadhavan Venkataraman 
485*cd1c8b85SMatthew Ahrens /*
486*cd1c8b85SMatthew Ahrens  * Wait until the specified time.
487*cd1c8b85SMatthew Ahrens  * If tim == -1, waits without timeout using cv_wait_sig_swap().
488*cd1c8b85SMatthew Ahrens  */
489*cd1c8b85SMatthew Ahrens int
490*cd1c8b85SMatthew Ahrens cv_timedwait_sig_hrtime(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim)
491*cd1c8b85SMatthew Ahrens {
492*cd1c8b85SMatthew Ahrens 	if (tim == -1) {
493*cd1c8b85SMatthew Ahrens 		return (cv_wait_sig_swap(cvp, mp));
494*cd1c8b85SMatthew Ahrens 	} else {
495*cd1c8b85SMatthew Ahrens 		return (cv_timedwait_sig_hires(cvp, mp, tim, 1,
496*cd1c8b85SMatthew Ahrens 		    CALLOUT_FLAG_ABSOLUTE | CALLOUT_FLAG_ROUNDUP));
497*cd1c8b85SMatthew Ahrens 	}
498*cd1c8b85SMatthew Ahrens }
499*cd1c8b85SMatthew Ahrens 
500d3d50737SRafael Vanoni /*
501d3d50737SRafael Vanoni  * Same as cv_timedwait_sig() except that the third argument is a relative
502d3d50737SRafael Vanoni  * timeout value, as opposed to an absolute one. There is also a fourth
503d3d50737SRafael Vanoni  * argument that specifies how accurately the timeout must be implemented.
504d3d50737SRafael Vanoni  */
505d3d50737SRafael Vanoni clock_t
506d3d50737SRafael Vanoni cv_reltimedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t delta,
507d3d50737SRafael Vanoni     time_res_t res)
508d3d50737SRafael Vanoni {
5091c4c388cSRafael Vanoni 	hrtime_t exp = 0;
510d3d50737SRafael Vanoni 
511d3d50737SRafael Vanoni 	ASSERT(TIME_RES_VALID(res));
512d3d50737SRafael Vanoni 
5131c4c388cSRafael Vanoni 	if (delta > 0) {
5141c4c388cSRafael Vanoni 		if ((exp = TICK_TO_NSEC(delta)) < 0)
5151c4c388cSRafael Vanoni 			exp = CY_INFINITY;
5161c4c388cSRafael Vanoni 	}
517d3d50737SRafael Vanoni 
518d3d50737SRafael Vanoni 	return (cv_timedwait_sig_hires(cvp, mp, exp, time_res[res], 0));
519d3d50737SRafael Vanoni }
520d3d50737SRafael Vanoni 
5217c478bd9Sstevel@tonic-gate /*
5227c478bd9Sstevel@tonic-gate  * Like cv_wait_sig_swap but allows the caller to indicate (with a
5237c478bd9Sstevel@tonic-gate  * non-NULL sigret) that they will take care of signalling the cv
5247c478bd9Sstevel@tonic-gate  * after wakeup, if necessary.  This is a vile hack that should only
5257c478bd9Sstevel@tonic-gate  * be used when no other option is available; almost all callers
5267c478bd9Sstevel@tonic-gate  * should just use cv_wait_sig_swap (which takes care of the cv_signal
5277c478bd9Sstevel@tonic-gate  * stuff automatically) instead.
5287c478bd9Sstevel@tonic-gate  */
5297c478bd9Sstevel@tonic-gate int
5307c478bd9Sstevel@tonic-gate cv_wait_sig_swap_core(kcondvar_t *cvp, kmutex_t *mp, int *sigret)
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
5337c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
5347c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
535a574db85Sraf 	int cancel_pending;
5367c478bd9Sstevel@tonic-gate 	int rval = 1;
5377c478bd9Sstevel@tonic-gate 	int signalled = 0;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	if (panicstr)
5407c478bd9Sstevel@tonic-gate 		return (rval);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/*
54335a5a358SJonathan Adams 	 * Threads in system processes don't process signals.  This is
54435a5a358SJonathan Adams 	 * true both for standard threads of system processes and for
54535a5a358SJonathan Adams 	 * interrupt threads which have borrowed their pinned thread's LWP.
5467c478bd9Sstevel@tonic-gate 	 */
54735a5a358SJonathan Adams 	if (lwp == NULL || (p->p_flag & SSYS)) {
5487c478bd9Sstevel@tonic-gate 		cv_wait(cvp, mp);
5497c478bd9Sstevel@tonic-gate 		return (rval);
5507c478bd9Sstevel@tonic-gate 	}
55135a5a358SJonathan Adams 	ASSERT(t->t_intr == NULL);
5527c478bd9Sstevel@tonic-gate 
553a574db85Sraf 	cancel_pending = schedctl_cancel_pending();
5547c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
5557c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
5567c478bd9Sstevel@tonic-gate 	thread_lock(t);
5577c478bd9Sstevel@tonic-gate 	t->t_kpri_req = 0;	/* don't need kernel priority */
5587c478bd9Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
5597c478bd9Sstevel@tonic-gate 	/* I can be swapped now */
5607c478bd9Sstevel@tonic-gate 	curthread->t_schedflag &= ~TS_DONT_SWAP;
5617c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
5627c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
563a574db85Sraf 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
5647c478bd9Sstevel@tonic-gate 		setrun(t);
5657c478bd9Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
5667c478bd9Sstevel@tonic-gate 	swtch();
5677c478bd9Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
5687c478bd9Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
5697c478bd9Sstevel@tonic-gate 	/* TS_DONT_SWAP set by disp() */
5707c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
5717c478bd9Sstevel@tonic-gate 	mutex_enter(mp);
5727c478bd9Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
5737c478bd9Sstevel@tonic-gate 		mutex_exit(mp);
5747c478bd9Sstevel@tonic-gate 		if (issig(FORREAL))
5757c478bd9Sstevel@tonic-gate 			rval = 0;
5767c478bd9Sstevel@tonic-gate 		mutex_enter(mp);
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
5797c478bd9Sstevel@tonic-gate 		rval = 0;
580a574db85Sraf 	if (rval != 0 && cancel_pending) {
581a574db85Sraf 		schedctl_cancel_eintr();
582a574db85Sraf 		rval = 0;
583a574db85Sraf 	}
5847c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
5857c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
5867c478bd9Sstevel@tonic-gate 	if (rval == 0) {
5877c478bd9Sstevel@tonic-gate 		if (sigret != NULL)
5887c478bd9Sstevel@tonic-gate 			*sigret = signalled;	/* just tell the caller */
5897c478bd9Sstevel@tonic-gate 		else if (signalled)
5907c478bd9Sstevel@tonic-gate 			cv_signal(cvp);	/* avoid consuming the cv_signal() */
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 	return (rval);
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate /*
5967c478bd9Sstevel@tonic-gate  * Same as cv_wait_sig but the thread can be swapped out while waiting.
5977c478bd9Sstevel@tonic-gate  * This should only be used when we know we aren't holding any locks.
5987c478bd9Sstevel@tonic-gate  */
5997c478bd9Sstevel@tonic-gate int
6007c478bd9Sstevel@tonic-gate cv_wait_sig_swap(kcondvar_t *cvp, kmutex_t *mp)
6017c478bd9Sstevel@tonic-gate {
6027c478bd9Sstevel@tonic-gate 	return (cv_wait_sig_swap_core(cvp, mp, NULL));
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate void
6067c478bd9Sstevel@tonic-gate cv_signal(kcondvar_t *cvp)
6077c478bd9Sstevel@tonic-gate {
6087c478bd9Sstevel@tonic-gate 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	/* make sure the cv_waiters field looks sane */
6117c478bd9Sstevel@tonic-gate 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
6127c478bd9Sstevel@tonic-gate 	if (cp->cv_waiters > 0) {
6137c478bd9Sstevel@tonic-gate 		sleepq_head_t *sqh = SQHASH(cp);
6147c478bd9Sstevel@tonic-gate 		disp_lock_enter(&sqh->sq_lock);
6157c478bd9Sstevel@tonic-gate 		ASSERT(CPU_ON_INTR(CPU) == 0);
6167c478bd9Sstevel@tonic-gate 		if (cp->cv_waiters & CV_WAITERS_MASK) {
6177c478bd9Sstevel@tonic-gate 			kthread_t *t;
6187c478bd9Sstevel@tonic-gate 			cp->cv_waiters--;
6197c478bd9Sstevel@tonic-gate 			t = sleepq_wakeone_chan(&sqh->sq_queue, cp);
6207c478bd9Sstevel@tonic-gate 			/*
6217c478bd9Sstevel@tonic-gate 			 * If cv_waiters is non-zero (and less than
6227c478bd9Sstevel@tonic-gate 			 * CV_MAX_WAITERS) there should be a thread
6237c478bd9Sstevel@tonic-gate 			 * in the queue.
6247c478bd9Sstevel@tonic-gate 			 */
6257c478bd9Sstevel@tonic-gate 			ASSERT(t != NULL);
6267c478bd9Sstevel@tonic-gate 		} else if (sleepq_wakeone_chan(&sqh->sq_queue, cp) == NULL) {
6277c478bd9Sstevel@tonic-gate 			cp->cv_waiters = 0;
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 		disp_lock_exit(&sqh->sq_lock);
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate void
6347c478bd9Sstevel@tonic-gate cv_broadcast(kcondvar_t *cvp)
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	/* make sure the cv_waiters field looks sane */
6397c478bd9Sstevel@tonic-gate 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
6407c478bd9Sstevel@tonic-gate 	if (cp->cv_waiters > 0) {
6417c478bd9Sstevel@tonic-gate 		sleepq_head_t *sqh = SQHASH(cp);
6427c478bd9Sstevel@tonic-gate 		disp_lock_enter(&sqh->sq_lock);
6437c478bd9Sstevel@tonic-gate 		ASSERT(CPU_ON_INTR(CPU) == 0);
6447c478bd9Sstevel@tonic-gate 		sleepq_wakeall_chan(&sqh->sq_queue, cp);
6457c478bd9Sstevel@tonic-gate 		cp->cv_waiters = 0;
6467c478bd9Sstevel@tonic-gate 		disp_lock_exit(&sqh->sq_lock);
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate /*
6517c478bd9Sstevel@tonic-gate  * Same as cv_wait(), but wakes up (after wakeup_time milliseconds) to check
6527c478bd9Sstevel@tonic-gate  * for requests to stop, like cv_wait_sig() but without dealing with signals.
6537c478bd9Sstevel@tonic-gate  * This is a horrible kludge.  It is evil.  It is vile.  It is swill.
6547c478bd9Sstevel@tonic-gate  * If your code has to call this function then your code is the same.
6557c478bd9Sstevel@tonic-gate  */
6567c478bd9Sstevel@tonic-gate void
6577c478bd9Sstevel@tonic-gate cv_wait_stop(kcondvar_t *cvp, kmutex_t *mp, int wakeup_time)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
6607c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
6617c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
66287a18d3fSMadhavan Venkataraman 	callout_id_t id;
6637c478bd9Sstevel@tonic-gate 	clock_t tim;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	if (panicstr)
6667c478bd9Sstevel@tonic-gate 		return;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	/*
66935a5a358SJonathan Adams 	 * Threads in system processes don't process signals.  This is
67035a5a358SJonathan Adams 	 * true both for standard threads of system processes and for
67135a5a358SJonathan Adams 	 * interrupt threads which have borrowed their pinned thread's LWP.
6727c478bd9Sstevel@tonic-gate 	 */
67335a5a358SJonathan Adams 	if (lwp == NULL || (p->p_flag & SSYS)) {
6747c478bd9Sstevel@tonic-gate 		cv_wait(cvp, mp);
6757c478bd9Sstevel@tonic-gate 		return;
6767c478bd9Sstevel@tonic-gate 	}
67735a5a358SJonathan Adams 	ASSERT(t->t_intr == NULL);
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	/*
6807c478bd9Sstevel@tonic-gate 	 * Wakeup in wakeup_time milliseconds, i.e., human time.
6817c478bd9Sstevel@tonic-gate 	 */
682d3d50737SRafael Vanoni 	tim = ddi_get_lbolt() + MSEC_TO_TICK(wakeup_time);
683454ab202SMadhavan Venkataraman 	mutex_enter(&t->t_wait_mutex);
68487a18d3fSMadhavan Venkataraman 	id = realtime_timeout_default((void (*)(void *))cv_wakeup, t,
685d3d50737SRafael Vanoni 	    tim - ddi_get_lbolt());
6867c478bd9Sstevel@tonic-gate 	thread_lock(t);			/* lock the thread */
6877c478bd9Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
6887c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
689454ab202SMadhavan Venkataraman 	mutex_exit(&t->t_wait_mutex);
6907c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
6917c478bd9Sstevel@tonic-gate 	/* ASSERT(no locks are held); */
6927c478bd9Sstevel@tonic-gate 	swtch();
693454ab202SMadhavan Venkataraman 	(void) untimeout_default(id, 0);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	/*
6968548bf79Snr 	 * Check for reasons to stop, if lwp_nostop is not true.
6977c478bd9Sstevel@tonic-gate 	 * See issig_forreal() for explanations of the various stops.
6987c478bd9Sstevel@tonic-gate 	 */
6997c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
7008548bf79Snr 	while (lwp->lwp_nostop == 0 && !(p->p_flag & SEXITLWPS)) {
7017c478bd9Sstevel@tonic-gate 		/*
7027c478bd9Sstevel@tonic-gate 		 * Hold the lwp here for watchpoint manipulation.
7037c478bd9Sstevel@tonic-gate 		 */
7048548bf79Snr 		if (t->t_proc_flag & TP_PAUSE) {
7057c478bd9Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_PAUSE);
7067c478bd9Sstevel@tonic-gate 			continue;
7077c478bd9Sstevel@tonic-gate 		}
7087c478bd9Sstevel@tonic-gate 		/*
7097c478bd9Sstevel@tonic-gate 		 * System checkpoint.
7107c478bd9Sstevel@tonic-gate 		 */
7118548bf79Snr 		if (t->t_proc_flag & TP_CHKPT) {
7127c478bd9Sstevel@tonic-gate 			stop(PR_CHECKPOINT, 0);
7137c478bd9Sstevel@tonic-gate 			continue;
7147c478bd9Sstevel@tonic-gate 		}
7157c478bd9Sstevel@tonic-gate 		/*
7167c478bd9Sstevel@tonic-gate 		 * Honor fork1(), watchpoint activity (remapping a page),
7178548bf79Snr 		 * and lwp_suspend() requests.
7187c478bd9Sstevel@tonic-gate 		 */
7198548bf79Snr 		if ((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
7208548bf79Snr 		    (t->t_proc_flag & TP_HOLDLWP)) {
7217c478bd9Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_NORMAL);
7227c478bd9Sstevel@tonic-gate 			continue;
7237c478bd9Sstevel@tonic-gate 		}
7247c478bd9Sstevel@tonic-gate 		/*
7257c478bd9Sstevel@tonic-gate 		 * Honor /proc requested stop.
7267c478bd9Sstevel@tonic-gate 		 */
7278548bf79Snr 		if (t->t_proc_flag & TP_PRSTOP) {
7287c478bd9Sstevel@tonic-gate 			stop(PR_REQUESTED, 0);
7297c478bd9Sstevel@tonic-gate 		}
7307c478bd9Sstevel@tonic-gate 		/*
7317c478bd9Sstevel@tonic-gate 		 * If some lwp in the process has already stopped
7327c478bd9Sstevel@tonic-gate 		 * showing PR_JOBCONTROL, stop in sympathy with it.
7337c478bd9Sstevel@tonic-gate 		 */
7348548bf79Snr 		if (p->p_stopsig && t != p->p_agenttp) {
7357c478bd9Sstevel@tonic-gate 			stop(PR_JOBCONTROL, p->p_stopsig);
7367c478bd9Sstevel@tonic-gate 			continue;
7377c478bd9Sstevel@tonic-gate 		}
7387c478bd9Sstevel@tonic-gate 		break;
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
7417c478bd9Sstevel@tonic-gate 	mutex_enter(mp);
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate /*
7457c478bd9Sstevel@tonic-gate  * Like cv_timedwait_sig(), but takes an absolute hires future time
7467c478bd9Sstevel@tonic-gate  * rather than a future time in clock ticks.  Will not return showing
7477c478bd9Sstevel@tonic-gate  * that a timeout occurred until the future time is passed.
7487c478bd9Sstevel@tonic-gate  * If 'when' is a NULL pointer, no timeout will occur.
7497c478bd9Sstevel@tonic-gate  * Returns:
75087a18d3fSMadhavan Venkataraman  * 	Function result in order of precedence:
7517c478bd9Sstevel@tonic-gate  *		 0 if a signal was received
7527c478bd9Sstevel@tonic-gate  *		-1 if timeout occured
7537c478bd9Sstevel@tonic-gate  *	        >0 if awakened via cv_signal() or cv_broadcast()
7547c478bd9Sstevel@tonic-gate  *		   or by a spurious wakeup.
7557c478bd9Sstevel@tonic-gate  *		   (might return time remaining)
7563348528fSdm  * As a special test, if someone abruptly resets the system time
7573348528fSdm  * (but not through adjtime(2); drifting of the clock is allowed and
7583348528fSdm  * expected [see timespectohz_adj()]), then we force a return of -1
7593348528fSdm  * so the caller can return a premature timeout to the calling process
7603348528fSdm  * so it can reevaluate the situation in light of the new system time.
7613348528fSdm  * (The system clock has been reset if timecheck != timechanged.)
762*cd1c8b85SMatthew Ahrens  *
763*cd1c8b85SMatthew Ahrens  * Generally, cv_timedwait_sig_hrtime() should be used instead of this
764*cd1c8b85SMatthew Ahrens  * routine.  It waits based on hrtime rather than wall-clock time and therefore
765*cd1c8b85SMatthew Ahrens  * does not need to deal with the time changing.
7667c478bd9Sstevel@tonic-gate  */
7677c478bd9Sstevel@tonic-gate int
7683348528fSdm cv_waituntil_sig(kcondvar_t *cvp, kmutex_t *mp,
7693348528fSdm 	timestruc_t *when, int timecheck)
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	timestruc_t now;
772b2a1c443Svb 	timestruc_t delta;
77351b32bddSMadhavan Venkataraman 	hrtime_t interval;
7747c478bd9Sstevel@tonic-gate 	int rval;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	if (when == NULL)
7777c478bd9Sstevel@tonic-gate 		return (cv_wait_sig_swap(cvp, mp));
7787c478bd9Sstevel@tonic-gate 
77944e59b5cSDonghai Qiao 	gethrestime(&now);
780b2a1c443Svb 	delta = *when;
781b2a1c443Svb 	timespecsub(&delta, &now);
782b2a1c443Svb 	if (delta.tv_sec < 0 || (delta.tv_sec == 0 && delta.tv_nsec == 0)) {
7837c478bd9Sstevel@tonic-gate 		/*
7847c478bd9Sstevel@tonic-gate 		 * We have already reached the absolute future time.
7857c478bd9Sstevel@tonic-gate 		 * Call cv_timedwait_sig() just to check for signals.
7867c478bd9Sstevel@tonic-gate 		 * We will return immediately with either 0 or -1.
7877c478bd9Sstevel@tonic-gate 		 */
78851b32bddSMadhavan Venkataraman 		rval = cv_timedwait_sig_hires(cvp, mp, 0, 1, 0);
7897c478bd9Sstevel@tonic-gate 	} else {
7903348528fSdm 		if (timecheck == timechanged) {
79151b32bddSMadhavan Venkataraman 			/*
79251b32bddSMadhavan Venkataraman 			 * Make sure that the interval is atleast one tick.
79351b32bddSMadhavan Venkataraman 			 * This is to prevent a user from flooding the system
79451b32bddSMadhavan Venkataraman 			 * with very small, high resolution timers.
79551b32bddSMadhavan Venkataraman 			 */
79651b32bddSMadhavan Venkataraman 			interval = ts2hrt(&delta);
79751b32bddSMadhavan Venkataraman 			if (interval < nsec_per_tick)
79851b32bddSMadhavan Venkataraman 				interval = nsec_per_tick;
79951b32bddSMadhavan Venkataraman 			rval = cv_timedwait_sig_hires(cvp, mp, interval, 1,
80087a18d3fSMadhavan Venkataraman 			    CALLOUT_FLAG_HRESTIME);
8013348528fSdm 		} else {
8023348528fSdm 			/*
8033348528fSdm 			 * Someone reset the system time;
8043348528fSdm 			 * just force an immediate timeout.
8053348528fSdm 			 */
8063348528fSdm 			rval = -1;
8073348528fSdm 		}
8083348528fSdm 		if (rval == -1 && timecheck == timechanged) {
8093348528fSdm 			/*
8103348528fSdm 			 * Even though cv_timedwait_sig() returned showing a
8113348528fSdm 			 * timeout, the future time may not have passed yet.
8123348528fSdm 			 * If not, change rval to indicate a normal wakeup.
8133348528fSdm 			 */
8143348528fSdm 			gethrestime(&now);
8153348528fSdm 			delta = *when;
8163348528fSdm 			timespecsub(&delta, &now);
8173348528fSdm 			if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
8183348528fSdm 			    delta.tv_nsec > 0))
8197c478bd9Sstevel@tonic-gate 				rval = 1;
8203348528fSdm 		}
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 	return (rval);
8237c478bd9Sstevel@tonic-gate }
824