xref: /onnv-gate/usr/src/uts/common/os/condvar.c (revision 11066:cebb50cbe4f9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52498Sstevel  * Common Development and Distribution License (the "License").
62498Sstevel  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
215891Sraf 
220Sstevel@tonic-gate /*
238566SMadhavan.Venkataraman@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/thread.h>
280Sstevel@tonic-gate #include <sys/proc.h>
290Sstevel@tonic-gate #include <sys/debug.h>
300Sstevel@tonic-gate #include <sys/cmn_err.h>
310Sstevel@tonic-gate #include <sys/systm.h>
320Sstevel@tonic-gate #include <sys/sobject.h>
330Sstevel@tonic-gate #include <sys/sleepq.h>
340Sstevel@tonic-gate #include <sys/cpuvar.h>
350Sstevel@tonic-gate #include <sys/condvar.h>
360Sstevel@tonic-gate #include <sys/condvar_impl.h>
370Sstevel@tonic-gate #include <sys/schedctl.h>
380Sstevel@tonic-gate #include <sys/procfs.h>
390Sstevel@tonic-gate #include <sys/sdt.h>
408048SMadhavan.Venkataraman@Sun.COM #include <sys/callo.h>
410Sstevel@tonic-gate 
429334SMadhavan.Venkataraman@Sun.COM clock_t cv_timedwait_hires(kcondvar_t *, kmutex_t *, hrtime_t, hrtime_t, int);
439334SMadhavan.Venkataraman@Sun.COM 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * CV_MAX_WAITERS is the maximum number of waiters we track; once
460Sstevel@tonic-gate  * the number becomes higher than that, we look at the sleepq to
470Sstevel@tonic-gate  * see whether there are *really* any waiters.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate #define	CV_MAX_WAITERS		1024		/* must be power of 2 */
500Sstevel@tonic-gate #define	CV_WAITERS_MASK		(CV_MAX_WAITERS - 1)
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * Threads don't "own" condition variables.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate /* ARGSUSED */
560Sstevel@tonic-gate static kthread_t *
570Sstevel@tonic-gate cv_owner(void *cvp)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	return (NULL);
600Sstevel@tonic-gate }
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * Unsleep a thread that's blocked on a condition variable.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate static void
660Sstevel@tonic-gate cv_unsleep(kthread_t *t)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
690Sstevel@tonic-gate 	sleepq_head_t *sqh = SQHASH(cvp);
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if (cvp == NULL)
747240Srh87107 		panic("cv_unsleep: thread %p not on sleepq %p",
757240Srh87107 		    (void *)t, (void *)sqh);
762498Sstevel 	DTRACE_SCHED1(wakeup, kthread_t *, t);
770Sstevel@tonic-gate 	sleepq_unsleep(t);
780Sstevel@tonic-gate 	if (cvp->cv_waiters != CV_MAX_WAITERS)
790Sstevel@tonic-gate 		cvp->cv_waiters--;
800Sstevel@tonic-gate 	disp_lock_exit_high(&sqh->sq_lock);
810Sstevel@tonic-gate 	CL_SETRUN(t);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * Change the priority of a thread that's blocked on a condition variable.
860Sstevel@tonic-gate  */
870Sstevel@tonic-gate static void
880Sstevel@tonic-gate cv_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
910Sstevel@tonic-gate 	sleepq_t *sqp = t->t_sleepq;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
940Sstevel@tonic-gate 	ASSERT(&SQHASH(cvp)->sq_queue == sqp);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if (cvp == NULL)
977240Srh87107 		panic("cv_change_pri: %p not on sleep queue", (void *)t);
980Sstevel@tonic-gate 	sleepq_dequeue(t);
990Sstevel@tonic-gate 	*t_prip = pri;
1000Sstevel@tonic-gate 	sleepq_insert(sqp, t);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * The sobj_ops vector exports a set of functions needed when a thread
1050Sstevel@tonic-gate  * is asleep on a synchronization object of this type.
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate static sobj_ops_t cv_sobj_ops = {
1080Sstevel@tonic-gate 	SOBJ_CV, cv_owner, cv_unsleep, cv_change_pri
1090Sstevel@tonic-gate };
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /* ARGSUSED */
1120Sstevel@tonic-gate void
1130Sstevel@tonic-gate cv_init(kcondvar_t *cvp, char *name, kcv_type_t type, void *arg)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	((condvar_impl_t *)cvp)->cv_waiters = 0;
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * cv_destroy is not currently needed, but is part of the DDI.
1200Sstevel@tonic-gate  * This is in case cv_init ever needs to allocate something for a cv.
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate /* ARGSUSED */
1230Sstevel@tonic-gate void
1240Sstevel@tonic-gate cv_destroy(kcondvar_t *cvp)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate 	ASSERT((((condvar_impl_t *)cvp)->cv_waiters & CV_WAITERS_MASK) == 0);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate  * The cv_block() function blocks a thread on a condition variable
1310Sstevel@tonic-gate  * by putting it in a hashed sleep queue associated with the
1320Sstevel@tonic-gate  * synchronization object.
1330Sstevel@tonic-gate  *
1340Sstevel@tonic-gate  * Threads are taken off the hashed sleep queues via calls to
1350Sstevel@tonic-gate  * cv_signal(), cv_broadcast(), or cv_unsleep().
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate static void
1380Sstevel@tonic-gate cv_block(condvar_impl_t *cvp)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	kthread_t *t = curthread;
1410Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
1420Sstevel@tonic-gate 	sleepq_head_t *sqh;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1450Sstevel@tonic-gate 	ASSERT(t != CPU->cpu_idle_thread);
1460Sstevel@tonic-gate 	ASSERT(CPU_ON_INTR(CPU) == 0);
1470Sstevel@tonic-gate 	ASSERT(t->t_wchan0 == NULL && t->t_wchan == NULL);
1480Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	t->t_schedflag &= ~TS_SIGNALLED;
1510Sstevel@tonic-gate 	CL_SLEEP(t);			/* assign kernel priority */
1520Sstevel@tonic-gate 	t->t_wchan = (caddr_t)cvp;
1530Sstevel@tonic-gate 	t->t_sobj_ops = &cv_sobj_ops;
1540Sstevel@tonic-gate 	DTRACE_SCHED(sleep);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	/*
1570Sstevel@tonic-gate 	 * The check for t_intr is to avoid doing the
1580Sstevel@tonic-gate 	 * account for an interrupt thread on the still-pinned
1590Sstevel@tonic-gate 	 * lwp's statistics.
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 	if (lwp != NULL && t->t_intr == NULL) {
1620Sstevel@tonic-gate 		lwp->lwp_ru.nvcsw++;
1630Sstevel@tonic-gate 		(void) new_mstate(t, LMS_SLEEP);
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	sqh = SQHASH(cvp);
1670Sstevel@tonic-gate 	disp_lock_enter_high(&sqh->sq_lock);
1680Sstevel@tonic-gate 	if (cvp->cv_waiters < CV_MAX_WAITERS)
1690Sstevel@tonic-gate 		cvp->cv_waiters++;
1700Sstevel@tonic-gate 	ASSERT(cvp->cv_waiters <= CV_MAX_WAITERS);
1710Sstevel@tonic-gate 	THREAD_SLEEP(t, &sqh->sq_lock);
1720Sstevel@tonic-gate 	sleepq_insert(&sqh->sq_queue, t);
1730Sstevel@tonic-gate 	/*
1740Sstevel@tonic-gate 	 * THREAD_SLEEP() moves curthread->t_lockp to point to the
1750Sstevel@tonic-gate 	 * lock sqh->sq_lock. This lock is later released by the caller
1760Sstevel@tonic-gate 	 * when it calls thread_unlock() on curthread.
1770Sstevel@tonic-gate 	 */
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate #define	cv_block_sig(t, cvp)	\
1810Sstevel@tonic-gate 	{ (t)->t_flag |= T_WAKEABLE; cv_block(cvp); }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate  * Block on the indicated condition variable and release the
1850Sstevel@tonic-gate  * associated kmutex while blocked.
1860Sstevel@tonic-gate  */
1870Sstevel@tonic-gate void
1880Sstevel@tonic-gate cv_wait(kcondvar_t *cvp, kmutex_t *mp)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate 	if (panicstr)
1910Sstevel@tonic-gate 		return;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
1940Sstevel@tonic-gate 	thread_lock(curthread);			/* lock the thread */
1950Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
1960Sstevel@tonic-gate 	thread_unlock_nopreempt(curthread);	/* unlock the waiters field */
1970Sstevel@tonic-gate 	mutex_exit(mp);
1980Sstevel@tonic-gate 	swtch();
1990Sstevel@tonic-gate 	mutex_enter(mp);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2028048SMadhavan.Venkataraman@Sun.COM static void
2038048SMadhavan.Venkataraman@Sun.COM cv_wakeup(void *arg)
2048048SMadhavan.Venkataraman@Sun.COM {
2058048SMadhavan.Venkataraman@Sun.COM 	kthread_t *t = arg;
2068048SMadhavan.Venkataraman@Sun.COM 
2078048SMadhavan.Venkataraman@Sun.COM 	/*
2088048SMadhavan.Venkataraman@Sun.COM 	 * This mutex is acquired and released in order to make sure that
2098048SMadhavan.Venkataraman@Sun.COM 	 * the wakeup does not happen before the block itself happens.
2108048SMadhavan.Venkataraman@Sun.COM 	 */
2118566SMadhavan.Venkataraman@Sun.COM 	mutex_enter(&t->t_wait_mutex);
2128566SMadhavan.Venkataraman@Sun.COM 	mutex_exit(&t->t_wait_mutex);
2138048SMadhavan.Venkataraman@Sun.COM 	setrun(t);
2148048SMadhavan.Venkataraman@Sun.COM }
2158048SMadhavan.Venkataraman@Sun.COM 
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate  * Same as cv_wait except the thread will unblock at 'tim'
2180Sstevel@tonic-gate  * (an absolute time) if it hasn't already unblocked.
2190Sstevel@tonic-gate  *
2200Sstevel@tonic-gate  * Returns the amount of time left from the original 'tim' value
2210Sstevel@tonic-gate  * when it was unblocked.
2220Sstevel@tonic-gate  */
2230Sstevel@tonic-gate clock_t
2240Sstevel@tonic-gate cv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
2250Sstevel@tonic-gate {
2269334SMadhavan.Venkataraman@Sun.COM 	hrtime_t hrtim;
227*11066Srafael.vanoni@sun.com 	clock_t now = ddi_get_lbolt();
2289334SMadhavan.Venkataraman@Sun.COM 
229*11066Srafael.vanoni@sun.com 	if (tim <= now)
2309334SMadhavan.Venkataraman@Sun.COM 		return (-1);
2319334SMadhavan.Venkataraman@Sun.COM 
232*11066Srafael.vanoni@sun.com 	hrtim = TICK_TO_NSEC(tim - now);
2339334SMadhavan.Venkataraman@Sun.COM 	return (cv_timedwait_hires(cvp, mp, hrtim, nsec_per_tick, 0));
2349334SMadhavan.Venkataraman@Sun.COM }
2359334SMadhavan.Venkataraman@Sun.COM 
236*11066Srafael.vanoni@sun.com /*
237*11066Srafael.vanoni@sun.com  * Same as cv_timedwait() except that the third argument is a relative
238*11066Srafael.vanoni@sun.com  * timeout value, as opposed to an absolute one. There is also a fourth
239*11066Srafael.vanoni@sun.com  * argument that specifies how accurately the timeout must be implemented.
240*11066Srafael.vanoni@sun.com  */
241*11066Srafael.vanoni@sun.com clock_t
242*11066Srafael.vanoni@sun.com cv_reltimedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t delta, time_res_t res)
243*11066Srafael.vanoni@sun.com {
244*11066Srafael.vanoni@sun.com 	hrtime_t exp;
245*11066Srafael.vanoni@sun.com 
246*11066Srafael.vanoni@sun.com 	ASSERT(TIME_RES_VALID(res));
247*11066Srafael.vanoni@sun.com 
248*11066Srafael.vanoni@sun.com 	if (delta <= 0)
249*11066Srafael.vanoni@sun.com 		return (-1);
250*11066Srafael.vanoni@sun.com 
251*11066Srafael.vanoni@sun.com 	if ((exp = TICK_TO_NSEC(delta)) < 0)
252*11066Srafael.vanoni@sun.com 		exp = CY_INFINITY;
253*11066Srafael.vanoni@sun.com 
254*11066Srafael.vanoni@sun.com 	return (cv_timedwait_hires(cvp, mp, exp, time_res[res], 0));
255*11066Srafael.vanoni@sun.com }
256*11066Srafael.vanoni@sun.com 
2579334SMadhavan.Venkataraman@Sun.COM clock_t
2589334SMadhavan.Venkataraman@Sun.COM cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
2599334SMadhavan.Venkataraman@Sun.COM     hrtime_t res, int flag)
2609334SMadhavan.Venkataraman@Sun.COM {
2610Sstevel@tonic-gate 	kthread_t *t = curthread;
2628048SMadhavan.Venkataraman@Sun.COM 	callout_id_t id;
2630Sstevel@tonic-gate 	clock_t timeleft;
2649334SMadhavan.Venkataraman@Sun.COM 	hrtime_t limit;
2650Sstevel@tonic-gate 	int signalled;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	if (panicstr)
2680Sstevel@tonic-gate 		return (-1);
2690Sstevel@tonic-gate 
2709334SMadhavan.Venkataraman@Sun.COM 	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
2719334SMadhavan.Venkataraman@Sun.COM 	if (tim <= limit)
2720Sstevel@tonic-gate 		return (-1);
2738566SMadhavan.Venkataraman@Sun.COM 	mutex_enter(&t->t_wait_mutex);
2749334SMadhavan.Venkataraman@Sun.COM 	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
2759334SMadhavan.Venkataraman@Sun.COM 	    tim, res, flag);
2760Sstevel@tonic-gate 	thread_lock(t);		/* lock the thread */
2770Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
2780Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
2798566SMadhavan.Venkataraman@Sun.COM 	mutex_exit(&t->t_wait_mutex);
2800Sstevel@tonic-gate 	mutex_exit(mp);
2810Sstevel@tonic-gate 	swtch();
2820Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
2830Sstevel@tonic-gate 	/*
2840Sstevel@tonic-gate 	 * Get the time left. untimeout() returns -1 if the timeout has
2850Sstevel@tonic-gate 	 * occured or the time remaining.  If the time remaining is zero,
2860Sstevel@tonic-gate 	 * the timeout has occured between when we were awoken and
2870Sstevel@tonic-gate 	 * we called untimeout.  We will treat this as if the timeout
2880Sstevel@tonic-gate 	 * has occured and set timeleft to -1.
2890Sstevel@tonic-gate 	 */
2908566SMadhavan.Venkataraman@Sun.COM 	timeleft = untimeout_default(id, 0);
2910Sstevel@tonic-gate 	mutex_enter(mp);
2920Sstevel@tonic-gate 	if (timeleft <= 0) {
2930Sstevel@tonic-gate 		timeleft = -1;
2940Sstevel@tonic-gate 		if (signalled)	/* avoid consuming the cv_signal() */
2950Sstevel@tonic-gate 			cv_signal(cvp);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 	return (timeleft);
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate int
3010Sstevel@tonic-gate cv_wait_sig(kcondvar_t *cvp, kmutex_t *mp)
3020Sstevel@tonic-gate {
3030Sstevel@tonic-gate 	kthread_t *t = curthread;
3040Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
3050Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
3065891Sraf 	int cancel_pending;
3070Sstevel@tonic-gate 	int rval = 1;
3080Sstevel@tonic-gate 	int signalled = 0;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if (panicstr)
3110Sstevel@tonic-gate 		return (rval);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	/*
3140Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
3150Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
3160Sstevel@tonic-gate 	 */
3170Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr) {
3180Sstevel@tonic-gate 		cv_wait(cvp, mp);
3190Sstevel@tonic-gate 		return (rval);
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
3235891Sraf 	cancel_pending = schedctl_cancel_pending();
3240Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
3250Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
3260Sstevel@tonic-gate 	thread_lock(t);
3270Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
3280Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
3290Sstevel@tonic-gate 	mutex_exit(mp);
3305891Sraf 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
3310Sstevel@tonic-gate 		setrun(t);
3320Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
3330Sstevel@tonic-gate 	swtch();
3340Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
3350Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
3360Sstevel@tonic-gate 	mutex_enter(mp);
3370Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
3380Sstevel@tonic-gate 		mutex_exit(mp);
3390Sstevel@tonic-gate 		if (issig(FORREAL))
3400Sstevel@tonic-gate 			rval = 0;
3410Sstevel@tonic-gate 		mutex_enter(mp);
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
3440Sstevel@tonic-gate 		rval = 0;
3455891Sraf 	if (rval != 0 && cancel_pending) {
3465891Sraf 		schedctl_cancel_eintr();
3475891Sraf 		rval = 0;
3485891Sraf 	}
3490Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
3500Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
3510Sstevel@tonic-gate 	if (rval == 0 && signalled)	/* avoid consuming the cv_signal() */
3520Sstevel@tonic-gate 		cv_signal(cvp);
3530Sstevel@tonic-gate 	return (rval);
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate 
3568048SMadhavan.Venkataraman@Sun.COM static clock_t
3579334SMadhavan.Venkataraman@Sun.COM cv_timedwait_sig_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
3589334SMadhavan.Venkataraman@Sun.COM     hrtime_t res, int flag)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	kthread_t *t = curthread;
3610Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
3620Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
3635891Sraf 	int cancel_pending = 0;
3648048SMadhavan.Venkataraman@Sun.COM 	callout_id_t id;
3650Sstevel@tonic-gate 	clock_t rval = 1;
3669334SMadhavan.Venkataraman@Sun.COM 	hrtime_t limit;
3670Sstevel@tonic-gate 	int signalled = 0;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	if (panicstr)
3700Sstevel@tonic-gate 		return (rval);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	/*
3730Sstevel@tonic-gate 	 * If there is no lwp, then we don't need to wait for a signal.
3740Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
3750Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
3760Sstevel@tonic-gate 	 */
3770Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr)
3789334SMadhavan.Venkataraman@Sun.COM 		return (cv_timedwait_hires(cvp, mp, tim, res, flag));
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	/*
3819334SMadhavan.Venkataraman@Sun.COM 	 * If tim is less than or equal to current hrtime, then the timeout
3820Sstevel@tonic-gate 	 * has already occured.  So just check to see if there is a signal
3830Sstevel@tonic-gate 	 * pending.  If so return 0 indicating that there is a signal pending.
3840Sstevel@tonic-gate 	 * Else return -1 indicating that the timeout occured. No need to
3850Sstevel@tonic-gate 	 * wait on anything.
3860Sstevel@tonic-gate 	 */
3879334SMadhavan.Venkataraman@Sun.COM 	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
3889334SMadhavan.Venkataraman@Sun.COM 	if (tim <= limit) {
3890Sstevel@tonic-gate 		lwp->lwp_asleep = 1;
3900Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;
3910Sstevel@tonic-gate 		rval = -1;
3920Sstevel@tonic-gate 		goto out;
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	/*
3960Sstevel@tonic-gate 	 * Set the timeout and wait.
3970Sstevel@tonic-gate 	 */
3985891Sraf 	cancel_pending = schedctl_cancel_pending();
3998566SMadhavan.Venkataraman@Sun.COM 	mutex_enter(&t->t_wait_mutex);
4008048SMadhavan.Venkataraman@Sun.COM 	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
4019334SMadhavan.Venkataraman@Sun.COM 	    tim, res, flag);
4020Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
4030Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
4040Sstevel@tonic-gate 	thread_lock(t);
4050Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
4060Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
4078566SMadhavan.Venkataraman@Sun.COM 	mutex_exit(&t->t_wait_mutex);
4080Sstevel@tonic-gate 	mutex_exit(mp);
4098048SMadhavan.Venkataraman@Sun.COM 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
4100Sstevel@tonic-gate 		setrun(t);
4110Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
4120Sstevel@tonic-gate 	swtch();
4130Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
4140Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	/*
4170Sstevel@tonic-gate 	 * Untimeout the thread.  untimeout() returns -1 if the timeout has
4180Sstevel@tonic-gate 	 * occured or the time remaining.  If the time remaining is zero,
4190Sstevel@tonic-gate 	 * the timeout has occured between when we were awoken and
4200Sstevel@tonic-gate 	 * we called untimeout.  We will treat this as if the timeout
4210Sstevel@tonic-gate 	 * has occured and set rval to -1.
4220Sstevel@tonic-gate 	 */
4238566SMadhavan.Venkataraman@Sun.COM 	rval = untimeout_default(id, 0);
4248048SMadhavan.Venkataraman@Sun.COM 	mutex_enter(mp);
4250Sstevel@tonic-gate 	if (rval <= 0)
4260Sstevel@tonic-gate 		rval = -1;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	/*
4290Sstevel@tonic-gate 	 * Check to see if a signal is pending.  If so, regardless of whether
4300Sstevel@tonic-gate 	 * or not we were awoken due to the signal, the signal is now pending
4310Sstevel@tonic-gate 	 * and a return of 0 has the highest priority.
4320Sstevel@tonic-gate 	 */
4330Sstevel@tonic-gate out:
4340Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
4350Sstevel@tonic-gate 		mutex_exit(mp);
4360Sstevel@tonic-gate 		if (issig(FORREAL))
4370Sstevel@tonic-gate 			rval = 0;
4380Sstevel@tonic-gate 		mutex_enter(mp);
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
4410Sstevel@tonic-gate 		rval = 0;
4425891Sraf 	if (rval != 0 && cancel_pending) {
4435891Sraf 		schedctl_cancel_eintr();
4445891Sraf 		rval = 0;
4455891Sraf 	}
4460Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
4470Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
4480Sstevel@tonic-gate 	if (rval <= 0 && signalled)	/* avoid consuming the cv_signal() */
4490Sstevel@tonic-gate 		cv_signal(cvp);
4500Sstevel@tonic-gate 	return (rval);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate /*
4548048SMadhavan.Venkataraman@Sun.COM  * Returns:
4558048SMadhavan.Venkataraman@Sun.COM  * 	Function result in order of precedence:
4568048SMadhavan.Venkataraman@Sun.COM  *		 0 if a signal was received
4578048SMadhavan.Venkataraman@Sun.COM  *		-1 if timeout occured
4588048SMadhavan.Venkataraman@Sun.COM  *		>0 if awakened via cv_signal() or cv_broadcast().
4598048SMadhavan.Venkataraman@Sun.COM  *		   (returns time remaining)
4608048SMadhavan.Venkataraman@Sun.COM  *
4618048SMadhavan.Venkataraman@Sun.COM  * cv_timedwait_sig() is now part of the DDI.
4628048SMadhavan.Venkataraman@Sun.COM  *
4639334SMadhavan.Venkataraman@Sun.COM  * This function is now just a wrapper for cv_timedwait_sig_hires().
4648048SMadhavan.Venkataraman@Sun.COM  */
4658048SMadhavan.Venkataraman@Sun.COM clock_t
4668048SMadhavan.Venkataraman@Sun.COM cv_timedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
4678048SMadhavan.Venkataraman@Sun.COM {
4689334SMadhavan.Venkataraman@Sun.COM 	hrtime_t hrtim;
4699334SMadhavan.Venkataraman@Sun.COM 
470*11066Srafael.vanoni@sun.com 	hrtim = TICK_TO_NSEC(tim - ddi_get_lbolt());
4719334SMadhavan.Venkataraman@Sun.COM 	return (cv_timedwait_sig_hires(cvp, mp, hrtim, nsec_per_tick, 0));
4728048SMadhavan.Venkataraman@Sun.COM }
4738048SMadhavan.Venkataraman@Sun.COM 
4748048SMadhavan.Venkataraman@Sun.COM /*
475*11066Srafael.vanoni@sun.com  * Same as cv_timedwait_sig() except that the third argument is a relative
476*11066Srafael.vanoni@sun.com  * timeout value, as opposed to an absolute one. There is also a fourth
477*11066Srafael.vanoni@sun.com  * argument that specifies how accurately the timeout must be implemented.
478*11066Srafael.vanoni@sun.com  */
479*11066Srafael.vanoni@sun.com clock_t
480*11066Srafael.vanoni@sun.com cv_reltimedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t delta,
481*11066Srafael.vanoni@sun.com     time_res_t res)
482*11066Srafael.vanoni@sun.com {
483*11066Srafael.vanoni@sun.com 	hrtime_t exp;
484*11066Srafael.vanoni@sun.com 
485*11066Srafael.vanoni@sun.com 	ASSERT(TIME_RES_VALID(res));
486*11066Srafael.vanoni@sun.com 
487*11066Srafael.vanoni@sun.com 	if ((exp = TICK_TO_NSEC(delta)) < 0)
488*11066Srafael.vanoni@sun.com 		exp = CY_INFINITY;
489*11066Srafael.vanoni@sun.com 
490*11066Srafael.vanoni@sun.com 	return (cv_timedwait_sig_hires(cvp, mp, exp, time_res[res], 0));
491*11066Srafael.vanoni@sun.com }
492*11066Srafael.vanoni@sun.com 
493*11066Srafael.vanoni@sun.com /*
4940Sstevel@tonic-gate  * Like cv_wait_sig_swap but allows the caller to indicate (with a
4950Sstevel@tonic-gate  * non-NULL sigret) that they will take care of signalling the cv
4960Sstevel@tonic-gate  * after wakeup, if necessary.  This is a vile hack that should only
4970Sstevel@tonic-gate  * be used when no other option is available; almost all callers
4980Sstevel@tonic-gate  * should just use cv_wait_sig_swap (which takes care of the cv_signal
4990Sstevel@tonic-gate  * stuff automatically) instead.
5000Sstevel@tonic-gate  */
5010Sstevel@tonic-gate int
5020Sstevel@tonic-gate cv_wait_sig_swap_core(kcondvar_t *cvp, kmutex_t *mp, int *sigret)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	kthread_t *t = curthread;
5050Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
5060Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
5075891Sraf 	int cancel_pending;
5080Sstevel@tonic-gate 	int rval = 1;
5090Sstevel@tonic-gate 	int signalled = 0;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	if (panicstr)
5120Sstevel@tonic-gate 		return (rval);
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	/*
5150Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
5160Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
5170Sstevel@tonic-gate 	 */
5180Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr) {
5190Sstevel@tonic-gate 		cv_wait(cvp, mp);
5200Sstevel@tonic-gate 		return (rval);
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
5235891Sraf 	cancel_pending = schedctl_cancel_pending();
5240Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
5250Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
5260Sstevel@tonic-gate 	thread_lock(t);
5270Sstevel@tonic-gate 	t->t_kpri_req = 0;	/* don't need kernel priority */
5280Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
5290Sstevel@tonic-gate 	/* I can be swapped now */
5300Sstevel@tonic-gate 	curthread->t_schedflag &= ~TS_DONT_SWAP;
5310Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
5320Sstevel@tonic-gate 	mutex_exit(mp);
5335891Sraf 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
5340Sstevel@tonic-gate 		setrun(t);
5350Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
5360Sstevel@tonic-gate 	swtch();
5370Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
5380Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
5390Sstevel@tonic-gate 	/* TS_DONT_SWAP set by disp() */
5400Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
5410Sstevel@tonic-gate 	mutex_enter(mp);
5420Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
5430Sstevel@tonic-gate 		mutex_exit(mp);
5440Sstevel@tonic-gate 		if (issig(FORREAL))
5450Sstevel@tonic-gate 			rval = 0;
5460Sstevel@tonic-gate 		mutex_enter(mp);
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
5490Sstevel@tonic-gate 		rval = 0;
5505891Sraf 	if (rval != 0 && cancel_pending) {
5515891Sraf 		schedctl_cancel_eintr();
5525891Sraf 		rval = 0;
5535891Sraf 	}
5540Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
5550Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
5560Sstevel@tonic-gate 	if (rval == 0) {
5570Sstevel@tonic-gate 		if (sigret != NULL)
5580Sstevel@tonic-gate 			*sigret = signalled;	/* just tell the caller */
5590Sstevel@tonic-gate 		else if (signalled)
5600Sstevel@tonic-gate 			cv_signal(cvp);	/* avoid consuming the cv_signal() */
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate 	return (rval);
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate /*
5660Sstevel@tonic-gate  * Same as cv_wait_sig but the thread can be swapped out while waiting.
5670Sstevel@tonic-gate  * This should only be used when we know we aren't holding any locks.
5680Sstevel@tonic-gate  */
5690Sstevel@tonic-gate int
5700Sstevel@tonic-gate cv_wait_sig_swap(kcondvar_t *cvp, kmutex_t *mp)
5710Sstevel@tonic-gate {
5720Sstevel@tonic-gate 	return (cv_wait_sig_swap_core(cvp, mp, NULL));
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate void
5760Sstevel@tonic-gate cv_signal(kcondvar_t *cvp)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	/* make sure the cv_waiters field looks sane */
5810Sstevel@tonic-gate 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
5820Sstevel@tonic-gate 	if (cp->cv_waiters > 0) {
5830Sstevel@tonic-gate 		sleepq_head_t *sqh = SQHASH(cp);
5840Sstevel@tonic-gate 		disp_lock_enter(&sqh->sq_lock);
5850Sstevel@tonic-gate 		ASSERT(CPU_ON_INTR(CPU) == 0);
5860Sstevel@tonic-gate 		if (cp->cv_waiters & CV_WAITERS_MASK) {
5870Sstevel@tonic-gate 			kthread_t *t;
5880Sstevel@tonic-gate 			cp->cv_waiters--;
5890Sstevel@tonic-gate 			t = sleepq_wakeone_chan(&sqh->sq_queue, cp);
5900Sstevel@tonic-gate 			/*
5910Sstevel@tonic-gate 			 * If cv_waiters is non-zero (and less than
5920Sstevel@tonic-gate 			 * CV_MAX_WAITERS) there should be a thread
5930Sstevel@tonic-gate 			 * in the queue.
5940Sstevel@tonic-gate 			 */
5950Sstevel@tonic-gate 			ASSERT(t != NULL);
5960Sstevel@tonic-gate 		} else if (sleepq_wakeone_chan(&sqh->sq_queue, cp) == NULL) {
5970Sstevel@tonic-gate 			cp->cv_waiters = 0;
5980Sstevel@tonic-gate 		}
5990Sstevel@tonic-gate 		disp_lock_exit(&sqh->sq_lock);
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate void
6040Sstevel@tonic-gate cv_broadcast(kcondvar_t *cvp)
6050Sstevel@tonic-gate {
6060Sstevel@tonic-gate 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	/* make sure the cv_waiters field looks sane */
6090Sstevel@tonic-gate 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
6100Sstevel@tonic-gate 	if (cp->cv_waiters > 0) {
6110Sstevel@tonic-gate 		sleepq_head_t *sqh = SQHASH(cp);
6120Sstevel@tonic-gate 		disp_lock_enter(&sqh->sq_lock);
6130Sstevel@tonic-gate 		ASSERT(CPU_ON_INTR(CPU) == 0);
6140Sstevel@tonic-gate 		sleepq_wakeall_chan(&sqh->sq_queue, cp);
6150Sstevel@tonic-gate 		cp->cv_waiters = 0;
6160Sstevel@tonic-gate 		disp_lock_exit(&sqh->sq_lock);
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate /*
6210Sstevel@tonic-gate  * Same as cv_wait(), but wakes up (after wakeup_time milliseconds) to check
6220Sstevel@tonic-gate  * for requests to stop, like cv_wait_sig() but without dealing with signals.
6230Sstevel@tonic-gate  * This is a horrible kludge.  It is evil.  It is vile.  It is swill.
6240Sstevel@tonic-gate  * If your code has to call this function then your code is the same.
6250Sstevel@tonic-gate  */
6260Sstevel@tonic-gate void
6270Sstevel@tonic-gate cv_wait_stop(kcondvar_t *cvp, kmutex_t *mp, int wakeup_time)
6280Sstevel@tonic-gate {
6290Sstevel@tonic-gate 	kthread_t *t = curthread;
6300Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
6310Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
6328048SMadhavan.Venkataraman@Sun.COM 	callout_id_t id;
6330Sstevel@tonic-gate 	clock_t tim;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	if (panicstr)
6360Sstevel@tonic-gate 		return;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/*
6390Sstevel@tonic-gate 	 * If there is no lwp, then we don't need to eventually stop it
6400Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
6410Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
6420Sstevel@tonic-gate 	 */
6430Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr) {
6440Sstevel@tonic-gate 		cv_wait(cvp, mp);
6450Sstevel@tonic-gate 		return;
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	/*
6490Sstevel@tonic-gate 	 * Wakeup in wakeup_time milliseconds, i.e., human time.
6500Sstevel@tonic-gate 	 */
651*11066Srafael.vanoni@sun.com 	tim = ddi_get_lbolt() + MSEC_TO_TICK(wakeup_time);
6528566SMadhavan.Venkataraman@Sun.COM 	mutex_enter(&t->t_wait_mutex);
6538048SMadhavan.Venkataraman@Sun.COM 	id = realtime_timeout_default((void (*)(void *))cv_wakeup, t,
654*11066Srafael.vanoni@sun.com 	    tim - ddi_get_lbolt());
6550Sstevel@tonic-gate 	thread_lock(t);			/* lock the thread */
6560Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
6570Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
6588566SMadhavan.Venkataraman@Sun.COM 	mutex_exit(&t->t_wait_mutex);
6590Sstevel@tonic-gate 	mutex_exit(mp);
6600Sstevel@tonic-gate 	/* ASSERT(no locks are held); */
6610Sstevel@tonic-gate 	swtch();
6628566SMadhavan.Venkataraman@Sun.COM 	(void) untimeout_default(id, 0);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	/*
6653930Snr123932 	 * Check for reasons to stop, if lwp_nostop is not true.
6660Sstevel@tonic-gate 	 * See issig_forreal() for explanations of the various stops.
6670Sstevel@tonic-gate 	 */
6680Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
6693930Snr123932 	while (lwp->lwp_nostop == 0 && !(p->p_flag & SEXITLWPS)) {
6700Sstevel@tonic-gate 		/*
6710Sstevel@tonic-gate 		 * Hold the lwp here for watchpoint manipulation.
6720Sstevel@tonic-gate 		 */
6733930Snr123932 		if (t->t_proc_flag & TP_PAUSE) {
6740Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_PAUSE);
6750Sstevel@tonic-gate 			continue;
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 		/*
6780Sstevel@tonic-gate 		 * System checkpoint.
6790Sstevel@tonic-gate 		 */
6803930Snr123932 		if (t->t_proc_flag & TP_CHKPT) {
6810Sstevel@tonic-gate 			stop(PR_CHECKPOINT, 0);
6820Sstevel@tonic-gate 			continue;
6830Sstevel@tonic-gate 		}
6840Sstevel@tonic-gate 		/*
6850Sstevel@tonic-gate 		 * Honor fork1(), watchpoint activity (remapping a page),
6863930Snr123932 		 * and lwp_suspend() requests.
6870Sstevel@tonic-gate 		 */
6883930Snr123932 		if ((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
6893930Snr123932 		    (t->t_proc_flag & TP_HOLDLWP)) {
6900Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_NORMAL);
6910Sstevel@tonic-gate 			continue;
6920Sstevel@tonic-gate 		}
6930Sstevel@tonic-gate 		/*
6940Sstevel@tonic-gate 		 * Honor /proc requested stop.
6950Sstevel@tonic-gate 		 */
6963930Snr123932 		if (t->t_proc_flag & TP_PRSTOP) {
6970Sstevel@tonic-gate 			stop(PR_REQUESTED, 0);
6980Sstevel@tonic-gate 		}
6990Sstevel@tonic-gate 		/*
7000Sstevel@tonic-gate 		 * If some lwp in the process has already stopped
7010Sstevel@tonic-gate 		 * showing PR_JOBCONTROL, stop in sympathy with it.
7020Sstevel@tonic-gate 		 */
7033930Snr123932 		if (p->p_stopsig && t != p->p_agenttp) {
7040Sstevel@tonic-gate 			stop(PR_JOBCONTROL, p->p_stopsig);
7050Sstevel@tonic-gate 			continue;
7060Sstevel@tonic-gate 		}
7070Sstevel@tonic-gate 		break;
7080Sstevel@tonic-gate 	}
7090Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
7100Sstevel@tonic-gate 	mutex_enter(mp);
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate /*
7140Sstevel@tonic-gate  * Like cv_timedwait_sig(), but takes an absolute hires future time
7150Sstevel@tonic-gate  * rather than a future time in clock ticks.  Will not return showing
7160Sstevel@tonic-gate  * that a timeout occurred until the future time is passed.
7170Sstevel@tonic-gate  * If 'when' is a NULL pointer, no timeout will occur.
7180Sstevel@tonic-gate  * Returns:
7198048SMadhavan.Venkataraman@Sun.COM  * 	Function result in order of precedence:
7200Sstevel@tonic-gate  *		 0 if a signal was received
7210Sstevel@tonic-gate  *		-1 if timeout occured
7220Sstevel@tonic-gate  *	        >0 if awakened via cv_signal() or cv_broadcast()
7230Sstevel@tonic-gate  *		   or by a spurious wakeup.
7240Sstevel@tonic-gate  *		   (might return time remaining)
7254123Sdm120769  * As a special test, if someone abruptly resets the system time
7264123Sdm120769  * (but not through adjtime(2); drifting of the clock is allowed and
7274123Sdm120769  * expected [see timespectohz_adj()]), then we force a return of -1
7284123Sdm120769  * so the caller can return a premature timeout to the calling process
7294123Sdm120769  * so it can reevaluate the situation in light of the new system time.
7304123Sdm120769  * (The system clock has been reset if timecheck != timechanged.)
7310Sstevel@tonic-gate  */
7320Sstevel@tonic-gate int
7334123Sdm120769 cv_waituntil_sig(kcondvar_t *cvp, kmutex_t *mp,
7344123Sdm120769 	timestruc_t *when, int timecheck)
7350Sstevel@tonic-gate {
7360Sstevel@tonic-gate 	timestruc_t now;
7373346Svb160487 	timestruc_t delta;
7389334SMadhavan.Venkataraman@Sun.COM 	hrtime_t interval;
7390Sstevel@tonic-gate 	int rval;
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	if (when == NULL)
7420Sstevel@tonic-gate 		return (cv_wait_sig_swap(cvp, mp));
7430Sstevel@tonic-gate 
7447982SDonghai.Qiao@Sun.COM 	gethrestime(&now);
7453346Svb160487 	delta = *when;
7463346Svb160487 	timespecsub(&delta, &now);
7473346Svb160487 	if (delta.tv_sec < 0 || (delta.tv_sec == 0 && delta.tv_nsec == 0)) {
7480Sstevel@tonic-gate 		/*
7490Sstevel@tonic-gate 		 * We have already reached the absolute future time.
7500Sstevel@tonic-gate 		 * Call cv_timedwait_sig() just to check for signals.
7510Sstevel@tonic-gate 		 * We will return immediately with either 0 or -1.
7520Sstevel@tonic-gate 		 */
7539334SMadhavan.Venkataraman@Sun.COM 		rval = cv_timedwait_sig_hires(cvp, mp, 0, 1, 0);
7540Sstevel@tonic-gate 	} else {
7554123Sdm120769 		if (timecheck == timechanged) {
7569334SMadhavan.Venkataraman@Sun.COM 			/*
7579334SMadhavan.Venkataraman@Sun.COM 			 * Make sure that the interval is atleast one tick.
7589334SMadhavan.Venkataraman@Sun.COM 			 * This is to prevent a user from flooding the system
7599334SMadhavan.Venkataraman@Sun.COM 			 * with very small, high resolution timers.
7609334SMadhavan.Venkataraman@Sun.COM 			 */
7619334SMadhavan.Venkataraman@Sun.COM 			interval = ts2hrt(&delta);
7629334SMadhavan.Venkataraman@Sun.COM 			if (interval < nsec_per_tick)
7639334SMadhavan.Venkataraman@Sun.COM 				interval = nsec_per_tick;
7649334SMadhavan.Venkataraman@Sun.COM 			rval = cv_timedwait_sig_hires(cvp, mp, interval, 1,
7658048SMadhavan.Venkataraman@Sun.COM 			    CALLOUT_FLAG_HRESTIME);
7664123Sdm120769 		} else {
7674123Sdm120769 			/*
7684123Sdm120769 			 * Someone reset the system time;
7694123Sdm120769 			 * just force an immediate timeout.
7704123Sdm120769 			 */
7714123Sdm120769 			rval = -1;
7724123Sdm120769 		}
7734123Sdm120769 		if (rval == -1 && timecheck == timechanged) {
7744123Sdm120769 			/*
7754123Sdm120769 			 * Even though cv_timedwait_sig() returned showing a
7764123Sdm120769 			 * timeout, the future time may not have passed yet.
7774123Sdm120769 			 * If not, change rval to indicate a normal wakeup.
7784123Sdm120769 			 */
7794123Sdm120769 			gethrestime(&now);
7804123Sdm120769 			delta = *when;
7814123Sdm120769 			timespecsub(&delta, &now);
7824123Sdm120769 			if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
7834123Sdm120769 			    delta.tv_nsec > 0))
7840Sstevel@tonic-gate 				rval = 1;
7854123Sdm120769 		}
7860Sstevel@tonic-gate 	}
7870Sstevel@tonic-gate 	return (rval);
7880Sstevel@tonic-gate }
789