xref: /onnv-gate/usr/src/uts/common/os/condvar.c (revision 9334:197b8bfa4217)
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 
42*9334SMadhavan.Venkataraman@Sun.COM clock_t cv_timedwait_hires(kcondvar_t *, kmutex_t *, hrtime_t, hrtime_t, int);
43*9334SMadhavan.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 {
226*9334SMadhavan.Venkataraman@Sun.COM 	hrtime_t hrtim;
227*9334SMadhavan.Venkataraman@Sun.COM 
228*9334SMadhavan.Venkataraman@Sun.COM 	if (tim <= lbolt)
229*9334SMadhavan.Venkataraman@Sun.COM 		return (-1);
230*9334SMadhavan.Venkataraman@Sun.COM 
231*9334SMadhavan.Venkataraman@Sun.COM 	hrtim = TICK_TO_NSEC(tim - lbolt);
232*9334SMadhavan.Venkataraman@Sun.COM 	return (cv_timedwait_hires(cvp, mp, hrtim, nsec_per_tick, 0));
233*9334SMadhavan.Venkataraman@Sun.COM }
234*9334SMadhavan.Venkataraman@Sun.COM 
235*9334SMadhavan.Venkataraman@Sun.COM clock_t
236*9334SMadhavan.Venkataraman@Sun.COM cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
237*9334SMadhavan.Venkataraman@Sun.COM     hrtime_t res, int flag)
238*9334SMadhavan.Venkataraman@Sun.COM {
2390Sstevel@tonic-gate 	kthread_t *t = curthread;
2408048SMadhavan.Venkataraman@Sun.COM 	callout_id_t id;
2410Sstevel@tonic-gate 	clock_t timeleft;
242*9334SMadhavan.Venkataraman@Sun.COM 	hrtime_t limit;
2430Sstevel@tonic-gate 	int signalled;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	if (panicstr)
2460Sstevel@tonic-gate 		return (-1);
2470Sstevel@tonic-gate 
248*9334SMadhavan.Venkataraman@Sun.COM 	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
249*9334SMadhavan.Venkataraman@Sun.COM 	if (tim <= limit)
2500Sstevel@tonic-gate 		return (-1);
2518566SMadhavan.Venkataraman@Sun.COM 	mutex_enter(&t->t_wait_mutex);
252*9334SMadhavan.Venkataraman@Sun.COM 	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
253*9334SMadhavan.Venkataraman@Sun.COM 	    tim, res, flag);
2540Sstevel@tonic-gate 	thread_lock(t);		/* lock the thread */
2550Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
2560Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
2578566SMadhavan.Venkataraman@Sun.COM 	mutex_exit(&t->t_wait_mutex);
2580Sstevel@tonic-gate 	mutex_exit(mp);
2590Sstevel@tonic-gate 	swtch();
2600Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
2610Sstevel@tonic-gate 	/*
2620Sstevel@tonic-gate 	 * Get the time left. untimeout() returns -1 if the timeout has
2630Sstevel@tonic-gate 	 * occured or the time remaining.  If the time remaining is zero,
2640Sstevel@tonic-gate 	 * the timeout has occured between when we were awoken and
2650Sstevel@tonic-gate 	 * we called untimeout.  We will treat this as if the timeout
2660Sstevel@tonic-gate 	 * has occured and set timeleft to -1.
2670Sstevel@tonic-gate 	 */
2688566SMadhavan.Venkataraman@Sun.COM 	timeleft = untimeout_default(id, 0);
2690Sstevel@tonic-gate 	mutex_enter(mp);
2700Sstevel@tonic-gate 	if (timeleft <= 0) {
2710Sstevel@tonic-gate 		timeleft = -1;
2720Sstevel@tonic-gate 		if (signalled)	/* avoid consuming the cv_signal() */
2730Sstevel@tonic-gate 			cv_signal(cvp);
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate 	return (timeleft);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate int
2790Sstevel@tonic-gate cv_wait_sig(kcondvar_t *cvp, kmutex_t *mp)
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate 	kthread_t *t = curthread;
2820Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
2830Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
2845891Sraf 	int cancel_pending;
2850Sstevel@tonic-gate 	int rval = 1;
2860Sstevel@tonic-gate 	int signalled = 0;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	if (panicstr)
2890Sstevel@tonic-gate 		return (rval);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	/*
2920Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
2930Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
2940Sstevel@tonic-gate 	 */
2950Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr) {
2960Sstevel@tonic-gate 		cv_wait(cvp, mp);
2970Sstevel@tonic-gate 		return (rval);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
3015891Sraf 	cancel_pending = schedctl_cancel_pending();
3020Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
3030Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
3040Sstevel@tonic-gate 	thread_lock(t);
3050Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
3060Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
3070Sstevel@tonic-gate 	mutex_exit(mp);
3085891Sraf 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
3090Sstevel@tonic-gate 		setrun(t);
3100Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
3110Sstevel@tonic-gate 	swtch();
3120Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
3130Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
3140Sstevel@tonic-gate 	mutex_enter(mp);
3150Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
3160Sstevel@tonic-gate 		mutex_exit(mp);
3170Sstevel@tonic-gate 		if (issig(FORREAL))
3180Sstevel@tonic-gate 			rval = 0;
3190Sstevel@tonic-gate 		mutex_enter(mp);
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
3220Sstevel@tonic-gate 		rval = 0;
3235891Sraf 	if (rval != 0 && cancel_pending) {
3245891Sraf 		schedctl_cancel_eintr();
3255891Sraf 		rval = 0;
3265891Sraf 	}
3270Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
3280Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
3290Sstevel@tonic-gate 	if (rval == 0 && signalled)	/* avoid consuming the cv_signal() */
3300Sstevel@tonic-gate 		cv_signal(cvp);
3310Sstevel@tonic-gate 	return (rval);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate 
3348048SMadhavan.Venkataraman@Sun.COM static clock_t
335*9334SMadhavan.Venkataraman@Sun.COM cv_timedwait_sig_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
336*9334SMadhavan.Venkataraman@Sun.COM     hrtime_t res, int flag)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	kthread_t *t = curthread;
3390Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
3400Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
3415891Sraf 	int cancel_pending = 0;
3428048SMadhavan.Venkataraman@Sun.COM 	callout_id_t id;
3430Sstevel@tonic-gate 	clock_t rval = 1;
344*9334SMadhavan.Venkataraman@Sun.COM 	hrtime_t limit;
3450Sstevel@tonic-gate 	int signalled = 0;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if (panicstr)
3480Sstevel@tonic-gate 		return (rval);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	/*
3510Sstevel@tonic-gate 	 * If there is no lwp, then we don't need to wait for a signal.
3520Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
3530Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
3540Sstevel@tonic-gate 	 */
3550Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr)
356*9334SMadhavan.Venkataraman@Sun.COM 		return (cv_timedwait_hires(cvp, mp, tim, res, flag));
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	/*
359*9334SMadhavan.Venkataraman@Sun.COM 	 * If tim is less than or equal to current hrtime, then the timeout
3600Sstevel@tonic-gate 	 * has already occured.  So just check to see if there is a signal
3610Sstevel@tonic-gate 	 * pending.  If so return 0 indicating that there is a signal pending.
3620Sstevel@tonic-gate 	 * Else return -1 indicating that the timeout occured. No need to
3630Sstevel@tonic-gate 	 * wait on anything.
3640Sstevel@tonic-gate 	 */
365*9334SMadhavan.Venkataraman@Sun.COM 	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
366*9334SMadhavan.Venkataraman@Sun.COM 	if (tim <= limit) {
3670Sstevel@tonic-gate 		lwp->lwp_asleep = 1;
3680Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;
3690Sstevel@tonic-gate 		rval = -1;
3700Sstevel@tonic-gate 		goto out;
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	/*
3740Sstevel@tonic-gate 	 * Set the timeout and wait.
3750Sstevel@tonic-gate 	 */
3765891Sraf 	cancel_pending = schedctl_cancel_pending();
3778566SMadhavan.Venkataraman@Sun.COM 	mutex_enter(&t->t_wait_mutex);
3788048SMadhavan.Venkataraman@Sun.COM 	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
379*9334SMadhavan.Venkataraman@Sun.COM 	    tim, res, flag);
3800Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
3810Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
3820Sstevel@tonic-gate 	thread_lock(t);
3830Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
3840Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
3858566SMadhavan.Venkataraman@Sun.COM 	mutex_exit(&t->t_wait_mutex);
3860Sstevel@tonic-gate 	mutex_exit(mp);
3878048SMadhavan.Venkataraman@Sun.COM 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
3880Sstevel@tonic-gate 		setrun(t);
3890Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
3900Sstevel@tonic-gate 	swtch();
3910Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
3920Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	/*
3950Sstevel@tonic-gate 	 * Untimeout the thread.  untimeout() returns -1 if the timeout has
3960Sstevel@tonic-gate 	 * occured or the time remaining.  If the time remaining is zero,
3970Sstevel@tonic-gate 	 * the timeout has occured between when we were awoken and
3980Sstevel@tonic-gate 	 * we called untimeout.  We will treat this as if the timeout
3990Sstevel@tonic-gate 	 * has occured and set rval to -1.
4000Sstevel@tonic-gate 	 */
4018566SMadhavan.Venkataraman@Sun.COM 	rval = untimeout_default(id, 0);
4028048SMadhavan.Venkataraman@Sun.COM 	mutex_enter(mp);
4030Sstevel@tonic-gate 	if (rval <= 0)
4040Sstevel@tonic-gate 		rval = -1;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	/*
4070Sstevel@tonic-gate 	 * Check to see if a signal is pending.  If so, regardless of whether
4080Sstevel@tonic-gate 	 * or not we were awoken due to the signal, the signal is now pending
4090Sstevel@tonic-gate 	 * and a return of 0 has the highest priority.
4100Sstevel@tonic-gate 	 */
4110Sstevel@tonic-gate out:
4120Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
4130Sstevel@tonic-gate 		mutex_exit(mp);
4140Sstevel@tonic-gate 		if (issig(FORREAL))
4150Sstevel@tonic-gate 			rval = 0;
4160Sstevel@tonic-gate 		mutex_enter(mp);
4170Sstevel@tonic-gate 	}
4180Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
4190Sstevel@tonic-gate 		rval = 0;
4205891Sraf 	if (rval != 0 && cancel_pending) {
4215891Sraf 		schedctl_cancel_eintr();
4225891Sraf 		rval = 0;
4235891Sraf 	}
4240Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
4250Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
4260Sstevel@tonic-gate 	if (rval <= 0 && signalled)	/* avoid consuming the cv_signal() */
4270Sstevel@tonic-gate 		cv_signal(cvp);
4280Sstevel@tonic-gate 	return (rval);
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate /*
4328048SMadhavan.Venkataraman@Sun.COM  * Returns:
4338048SMadhavan.Venkataraman@Sun.COM  * 	Function result in order of precedence:
4348048SMadhavan.Venkataraman@Sun.COM  *		 0 if a signal was received
4358048SMadhavan.Venkataraman@Sun.COM  *		-1 if timeout occured
4368048SMadhavan.Venkataraman@Sun.COM  *		>0 if awakened via cv_signal() or cv_broadcast().
4378048SMadhavan.Venkataraman@Sun.COM  *		   (returns time remaining)
4388048SMadhavan.Venkataraman@Sun.COM  *
4398048SMadhavan.Venkataraman@Sun.COM  * cv_timedwait_sig() is now part of the DDI.
4408048SMadhavan.Venkataraman@Sun.COM  *
441*9334SMadhavan.Venkataraman@Sun.COM  * This function is now just a wrapper for cv_timedwait_sig_hires().
4428048SMadhavan.Venkataraman@Sun.COM  */
4438048SMadhavan.Venkataraman@Sun.COM clock_t
4448048SMadhavan.Venkataraman@Sun.COM cv_timedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
4458048SMadhavan.Venkataraman@Sun.COM {
446*9334SMadhavan.Venkataraman@Sun.COM 	hrtime_t hrtim;
447*9334SMadhavan.Venkataraman@Sun.COM 
448*9334SMadhavan.Venkataraman@Sun.COM 	hrtim = TICK_TO_NSEC(tim - lbolt);
449*9334SMadhavan.Venkataraman@Sun.COM 	return (cv_timedwait_sig_hires(cvp, mp, hrtim, nsec_per_tick, 0));
4508048SMadhavan.Venkataraman@Sun.COM }
4518048SMadhavan.Venkataraman@Sun.COM 
4528048SMadhavan.Venkataraman@Sun.COM /*
4530Sstevel@tonic-gate  * Like cv_wait_sig_swap but allows the caller to indicate (with a
4540Sstevel@tonic-gate  * non-NULL sigret) that they will take care of signalling the cv
4550Sstevel@tonic-gate  * after wakeup, if necessary.  This is a vile hack that should only
4560Sstevel@tonic-gate  * be used when no other option is available; almost all callers
4570Sstevel@tonic-gate  * should just use cv_wait_sig_swap (which takes care of the cv_signal
4580Sstevel@tonic-gate  * stuff automatically) instead.
4590Sstevel@tonic-gate  */
4600Sstevel@tonic-gate int
4610Sstevel@tonic-gate cv_wait_sig_swap_core(kcondvar_t *cvp, kmutex_t *mp, int *sigret)
4620Sstevel@tonic-gate {
4630Sstevel@tonic-gate 	kthread_t *t = curthread;
4640Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
4650Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
4665891Sraf 	int cancel_pending;
4670Sstevel@tonic-gate 	int rval = 1;
4680Sstevel@tonic-gate 	int signalled = 0;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	if (panicstr)
4710Sstevel@tonic-gate 		return (rval);
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	/*
4740Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
4750Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
4760Sstevel@tonic-gate 	 */
4770Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr) {
4780Sstevel@tonic-gate 		cv_wait(cvp, mp);
4790Sstevel@tonic-gate 		return (rval);
4800Sstevel@tonic-gate 	}
4810Sstevel@tonic-gate 
4825891Sraf 	cancel_pending = schedctl_cancel_pending();
4830Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
4840Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
4850Sstevel@tonic-gate 	thread_lock(t);
4860Sstevel@tonic-gate 	t->t_kpri_req = 0;	/* don't need kernel priority */
4870Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
4880Sstevel@tonic-gate 	/* I can be swapped now */
4890Sstevel@tonic-gate 	curthread->t_schedflag &= ~TS_DONT_SWAP;
4900Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
4910Sstevel@tonic-gate 	mutex_exit(mp);
4925891Sraf 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
4930Sstevel@tonic-gate 		setrun(t);
4940Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
4950Sstevel@tonic-gate 	swtch();
4960Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
4970Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
4980Sstevel@tonic-gate 	/* TS_DONT_SWAP set by disp() */
4990Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
5000Sstevel@tonic-gate 	mutex_enter(mp);
5010Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
5020Sstevel@tonic-gate 		mutex_exit(mp);
5030Sstevel@tonic-gate 		if (issig(FORREAL))
5040Sstevel@tonic-gate 			rval = 0;
5050Sstevel@tonic-gate 		mutex_enter(mp);
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
5080Sstevel@tonic-gate 		rval = 0;
5095891Sraf 	if (rval != 0 && cancel_pending) {
5105891Sraf 		schedctl_cancel_eintr();
5115891Sraf 		rval = 0;
5125891Sraf 	}
5130Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
5140Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
5150Sstevel@tonic-gate 	if (rval == 0) {
5160Sstevel@tonic-gate 		if (sigret != NULL)
5170Sstevel@tonic-gate 			*sigret = signalled;	/* just tell the caller */
5180Sstevel@tonic-gate 		else if (signalled)
5190Sstevel@tonic-gate 			cv_signal(cvp);	/* avoid consuming the cv_signal() */
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 	return (rval);
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate /*
5250Sstevel@tonic-gate  * Same as cv_wait_sig but the thread can be swapped out while waiting.
5260Sstevel@tonic-gate  * This should only be used when we know we aren't holding any locks.
5270Sstevel@tonic-gate  */
5280Sstevel@tonic-gate int
5290Sstevel@tonic-gate cv_wait_sig_swap(kcondvar_t *cvp, kmutex_t *mp)
5300Sstevel@tonic-gate {
5310Sstevel@tonic-gate 	return (cv_wait_sig_swap_core(cvp, mp, NULL));
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate void
5350Sstevel@tonic-gate cv_signal(kcondvar_t *cvp)
5360Sstevel@tonic-gate {
5370Sstevel@tonic-gate 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	/* make sure the cv_waiters field looks sane */
5400Sstevel@tonic-gate 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
5410Sstevel@tonic-gate 	if (cp->cv_waiters > 0) {
5420Sstevel@tonic-gate 		sleepq_head_t *sqh = SQHASH(cp);
5430Sstevel@tonic-gate 		disp_lock_enter(&sqh->sq_lock);
5440Sstevel@tonic-gate 		ASSERT(CPU_ON_INTR(CPU) == 0);
5450Sstevel@tonic-gate 		if (cp->cv_waiters & CV_WAITERS_MASK) {
5460Sstevel@tonic-gate 			kthread_t *t;
5470Sstevel@tonic-gate 			cp->cv_waiters--;
5480Sstevel@tonic-gate 			t = sleepq_wakeone_chan(&sqh->sq_queue, cp);
5490Sstevel@tonic-gate 			/*
5500Sstevel@tonic-gate 			 * If cv_waiters is non-zero (and less than
5510Sstevel@tonic-gate 			 * CV_MAX_WAITERS) there should be a thread
5520Sstevel@tonic-gate 			 * in the queue.
5530Sstevel@tonic-gate 			 */
5540Sstevel@tonic-gate 			ASSERT(t != NULL);
5550Sstevel@tonic-gate 		} else if (sleepq_wakeone_chan(&sqh->sq_queue, cp) == NULL) {
5560Sstevel@tonic-gate 			cp->cv_waiters = 0;
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 		disp_lock_exit(&sqh->sq_lock);
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate void
5630Sstevel@tonic-gate cv_broadcast(kcondvar_t *cvp)
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	/* make sure the cv_waiters field looks sane */
5680Sstevel@tonic-gate 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
5690Sstevel@tonic-gate 	if (cp->cv_waiters > 0) {
5700Sstevel@tonic-gate 		sleepq_head_t *sqh = SQHASH(cp);
5710Sstevel@tonic-gate 		disp_lock_enter(&sqh->sq_lock);
5720Sstevel@tonic-gate 		ASSERT(CPU_ON_INTR(CPU) == 0);
5730Sstevel@tonic-gate 		sleepq_wakeall_chan(&sqh->sq_queue, cp);
5740Sstevel@tonic-gate 		cp->cv_waiters = 0;
5750Sstevel@tonic-gate 		disp_lock_exit(&sqh->sq_lock);
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate /*
5800Sstevel@tonic-gate  * Same as cv_wait(), but wakes up (after wakeup_time milliseconds) to check
5810Sstevel@tonic-gate  * for requests to stop, like cv_wait_sig() but without dealing with signals.
5820Sstevel@tonic-gate  * This is a horrible kludge.  It is evil.  It is vile.  It is swill.
5830Sstevel@tonic-gate  * If your code has to call this function then your code is the same.
5840Sstevel@tonic-gate  */
5850Sstevel@tonic-gate void
5860Sstevel@tonic-gate cv_wait_stop(kcondvar_t *cvp, kmutex_t *mp, int wakeup_time)
5870Sstevel@tonic-gate {
5880Sstevel@tonic-gate 	kthread_t *t = curthread;
5890Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
5900Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
5918048SMadhavan.Venkataraman@Sun.COM 	callout_id_t id;
5920Sstevel@tonic-gate 	clock_t tim;
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	if (panicstr)
5950Sstevel@tonic-gate 		return;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	/*
5980Sstevel@tonic-gate 	 * If there is no lwp, then we don't need to eventually stop it
5990Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
6000Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
6010Sstevel@tonic-gate 	 */
6020Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr) {
6030Sstevel@tonic-gate 		cv_wait(cvp, mp);
6040Sstevel@tonic-gate 		return;
6050Sstevel@tonic-gate 	}
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	/*
6080Sstevel@tonic-gate 	 * Wakeup in wakeup_time milliseconds, i.e., human time.
6090Sstevel@tonic-gate 	 */
6100Sstevel@tonic-gate 	tim = lbolt + MSEC_TO_TICK(wakeup_time);
6118566SMadhavan.Venkataraman@Sun.COM 	mutex_enter(&t->t_wait_mutex);
6128048SMadhavan.Venkataraman@Sun.COM 	id = realtime_timeout_default((void (*)(void *))cv_wakeup, t,
6138048SMadhavan.Venkataraman@Sun.COM 	    tim - lbolt);
6140Sstevel@tonic-gate 	thread_lock(t);			/* lock the thread */
6150Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
6160Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
6178566SMadhavan.Venkataraman@Sun.COM 	mutex_exit(&t->t_wait_mutex);
6180Sstevel@tonic-gate 	mutex_exit(mp);
6190Sstevel@tonic-gate 	/* ASSERT(no locks are held); */
6200Sstevel@tonic-gate 	swtch();
6218566SMadhavan.Venkataraman@Sun.COM 	(void) untimeout_default(id, 0);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	/*
6243930Snr123932 	 * Check for reasons to stop, if lwp_nostop is not true.
6250Sstevel@tonic-gate 	 * See issig_forreal() for explanations of the various stops.
6260Sstevel@tonic-gate 	 */
6270Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
6283930Snr123932 	while (lwp->lwp_nostop == 0 && !(p->p_flag & SEXITLWPS)) {
6290Sstevel@tonic-gate 		/*
6300Sstevel@tonic-gate 		 * Hold the lwp here for watchpoint manipulation.
6310Sstevel@tonic-gate 		 */
6323930Snr123932 		if (t->t_proc_flag & TP_PAUSE) {
6330Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_PAUSE);
6340Sstevel@tonic-gate 			continue;
6350Sstevel@tonic-gate 		}
6360Sstevel@tonic-gate 		/*
6370Sstevel@tonic-gate 		 * System checkpoint.
6380Sstevel@tonic-gate 		 */
6393930Snr123932 		if (t->t_proc_flag & TP_CHKPT) {
6400Sstevel@tonic-gate 			stop(PR_CHECKPOINT, 0);
6410Sstevel@tonic-gate 			continue;
6420Sstevel@tonic-gate 		}
6430Sstevel@tonic-gate 		/*
6440Sstevel@tonic-gate 		 * Honor fork1(), watchpoint activity (remapping a page),
6453930Snr123932 		 * and lwp_suspend() requests.
6460Sstevel@tonic-gate 		 */
6473930Snr123932 		if ((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
6483930Snr123932 		    (t->t_proc_flag & TP_HOLDLWP)) {
6490Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_NORMAL);
6500Sstevel@tonic-gate 			continue;
6510Sstevel@tonic-gate 		}
6520Sstevel@tonic-gate 		/*
6530Sstevel@tonic-gate 		 * Honor /proc requested stop.
6540Sstevel@tonic-gate 		 */
6553930Snr123932 		if (t->t_proc_flag & TP_PRSTOP) {
6560Sstevel@tonic-gate 			stop(PR_REQUESTED, 0);
6570Sstevel@tonic-gate 		}
6580Sstevel@tonic-gate 		/*
6590Sstevel@tonic-gate 		 * If some lwp in the process has already stopped
6600Sstevel@tonic-gate 		 * showing PR_JOBCONTROL, stop in sympathy with it.
6610Sstevel@tonic-gate 		 */
6623930Snr123932 		if (p->p_stopsig && t != p->p_agenttp) {
6630Sstevel@tonic-gate 			stop(PR_JOBCONTROL, p->p_stopsig);
6640Sstevel@tonic-gate 			continue;
6650Sstevel@tonic-gate 		}
6660Sstevel@tonic-gate 		break;
6670Sstevel@tonic-gate 	}
6680Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
6690Sstevel@tonic-gate 	mutex_enter(mp);
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate /*
6730Sstevel@tonic-gate  * Like cv_timedwait_sig(), but takes an absolute hires future time
6740Sstevel@tonic-gate  * rather than a future time in clock ticks.  Will not return showing
6750Sstevel@tonic-gate  * that a timeout occurred until the future time is passed.
6760Sstevel@tonic-gate  * If 'when' is a NULL pointer, no timeout will occur.
6770Sstevel@tonic-gate  * Returns:
6788048SMadhavan.Venkataraman@Sun.COM  * 	Function result in order of precedence:
6790Sstevel@tonic-gate  *		 0 if a signal was received
6800Sstevel@tonic-gate  *		-1 if timeout occured
6810Sstevel@tonic-gate  *	        >0 if awakened via cv_signal() or cv_broadcast()
6820Sstevel@tonic-gate  *		   or by a spurious wakeup.
6830Sstevel@tonic-gate  *		   (might return time remaining)
6844123Sdm120769  * As a special test, if someone abruptly resets the system time
6854123Sdm120769  * (but not through adjtime(2); drifting of the clock is allowed and
6864123Sdm120769  * expected [see timespectohz_adj()]), then we force a return of -1
6874123Sdm120769  * so the caller can return a premature timeout to the calling process
6884123Sdm120769  * so it can reevaluate the situation in light of the new system time.
6894123Sdm120769  * (The system clock has been reset if timecheck != timechanged.)
6900Sstevel@tonic-gate  */
6910Sstevel@tonic-gate int
6924123Sdm120769 cv_waituntil_sig(kcondvar_t *cvp, kmutex_t *mp,
6934123Sdm120769 	timestruc_t *when, int timecheck)
6940Sstevel@tonic-gate {
6950Sstevel@tonic-gate 	timestruc_t now;
6963346Svb160487 	timestruc_t delta;
697*9334SMadhavan.Venkataraman@Sun.COM 	hrtime_t interval;
6980Sstevel@tonic-gate 	int rval;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	if (when == NULL)
7010Sstevel@tonic-gate 		return (cv_wait_sig_swap(cvp, mp));
7020Sstevel@tonic-gate 
7037982SDonghai.Qiao@Sun.COM 	gethrestime(&now);
7043346Svb160487 	delta = *when;
7053346Svb160487 	timespecsub(&delta, &now);
7063346Svb160487 	if (delta.tv_sec < 0 || (delta.tv_sec == 0 && delta.tv_nsec == 0)) {
7070Sstevel@tonic-gate 		/*
7080Sstevel@tonic-gate 		 * We have already reached the absolute future time.
7090Sstevel@tonic-gate 		 * Call cv_timedwait_sig() just to check for signals.
7100Sstevel@tonic-gate 		 * We will return immediately with either 0 or -1.
7110Sstevel@tonic-gate 		 */
712*9334SMadhavan.Venkataraman@Sun.COM 		rval = cv_timedwait_sig_hires(cvp, mp, 0, 1, 0);
7130Sstevel@tonic-gate 	} else {
7144123Sdm120769 		if (timecheck == timechanged) {
715*9334SMadhavan.Venkataraman@Sun.COM 			/*
716*9334SMadhavan.Venkataraman@Sun.COM 			 * Make sure that the interval is atleast one tick.
717*9334SMadhavan.Venkataraman@Sun.COM 			 * This is to prevent a user from flooding the system
718*9334SMadhavan.Venkataraman@Sun.COM 			 * with very small, high resolution timers.
719*9334SMadhavan.Venkataraman@Sun.COM 			 */
720*9334SMadhavan.Venkataraman@Sun.COM 			interval = ts2hrt(&delta);
721*9334SMadhavan.Venkataraman@Sun.COM 			if (interval < nsec_per_tick)
722*9334SMadhavan.Venkataraman@Sun.COM 				interval = nsec_per_tick;
723*9334SMadhavan.Venkataraman@Sun.COM 			rval = cv_timedwait_sig_hires(cvp, mp, interval, 1,
7248048SMadhavan.Venkataraman@Sun.COM 			    CALLOUT_FLAG_HRESTIME);
7254123Sdm120769 		} else {
7264123Sdm120769 			/*
7274123Sdm120769 			 * Someone reset the system time;
7284123Sdm120769 			 * just force an immediate timeout.
7294123Sdm120769 			 */
7304123Sdm120769 			rval = -1;
7314123Sdm120769 		}
7324123Sdm120769 		if (rval == -1 && timecheck == timechanged) {
7334123Sdm120769 			/*
7344123Sdm120769 			 * Even though cv_timedwait_sig() returned showing a
7354123Sdm120769 			 * timeout, the future time may not have passed yet.
7364123Sdm120769 			 * If not, change rval to indicate a normal wakeup.
7374123Sdm120769 			 */
7384123Sdm120769 			gethrestime(&now);
7394123Sdm120769 			delta = *when;
7404123Sdm120769 			timespecsub(&delta, &now);
7414123Sdm120769 			if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
7424123Sdm120769 			    delta.tv_nsec > 0))
7430Sstevel@tonic-gate 				rval = 1;
7444123Sdm120769 		}
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 	return (rval);
7470Sstevel@tonic-gate }
748