xref: /onnv-gate/usr/src/uts/common/os/condvar.c (revision 3930:59477feabc54)
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  */
210Sstevel@tonic-gate /*
223642Sqiao  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/thread.h>
290Sstevel@tonic-gate #include <sys/proc.h>
300Sstevel@tonic-gate #include <sys/debug.h>
310Sstevel@tonic-gate #include <sys/cmn_err.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/sobject.h>
340Sstevel@tonic-gate #include <sys/sleepq.h>
350Sstevel@tonic-gate #include <sys/cpuvar.h>
360Sstevel@tonic-gate #include <sys/condvar.h>
370Sstevel@tonic-gate #include <sys/condvar_impl.h>
380Sstevel@tonic-gate #include <sys/schedctl.h>
390Sstevel@tonic-gate #include <sys/procfs.h>
400Sstevel@tonic-gate #include <sys/sdt.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * CV_MAX_WAITERS is the maximum number of waiters we track; once
440Sstevel@tonic-gate  * the number becomes higher than that, we look at the sleepq to
450Sstevel@tonic-gate  * see whether there are *really* any waiters.
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate #define	CV_MAX_WAITERS		1024		/* must be power of 2 */
480Sstevel@tonic-gate #define	CV_WAITERS_MASK		(CV_MAX_WAITERS - 1)
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * Threads don't "own" condition variables.
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate /* ARGSUSED */
540Sstevel@tonic-gate static kthread_t *
550Sstevel@tonic-gate cv_owner(void *cvp)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	return (NULL);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * Unsleep a thread that's blocked on a condition variable.
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate static void
640Sstevel@tonic-gate cv_unsleep(kthread_t *t)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
670Sstevel@tonic-gate 	sleepq_head_t *sqh = SQHASH(cvp);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	if (cvp == NULL)
720Sstevel@tonic-gate 		panic("cv_unsleep: thread %p not on sleepq %p", t, sqh);
732498Sstevel 	DTRACE_SCHED1(wakeup, kthread_t *, t);
740Sstevel@tonic-gate 	sleepq_unsleep(t);
750Sstevel@tonic-gate 	if (cvp->cv_waiters != CV_MAX_WAITERS)
760Sstevel@tonic-gate 		cvp->cv_waiters--;
770Sstevel@tonic-gate 	disp_lock_exit_high(&sqh->sq_lock);
780Sstevel@tonic-gate 	CL_SETRUN(t);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * Change the priority of a thread that's blocked on a condition variable.
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate static void
850Sstevel@tonic-gate cv_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
880Sstevel@tonic-gate 	sleepq_t *sqp = t->t_sleepq;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
910Sstevel@tonic-gate 	ASSERT(&SQHASH(cvp)->sq_queue == sqp);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	if (cvp == NULL)
940Sstevel@tonic-gate 		panic("cv_change_pri: %p not on sleep queue", t);
950Sstevel@tonic-gate 	sleepq_dequeue(t);
960Sstevel@tonic-gate 	*t_prip = pri;
970Sstevel@tonic-gate 	sleepq_insert(sqp, t);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * The sobj_ops vector exports a set of functions needed when a thread
1020Sstevel@tonic-gate  * is asleep on a synchronization object of this type.
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate static sobj_ops_t cv_sobj_ops = {
1050Sstevel@tonic-gate 	SOBJ_CV, cv_owner, cv_unsleep, cv_change_pri
1060Sstevel@tonic-gate };
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /* ARGSUSED */
1090Sstevel@tonic-gate void
1100Sstevel@tonic-gate cv_init(kcondvar_t *cvp, char *name, kcv_type_t type, void *arg)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	((condvar_impl_t *)cvp)->cv_waiters = 0;
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate  * cv_destroy is not currently needed, but is part of the DDI.
1170Sstevel@tonic-gate  * This is in case cv_init ever needs to allocate something for a cv.
1180Sstevel@tonic-gate  */
1190Sstevel@tonic-gate /* ARGSUSED */
1200Sstevel@tonic-gate void
1210Sstevel@tonic-gate cv_destroy(kcondvar_t *cvp)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	ASSERT((((condvar_impl_t *)cvp)->cv_waiters & CV_WAITERS_MASK) == 0);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate  * The cv_block() function blocks a thread on a condition variable
1280Sstevel@tonic-gate  * by putting it in a hashed sleep queue associated with the
1290Sstevel@tonic-gate  * synchronization object.
1300Sstevel@tonic-gate  *
1310Sstevel@tonic-gate  * Threads are taken off the hashed sleep queues via calls to
1320Sstevel@tonic-gate  * cv_signal(), cv_broadcast(), or cv_unsleep().
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate static void
1350Sstevel@tonic-gate cv_block(condvar_impl_t *cvp)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate 	kthread_t *t = curthread;
1380Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
1390Sstevel@tonic-gate 	sleepq_head_t *sqh;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1420Sstevel@tonic-gate 	ASSERT(t != CPU->cpu_idle_thread);
1430Sstevel@tonic-gate 	ASSERT(CPU_ON_INTR(CPU) == 0);
1440Sstevel@tonic-gate 	ASSERT(t->t_wchan0 == NULL && t->t_wchan == NULL);
1450Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	t->t_schedflag &= ~TS_SIGNALLED;
1480Sstevel@tonic-gate 	CL_SLEEP(t);			/* assign kernel priority */
1490Sstevel@tonic-gate 	t->t_wchan = (caddr_t)cvp;
1500Sstevel@tonic-gate 	t->t_sobj_ops = &cv_sobj_ops;
1510Sstevel@tonic-gate 	DTRACE_SCHED(sleep);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/*
1540Sstevel@tonic-gate 	 * The check for t_intr is to avoid doing the
1550Sstevel@tonic-gate 	 * account for an interrupt thread on the still-pinned
1560Sstevel@tonic-gate 	 * lwp's statistics.
1570Sstevel@tonic-gate 	 */
1580Sstevel@tonic-gate 	if (lwp != NULL && t->t_intr == NULL) {
1590Sstevel@tonic-gate 		lwp->lwp_ru.nvcsw++;
1600Sstevel@tonic-gate 		(void) new_mstate(t, LMS_SLEEP);
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	sqh = SQHASH(cvp);
1640Sstevel@tonic-gate 	disp_lock_enter_high(&sqh->sq_lock);
1650Sstevel@tonic-gate 	if (cvp->cv_waiters < CV_MAX_WAITERS)
1660Sstevel@tonic-gate 		cvp->cv_waiters++;
1670Sstevel@tonic-gate 	ASSERT(cvp->cv_waiters <= CV_MAX_WAITERS);
1680Sstevel@tonic-gate 	THREAD_SLEEP(t, &sqh->sq_lock);
1690Sstevel@tonic-gate 	sleepq_insert(&sqh->sq_queue, t);
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * THREAD_SLEEP() moves curthread->t_lockp to point to the
1720Sstevel@tonic-gate 	 * lock sqh->sq_lock. This lock is later released by the caller
1730Sstevel@tonic-gate 	 * when it calls thread_unlock() on curthread.
1740Sstevel@tonic-gate 	 */
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate #define	cv_block_sig(t, cvp)	\
1780Sstevel@tonic-gate 	{ (t)->t_flag |= T_WAKEABLE; cv_block(cvp); }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate  * Block on the indicated condition variable and release the
1820Sstevel@tonic-gate  * associated kmutex while blocked.
1830Sstevel@tonic-gate  */
1840Sstevel@tonic-gate void
1850Sstevel@tonic-gate cv_wait(kcondvar_t *cvp, kmutex_t *mp)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	if (panicstr)
1880Sstevel@tonic-gate 		return;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
1910Sstevel@tonic-gate 	thread_lock(curthread);			/* lock the thread */
1920Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
1930Sstevel@tonic-gate 	thread_unlock_nopreempt(curthread);	/* unlock the waiters field */
1940Sstevel@tonic-gate 	mutex_exit(mp);
1950Sstevel@tonic-gate 	swtch();
1960Sstevel@tonic-gate 	mutex_enter(mp);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate  * Same as cv_wait except the thread will unblock at 'tim'
2010Sstevel@tonic-gate  * (an absolute time) if it hasn't already unblocked.
2020Sstevel@tonic-gate  *
2030Sstevel@tonic-gate  * Returns the amount of time left from the original 'tim' value
2040Sstevel@tonic-gate  * when it was unblocked.
2050Sstevel@tonic-gate  */
2060Sstevel@tonic-gate clock_t
2070Sstevel@tonic-gate cv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	kthread_t *t = curthread;
2100Sstevel@tonic-gate 	timeout_id_t id;
2110Sstevel@tonic-gate 	clock_t timeleft;
2120Sstevel@tonic-gate 	int signalled;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	if (panicstr)
2150Sstevel@tonic-gate 		return (-1);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	timeleft = tim - lbolt;
2180Sstevel@tonic-gate 	if (timeleft <= 0)
2190Sstevel@tonic-gate 		return (-1);
2200Sstevel@tonic-gate 	id = realtime_timeout((void (*)(void *))setrun, t, timeleft);
2210Sstevel@tonic-gate 	thread_lock(t);		/* lock the thread */
2220Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
2230Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
2240Sstevel@tonic-gate 	mutex_exit(mp);
2250Sstevel@tonic-gate 	if ((tim - lbolt) <= 0)		/* allow for wrap */
2260Sstevel@tonic-gate 		setrun(t);
2270Sstevel@tonic-gate 	swtch();
2280Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
2290Sstevel@tonic-gate 	/*
2300Sstevel@tonic-gate 	 * Get the time left. untimeout() returns -1 if the timeout has
2310Sstevel@tonic-gate 	 * occured or the time remaining.  If the time remaining is zero,
2320Sstevel@tonic-gate 	 * the timeout has occured between when we were awoken and
2330Sstevel@tonic-gate 	 * we called untimeout.  We will treat this as if the timeout
2340Sstevel@tonic-gate 	 * has occured and set timeleft to -1.
2350Sstevel@tonic-gate 	 */
2360Sstevel@tonic-gate 	timeleft = untimeout(id);
2370Sstevel@tonic-gate 	mutex_enter(mp);
2380Sstevel@tonic-gate 	if (timeleft <= 0) {
2390Sstevel@tonic-gate 		timeleft = -1;
2400Sstevel@tonic-gate 		if (signalled)	/* avoid consuming the cv_signal() */
2410Sstevel@tonic-gate 			cv_signal(cvp);
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate 	return (timeleft);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate int
2470Sstevel@tonic-gate cv_wait_sig(kcondvar_t *cvp, kmutex_t *mp)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	kthread_t *t = curthread;
2500Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
2510Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
2520Sstevel@tonic-gate 	int rval = 1;
2530Sstevel@tonic-gate 	int signalled = 0;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	if (panicstr)
2560Sstevel@tonic-gate 		return (rval);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	/*
2590Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
2600Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
2610Sstevel@tonic-gate 	 */
2620Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr) {
2630Sstevel@tonic-gate 		cv_wait(cvp, mp);
2640Sstevel@tonic-gate 		return (rval);
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
2680Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
2690Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
2700Sstevel@tonic-gate 	thread_lock(t);
2710Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
2720Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
2730Sstevel@tonic-gate 	mutex_exit(mp);
2740Sstevel@tonic-gate 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t))
2750Sstevel@tonic-gate 		setrun(t);
2760Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
2770Sstevel@tonic-gate 	swtch();
2780Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
2790Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
2800Sstevel@tonic-gate 	mutex_enter(mp);
2810Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
2820Sstevel@tonic-gate 		mutex_exit(mp);
2830Sstevel@tonic-gate 		if (issig(FORREAL))
2840Sstevel@tonic-gate 			rval = 0;
2850Sstevel@tonic-gate 		mutex_enter(mp);
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
2880Sstevel@tonic-gate 		rval = 0;
2890Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
2900Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
2910Sstevel@tonic-gate 	if (rval == 0 && signalled)	/* avoid consuming the cv_signal() */
2920Sstevel@tonic-gate 		cv_signal(cvp);
2930Sstevel@tonic-gate 	return (rval);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate  * Returns:
2980Sstevel@tonic-gate  * 	Function result in order of presidence:
2990Sstevel@tonic-gate  *		 0 if a signal was received
3000Sstevel@tonic-gate  *		-1 if timeout occured
3010Sstevel@tonic-gate  *		>0 if awakened via cv_signal() or cv_broadcast().
3020Sstevel@tonic-gate  *		   (returns time remaining)
3030Sstevel@tonic-gate  *
3040Sstevel@tonic-gate  * cv_timedwait_sig() is now part of the DDI.
3050Sstevel@tonic-gate  */
3060Sstevel@tonic-gate clock_t
3070Sstevel@tonic-gate cv_timedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	kthread_t *t = curthread;
3100Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
3110Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
3120Sstevel@tonic-gate 	timeout_id_t id;
3130Sstevel@tonic-gate 	clock_t rval = 1;
3140Sstevel@tonic-gate 	clock_t timeleft;
3150Sstevel@tonic-gate 	int signalled = 0;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (panicstr)
3180Sstevel@tonic-gate 		return (rval);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/*
3210Sstevel@tonic-gate 	 * If there is no lwp, then we don't need to wait for a signal.
3220Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
3230Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
3240Sstevel@tonic-gate 	 */
3250Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr)
3260Sstevel@tonic-gate 		return (cv_timedwait(cvp, mp, tim));
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	/*
3290Sstevel@tonic-gate 	 * If tim is less than or equal to lbolt, then the timeout
3300Sstevel@tonic-gate 	 * has already occured.  So just check to see if there is a signal
3310Sstevel@tonic-gate 	 * pending.  If so return 0 indicating that there is a signal pending.
3320Sstevel@tonic-gate 	 * Else return -1 indicating that the timeout occured. No need to
3330Sstevel@tonic-gate 	 * wait on anything.
3340Sstevel@tonic-gate 	 */
3350Sstevel@tonic-gate 	timeleft = tim - lbolt;
3360Sstevel@tonic-gate 	if (timeleft <= 0) {
3370Sstevel@tonic-gate 		lwp->lwp_asleep = 1;
3380Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;
3390Sstevel@tonic-gate 		rval = -1;
3400Sstevel@tonic-gate 		goto out;
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/*
3440Sstevel@tonic-gate 	 * Set the timeout and wait.
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 	id = realtime_timeout((void (*)(void *))setrun, t, timeleft);
3470Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
3480Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
3490Sstevel@tonic-gate 	thread_lock(t);
3500Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
3510Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
3520Sstevel@tonic-gate 	mutex_exit(mp);
3530Sstevel@tonic-gate 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || (tim - lbolt <= 0))
3540Sstevel@tonic-gate 		setrun(t);
3550Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
3560Sstevel@tonic-gate 	swtch();
3570Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
3580Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
3590Sstevel@tonic-gate 	mutex_enter(mp);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	/*
3620Sstevel@tonic-gate 	 * Untimeout the thread.  untimeout() returns -1 if the timeout has
3630Sstevel@tonic-gate 	 * occured or the time remaining.  If the time remaining is zero,
3640Sstevel@tonic-gate 	 * the timeout has occured between when we were awoken and
3650Sstevel@tonic-gate 	 * we called untimeout.  We will treat this as if the timeout
3660Sstevel@tonic-gate 	 * has occured and set rval to -1.
3670Sstevel@tonic-gate 	 */
3680Sstevel@tonic-gate 	rval = untimeout(id);
3690Sstevel@tonic-gate 	if (rval <= 0)
3700Sstevel@tonic-gate 		rval = -1;
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	/*
3730Sstevel@tonic-gate 	 * Check to see if a signal is pending.  If so, regardless of whether
3740Sstevel@tonic-gate 	 * or not we were awoken due to the signal, the signal is now pending
3750Sstevel@tonic-gate 	 * and a return of 0 has the highest priority.
3760Sstevel@tonic-gate 	 */
3770Sstevel@tonic-gate out:
3780Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
3790Sstevel@tonic-gate 		mutex_exit(mp);
3800Sstevel@tonic-gate 		if (issig(FORREAL))
3810Sstevel@tonic-gate 			rval = 0;
3820Sstevel@tonic-gate 		mutex_enter(mp);
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
3850Sstevel@tonic-gate 		rval = 0;
3860Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
3870Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
3880Sstevel@tonic-gate 	if (rval <= 0 && signalled)	/* avoid consuming the cv_signal() */
3890Sstevel@tonic-gate 		cv_signal(cvp);
3900Sstevel@tonic-gate 	return (rval);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate /*
3940Sstevel@tonic-gate  * Like cv_wait_sig_swap but allows the caller to indicate (with a
3950Sstevel@tonic-gate  * non-NULL sigret) that they will take care of signalling the cv
3960Sstevel@tonic-gate  * after wakeup, if necessary.  This is a vile hack that should only
3970Sstevel@tonic-gate  * be used when no other option is available; almost all callers
3980Sstevel@tonic-gate  * should just use cv_wait_sig_swap (which takes care of the cv_signal
3990Sstevel@tonic-gate  * stuff automatically) instead.
4000Sstevel@tonic-gate  */
4010Sstevel@tonic-gate int
4020Sstevel@tonic-gate cv_wait_sig_swap_core(kcondvar_t *cvp, kmutex_t *mp, int *sigret)
4030Sstevel@tonic-gate {
4040Sstevel@tonic-gate 	kthread_t *t = curthread;
4050Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
4060Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
4070Sstevel@tonic-gate 	int rval = 1;
4080Sstevel@tonic-gate 	int signalled = 0;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	if (panicstr)
4110Sstevel@tonic-gate 		return (rval);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	/*
4140Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
4150Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
4160Sstevel@tonic-gate 	 */
4170Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr) {
4180Sstevel@tonic-gate 		cv_wait(cvp, mp);
4190Sstevel@tonic-gate 		return (rval);
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	lwp->lwp_asleep = 1;
4230Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
4240Sstevel@tonic-gate 	thread_lock(t);
4250Sstevel@tonic-gate 	t->t_kpri_req = 0;	/* don't need kernel priority */
4260Sstevel@tonic-gate 	cv_block_sig(t, (condvar_impl_t *)cvp);
4270Sstevel@tonic-gate 	/* I can be swapped now */
4280Sstevel@tonic-gate 	curthread->t_schedflag &= ~TS_DONT_SWAP;
4290Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
4300Sstevel@tonic-gate 	mutex_exit(mp);
4310Sstevel@tonic-gate 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t))
4320Sstevel@tonic-gate 		setrun(t);
4330Sstevel@tonic-gate 	/* ASSERT(no locks are held) */
4340Sstevel@tonic-gate 	swtch();
4350Sstevel@tonic-gate 	signalled = (t->t_schedflag & TS_SIGNALLED);
4360Sstevel@tonic-gate 	t->t_flag &= ~T_WAKEABLE;
4370Sstevel@tonic-gate 	/* TS_DONT_SWAP set by disp() */
4380Sstevel@tonic-gate 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
4390Sstevel@tonic-gate 	mutex_enter(mp);
4400Sstevel@tonic-gate 	if (ISSIG_PENDING(t, lwp, p)) {
4410Sstevel@tonic-gate 		mutex_exit(mp);
4420Sstevel@tonic-gate 		if (issig(FORREAL))
4430Sstevel@tonic-gate 			rval = 0;
4440Sstevel@tonic-gate 		mutex_enter(mp);
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
4470Sstevel@tonic-gate 		rval = 0;
4480Sstevel@tonic-gate 	lwp->lwp_asleep = 0;
4490Sstevel@tonic-gate 	lwp->lwp_sysabort = 0;
4500Sstevel@tonic-gate 	if (rval == 0) {
4510Sstevel@tonic-gate 		if (sigret != NULL)
4520Sstevel@tonic-gate 			*sigret = signalled;	/* just tell the caller */
4530Sstevel@tonic-gate 		else if (signalled)
4540Sstevel@tonic-gate 			cv_signal(cvp);	/* avoid consuming the cv_signal() */
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 	return (rval);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate /*
4600Sstevel@tonic-gate  * Same as cv_wait_sig but the thread can be swapped out while waiting.
4610Sstevel@tonic-gate  * This should only be used when we know we aren't holding any locks.
4620Sstevel@tonic-gate  */
4630Sstevel@tonic-gate int
4640Sstevel@tonic-gate cv_wait_sig_swap(kcondvar_t *cvp, kmutex_t *mp)
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate 	return (cv_wait_sig_swap_core(cvp, mp, NULL));
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate void
4700Sstevel@tonic-gate cv_signal(kcondvar_t *cvp)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	/* make sure the cv_waiters field looks sane */
4750Sstevel@tonic-gate 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
4760Sstevel@tonic-gate 	if (cp->cv_waiters > 0) {
4770Sstevel@tonic-gate 		sleepq_head_t *sqh = SQHASH(cp);
4780Sstevel@tonic-gate 		disp_lock_enter(&sqh->sq_lock);
4790Sstevel@tonic-gate 		ASSERT(CPU_ON_INTR(CPU) == 0);
4800Sstevel@tonic-gate 		if (cp->cv_waiters & CV_WAITERS_MASK) {
4810Sstevel@tonic-gate 			kthread_t *t;
4820Sstevel@tonic-gate 			cp->cv_waiters--;
4830Sstevel@tonic-gate 			t = sleepq_wakeone_chan(&sqh->sq_queue, cp);
4840Sstevel@tonic-gate 			/*
4850Sstevel@tonic-gate 			 * If cv_waiters is non-zero (and less than
4860Sstevel@tonic-gate 			 * CV_MAX_WAITERS) there should be a thread
4870Sstevel@tonic-gate 			 * in the queue.
4880Sstevel@tonic-gate 			 */
4890Sstevel@tonic-gate 			ASSERT(t != NULL);
4900Sstevel@tonic-gate 		} else if (sleepq_wakeone_chan(&sqh->sq_queue, cp) == NULL) {
4910Sstevel@tonic-gate 			cp->cv_waiters = 0;
4920Sstevel@tonic-gate 		}
4930Sstevel@tonic-gate 		disp_lock_exit(&sqh->sq_lock);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate void
4980Sstevel@tonic-gate cv_broadcast(kcondvar_t *cvp)
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	/* make sure the cv_waiters field looks sane */
5030Sstevel@tonic-gate 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
5040Sstevel@tonic-gate 	if (cp->cv_waiters > 0) {
5050Sstevel@tonic-gate 		sleepq_head_t *sqh = SQHASH(cp);
5060Sstevel@tonic-gate 		disp_lock_enter(&sqh->sq_lock);
5070Sstevel@tonic-gate 		ASSERT(CPU_ON_INTR(CPU) == 0);
5080Sstevel@tonic-gate 		sleepq_wakeall_chan(&sqh->sq_queue, cp);
5090Sstevel@tonic-gate 		cp->cv_waiters = 0;
5100Sstevel@tonic-gate 		disp_lock_exit(&sqh->sq_lock);
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate /*
5150Sstevel@tonic-gate  * Same as cv_wait(), but wakes up (after wakeup_time milliseconds) to check
5160Sstevel@tonic-gate  * for requests to stop, like cv_wait_sig() but without dealing with signals.
5170Sstevel@tonic-gate  * This is a horrible kludge.  It is evil.  It is vile.  It is swill.
5180Sstevel@tonic-gate  * If your code has to call this function then your code is the same.
5190Sstevel@tonic-gate  */
5200Sstevel@tonic-gate void
5210Sstevel@tonic-gate cv_wait_stop(kcondvar_t *cvp, kmutex_t *mp, int wakeup_time)
5220Sstevel@tonic-gate {
5230Sstevel@tonic-gate 	kthread_t *t = curthread;
5240Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
5250Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
5260Sstevel@tonic-gate 	timeout_id_t id;
5270Sstevel@tonic-gate 	clock_t tim;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	if (panicstr)
5300Sstevel@tonic-gate 		return;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	/*
5330Sstevel@tonic-gate 	 * If there is no lwp, then we don't need to eventually stop it
5340Sstevel@tonic-gate 	 * The check for t_intr is to catch an interrupt thread
5350Sstevel@tonic-gate 	 * that has not yet unpinned the thread underneath.
5360Sstevel@tonic-gate 	 */
5370Sstevel@tonic-gate 	if (lwp == NULL || t->t_intr) {
5380Sstevel@tonic-gate 		cv_wait(cvp, mp);
5390Sstevel@tonic-gate 		return;
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	/*
5430Sstevel@tonic-gate 	 * Wakeup in wakeup_time milliseconds, i.e., human time.
5440Sstevel@tonic-gate 	 */
5450Sstevel@tonic-gate 	tim = lbolt + MSEC_TO_TICK(wakeup_time);
5460Sstevel@tonic-gate 	id = realtime_timeout((void (*)(void *))setrun, t, tim - lbolt);
5470Sstevel@tonic-gate 	thread_lock(t);			/* lock the thread */
5480Sstevel@tonic-gate 	cv_block((condvar_impl_t *)cvp);
5490Sstevel@tonic-gate 	thread_unlock_nopreempt(t);
5500Sstevel@tonic-gate 	mutex_exit(mp);
5510Sstevel@tonic-gate 	/* ASSERT(no locks are held); */
5520Sstevel@tonic-gate 	if ((tim - lbolt) <= 0)		/* allow for wrap */
5530Sstevel@tonic-gate 		setrun(t);
5540Sstevel@tonic-gate 	swtch();
5550Sstevel@tonic-gate 	(void) untimeout(id);
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	/*
558*3930Snr123932 	 * Check for reasons to stop, if lwp_nostop is not true.
5590Sstevel@tonic-gate 	 * See issig_forreal() for explanations of the various stops.
5600Sstevel@tonic-gate 	 */
5610Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
562*3930Snr123932 	while (lwp->lwp_nostop == 0 && !(p->p_flag & SEXITLWPS)) {
5630Sstevel@tonic-gate 		/*
5640Sstevel@tonic-gate 		 * Hold the lwp here for watchpoint manipulation.
5650Sstevel@tonic-gate 		 */
566*3930Snr123932 		if (t->t_proc_flag & TP_PAUSE) {
5670Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_PAUSE);
5680Sstevel@tonic-gate 			continue;
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 		/*
5710Sstevel@tonic-gate 		 * System checkpoint.
5720Sstevel@tonic-gate 		 */
573*3930Snr123932 		if (t->t_proc_flag & TP_CHKPT) {
5740Sstevel@tonic-gate 			stop(PR_CHECKPOINT, 0);
5750Sstevel@tonic-gate 			continue;
5760Sstevel@tonic-gate 		}
5770Sstevel@tonic-gate 		/*
5780Sstevel@tonic-gate 		 * Honor fork1(), watchpoint activity (remapping a page),
579*3930Snr123932 		 * and lwp_suspend() requests.
5800Sstevel@tonic-gate 		 */
581*3930Snr123932 		if ((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
582*3930Snr123932 		    (t->t_proc_flag & TP_HOLDLWP)) {
5830Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_NORMAL);
5840Sstevel@tonic-gate 			continue;
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate 		/*
5870Sstevel@tonic-gate 		 * Honor /proc requested stop.
5880Sstevel@tonic-gate 		 */
589*3930Snr123932 		if (t->t_proc_flag & TP_PRSTOP) {
5900Sstevel@tonic-gate 			stop(PR_REQUESTED, 0);
5910Sstevel@tonic-gate 		}
5920Sstevel@tonic-gate 		/*
5930Sstevel@tonic-gate 		 * If some lwp in the process has already stopped
5940Sstevel@tonic-gate 		 * showing PR_JOBCONTROL, stop in sympathy with it.
5950Sstevel@tonic-gate 		 */
596*3930Snr123932 		if (p->p_stopsig && t != p->p_agenttp) {
5970Sstevel@tonic-gate 			stop(PR_JOBCONTROL, p->p_stopsig);
5980Sstevel@tonic-gate 			continue;
5990Sstevel@tonic-gate 		}
6000Sstevel@tonic-gate 		break;
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
6030Sstevel@tonic-gate 	mutex_enter(mp);
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate /*
6070Sstevel@tonic-gate  * Like cv_timedwait_sig(), but takes an absolute hires future time
6080Sstevel@tonic-gate  * rather than a future time in clock ticks.  Will not return showing
6090Sstevel@tonic-gate  * that a timeout occurred until the future time is passed.
6100Sstevel@tonic-gate  * If 'when' is a NULL pointer, no timeout will occur.
6110Sstevel@tonic-gate  * Returns:
6120Sstevel@tonic-gate  * 	Function result in order of presidence:
6130Sstevel@tonic-gate  *		 0 if a signal was received
6140Sstevel@tonic-gate  *		-1 if timeout occured
6150Sstevel@tonic-gate  *	        >0 if awakened via cv_signal() or cv_broadcast()
6160Sstevel@tonic-gate  *		   or by a spurious wakeup.
6170Sstevel@tonic-gate  *		   (might return time remaining)
6180Sstevel@tonic-gate  */
6190Sstevel@tonic-gate int
6203642Sqiao cv_waituntil_sig(kcondvar_t *cvp, kmutex_t *mp, timestruc_t *when)
6210Sstevel@tonic-gate {
6220Sstevel@tonic-gate 	timestruc_t now;
6233346Svb160487 	timestruc_t delta;
6243642Sqiao 	clock_t ticks;
6250Sstevel@tonic-gate 	int rval;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	if (when == NULL)
6280Sstevel@tonic-gate 		return (cv_wait_sig_swap(cvp, mp));
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	gethrestime(&now);
6313346Svb160487 	delta = *when;
6323346Svb160487 	timespecsub(&delta, &now);
6333346Svb160487 	if (delta.tv_sec < 0 || (delta.tv_sec == 0 && delta.tv_nsec == 0)) {
6340Sstevel@tonic-gate 		/*
6350Sstevel@tonic-gate 		 * We have already reached the absolute future time.
6360Sstevel@tonic-gate 		 * Call cv_timedwait_sig() just to check for signals.
6370Sstevel@tonic-gate 		 * We will return immediately with either 0 or -1.
6380Sstevel@tonic-gate 		 */
6390Sstevel@tonic-gate 		rval = cv_timedwait_sig(cvp, mp, lbolt);
6400Sstevel@tonic-gate 	} else {
6413642Sqiao 		ticks = lbolt + timespectohz(when, now);
6423642Sqiao 		rval = cv_timedwait_sig(cvp, mp, ticks);
6433642Sqiao 
6443642Sqiao 		gethrestime(&now);
6453642Sqiao 		delta = *when;
6463642Sqiao 		timespecsub(&delta, &now);
6473642Sqiao 
6483642Sqiao 		/*
6493642Sqiao 		 * timeout is premature iff
6503642Sqiao 		 *	ticks >= lbolt  and  when > now
6513642Sqiao 		 */
6523642Sqiao 		if (rval == -1 && ticks >= lbolt && (delta.tv_sec > 0 ||
6533642Sqiao 		    (delta.tv_sec == 0 && delta.tv_nsec > 0)))
6540Sstevel@tonic-gate 				rval = 1;
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate 	return (rval);
6570Sstevel@tonic-gate }
658