xref: /illumos-gate/usr/src/uts/common/os/condvar.c (revision 87a18d3f5ba5da1985af86439cf1c94a9118b665)
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 /*
23a574db85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/thread.h>
287c478bd9Sstevel@tonic-gate #include <sys/proc.h>
297c478bd9Sstevel@tonic-gate #include <sys/debug.h>
307c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate #include <sys/sobject.h>
337c478bd9Sstevel@tonic-gate #include <sys/sleepq.h>
347c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
357c478bd9Sstevel@tonic-gate #include <sys/condvar.h>
367c478bd9Sstevel@tonic-gate #include <sys/condvar_impl.h>
377c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
387c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
397c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
40*87a18d3fSMadhavan Venkataraman #include <sys/callo.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * CV_MAX_WAITERS is the maximum number of waiters we track; once
447c478bd9Sstevel@tonic-gate  * the number becomes higher than that, we look at the sleepq to
457c478bd9Sstevel@tonic-gate  * see whether there are *really* any waiters.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate #define	CV_MAX_WAITERS		1024		/* must be power of 2 */
487c478bd9Sstevel@tonic-gate #define	CV_WAITERS_MASK		(CV_MAX_WAITERS - 1)
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Threads don't "own" condition variables.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate /* ARGSUSED */
547c478bd9Sstevel@tonic-gate static kthread_t *
557c478bd9Sstevel@tonic-gate cv_owner(void *cvp)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	return (NULL);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Unsleep a thread that's blocked on a condition variable.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate static void
647c478bd9Sstevel@tonic-gate cv_unsleep(kthread_t *t)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
677c478bd9Sstevel@tonic-gate 	sleepq_head_t *sqh = SQHASH(cvp);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	if (cvp == NULL)
72903a11ebSrh 		panic("cv_unsleep: thread %p not on sleepq %p",
73903a11ebSrh 		    (void *)t, (void *)sqh);
742db6c79fSstevel 	DTRACE_SCHED1(wakeup, kthread_t *, t);
757c478bd9Sstevel@tonic-gate 	sleepq_unsleep(t);
767c478bd9Sstevel@tonic-gate 	if (cvp->cv_waiters != CV_MAX_WAITERS)
777c478bd9Sstevel@tonic-gate 		cvp->cv_waiters--;
787c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&sqh->sq_lock);
797c478bd9Sstevel@tonic-gate 	CL_SETRUN(t);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Change the priority of a thread that's blocked on a condition variable.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate static void
867c478bd9Sstevel@tonic-gate cv_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
897c478bd9Sstevel@tonic-gate 	sleepq_t *sqp = t->t_sleepq;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
927c478bd9Sstevel@tonic-gate 	ASSERT(&SQHASH(cvp)->sq_queue == sqp);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (cvp == NULL)
95903a11ebSrh 		panic("cv_change_pri: %p not on sleep queue", (void *)t);
967c478bd9Sstevel@tonic-gate 	sleepq_dequeue(t);
977c478bd9Sstevel@tonic-gate 	*t_prip = pri;
987c478bd9Sstevel@tonic-gate 	sleepq_insert(sqp, t);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * The sobj_ops vector exports a set of functions needed when a thread
1037c478bd9Sstevel@tonic-gate  * is asleep on a synchronization object of this type.
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate static sobj_ops_t cv_sobj_ops = {
1067c478bd9Sstevel@tonic-gate 	SOBJ_CV, cv_owner, cv_unsleep, cv_change_pri
1077c478bd9Sstevel@tonic-gate };
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /* ARGSUSED */
1107c478bd9Sstevel@tonic-gate void
1117c478bd9Sstevel@tonic-gate cv_init(kcondvar_t *cvp, char *name, kcv_type_t type, void *arg)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	((condvar_impl_t *)cvp)->cv_waiters = 0;
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * cv_destroy is not currently needed, but is part of the DDI.
1187c478bd9Sstevel@tonic-gate  * This is in case cv_init ever needs to allocate something for a cv.
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate /* ARGSUSED */
1217c478bd9Sstevel@tonic-gate void
1227c478bd9Sstevel@tonic-gate cv_destroy(kcondvar_t *cvp)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	ASSERT((((condvar_impl_t *)cvp)->cv_waiters & CV_WAITERS_MASK) == 0);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * The cv_block() function blocks a thread on a condition variable
1297c478bd9Sstevel@tonic-gate  * by putting it in a hashed sleep queue associated with the
1307c478bd9Sstevel@tonic-gate  * synchronization object.
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  * Threads are taken off the hashed sleep queues via calls to
1337c478bd9Sstevel@tonic-gate  * cv_signal(), cv_broadcast(), or cv_unsleep().
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate static void
1367c478bd9Sstevel@tonic-gate cv_block(condvar_impl_t *cvp)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
1397c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
1407c478bd9Sstevel@tonic-gate 	sleepq_head_t *sqh;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1437c478bd9Sstevel@tonic-gate 	ASSERT(t != CPU->cpu_idle_thread);
1447c478bd9Sstevel@tonic-gate 	ASSERT(CPU_ON_INTR(CPU) == 0);
1457c478bd9Sstevel@tonic-gate 	ASSERT(t->t_wchan0 == NULL && t->t_wchan == NULL);
1467c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	t->t_schedflag &= ~TS_SIGNALLED;
1497c478bd9Sstevel@tonic-gate 	CL_SLEEP(t);			/* assign kernel priority */
1507c478bd9Sstevel@tonic-gate 	t->t_wchan = (caddr_t)cvp;
1517c478bd9Sstevel@tonic-gate 	t->t_sobj_ops = &cv_sobj_ops;
1527c478bd9Sstevel@tonic-gate 	DTRACE_SCHED(sleep);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/*
1557c478bd9Sstevel@tonic-gate 	 * The check for t_intr is to avoid doing the
1567c478bd9Sstevel@tonic-gate 	 * account for an interrupt thread on the still-pinned
1577c478bd9Sstevel@tonic-gate 	 * lwp's statistics.
1587c478bd9Sstevel@tonic-gate 	 */
1597c478bd9Sstevel@tonic-gate 	if (lwp != NULL && t->t_intr == NULL) {
1607c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.nvcsw++;
1617c478bd9Sstevel@tonic-gate 		(void) new_mstate(t, LMS_SLEEP);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	sqh = SQHASH(cvp);
1657c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&sqh->sq_lock);
1667c478bd9Sstevel@tonic-gate 	if (cvp->cv_waiters < CV_MAX_WAITERS)
1677c478bd9Sstevel@tonic-gate 		cvp->cv_waiters++;
1687c478bd9Sstevel@tonic-gate 	ASSERT(cvp->cv_waiters <= CV_MAX_WAITERS);
1697c478bd9Sstevel@tonic-gate 	THREAD_SLEEP(t, &sqh->sq_lock);
1707c478bd9Sstevel@tonic-gate 	sleepq_insert(&sqh->sq_queue, t);
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * THREAD_SLEEP() moves curthread->t_lockp to point to the
1737c478bd9Sstevel@tonic-gate 	 * lock sqh->sq_lock. This lock is later released by the caller
1747c478bd9Sstevel@tonic-gate 	 * when it calls thread_unlock() on curthread.
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate #define	cv_block_sig(t, cvp)	\
1797c478bd9Sstevel@tonic-gate 	{ (t)->t_flag |= T_WAKEABLE; cv_block(cvp); }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * Block on the indicated condition variable and release the
1837c478bd9Sstevel@tonic-gate  * associated kmutex while blocked.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate void
1867c478bd9Sstevel@tonic-gate cv_wait(kcondvar_t *cvp, kmutex_t *mp)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	if (panicstr)
1897c478bd9Sstevel@tonic-gate 		return;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
1927c478bd9Sstevel@tonic-gate 	thread_lock(curthread);			/* lock the thread */
1937c478bd9Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
1947c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(curthread);	/* unlock the waiters field */
1957c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
1967c478bd9Sstevel@tonic-gate 	swtch();
1977c478bd9Sstevel@tonic-gate 	mutex_enter(mp);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
200*87a18d3fSMadhavan Venkataraman static void
201*87a18d3fSMadhavan Venkataraman cv_wakeup(void *arg)
202*87a18d3fSMadhavan Venkataraman {
203*87a18d3fSMadhavan Venkataraman 	kthread_t *t = arg;
204*87a18d3fSMadhavan Venkataraman 
205*87a18d3fSMadhavan Venkataraman 	/*
206*87a18d3fSMadhavan Venkataraman 	 * This mutex is acquired and released in order to make sure that
207*87a18d3fSMadhavan Venkataraman 	 * the wakeup does not happen before the block itself happens.
208*87a18d3fSMadhavan Venkataraman 	 */
209*87a18d3fSMadhavan Venkataraman 	mutex_enter(t->t_wait_mp);
210*87a18d3fSMadhavan Venkataraman 	mutex_exit(t->t_wait_mp);
211*87a18d3fSMadhavan Venkataraman 	setrun(t);
212*87a18d3fSMadhavan Venkataraman 	t->t_wait_mp = NULL;
213*87a18d3fSMadhavan Venkataraman }
214*87a18d3fSMadhavan Venkataraman 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * Same as cv_wait except the thread will unblock at 'tim'
2177c478bd9Sstevel@tonic-gate  * (an absolute time) if it hasn't already unblocked.
2187c478bd9Sstevel@tonic-gate  *
2197c478bd9Sstevel@tonic-gate  * Returns the amount of time left from the original 'tim' value
2207c478bd9Sstevel@tonic-gate  * when it was unblocked.
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate clock_t
2237c478bd9Sstevel@tonic-gate cv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
226*87a18d3fSMadhavan Venkataraman 	callout_id_t id;
2277c478bd9Sstevel@tonic-gate 	clock_t timeleft;
2287c478bd9Sstevel@tonic-gate 	int signalled;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if (panicstr)
2317c478bd9Sstevel@tonic-gate 		return (-1);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	timeleft = tim - lbolt;
2347c478bd9Sstevel@tonic-gate 	if (timeleft <= 0)
2357c478bd9Sstevel@tonic-gate 		return (-1);
236*87a18d3fSMadhavan Venkataraman 	t->t_wait_mp = mp;
237*87a18d3fSMadhavan Venkataraman 	id = realtime_timeout_default((void (*)(void *))cv_wakeup, t, timeleft);
2387c478bd9Sstevel@tonic-gate 	thread_lock(t);		/* lock the thread */
2397c478bd9Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
2407c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
2417c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
2427c478bd9Sstevel@tonic-gate 	swtch();
2437c478bd9Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
2447c478bd9Sstevel@tonic-gate 	/*
2457c478bd9Sstevel@tonic-gate 	 * Get the time left. untimeout() returns -1 if the timeout has
2467c478bd9Sstevel@tonic-gate 	 * occured or the time remaining.  If the time remaining is zero,
2477c478bd9Sstevel@tonic-gate 	 * the timeout has occured between when we were awoken and
2487c478bd9Sstevel@tonic-gate 	 * we called untimeout.  We will treat this as if the timeout
2497c478bd9Sstevel@tonic-gate 	 * has occured and set timeleft to -1.
2507c478bd9Sstevel@tonic-gate 	 */
251*87a18d3fSMadhavan Venkataraman 	timeleft = (t->t_wait_mp == NULL) ? -1 : untimeout_default(id, 0);
2527c478bd9Sstevel@tonic-gate 	mutex_enter(mp);
2537c478bd9Sstevel@tonic-gate 	if (timeleft <= 0) {
2547c478bd9Sstevel@tonic-gate 		timeleft = -1;
2557c478bd9Sstevel@tonic-gate 		if (signalled)	/* avoid consuming the cv_signal() */
2567c478bd9Sstevel@tonic-gate 			cv_signal(cvp);
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 	return (timeleft);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate int
2627c478bd9Sstevel@tonic-gate cv_wait_sig(kcondvar_t *cvp, kmutex_t *mp)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
2657c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
2667c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
267a574db85Sraf 	int cancel_pending;
2687c478bd9Sstevel@tonic-gate 	int rval = 1;
2697c478bd9Sstevel@tonic-gate 	int signalled = 0;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if (panicstr)
2727c478bd9Sstevel@tonic-gate 		return (rval);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/*
2757c478bd9Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
2767c478bd9Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
2777c478bd9Sstevel@tonic-gate 	 */
2787c478bd9Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr) {
2797c478bd9Sstevel@tonic-gate 		cv_wait(cvp, mp);
2807c478bd9Sstevel@tonic-gate 		return (rval);
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
284a574db85Sraf 	cancel_pending = schedctl_cancel_pending();
2857c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
2867c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
2877c478bd9Sstevel@tonic-gate 	thread_lock(t);
2887c478bd9Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
2897c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
2907c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
291a574db85Sraf 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
2927c478bd9Sstevel@tonic-gate 		setrun(t);
2937c478bd9Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
2947c478bd9Sstevel@tonic-gate 	swtch();
2957c478bd9Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
2967c478bd9Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
2977c478bd9Sstevel@tonic-gate 	mutex_enter(mp);
2987c478bd9Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
2997c478bd9Sstevel@tonic-gate 		mutex_exit(mp);
3007c478bd9Sstevel@tonic-gate 		if (issig(FORREAL))
3017c478bd9Sstevel@tonic-gate 			rval = 0;
3027c478bd9Sstevel@tonic-gate 		mutex_enter(mp);
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
3057c478bd9Sstevel@tonic-gate 		rval = 0;
306a574db85Sraf 	if (rval != 0 && cancel_pending) {
307a574db85Sraf 		schedctl_cancel_eintr();
308a574db85Sraf 		rval = 0;
309a574db85Sraf 	}
3107c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
3117c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
3127c478bd9Sstevel@tonic-gate 	if (rval == 0 && signalled)	/* avoid consuming the cv_signal() */
3137c478bd9Sstevel@tonic-gate 		cv_signal(cvp);
3147c478bd9Sstevel@tonic-gate 	return (rval);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
317*87a18d3fSMadhavan Venkataraman static clock_t
318*87a18d3fSMadhavan Venkataraman cv_timedwait_sig_internal(kcondvar_t *cvp, kmutex_t *mp, clock_t tim, int flag)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
3217c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
3227c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
323a574db85Sraf 	int cancel_pending = 0;
324*87a18d3fSMadhavan Venkataraman 	callout_id_t id;
3257c478bd9Sstevel@tonic-gate 	clock_t rval = 1;
3267c478bd9Sstevel@tonic-gate 	clock_t timeleft;
3277c478bd9Sstevel@tonic-gate 	int signalled = 0;
3287c478bd9Sstevel@tonic-gate 
329*87a18d3fSMadhavan Venkataraman 	/*
330*87a18d3fSMadhavan Venkataraman 	 * If the flag is 0, then realtime_timeout() below creates a
331*87a18d3fSMadhavan Venkataraman 	 * regular realtime timeout. If the flag is CALLOUT_FLAG_HRESTIME,
332*87a18d3fSMadhavan Venkataraman 	 * then, it creates a special realtime timeout which is affected by
333*87a18d3fSMadhavan Venkataraman 	 * changes to hrestime. See callo.h for details.
334*87a18d3fSMadhavan Venkataraman 	 */
335*87a18d3fSMadhavan Venkataraman 	ASSERT((flag == 0) || (flag == CALLOUT_FLAG_HRESTIME));
3367c478bd9Sstevel@tonic-gate 	if (panicstr)
3377c478bd9Sstevel@tonic-gate 		return (rval);
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	/*
3407c478bd9Sstevel@tonic-gate 	 * If there is no lwp, then we don't need to wait for a signal.
3417c478bd9Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
3427c478bd9Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
3437c478bd9Sstevel@tonic-gate 	 */
3447c478bd9Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr)
3457c478bd9Sstevel@tonic-gate 		return (cv_timedwait(cvp, mp, tim));
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	/*
3487c478bd9Sstevel@tonic-gate 	 * If tim is less than or equal to lbolt, then the timeout
3497c478bd9Sstevel@tonic-gate 	 * has already occured.  So just check to see if there is a signal
3507c478bd9Sstevel@tonic-gate 	 * pending.  If so return 0 indicating that there is a signal pending.
3517c478bd9Sstevel@tonic-gate 	 * Else return -1 indicating that the timeout occured. No need to
3527c478bd9Sstevel@tonic-gate 	 * wait on anything.
3537c478bd9Sstevel@tonic-gate 	 */
3547c478bd9Sstevel@tonic-gate 	timeleft = tim - lbolt;
3557c478bd9Sstevel@tonic-gate 	if (timeleft <= 0) {
3567c478bd9Sstevel@tonic-gate 		lwp->lwp_asleep = 1;
3577c478bd9Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;
3587c478bd9Sstevel@tonic-gate 		rval = -1;
3597c478bd9Sstevel@tonic-gate 		goto out;
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/*
3637c478bd9Sstevel@tonic-gate 	 * Set the timeout and wait.
3647c478bd9Sstevel@tonic-gate 	 */
365a574db85Sraf 	cancel_pending = schedctl_cancel_pending();
366*87a18d3fSMadhavan Venkataraman 	t->t_wait_mp = mp;
367*87a18d3fSMadhavan Venkataraman 	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
368*87a18d3fSMadhavan Venkataraman 	    TICK_TO_NSEC(timeleft), nsec_per_tick, flag);
3697c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
3707c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
3717c478bd9Sstevel@tonic-gate 	thread_lock(t);
3727c478bd9Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
3737c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
3747c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
375*87a18d3fSMadhavan Venkataraman 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
3767c478bd9Sstevel@tonic-gate 		setrun(t);
3777c478bd9Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
3787c478bd9Sstevel@tonic-gate 	swtch();
3797c478bd9Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
3807c478bd9Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * Untimeout the thread.  untimeout() returns -1 if the timeout has
3847c478bd9Sstevel@tonic-gate 	 * occured or the time remaining.  If the time remaining is zero,
3857c478bd9Sstevel@tonic-gate 	 * the timeout has occured between when we were awoken and
3867c478bd9Sstevel@tonic-gate 	 * we called untimeout.  We will treat this as if the timeout
3877c478bd9Sstevel@tonic-gate 	 * has occured and set rval to -1.
3887c478bd9Sstevel@tonic-gate 	 */
389*87a18d3fSMadhavan Venkataraman 	rval = (t->t_wait_mp == NULL) ? -1 : untimeout_default(id, 0);
390*87a18d3fSMadhavan Venkataraman 	mutex_enter(mp);
3917c478bd9Sstevel@tonic-gate 	if (rval <= 0)
3927c478bd9Sstevel@tonic-gate 		rval = -1;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/*
3957c478bd9Sstevel@tonic-gate 	 * Check to see if a signal is pending.  If so, regardless of whether
3967c478bd9Sstevel@tonic-gate 	 * or not we were awoken due to the signal, the signal is now pending
3977c478bd9Sstevel@tonic-gate 	 * and a return of 0 has the highest priority.
3987c478bd9Sstevel@tonic-gate 	 */
3997c478bd9Sstevel@tonic-gate out:
4007c478bd9Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
4017c478bd9Sstevel@tonic-gate 		mutex_exit(mp);
4027c478bd9Sstevel@tonic-gate 		if (issig(FORREAL))
4037c478bd9Sstevel@tonic-gate 			rval = 0;
4047c478bd9Sstevel@tonic-gate 		mutex_enter(mp);
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
4077c478bd9Sstevel@tonic-gate 		rval = 0;
408a574db85Sraf 	if (rval != 0 && cancel_pending) {
409a574db85Sraf 		schedctl_cancel_eintr();
410a574db85Sraf 		rval = 0;
411a574db85Sraf 	}
4127c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
4137c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
4147c478bd9Sstevel@tonic-gate 	if (rval <= 0 && signalled)	/* avoid consuming the cv_signal() */
4157c478bd9Sstevel@tonic-gate 		cv_signal(cvp);
4167c478bd9Sstevel@tonic-gate 	return (rval);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
419*87a18d3fSMadhavan Venkataraman /*
420*87a18d3fSMadhavan Venkataraman  * Returns:
421*87a18d3fSMadhavan Venkataraman  * 	Function result in order of precedence:
422*87a18d3fSMadhavan Venkataraman  *		 0 if a signal was received
423*87a18d3fSMadhavan Venkataraman  *		-1 if timeout occured
424*87a18d3fSMadhavan Venkataraman  *		>0 if awakened via cv_signal() or cv_broadcast().
425*87a18d3fSMadhavan Venkataraman  *		   (returns time remaining)
426*87a18d3fSMadhavan Venkataraman  *
427*87a18d3fSMadhavan Venkataraman  * cv_timedwait_sig() is now part of the DDI.
428*87a18d3fSMadhavan Venkataraman  *
429*87a18d3fSMadhavan Venkataraman  * This function is now just a wrapper for cv_timedwait_sig_internal().
430*87a18d3fSMadhavan Venkataraman  */
431*87a18d3fSMadhavan Venkataraman clock_t
432*87a18d3fSMadhavan Venkataraman cv_timedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
433*87a18d3fSMadhavan Venkataraman {
434*87a18d3fSMadhavan Venkataraman 	return (cv_timedwait_sig_internal(cvp, mp, tim, 0));
435*87a18d3fSMadhavan Venkataraman }
436*87a18d3fSMadhavan Venkataraman 
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate  * Like cv_wait_sig_swap but allows the caller to indicate (with a
4397c478bd9Sstevel@tonic-gate  * non-NULL sigret) that they will take care of signalling the cv
4407c478bd9Sstevel@tonic-gate  * after wakeup, if necessary.  This is a vile hack that should only
4417c478bd9Sstevel@tonic-gate  * be used when no other option is available; almost all callers
4427c478bd9Sstevel@tonic-gate  * should just use cv_wait_sig_swap (which takes care of the cv_signal
4437c478bd9Sstevel@tonic-gate  * stuff automatically) instead.
4447c478bd9Sstevel@tonic-gate  */
4457c478bd9Sstevel@tonic-gate int
4467c478bd9Sstevel@tonic-gate cv_wait_sig_swap_core(kcondvar_t *cvp, kmutex_t *mp, int *sigret)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
4497c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
4507c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
451a574db85Sraf 	int cancel_pending;
4527c478bd9Sstevel@tonic-gate 	int rval = 1;
4537c478bd9Sstevel@tonic-gate 	int signalled = 0;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (panicstr)
4567c478bd9Sstevel@tonic-gate 		return (rval);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	/*
4597c478bd9Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
4607c478bd9Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
4617c478bd9Sstevel@tonic-gate 	 */
4627c478bd9Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr) {
4637c478bd9Sstevel@tonic-gate 		cv_wait(cvp, mp);
4647c478bd9Sstevel@tonic-gate 		return (rval);
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
467a574db85Sraf 	cancel_pending = schedctl_cancel_pending();
4687c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
4697c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
4707c478bd9Sstevel@tonic-gate 	thread_lock(t);
4717c478bd9Sstevel@tonic-gate 	t->t_kpri_req = 0;	/* don't need kernel priority */
4727c478bd9Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
4737c478bd9Sstevel@tonic-gate 	/* I can be swapped now */
4747c478bd9Sstevel@tonic-gate 	curthread->t_schedflag &= ~TS_DONT_SWAP;
4757c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
4767c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
477a574db85Sraf 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
4787c478bd9Sstevel@tonic-gate 		setrun(t);
4797c478bd9Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
4807c478bd9Sstevel@tonic-gate 	swtch();
4817c478bd9Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
4827c478bd9Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
4837c478bd9Sstevel@tonic-gate 	/* TS_DONT_SWAP set by disp() */
4847c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
4857c478bd9Sstevel@tonic-gate 	mutex_enter(mp);
4867c478bd9Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
4877c478bd9Sstevel@tonic-gate 		mutex_exit(mp);
4887c478bd9Sstevel@tonic-gate 		if (issig(FORREAL))
4897c478bd9Sstevel@tonic-gate 			rval = 0;
4907c478bd9Sstevel@tonic-gate 		mutex_enter(mp);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
4937c478bd9Sstevel@tonic-gate 		rval = 0;
494a574db85Sraf 	if (rval != 0 && cancel_pending) {
495a574db85Sraf 		schedctl_cancel_eintr();
496a574db85Sraf 		rval = 0;
497a574db85Sraf 	}
4987c478bd9Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
4997c478bd9Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
5007c478bd9Sstevel@tonic-gate 	if (rval == 0) {
5017c478bd9Sstevel@tonic-gate 		if (sigret != NULL)
5027c478bd9Sstevel@tonic-gate 			*sigret = signalled;	/* just tell the caller */
5037c478bd9Sstevel@tonic-gate 		else if (signalled)
5047c478bd9Sstevel@tonic-gate 			cv_signal(cvp);	/* avoid consuming the cv_signal() */
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 	return (rval);
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate /*
5107c478bd9Sstevel@tonic-gate  * Same as cv_wait_sig but the thread can be swapped out while waiting.
5117c478bd9Sstevel@tonic-gate  * This should only be used when we know we aren't holding any locks.
5127c478bd9Sstevel@tonic-gate  */
5137c478bd9Sstevel@tonic-gate int
5147c478bd9Sstevel@tonic-gate cv_wait_sig_swap(kcondvar_t *cvp, kmutex_t *mp)
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate 	return (cv_wait_sig_swap_core(cvp, mp, NULL));
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate void
5207c478bd9Sstevel@tonic-gate cv_signal(kcondvar_t *cvp)
5217c478bd9Sstevel@tonic-gate {
5227c478bd9Sstevel@tonic-gate 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/* make sure the cv_waiters field looks sane */
5257c478bd9Sstevel@tonic-gate 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
5267c478bd9Sstevel@tonic-gate 	if (cp->cv_waiters > 0) {
5277c478bd9Sstevel@tonic-gate 		sleepq_head_t *sqh = SQHASH(cp);
5287c478bd9Sstevel@tonic-gate 		disp_lock_enter(&sqh->sq_lock);
5297c478bd9Sstevel@tonic-gate 		ASSERT(CPU_ON_INTR(CPU) == 0);
5307c478bd9Sstevel@tonic-gate 		if (cp->cv_waiters & CV_WAITERS_MASK) {
5317c478bd9Sstevel@tonic-gate 			kthread_t *t;
5327c478bd9Sstevel@tonic-gate 			cp->cv_waiters--;
5337c478bd9Sstevel@tonic-gate 			t = sleepq_wakeone_chan(&sqh->sq_queue, cp);
5347c478bd9Sstevel@tonic-gate 			/*
5357c478bd9Sstevel@tonic-gate 			 * If cv_waiters is non-zero (and less than
5367c478bd9Sstevel@tonic-gate 			 * CV_MAX_WAITERS) there should be a thread
5377c478bd9Sstevel@tonic-gate 			 * in the queue.
5387c478bd9Sstevel@tonic-gate 			 */
5397c478bd9Sstevel@tonic-gate 			ASSERT(t != NULL);
5407c478bd9Sstevel@tonic-gate 		} else if (sleepq_wakeone_chan(&sqh->sq_queue, cp) == NULL) {
5417c478bd9Sstevel@tonic-gate 			cp->cv_waiters = 0;
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 		disp_lock_exit(&sqh->sq_lock);
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate void
5487c478bd9Sstevel@tonic-gate cv_broadcast(kcondvar_t *cvp)
5497c478bd9Sstevel@tonic-gate {
5507c478bd9Sstevel@tonic-gate 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/* make sure the cv_waiters field looks sane */
5537c478bd9Sstevel@tonic-gate 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
5547c478bd9Sstevel@tonic-gate 	if (cp->cv_waiters > 0) {
5557c478bd9Sstevel@tonic-gate 		sleepq_head_t *sqh = SQHASH(cp);
5567c478bd9Sstevel@tonic-gate 		disp_lock_enter(&sqh->sq_lock);
5577c478bd9Sstevel@tonic-gate 		ASSERT(CPU_ON_INTR(CPU) == 0);
5587c478bd9Sstevel@tonic-gate 		sleepq_wakeall_chan(&sqh->sq_queue, cp);
5597c478bd9Sstevel@tonic-gate 		cp->cv_waiters = 0;
5607c478bd9Sstevel@tonic-gate 		disp_lock_exit(&sqh->sq_lock);
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate  * Same as cv_wait(), but wakes up (after wakeup_time milliseconds) to check
5667c478bd9Sstevel@tonic-gate  * for requests to stop, like cv_wait_sig() but without dealing with signals.
5677c478bd9Sstevel@tonic-gate  * This is a horrible kludge.  It is evil.  It is vile.  It is swill.
5687c478bd9Sstevel@tonic-gate  * If your code has to call this function then your code is the same.
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate void
5717c478bd9Sstevel@tonic-gate cv_wait_stop(kcondvar_t *cvp, kmutex_t *mp, int wakeup_time)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
5747c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
5757c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
576*87a18d3fSMadhavan Venkataraman 	callout_id_t id;
5777c478bd9Sstevel@tonic-gate 	clock_t tim;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if (panicstr)
5807c478bd9Sstevel@tonic-gate 		return;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	/*
5837c478bd9Sstevel@tonic-gate 	 * If there is no lwp, then we don't need to eventually stop it
5847c478bd9Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
5857c478bd9Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
5867c478bd9Sstevel@tonic-gate 	 */
5877c478bd9Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr) {
5887c478bd9Sstevel@tonic-gate 		cv_wait(cvp, mp);
5897c478bd9Sstevel@tonic-gate 		return;
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * Wakeup in wakeup_time milliseconds, i.e., human time.
5947c478bd9Sstevel@tonic-gate 	 */
5957c478bd9Sstevel@tonic-gate 	tim = lbolt + MSEC_TO_TICK(wakeup_time);
596*87a18d3fSMadhavan Venkataraman 	t->t_wait_mp = mp;
597*87a18d3fSMadhavan Venkataraman 	id = realtime_timeout_default((void (*)(void *))cv_wakeup, t,
598*87a18d3fSMadhavan Venkataraman 	    tim - lbolt);
5997c478bd9Sstevel@tonic-gate 	thread_lock(t);			/* lock the thread */
6007c478bd9Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
6017c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
6027c478bd9Sstevel@tonic-gate 	mutex_exit(mp);
6037c478bd9Sstevel@tonic-gate 	/* ASSERT(no locks are held); */
6047c478bd9Sstevel@tonic-gate 	swtch();
605*87a18d3fSMadhavan Venkataraman 	if (t->t_wait_mp != NULL)
606*87a18d3fSMadhavan Venkataraman 		(void) untimeout_default(id, 0);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/*
6098548bf79Snr 	 * Check for reasons to stop, if lwp_nostop is not true.
6107c478bd9Sstevel@tonic-gate 	 * See issig_forreal() for explanations of the various stops.
6117c478bd9Sstevel@tonic-gate 	 */
6127c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
6138548bf79Snr 	while (lwp->lwp_nostop == 0 && !(p->p_flag & SEXITLWPS)) {
6147c478bd9Sstevel@tonic-gate 		/*
6157c478bd9Sstevel@tonic-gate 		 * Hold the lwp here for watchpoint manipulation.
6167c478bd9Sstevel@tonic-gate 		 */
6178548bf79Snr 		if (t->t_proc_flag & TP_PAUSE) {
6187c478bd9Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_PAUSE);
6197c478bd9Sstevel@tonic-gate 			continue;
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 		/*
6227c478bd9Sstevel@tonic-gate 		 * System checkpoint.
6237c478bd9Sstevel@tonic-gate 		 */
6248548bf79Snr 		if (t->t_proc_flag & TP_CHKPT) {
6257c478bd9Sstevel@tonic-gate 			stop(PR_CHECKPOINT, 0);
6267c478bd9Sstevel@tonic-gate 			continue;
6277c478bd9Sstevel@tonic-gate 		}
6287c478bd9Sstevel@tonic-gate 		/*
6297c478bd9Sstevel@tonic-gate 		 * Honor fork1(), watchpoint activity (remapping a page),
6308548bf79Snr 		 * and lwp_suspend() requests.
6317c478bd9Sstevel@tonic-gate 		 */
6328548bf79Snr 		if ((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
6338548bf79Snr 		    (t->t_proc_flag & TP_HOLDLWP)) {
6347c478bd9Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_NORMAL);
6357c478bd9Sstevel@tonic-gate 			continue;
6367c478bd9Sstevel@tonic-gate 		}
6377c478bd9Sstevel@tonic-gate 		/*
6387c478bd9Sstevel@tonic-gate 		 * Honor /proc requested stop.
6397c478bd9Sstevel@tonic-gate 		 */
6408548bf79Snr 		if (t->t_proc_flag & TP_PRSTOP) {
6417c478bd9Sstevel@tonic-gate 			stop(PR_REQUESTED, 0);
6427c478bd9Sstevel@tonic-gate 		}
6437c478bd9Sstevel@tonic-gate 		/*
6447c478bd9Sstevel@tonic-gate 		 * If some lwp in the process has already stopped
6457c478bd9Sstevel@tonic-gate 		 * showing PR_JOBCONTROL, stop in sympathy with it.
6467c478bd9Sstevel@tonic-gate 		 */
6478548bf79Snr 		if (p->p_stopsig && t != p->p_agenttp) {
6487c478bd9Sstevel@tonic-gate 			stop(PR_JOBCONTROL, p->p_stopsig);
6497c478bd9Sstevel@tonic-gate 			continue;
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 		break;
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
6547c478bd9Sstevel@tonic-gate 	mutex_enter(mp);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate /*
6587c478bd9Sstevel@tonic-gate  * Like cv_timedwait_sig(), but takes an absolute hires future time
6597c478bd9Sstevel@tonic-gate  * rather than a future time in clock ticks.  Will not return showing
6607c478bd9Sstevel@tonic-gate  * that a timeout occurred until the future time is passed.
6617c478bd9Sstevel@tonic-gate  * If 'when' is a NULL pointer, no timeout will occur.
6627c478bd9Sstevel@tonic-gate  * Returns:
663*87a18d3fSMadhavan Venkataraman  * 	Function result in order of precedence:
6647c478bd9Sstevel@tonic-gate  *		 0 if a signal was received
6657c478bd9Sstevel@tonic-gate  *		-1 if timeout occured
6667c478bd9Sstevel@tonic-gate  *	        >0 if awakened via cv_signal() or cv_broadcast()
6677c478bd9Sstevel@tonic-gate  *		   or by a spurious wakeup.
6687c478bd9Sstevel@tonic-gate  *		   (might return time remaining)
6693348528fSdm  * As a special test, if someone abruptly resets the system time
6703348528fSdm  * (but not through adjtime(2); drifting of the clock is allowed and
6713348528fSdm  * expected [see timespectohz_adj()]), then we force a return of -1
6723348528fSdm  * so the caller can return a premature timeout to the calling process
6733348528fSdm  * so it can reevaluate the situation in light of the new system time.
6743348528fSdm  * (The system clock has been reset if timecheck != timechanged.)
6757c478bd9Sstevel@tonic-gate  */
6767c478bd9Sstevel@tonic-gate int
6773348528fSdm cv_waituntil_sig(kcondvar_t *cvp, kmutex_t *mp,
6783348528fSdm 	timestruc_t *when, int timecheck)
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate 	timestruc_t now;
681b2a1c443Svb 	timestruc_t delta;
6827c478bd9Sstevel@tonic-gate 	int rval;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (when == NULL)
6857c478bd9Sstevel@tonic-gate 		return (cv_wait_sig_swap(cvp, mp));
6867c478bd9Sstevel@tonic-gate 
68744e59b5cSDonghai Qiao 	gethrestime(&now);
688b2a1c443Svb 	delta = *when;
689b2a1c443Svb 	timespecsub(&delta, &now);
690b2a1c443Svb 	if (delta.tv_sec < 0 || (delta.tv_sec == 0 && delta.tv_nsec == 0)) {
6917c478bd9Sstevel@tonic-gate 		/*
6927c478bd9Sstevel@tonic-gate 		 * We have already reached the absolute future time.
6937c478bd9Sstevel@tonic-gate 		 * Call cv_timedwait_sig() just to check for signals.
6947c478bd9Sstevel@tonic-gate 		 * We will return immediately with either 0 or -1.
6957c478bd9Sstevel@tonic-gate 		 */
6967c478bd9Sstevel@tonic-gate 		rval = cv_timedwait_sig(cvp, mp, lbolt);
6977c478bd9Sstevel@tonic-gate 	} else {
69844e59b5cSDonghai Qiao 		gethrestime_lasttick(&now);
6993348528fSdm 		if (timecheck == timechanged) {
700*87a18d3fSMadhavan Venkataraman 			rval = cv_timedwait_sig_internal(cvp, mp,
701*87a18d3fSMadhavan Venkataraman 			    lbolt + timespectohz(when, now),
702*87a18d3fSMadhavan Venkataraman 			    CALLOUT_FLAG_HRESTIME);
703f635d46aSqiao 
7043348528fSdm 		} else {
7053348528fSdm 			/*
7063348528fSdm 			 * Someone reset the system time;
7073348528fSdm 			 * just force an immediate timeout.
7083348528fSdm 			 */
7093348528fSdm 			rval = -1;
7103348528fSdm 		}
7113348528fSdm 		if (rval == -1 && timecheck == timechanged) {
7123348528fSdm 			/*
7133348528fSdm 			 * Even though cv_timedwait_sig() returned showing a
7143348528fSdm 			 * timeout, the future time may not have passed yet.
7153348528fSdm 			 * If not, change rval to indicate a normal wakeup.
7163348528fSdm 			 */
7173348528fSdm 			gethrestime(&now);
7183348528fSdm 			delta = *when;
7193348528fSdm 			timespecsub(&delta, &now);
7203348528fSdm 			if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
7213348528fSdm 			    delta.tv_nsec > 0))
7227c478bd9Sstevel@tonic-gate 				rval = 1;
7233348528fSdm 		}
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 	return (rval);
7267c478bd9Sstevel@tonic-gate }
727