17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
52db6c79stevel * Common Development and Distribution License (the "License").
62db6c79stevel * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
21a574db8raf
227c478bdstevel@tonic-gate/*
231c4c388Rafael Vanoni * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
27cd1c8b8Matthew Ahrens/*
28cd1c8b8Matthew Ahrens * Copyright (c) 2012 by Delphix. All rights reserved.
296a0b121Patrick Mooney * Copyright 2019 Joyent, Inc.
30cd1c8b8Matthew Ahrens */
31fe234e7Matt Amdur
327c478bdstevel@tonic-gate#include <sys/thread.h>
337c478bdstevel@tonic-gate#include <sys/proc.h>
347c478bdstevel@tonic-gate#include <sys/debug.h>
357c478bdstevel@tonic-gate#include <sys/cmn_err.h>
367c478bdstevel@tonic-gate#include <sys/systm.h>
377c478bdstevel@tonic-gate#include <sys/sobject.h>
387c478bdstevel@tonic-gate#include <sys/sleepq.h>
397c478bdstevel@tonic-gate#include <sys/cpuvar.h>
407c478bdstevel@tonic-gate#include <sys/condvar.h>
417c478bdstevel@tonic-gate#include <sys/condvar_impl.h>
427c478bdstevel@tonic-gate#include <sys/schedctl.h>
437c478bdstevel@tonic-gate#include <sys/procfs.h>
447c478bdstevel@tonic-gate#include <sys/sdt.h>
4587a18d3Madhavan Venkataraman#include <sys/callo.h>
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gate/*
487c478bdstevel@tonic-gate * CV_MAX_WAITERS is the maximum number of waiters we track; once
497c478bdstevel@tonic-gate * the number becomes higher than that, we look at the sleepq to
507c478bdstevel@tonic-gate * see whether there are *really* any waiters.
517c478bdstevel@tonic-gate */
527c478bdstevel@tonic-gate#define	CV_MAX_WAITERS		1024		/* must be power of 2 */
537c478bdstevel@tonic-gate#define	CV_WAITERS_MASK		(CV_MAX_WAITERS - 1)
547c478bdstevel@tonic-gate
557c478bdstevel@tonic-gate/*
567c478bdstevel@tonic-gate * Threads don't "own" condition variables.
577c478bdstevel@tonic-gate */
587c478bdstevel@tonic-gate/* ARGSUSED */
597c478bdstevel@tonic-gatestatic kthread_t *
607c478bdstevel@tonic-gatecv_owner(void *cvp)
617c478bdstevel@tonic-gate{
627c478bdstevel@tonic-gate	return (NULL);
637c478bdstevel@tonic-gate}
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gate/*
667c478bdstevel@tonic-gate * Unsleep a thread that's blocked on a condition variable.
677c478bdstevel@tonic-gate */
687c478bdstevel@tonic-gatestatic void
697c478bdstevel@tonic-gatecv_unsleep(kthread_t *t)
707c478bdstevel@tonic-gate{
717c478bdstevel@tonic-gate	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
727c478bdstevel@tonic-gate	sleepq_head_t *sqh = SQHASH(cvp);
737c478bdstevel@tonic-gate
747c478bdstevel@tonic-gate	ASSERT(THREAD_LOCK_HELD(t));
757c478bdstevel@tonic-gate
767c478bdstevel@tonic-gate	if (cvp == NULL)
77903a11erh		panic("cv_unsleep: thread %p not on sleepq %p",
78903a11erh		    (void *)t, (void *)sqh);
792db6c79stevel	DTRACE_SCHED1(wakeup, kthread_t *, t);
807c478bdstevel@tonic-gate	sleepq_unsleep(t);
817c478bdstevel@tonic-gate	if (cvp->cv_waiters != CV_MAX_WAITERS)
827c478bdstevel@tonic-gate		cvp->cv_waiters--;
837c478bdstevel@tonic-gate	disp_lock_exit_high(&sqh->sq_lock);
847c478bdstevel@tonic-gate	CL_SETRUN(t);
857c478bdstevel@tonic-gate}
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gate/*
887c478bdstevel@tonic-gate * Change the priority of a thread that's blocked on a condition variable.
897c478bdstevel@tonic-gate */
907c478bdstevel@tonic-gatestatic void
917c478bdstevel@tonic-gatecv_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip)
927c478bdstevel@tonic-gate{
937c478bdstevel@tonic-gate	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
947c478bdstevel@tonic-gate	sleepq_t *sqp = t->t_sleepq;
957c478bdstevel@tonic-gate
967c478bdstevel@tonic-gate	ASSERT(THREAD_LOCK_HELD(t));
977c478bdstevel@tonic-gate	ASSERT(&SQHASH(cvp)->sq_queue == sqp);
987c478bdstevel@tonic-gate
997c478bdstevel@tonic-gate	if (cvp == NULL)
100903a11erh		panic("cv_change_pri: %p not on sleep queue", (void *)t);
1017c478bdstevel@tonic-gate	sleepq_dequeue(t);
1027c478bdstevel@tonic-gate	*t_prip = pri;
1037c478bdstevel@tonic-gate	sleepq_insert(sqp, t);
1047c478bdstevel@tonic-gate}
1057c478bdstevel@tonic-gate
1067c478bdstevel@tonic-gate/*
1077c478bdstevel@tonic-gate * The sobj_ops vector exports a set of functions needed when a thread
1087c478bdstevel@tonic-gate * is asleep on a synchronization object of this type.
1097c478bdstevel@tonic-gate */
1107c478bdstevel@tonic-gatestatic sobj_ops_t cv_sobj_ops = {
1117c478bdstevel@tonic-gate	SOBJ_CV, cv_owner, cv_unsleep, cv_change_pri
1127c478bdstevel@tonic-gate};
1137c478bdstevel@tonic-gate
1147c478bdstevel@tonic-gate/* ARGSUSED */
1157c478bdstevel@tonic-gatevoid
1167c478bdstevel@tonic-gatecv_init(kcondvar_t *cvp, char *name, kcv_type_t type, void *arg)
1177c478bdstevel@tonic-gate{
1187c478bdstevel@tonic-gate	((condvar_impl_t *)cvp)->cv_waiters = 0;
1197c478bdstevel@tonic-gate}
1207c478bdstevel@tonic-gate
1217c478bdstevel@tonic-gate/*
1227c478bdstevel@tonic-gate * cv_destroy is not currently needed, but is part of the DDI.
1237c478bdstevel@tonic-gate * This is in case cv_init ever needs to allocate something for a cv.
1247c478bdstevel@tonic-gate */
1257c478bdstevel@tonic-gate/* ARGSUSED */
1267c478bdstevel@tonic-gatevoid
1277c478bdstevel@tonic-gatecv_destroy(kcondvar_t *cvp)
1287c478bdstevel@tonic-gate{
1297c478bdstevel@tonic-gate	ASSERT((((condvar_impl_t *)cvp)->cv_waiters & CV_WAITERS_MASK) == 0);
1307c478bdstevel@tonic-gate}
1317c478bdstevel@tonic-gate
1327c478bdstevel@tonic-gate/*
1337c478bdstevel@tonic-gate * The cv_block() function blocks a thread on a condition variable
1347c478bdstevel@tonic-gate * by putting it in a hashed sleep queue associated with the
1357c478bdstevel@tonic-gate * synchronization object.
1367c478bdstevel@tonic-gate *
1377c478bdstevel@tonic-gate * Threads are taken off the hashed sleep queues via calls to
1387c478bdstevel@tonic-gate * cv_signal(), cv_broadcast(), or cv_unsleep().
1397c478bdstevel@tonic-gate */
1407c478bdstevel@tonic-gatestatic void
1417c478bdstevel@tonic-gatecv_block(condvar_impl_t *cvp)
1427c478bdstevel@tonic-gate{
1437c478bdstevel@tonic-gate	kthread_t *t = curthread;
1447c478bdstevel@tonic-gate	klwp_t *lwp = ttolwp(t);
1457c478bdstevel@tonic-gate	sleepq_head_t *sqh;
1467c478bdstevel@tonic-gate
1477c478bdstevel@tonic-gate	ASSERT(THREAD_LOCK_HELD(t));
1487c478bdstevel@tonic-gate	ASSERT(t != CPU->cpu_idle_thread);
1497c478bdstevel@tonic-gate	ASSERT(CPU_ON_INTR(CPU) == 0);
1507c478bdstevel@tonic-gate	ASSERT(t->t_wchan0 == NULL && t->t_wchan == NULL);
1517c478bdstevel@tonic-gate	ASSERT(t->t_state == TS_ONPROC);
1527c478bdstevel@tonic-gate
1537c478bdstevel@tonic-gate	t->t_schedflag &= ~TS_SIGNALLED;
1547c478bdstevel@tonic-gate	CL_SLEEP(t);			/* assign kernel priority */
1557c478bdstevel@tonic-gate	t->t_wchan = (caddr_t)cvp;
1567c478bdstevel@tonic-gate	t->t_sobj_ops = &cv_sobj_ops;
1577c478bdstevel@tonic-gate	DTRACE_SCHED(sleep);
1587c478bdstevel@tonic-gate
1597c478bdstevel@tonic-gate	/*
1607c478bdstevel@tonic-gate	 * The check for t_intr is to avoid doing the
1617c478bdstevel@tonic-gate	 * account for an interrupt thread on the still-pinned
1627c478bdstevel@tonic-gate	 * lwp's statistics.
1637c478bdstevel@tonic-gate	 */
1647c478bdstevel@tonic-gate	if (lwp != NULL && t->t_intr == NULL) {
1657c478bdstevel@tonic-gate		lwp->lwp_ru.nvcsw++;
1667c478bdstevel@tonic-gate		(void) new_mstate(t, LMS_SLEEP);
1677c478bdstevel@tonic-gate	}
1687c478bdstevel@tonic-gate
1697c478bdstevel@tonic-gate	sqh = SQHASH(cvp);
1707c478bdstevel@tonic-gate	disp_lock_enter_high(&sqh->sq_lock);
1717c478bdstevel@tonic-gate	if (cvp->cv_waiters < CV_MAX_WAITERS)
1727c478bdstevel@tonic-gate		cvp->cv_waiters++;
1737c478bdstevel@tonic-gate	ASSERT(cvp->cv_waiters <= CV_MAX_WAITERS);
1747c478bdstevel@tonic-gate	THREAD_SLEEP(t, &sqh->sq_lock);
1757c478bdstevel@tonic-gate	sleepq_insert(&sqh->sq_queue, t);
1767c478bdstevel@tonic-gate	/*
1777c478bdstevel@tonic-gate	 * THREAD_SLEEP() moves curthread->t_lockp to point to the
1787c478bdstevel@tonic-gate	 * lock sqh->sq_lock. This lock is later released by the caller
1797c478bdstevel@tonic-gate	 * when it calls thread_unlock() on curthread.
1807c478bdstevel@tonic-gate	 */
1817c478bdstevel@tonic-gate}
1827c478bdstevel@tonic-gate
1837c478bdstevel@tonic-gate#define	cv_block_sig(t, cvp)	\
1847c478bdstevel@tonic-gate	{ (t)->t_flag |= T_WAKEABLE; cv_block(cvp); }
1857c478bdstevel@tonic-gate
1867c478bdstevel@tonic-gate/*
1877c478bdstevel@tonic-gate * Block on the indicated condition variable and release the
1887c478bdstevel@tonic-gate * associated kmutex while blocked.
1897c478bdstevel@tonic-gate */
1907c478bdstevel@tonic-gatevoid
1917c478bdstevel@tonic-gatecv_wait(kcondvar_t *cvp, kmutex_t *mp)
1927c478bdstevel@tonic-gate{
1937c478bdstevel@tonic-gate	if (panicstr)
1947c478bdstevel@tonic-gate		return;
1958a3feaaJerry Gilliam	ASSERT(!quiesce_active);
1967c478bdstevel@tonic-gate
1977c478bdstevel@tonic-gate	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
1987c478bdstevel@tonic-gate	thread_lock(curthread);			/* lock the thread */
1997c478bdstevel@tonic-gate	cv_block((condvar_impl_t *)cvp);
2007c478bdstevel@tonic-gate	thread_unlock_nopreempt(curthread);	/* unlock the waiters field */
2017c478bdstevel@tonic-gate	mutex_exit(mp);
2027c478bdstevel@tonic-gate	swtch();
2037c478bdstevel@tonic-gate	mutex_enter(mp);
2047c478bdstevel@tonic-gate}
2057c478bdstevel@tonic-gate
20687a18d3Madhavan Venkataramanstatic void
20787a18d3Madhavan Venkataramancv_wakeup(void *arg)
20887a18d3Madhavan Venkataraman{
20987a18d3Madhavan Venkataraman	kthread_t *t = arg;
21087a18d3Madhavan Venkataraman
21187a18d3Madhavan Venkataraman	/*
21287a18d3Madhavan Venkataraman	 * This mutex is acquired and released in order to make sure that
21387a18d3Madhavan Venkataraman	 * the wakeup does not happen before the block itself happens.
21487a18d3Madhavan Venkataraman	 */
215454ab20Madhavan Venkataraman	mutex_enter(&t->t_wait_mutex);
216454ab20Madhavan Venkataraman	mutex_exit(&t->t_wait_mutex);
21787a18d3Madhavan Venkataraman	setrun(t);
21887a18d3Madhavan Venkataraman}
21987a18d3Madhavan Venkataraman
2207c478bdstevel@tonic-gate/*
2217c478bdstevel@tonic-gate * Same as cv_wait except the thread will unblock at 'tim'
2227c478bdstevel@tonic-gate * (an absolute time) if it hasn't already unblocked.
2237c478bdstevel@tonic-gate *
2247c478bdstevel@tonic-gate * Returns the amount of time left from the original 'tim' value
2257c478bdstevel@tonic-gate * when it was unblocked.
2267c478bdstevel@tonic-gate */
2277c478bdstevel@tonic-gateclock_t
2287c478bdstevel@tonic-gatecv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
2297c478bdstevel@tonic-gate{
23051b32bdMadhavan Venkataraman	hrtime_t hrtim;
231d3d5073Rafael Vanoni	clock_t now = ddi_get_lbolt();
23251b32bdMadhavan Venkataraman
233d3d5073Rafael Vanoni	if (tim <= now)
23451b32bdMadhavan Venkataraman		return (-1);
23551b32bdMadhavan Venkataraman
236d3d5073Rafael Vanoni	hrtim = TICK_TO_NSEC(tim - now);
23751b32bdMadhavan Venkataraman	return (cv_timedwait_hires(cvp, mp, hrtim, nsec_per_tick, 0));
23851b32bdMadhavan Venkataraman}
23951b32bdMadhavan Venkataraman
240d3d5073Rafael Vanoni/*
241d3d5073Rafael Vanoni * Same as cv_timedwait() except that the third argument is a relative
242d3d5073Rafael Vanoni * timeout value, as opposed to an absolute one. There is also a fourth
243d3d5073Rafael Vanoni * argument that specifies how accurately the timeout must be implemented.
244d3d5073Rafael Vanoni */
245d3d5073Rafael Vanoniclock_t
246d3d5073Rafael Vanonicv_reltimedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t delta, time_res_t res)
247d3d5073Rafael Vanoni{
248d3d5073Rafael Vanoni	hrtime_t exp;
249d3d5073Rafael Vanoni
250d3d5073Rafael Vanoni	ASSERT(TIME_RES_VALID(res));
251d3d5073Rafael Vanoni
252d3d5073Rafael Vanoni	if (delta <= 0)
253d3d5073Rafael Vanoni		return (-1);
254d3d5073Rafael Vanoni
255d3d5073Rafael Vanoni	if ((exp = TICK_TO_NSEC(delta)) < 0)
256d3d5073Rafael Vanoni		exp = CY_INFINITY;
257d3d5073Rafael Vanoni
258d3d5073Rafael Vanoni	return (cv_timedwait_hires(cvp, mp, exp, time_res[res], 0));
259d3d5073Rafael Vanoni}
260d3d5073Rafael Vanoni
26151b32bdMadhavan Venkataramanclock_t
26251b32bdMadhavan Venkataramancv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
26351b32bdMadhavan Venkataraman    hrtime_t res, int flag)
26451b32bdMadhavan Venkataraman{
2657c478bdstevel@tonic-gate	kthread_t *t = curthread;
26687a18d3Madhavan Venkataraman	callout_id_t id;
2677c478bdstevel@tonic-gate	clock_t timeleft;
26851b32bdMadhavan Venkataraman	hrtime_t limit;
2697c478bdstevel@tonic-gate	int signalled;
2707c478bdstevel@tonic-gate
2717c478bdstevel@tonic-gate	if (panicstr)
2727c478bdstevel@tonic-gate		return (-1);
2738a3feaaJerry Gilliam	ASSERT(!quiesce_active);
2747c478bdstevel@tonic-gate
27551b32bdMadhavan Venkataraman	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
27651b32bdMadhavan Venkataraman	if (tim <= limit)
2777c478bdstevel@tonic-gate		return (-1);
278454ab20Madhavan Venkataraman	mutex_enter(&t->t_wait_mutex);
27951b32bdMadhavan Venkataraman	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
28051b32bdMadhavan Venkataraman	    tim, res, flag);
2817c478bdstevel@tonic-gate	thread_lock(t);		/* lock the thread */
2827c478bdstevel@tonic-gate	cv_block((condvar_impl_t *)cvp);
2837c478bdstevel@tonic-gate	thread_unlock_nopreempt(t);
284454ab20Madhavan Venkataraman	mutex_exit(&t->t_wait_mutex);
2857c478bdstevel@tonic-gate	mutex_exit(mp);
2867c478bdstevel@tonic-gate	swtch();
2877c478bdstevel@tonic-gate	signalled = (t->t_schedflag & TS_SIGNALLED);
2887c478bdstevel@tonic-gate	/*
2897c478bdstevel@tonic-gate	 * Get the time left. untimeout() returns -1 if the timeout has
2907c478bdstevel@tonic-gate	 * occured or the time remaining.  If the time remaining is zero,
2917c478bdstevel@tonic-gate	 * the timeout has occured between when we were awoken and
2927c478bdstevel@tonic-gate	 * we called untimeout.  We will treat this as if the timeout
2937c478bdstevel@tonic-gate	 * has occured and set timeleft to -1.
2947c478bdstevel@tonic-gate	 */
295454ab20Madhavan Venkataraman	timeleft = untimeout_default(id, 0);
2967c478bdstevel@tonic-gate	mutex_enter(mp);
2977c478bdstevel@tonic-gate	if (timeleft <= 0) {
2987c478bdstevel@tonic-gate		timeleft = -1;
2997c478bdstevel@tonic-gate		if (signalled)	/* avoid consuming the cv_signal() */
3007c478bdstevel@tonic-gate			cv_signal(cvp);
3017c478bdstevel@tonic-gate	}
3027c478bdstevel@tonic-gate	return (timeleft);
3037c478bdstevel@tonic-gate}
3047c478bdstevel@tonic-gate
3057c478bdstevel@tonic-gateint
3067c478bdstevel@tonic-gatecv_wait_sig(kcondvar_t *cvp, kmutex_t *mp)
3077c478bdstevel@tonic-gate{
3087c478bdstevel@tonic-gate	kthread_t *t = curthread;
3097c478bdstevel@tonic-gate	proc_t *p = ttoproc(t);
3107c478bdstevel@tonic-gate	klwp_t *lwp = ttolwp(t);
311a574db8raf	int cancel_pending;
3127c478bdstevel@tonic-gate	int rval = 1;
3137c478bdstevel@tonic-gate	int signalled = 0;
3147c478bdstevel@tonic-gate
3157c478bdstevel@tonic-gate	if (panicstr)
3167c478bdstevel@tonic-gate		return (rval);
3178a3feaaJerry Gilliam	ASSERT(!quiesce_active);
3187c478bdstevel@tonic-gate
3197c478bdstevel@tonic-gate	/*
32035a5a35Jonathan Adams	 * Threads in system processes don't process signals.  This is
32135a5a35Jonathan Adams	 * true both for standard threads of system processes and for
32235a5a35Jonathan Adams	 * interrupt threads which have borrowed their pinned thread's LWP.
3237c478bdstevel@tonic-gate	 */
32435a5a35Jonathan Adams	if (lwp == NULL || (p->p_flag & SSYS)) {
3257c478bdstevel@tonic-gate		cv_wait(cvp, mp);
3267c478bdstevel@tonic-gate		return (rval);
3277c478bdstevel@tonic-gate	}
32835a5a35Jonathan Adams	ASSERT(t->t_intr == NULL);
3297c478bdstevel@tonic-gate
3307c478bdstevel@tonic-gate	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
331a574db8raf	cancel_pending = schedctl_cancel_pending();
3327c478bdstevel@tonic-gate	lwp->lwp_asleep = 1;
3337c478bdstevel@tonic-gate	lwp->lwp_sysabort = 0;
3347c478bdstevel@tonic-gate	thread_lock(t);
3357c478bdstevel@tonic-gate	cv_block_sig(t, (condvar_impl_t *)cvp);
3367c478bdstevel@tonic-gate	thread_unlock_nopreempt(t);
3377c478bdstevel@tonic-gate	mutex_exit(mp);
338a574db8raf	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
3397c478bdstevel@tonic-gate		setrun(t);
3407c478bdstevel@tonic-gate	/* ASSERT(no locks are held) */
3417c478bdstevel@tonic-gate	swtch();
3427c478bdstevel@tonic-gate	signalled = (t->t_schedflag & TS_SIGNALLED);
3437c478bdstevel@tonic-gate	t->t_flag &= ~T_WAKEABLE;
3447c478bdstevel@tonic-gate	mutex_enter(mp);
3457c478bdstevel@tonic-gate	if (ISSIG_PENDING(t, lwp, p)) {
3467c478bdstevel@tonic-gate		mutex_exit(mp);
3477c478bdstevel@tonic-gate		if (issig(FORREAL))
3487c478bdstevel@tonic-gate			rval = 0;
3497c478bdstevel@tonic-gate		mutex_enter(mp);
3507c478bdstevel@tonic-gate	}
3517c478bdstevel@tonic-gate	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
3527c478bdstevel@tonic-gate		rval = 0;
353a574db8raf	if (rval != 0 && cancel_pending) {
354a574db8raf		schedctl_cancel_eintr();
355a574db8raf		rval = 0;
356a574db8raf	}
3577c478bdstevel@tonic-gate	lwp->lwp_asleep = 0;
3587c478bdstevel@tonic-gate	lwp->lwp_sysabort = 0;
3597c478bdstevel@tonic-gate	if (rval == 0 && signalled)	/* avoid consuming the cv_signal() */
3607c478bdstevel@tonic-gate		cv_signal(cvp);
3617c478bdstevel@tonic-gate	return (rval);
3627c478bdstevel@tonic-gate}
3637c478bdstevel@tonic-gate
36487a18d3Madhavan Venkataramanstatic clock_t
36551b32bdMadhavan Venkataramancv_timedwait_sig_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
36651b32bdMadhavan Venkataraman    hrtime_t res, int flag)
3677c478bdstevel@tonic-gate{
3687c478bdstevel@tonic-gate	kthread_t *t = curthread;
3697c478bdstevel@tonic-gate	proc_t *p = ttoproc(t);
3707c478bdstevel@tonic-gate	klwp_t *lwp = ttolwp(t);
371a574db8raf	int cancel_pending = 0;
37287a18d3Madhavan Venkataraman	callout_id_t id;
3737c478bdstevel@tonic-gate	clock_t rval = 1;
37451b32bdMadhavan Venkataraman	hrtime_t limit;
3757c478bdstevel@tonic-gate	int signalled = 0;
3767c478bdstevel@tonic-gate
3777c478bdstevel@tonic-gate	if (panicstr)
3787c478bdstevel@tonic-gate		return (rval);
3798a3feaaJerry Gilliam	ASSERT(!quiesce_active);
3807c478bdstevel@tonic-gate
3817c478bdstevel@tonic-gate	/*
38235a5a35Jonathan Adams	 * Threads in system processes don't process signals.  This is
38335a5a35Jonathan Adams	 * true both for standard threads of system processes and for
38435a5a35Jonathan Adams	 * interrupt threads which have borrowed their pinned thread's LWP.
3857c478bdstevel@tonic-gate	 */
38635a5a35Jonathan Adams	if (lwp == NULL || (p->p_flag & SSYS))
38751b32bdMadhavan Venkataraman		return (cv_timedwait_hires(cvp, mp, tim, res, flag));
38835a5a35Jonathan Adams	ASSERT(t->t_intr == NULL);
3897c478bdstevel@tonic-gate
3907c478bdstevel@tonic-gate	/*
39151b32bdMadhavan Venkataraman	 * If tim is less than or equal to current hrtime, then the timeout
3927c478bdstevel@tonic-gate	 * has already occured.  So just check to see if there is a signal
3937c478bdstevel@tonic-gate	 * pending.  If so return 0 indicating that there is a signal pending.
3947c478bdstevel@tonic-gate	 * Else return -1 indicating that the timeout occured. No need to
3957c478bdstevel@tonic-gate	 * wait on anything.
3967c478bdstevel@tonic-gate	 */
39751b32bdMadhavan Venkataraman	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
39851b32bdMadhavan Venkataraman	if (tim <= limit) {
3997c478bdstevel@tonic-gate		lwp->lwp_asleep = 1;
4007c478bdstevel@tonic-gate		lwp->lwp_sysabort = 0;
4017c478bdstevel@tonic-gate		rval = -1;
4027c478bdstevel@tonic-gate		goto out;
4037c478bdstevel@tonic-gate	}
4047c478bdstevel@tonic-gate
4057c478bdstevel@tonic-gate	/*
4067c478bdstevel@tonic-gate	 * Set the timeout and wait.
4077c478bdstevel@tonic-gate	 */
408a574db8raf	cancel_pending = schedctl_cancel_pending();
409454ab20Madhavan Venkataraman	mutex_enter(&t->t_wait_mutex);
41087a18d3Madhavan Venkataraman	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
41151b32bdMadhavan Venkataraman	    tim, res, flag);
4127c478bdstevel@tonic-gate	lwp->lwp_asleep = 1;
4137c478bdstevel@tonic-gate	lwp->lwp_sysabort = 0;
4147c478bdstevel@tonic-gate	thread_lock(t);
4157c478bdstevel@tonic-gate	cv_block_sig(t, (condvar_impl_t *)cvp);
4167c478bdstevel@tonic-gate	thread_unlock_nopreempt(t);
417454ab20Madhavan Venkataraman	mutex_exit(&t->t_wait_mutex);
4187c478bdstevel@tonic-gate	mutex_exit(mp);
41987a18d3Madhavan Venkataraman	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
4207c478bdstevel@tonic-gate		setrun(t);
4217c478bdstevel@tonic-gate	/* ASSERT(no locks are held) */
4227c478bdstevel@tonic-gate	swtch();
4237c478bdstevel@tonic-gate	signalled = (t->t_schedflag & TS_SIGNALLED);
4247c478bdstevel@tonic-gate	t->t_flag &= ~T_WAKEABLE;
4257c478bdstevel@tonic-gate
4267c478bdstevel@tonic-gate	/*
4277c478bdstevel@tonic-gate	 * Untimeout the thread.  untimeout() returns -1 if the timeout has
4287c478bdstevel@tonic-gate	 * occured or the time remaining.  If the time remaining is zero,
4297c478bdstevel@tonic-gate	 * the timeout has occured between when we were awoken and
4307c478bdstevel@tonic-gate	 * we called untimeout.  We will treat this as if the timeout
4317c478bdstevel@tonic-gate	 * has occured and set rval to -1.
4327c478bdstevel@tonic-gate	 */
433454ab20Madhavan Venkataraman	rval = untimeout_default(id, 0);
43487a18d3Madhavan Venkataraman	mutex_enter(mp);
4357c478bdstevel@tonic-gate	if (rval <= 0)
4367c478bdstevel@tonic-gate		rval = -1;
4377c478bdstevel@tonic-gate
4387c478bdstevel@tonic-gate	/*
4397c478bdstevel@tonic-gate	 * Check to see if a signal is pending.  If so, regardless of whether
4407c478bdstevel@tonic-gate	 * or not we were awoken due to the signal, the signal is now pending
4417c478bdstevel@tonic-gate	 * and a return of 0 has the highest priority.
4427c478bdstevel@tonic-gate	 */
4437c478bdstevel@tonic-gateout:
4447c478bdstevel@tonic-gate	if (ISSIG_PENDING(t, lwp, p)) {
4457c478bdstevel@tonic-gate		mutex_exit(mp);
4467c478bdstevel@tonic-gate		if (issig(FORREAL))
4477c478bdstevel@tonic-gate			rval = 0;
4487c478bdstevel@tonic-gate		mutex_enter(mp);
4497c478bdstevel@tonic-gate	}
4507c478bdstevel@tonic-gate	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
4517c478bdstevel@tonic-gate		rval = 0;
452a574db8raf	if (rval != 0 && cancel_pending) {
453a574db8raf		schedctl_cancel_eintr();
454a574db8raf		rval = 0;
455a574db8raf	}
4567c478bdstevel@tonic-gate	lwp->lwp_asleep = 0;
4577c478bdstevel@tonic-gate	lwp->lwp_sysabort = 0;
4587c478bdstevel@tonic-gate	if (rval <= 0 && signalled)	/* avoid consuming the cv_signal() */
4597c478bdstevel@tonic-gate		cv_signal(cvp);
4607c478bdstevel@tonic-gate	return (rval);
4617c478bdstevel@tonic-gate}
4627c478bdstevel@tonic-gate
4637c478bdstevel@tonic-gate/*
46487a18d3Madhavan Venkataraman * Returns:
4656a0b121Patrick Mooney *	Function result in order of precedence:
46687a18d3Madhavan Venkataraman *		 0 if a signal was received
46787a18d3Madhavan Venkataraman *		-1 if timeout occured
46887a18d3Madhavan Venkataraman *		>0 if awakened via cv_signal() or cv_broadcast().
46987a18d3Madhavan Venkataraman *		   (returns time remaining)
47087a18d3Madhavan Venkataraman *
47187a18d3Madhavan Venkataraman * cv_timedwait_sig() is now part of the DDI.
47287a18d3Madhavan Venkataraman *
47351b32bdMadhavan Venkataraman * This function is now just a wrapper for cv_timedwait_sig_hires().
47487a18d3Madhavan Venkataraman */
47587a18d3Madhavan Venkataramanclock_t
47687a18d3Madhavan Venkataramancv_timedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
47787a18d3Madhavan Venkataraman{
47851b32bdMadhavan Venkataraman	hrtime_t hrtim;
47951b32bdMadhavan Venkataraman
480d3d5073Rafael Vanoni	hrtim = TICK_TO_NSEC(tim - ddi_get_lbolt());
48151b32bdMadhavan Venkataraman	return (cv_timedwait_sig_hires(cvp, mp, hrtim, nsec_per_tick, 0));
48287a18d3Madhavan Venkataraman}
48387a18d3Madhavan Venkataraman
48487a18d3Madhavan Venkataraman/*
485cd1c8b8Matthew Ahrens * Wait until the specified time.
486cd1c8b8Matthew Ahrens * If tim == -1, waits without timeout using cv_wait_sig_swap().
487cd1c8b8Matthew Ahrens */
488cd1c8b8Matthew Ahrensint
489cd1c8b8Matthew Ahrenscv_timedwait_sig_hrtime(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim)
490cd1c8b8Matthew Ahrens{
491cd1c8b8Matthew Ahrens	if (tim == -1) {
492cd1c8b8Matthew Ahrens		return (cv_wait_sig_swap(cvp, mp));
493cd1c8b8Matthew Ahrens	} else {
494cd1c8b8Matthew Ahrens		return (cv_timedwait_sig_hires(cvp, mp, tim, 1,
495cd1c8b8Matthew Ahrens		    CALLOUT_FLAG_ABSOLUTE | CALLOUT_FLAG_ROUNDUP));
496cd1c8b8Matthew Ahrens	}
497cd1c8b8Matthew Ahrens}
498cd1c8b8Matthew Ahrens
499cd1c8b8Matthew Ahrens/*
500d3d5073Rafael Vanoni * Same as cv_timedwait_sig() except that the third argument is a relative
501d3d5073Rafael Vanoni * timeout value, as opposed to an absolute one. There is also a fourth
502d3d5073Rafael Vanoni * argument that specifies how accurately the timeout must be implemented.
503d3d5073Rafael Vanoni */
504d3d5073Rafael Vanoniclock_t
505d3d5073Rafael Vanonicv_reltimedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t delta,
506d3d5073Rafael Vanoni    time_res_t res)
507d3d5073Rafael Vanoni{
5081c4c388Rafael Vanoni	hrtime_t exp = 0;
509d3d5073Rafael Vanoni
510d3d5073Rafael Vanoni	ASSERT(TIME_RES_VALID(res));
511d3d5073Rafael Vanoni
5121c4c388Rafael Vanoni	if (delta > 0) {
5131c4c388Rafael Vanoni		if ((exp = TICK_TO_NSEC(delta)) < 0)
5141c4c388Rafael Vanoni			exp = CY_INFINITY;
5151c4c388Rafael Vanoni	}
516d3d5073Rafael Vanoni
517d3d5073Rafael Vanoni	return (cv_timedwait_sig_hires(cvp, mp, exp, time_res[res], 0));
518d3d5073Rafael Vanoni}
519d3d5073Rafael Vanoni
520d3d5073Rafael Vanoni/*
5217c478bdstevel@tonic-gate * Like cv_wait_sig_swap but allows the caller to indicate (with a
5227c478bdstevel@tonic-gate * non-NULL sigret) that they will take care of signalling the cv
5237c478bdstevel@tonic-gate * after wakeup, if necessary.  This is a vile hack that should only
5247c478bdstevel@tonic-gate * be used when no other option is available; almost all callers
5257c478bdstevel@tonic-gate * should just use cv_wait_sig_swap (which takes care of the cv_signal
5267c478bdstevel@tonic-gate * stuff automatically) instead.
5277c478bdstevel@tonic-gate */
5287c478bdstevel@tonic-gateint
5297c478bdstevel@tonic-gatecv_wait_sig_swap_core(kcondvar_t *cvp, kmutex_t *mp, int *sigret)
5307c478bdstevel@tonic-gate{
5317c478bdstevel@tonic-gate	kthread_t *t = curthread;
5327c478bdstevel@tonic-gate	proc_t *p = ttoproc(t);
5337c478bdstevel@tonic-gate	klwp_t *lwp = ttolwp(t);
534a574db8raf	int cancel_pending;
5357c478bdstevel@tonic-gate	int rval = 1;
5367c478bdstevel@tonic-gate	int signalled = 0;
5377c478bdstevel@tonic-gate
5387c478bdstevel@tonic-gate	if (panicstr)
5397c478bdstevel@tonic-gate		return (rval);
5407c478bdstevel@tonic-gate
5417c478bdstevel@tonic-gate	/*
54235a5a35Jonathan Adams	 * Threads in system processes don't process signals.  This is
54335a5a35Jonathan Adams	 * true both for standard threads of system processes and for
54435a5a35Jonathan Adams	 * interrupt threads which have borrowed their pinned thread's LWP.
5457c478bdstevel@tonic-gate	 */
54635a5a35Jonathan Adams	if (lwp == NULL || (p->p_flag & SSYS)) {
5477c478bdstevel@tonic-gate		cv_wait(cvp, mp);
5487c478bdstevel@tonic-gate		return (rval);
5497c478bdstevel@tonic-gate	}
55035a5a35Jonathan Adams	ASSERT(t->t_intr == NULL);
5517c478bdstevel@tonic-gate
552a574db8raf	cancel_pending = schedctl_cancel_pending();
5537c478bdstevel@tonic-gate	lwp->lwp_asleep = 1;
5547c478bdstevel@tonic-gate	lwp->lwp_sysabort = 0;
5557c478bdstevel@tonic-gate	thread_lock(t);
5567c478bdstevel@tonic-gate	cv_block_sig(t, (condvar_impl_t *)cvp);
5577c478bdstevel@tonic-gate	/* I can be swapped now */
5587c478bdstevel@tonic-gate	curthread->t_schedflag &= ~TS_DONT_SWAP;
5597c478bdstevel@tonic-gate	thread_unlock_nopreempt(t);
5607c478bdstevel@tonic-gate	mutex_exit(mp);
561a574db8raf	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
5627c478bdstevel@tonic-gate		setrun(t);
5637c478bdstevel@tonic-gate	/* ASSERT(no locks are held) */
5647c478bdstevel@tonic-gate	swtch();
5657c478bdstevel@tonic-gate	signalled = (t->t_schedflag & TS_SIGNALLED);
5667c478bdstevel@tonic-gate	t->t_flag &= ~T_WAKEABLE;
5677c478bdstevel@tonic-gate	/* TS_DONT_SWAP set by disp() */
5687c478bdstevel@tonic-gate	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
5697c478bdstevel@tonic-gate	mutex_enter(mp);
5707c478bdstevel@tonic-gate	if (ISSIG_PENDING(t, lwp, p)) {
5717c478bdstevel@tonic-gate		mutex_exit(mp);
5727c478bdstevel@tonic-gate		if (issig(FORREAL))
5737c478bdstevel@tonic-gate			rval = 0;
5747c478bdstevel@tonic-gate		mutex_enter(mp);
5757c478bdstevel@tonic-gate	}
5767c478bdstevel@tonic-gate	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
5777c478bdstevel@tonic-gate		rval = 0;
578a574db8raf	if (rval != 0 && cancel_pending) {
579a574db8raf		schedctl_cancel_eintr();
580a574db8raf		rval = 0;
581a574db8raf	}
5827c478bdstevel@tonic-gate	lwp->lwp_asleep = 0;
5837c478bdstevel@tonic-gate	lwp->lwp_sysabort = 0;
5847c478bdstevel@tonic-gate	if (rval == 0) {
5857c478bdstevel@tonic-gate		if (sigret != NULL)
5867c478bdstevel@tonic-gate			*sigret = signalled;	/* just tell the caller */
5877c478bdstevel@tonic-gate		else if (signalled)
5887c478bdstevel@tonic-gate			cv_signal(cvp);	/* avoid consuming the cv_signal() */
5897c478bdstevel@tonic-gate	}
5907c478bdstevel@tonic-gate	return (rval);
5917c478bdstevel@tonic-gate}
5927c478bdstevel@tonic-gate
5937c478bdstevel@tonic-gate/*
5947c478bdstevel@tonic-gate * Same as cv_wait_sig but the thread can be swapped out while waiting.
5957c478bdstevel@tonic-gate * This should only be used when we know we aren't holding any locks.
5967c478bdstevel@tonic-gate */
5977c478bdstevel@tonic-gateint
5987c478bdstevel@tonic-gatecv_wait_sig_swap(kcondvar_t *cvp, kmutex_t *mp)
5997c478bdstevel@tonic-gate{
6007c478bdstevel@tonic-gate	return (cv_wait_sig_swap_core(cvp, mp, NULL));
6017c478bdstevel@tonic-gate}
6027c478bdstevel@tonic-gate
6037c478bdstevel@tonic-gatevoid
6047c478bdstevel@tonic-gatecv_signal(kcondvar_t *cvp)
6057c478bdstevel@tonic-gate{
6067c478bdstevel@tonic-gate	condvar_impl_t *cp = (condvar_impl_t *)cvp;
6077c478bdstevel@tonic-gate
6087c478bdstevel@tonic-gate	/* make sure the cv_waiters field looks sane */
6097c478bdstevel@tonic-gate	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
6107c478bdstevel@tonic-gate	if (cp->cv_waiters > 0) {
6117c478bdstevel@tonic-gate		sleepq_head_t *sqh = SQHASH(cp);
6127c478bdstevel@tonic-gate		disp_lock_enter(&sqh->sq_lock);
6137c478bdstevel@tonic-gate		ASSERT(CPU_ON_INTR(CPU) == 0);
6147c478bdstevel@tonic-gate		if (cp->cv_waiters & CV_WAITERS_MASK) {
6157c478bdstevel@tonic-gate			kthread_t *t;
6167c478bdstevel@tonic-gate			cp->cv_waiters--;
6177c478bdstevel@tonic-gate			t = sleepq_wakeone_chan(&sqh->sq_queue, cp);
6187c478bdstevel@tonic-gate			/*
6197c478bdstevel@tonic-gate			 * If cv_waiters is non-zero (and less than
6207c478bdstevel@tonic-gate			 * CV_MAX_WAITERS) there should be a thread
6217c478bdstevel@tonic-gate			 * in the queue.
6227c478bdstevel@tonic-gate			 */
6237c478bdstevel@tonic-gate			ASSERT(t != NULL);
6247c478bdstevel@tonic-gate		} else if (sleepq_wakeone_chan(&sqh->sq_queue, cp) == NULL) {
6257c478bdstevel@tonic-gate			cp->cv_waiters = 0;
6267c478bdstevel@tonic-gate		}
6277c478bdstevel@tonic-gate		disp_lock_exit(&sqh->sq_lock);
6287c478bdstevel@tonic-gate	}
6297c478bdstevel@tonic-gate}
6307c478bdstevel@tonic-gate
6317c478bdstevel@tonic-gatevoid
6327c478bdstevel@tonic-gatecv_broadcast(kcondvar_t *cvp)
6337c478bdstevel@tonic-gate{
6347c478bdstevel@tonic-gate	condvar_impl_t *cp = (condvar_impl_t *)cvp;
6357c478bdstevel@tonic-gate
6367c478bdstevel@tonic-gate	/* make sure the cv_waiters field looks sane */
6377c478bdstevel@tonic-gate	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
6387c478bdstevel@tonic-gate	if (cp->cv_waiters > 0) {
6397c478bdstevel@tonic-gate		sleepq_head_t *sqh = SQHASH(cp);
6407c478bdstevel@tonic-gate		disp_lock_enter(&sqh->sq_lock);
6417c478bdstevel@tonic-gate		ASSERT(CPU_ON_INTR(CPU) == 0);
6427c478bdstevel@tonic-gate		sleepq_wakeall_chan(&sqh->sq_queue, cp);
6437c478bdstevel@tonic-gate		cp->cv_waiters = 0;
6447c478bdstevel@tonic-gate		disp_lock_exit(&sqh->sq_lock);
6457c478bdstevel@tonic-gate	}
6467c478bdstevel@tonic-gate}
6477c478bdstevel@tonic-gate
6487c478bdstevel@tonic-gate/*
6497c478bdstevel@tonic-gate * Same as cv_wait(), but wakes up (after wakeup_time milliseconds) to check
6507c478bdstevel@tonic-gate * for requests to stop, like cv_wait_sig() but without dealing with signals.
6517c478bdstevel@tonic-gate * This is a horrible kludge.  It is evil.  It is vile.  It is swill.
6527c478bdstevel@tonic-gate * If your code has to call this function then your code is the same.
6537c478bdstevel@tonic-gate */
6547c478bdstevel@tonic-gatevoid
6557c478bdstevel@tonic-gatecv_wait_stop(kcondvar_t *cvp, kmutex_t *mp, int wakeup_time)
6567c478bdstevel@tonic-gate{
6577c478bdstevel@tonic-gate	kthread_t *t = curthread;
6587c478bdstevel@tonic-gate	klwp_t *lwp = ttolwp(t);
6597c478bdstevel@tonic-gate	proc_t *p = ttoproc(t);
66087a18d3Madhavan Venkataraman	callout_id_t id;
6617c478bdstevel@tonic-gate	clock_t tim;
6627c478bdstevel@tonic-gate
6637c478bdstevel@tonic-gate	if (panicstr)
6647c478bdstevel@tonic-gate		return;
6657c478bdstevel@tonic-gate
6667c478bdstevel@tonic-gate	/*
66735a5a35Jonathan Adams	 * Threads in system processes don't process signals.  This is
66835a5a35Jonathan Adams	 * true both for standard threads of system processes and for
66935a5a35Jonathan Adams	 * interrupt threads which have borrowed their pinned thread's LWP.
6707c478bdstevel@tonic-gate	 */
67135a5a35Jonathan Adams	if (lwp == NULL || (p->p_flag & SSYS)) {
6727c478bdstevel@tonic-gate		cv_wait(cvp, mp);
6737c478bdstevel@tonic-gate		return;
6747c478bdstevel@tonic-gate	}
67535a5a35Jonathan Adams	ASSERT(t->t_intr == NULL);
6767c478bdstevel@tonic-gate
6777c478bdstevel@tonic-gate	/*
6787c478bdstevel@tonic-gate	 * Wakeup in wakeup_time milliseconds, i.e., human time.
6797c478bdstevel@tonic-gate	 */
680d3d5073Rafael Vanoni	tim = ddi_get_lbolt() + MSEC_TO_TICK(wakeup_time);
681454ab20Madhavan Venkataraman	mutex_enter(&t->t_wait_mutex);
68287a18d3Madhavan Venkataraman	id = realtime_timeout_default((void (*)(void *))cv_wakeup, t,
683d3d5073Rafael Vanoni	    tim - ddi_get_lbolt());
6847c478bdstevel@tonic-gate	thread_lock(t);			/* lock the thread */
6857c478bdstevel@tonic-gate	cv_block((condvar_impl_t *)cvp);
6867c478bdstevel@tonic-gate	thread_unlock_nopreempt(t);
687454ab20Madhavan Venkataraman	mutex_exit(&t->t_wait_mutex);
6887c478bdstevel@tonic-gate	mutex_exit(mp);
6897c478bdstevel@tonic-gate	/* ASSERT(no locks are held); */
6907c478bdstevel@tonic-gate	swtch();
691454ab20Madhavan Venkataraman	(void) untimeout_default(id, 0);
6927c478bdstevel@tonic-gate
6937c478bdstevel@tonic-gate	/*
6948548bf7nr	 * Check for reasons to stop, if lwp_nostop is not true.
6957c478bdstevel@tonic-gate	 * See issig_forreal() for explanations of the various stops.
6967c478bdstevel@tonic-gate	 */
6977c478bdstevel@tonic-gate	mutex_enter(&p->p_lock);
6988548bf7nr	while (lwp->lwp_nostop == 0 && !(p->p_flag & SEXITLWPS)) {
6997c478bdstevel@tonic-gate		/*
7007c478bdstevel@tonic-gate		 * Hold the lwp here for watchpoint manipulation.
7017c478bdstevel@tonic-gate		 */
7028548bf7nr		if (t->t_proc_flag & TP_PAUSE) {
7037c478bdstevel@tonic-gate			stop(PR_SUSPENDED, SUSPEND_PAUSE);
7047c478bdstevel@tonic-gate			continue;
7057c478bdstevel@tonic-gate		}
7067c478bdstevel@tonic-gate		/*
7077c478bdstevel@tonic-gate		 * System checkpoint.
7087c478bdstevel@tonic-gate		 */
7098548bf7nr		if (t->t_proc_flag & TP_CHKPT) {
7107c478bdstevel@tonic-gate			stop(PR_CHECKPOINT, 0);
7117c478bdstevel@tonic-gate			continue;
7127c478bdstevel@tonic-gate		}
7137c478bdstevel@tonic-gate		/*
7147c478bdstevel@tonic-gate		 * Honor fork1(), watchpoint activity (remapping a page),
7158548bf7nr		 * and lwp_suspend() requests.
7167c478bdstevel@tonic-gate		 */
7178548bf7nr		if ((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
7188548bf7nr		    (t->t_proc_flag & TP_HOLDLWP)) {
7197c478bdstevel@tonic-gate			stop(PR_SUSPENDED, SUSPEND_NORMAL);
7207c478bdstevel@tonic-gate			continue;
7217c478bdstevel@tonic-gate		}
7227c478bdstevel@tonic-gate		/*
7237c478bdstevel@tonic-gate		 * Honor /proc requested stop.
7247c478bdstevel@tonic-gate		 */
7258548bf7nr		if (t->t_proc_flag & TP_PRSTOP) {
7267c478bdstevel@tonic-gate			stop(PR_REQUESTED, 0);
7277c478bdstevel@tonic-gate		}
7287c478bdstevel@tonic-gate		/*
7297c478bdstevel@tonic-gate		 * If some lwp in the process has already stopped
7307c478bdstevel@tonic-gate		 * showing PR_JOBCONTROL, stop in sympathy with it.
7317c478bdstevel@tonic-gate		 */
7328548bf7nr		if (p->p_stopsig && t != p->p_agenttp) {
7337c478bdstevel@tonic-gate			stop(PR_JOBCONTROL, p->p_stopsig);
7347c478bdstevel@tonic-gate			continue;
7357c478bdstevel@tonic-gate		}
7367c478bdstevel@tonic-gate		break;
7377c478bdstevel@tonic-gate	}
7387c478bdstevel@tonic-gate	mutex_exit(&p->p_lock);
7397c478bdstevel@tonic-gate	mutex_enter(mp);
7407c478bdstevel@tonic-gate}
7417c478bdstevel@tonic-gate
7427c478bdstevel@tonic-gate/*
7437c478bdstevel@tonic-gate * Like cv_timedwait_sig(), but takes an absolute hires future time
7447c478bdstevel@tonic-gate * rather than a future time in clock ticks.  Will not return showing
7457c478bdstevel@tonic-gate * that a timeout occurred until the future time is passed.
7467c478bdstevel@tonic-gate * If 'when' is a NULL pointer, no timeout will occur.
7477c478bdstevel@tonic-gate * Returns:
7486a0b121Patrick Mooney *	Function result in order of precedence:
7497c478bdstevel@tonic-gate *		 0 if a signal was received
7507c478bdstevel@tonic-gate *		-1 if timeout occured
7517c478bdstevel@tonic-gate *	        >0 if awakened via cv_signal() or cv_broadcast()
7527c478bdstevel@tonic-gate *		   or by a spurious wakeup.
7537c478bdstevel@tonic-gate *		   (might return time remaining)
7543348528dm * As a special test, if someone abruptly resets the system time
7553348528dm * (but not through adjtime(2); drifting of the clock is allowed and
7563348528dm * expected [see timespectohz_adj()]), then we force a return of -1
7573348528dm * so the caller can return a premature timeout to the calling process
7583348528dm * so it can reevaluate the situation in light of the new system time.
7593348528dm * (The system clock has been reset if timecheck != timechanged.)
760cd1c8b8Matthew Ahrens *
761cd1c8b8Matthew Ahrens * Generally, cv_timedwait_sig_hrtime() should be used instead of this
762cd1c8b8Matthew Ahrens * routine.  It waits based on hrtime rather than wall-clock time and therefore
763cd1c8b8Matthew Ahrens * does not need to deal with the time changing.
7647c478bdstevel@tonic-gate */
7657c478bdstevel@tonic-gateint
7666a0b121Patrick Mooneycv_waituntil_sig(kcondvar_t *cvp, kmutex_t *mp, timestruc_t *when,
7676a0b121Patrick Mooney    int timecheck)
7687c478bdstevel@tonic-gate{
7697c478bdstevel@tonic-gate	timestruc_t now;
770b2a1c44vb	timestruc_t delta;
77151b32bdMadhavan Venkataraman	hrtime_t interval;
7727c478bdstevel@tonic-gate	int rval;
7737c478bdstevel@tonic-gate
7747c478bdstevel@tonic-gate	if (when == NULL)
7757c478bdstevel@tonic-gate		return (cv_wait_sig_swap(cvp, mp));
7767c478bdstevel@tonic-gate
77744e59b5Donghai Qiao	gethrestime(&now);
778b2a1c44vb	delta = *when;
779b2a1c44vb	timespecsub(&delta, &now);
780b2a1c44vb	if (delta.tv_sec < 0 || (delta.tv_sec == 0 && delta.tv_nsec == 0)) {
7817c478bdstevel@tonic-gate		/*
7827c478bdstevel@tonic-gate		 * We have already reached the absolute future time.
7837c478bdstevel@tonic-gate		 * Call cv_timedwait_sig() just to check for signals.
7847c478bdstevel@tonic-gate		 * We will return immediately with either 0 or -1.
7857c478bdstevel@tonic-gate		 */
78651b32bdMadhavan Venkataraman		rval = cv_timedwait_sig_hires(cvp, mp, 0, 1, 0);
7877c478bdstevel@tonic-gate	} else {
7883348528dm		if (timecheck == timechanged) {
78951b32bdMadhavan Venkataraman			/*
79051b32bdMadhavan Venkataraman			 * Make sure that the interval is atleast one tick.
79151b32bdMadhavan Venkataraman			 * This is to prevent a user from flooding the system
79251b32bdMadhavan Venkataraman			 * with very small, high resolution timers.
79351b32bdMadhavan Venkataraman			 */
79451b32bdMadhavan Venkataraman			interval = ts2hrt(&delta);
79551b32bdMadhavan Venkataraman			if (interval < nsec_per_tick)
79651b32bdMadhavan Venkataraman				interval = nsec_per_tick;
79751b32bdMadhavan Venkataraman			rval = cv_timedwait_sig_hires(cvp, mp, interval, 1,
79887a18d3Madhavan Venkataraman			    CALLOUT_FLAG_HRESTIME);
7993348528dm		} else {
8003348528dm			/*
8013348528dm			 * Someone reset the system time;
8023348528dm			 * just force an immediate timeout.
8033348528dm			 */
8043348528dm			rval = -1;
8053348528dm		}
8063348528dm		if (rval == -1 && timecheck == timechanged) {
8073348528dm			/*
8083348528dm			 * Even though cv_timedwait_sig() returned showing a
8093348528dm			 * timeout, the future time may not have passed yet.
8103348528dm			 * If not, change rval to indicate a normal wakeup.
8113348528dm			 */
8123348528dm			gethrestime(&now);
8133348528dm			delta = *when;
8143348528dm			timespecsub(&delta, &now);
8153348528dm			if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
8163348528dm			    delta.tv_nsec > 0))
8177c478bdstevel@tonic-gate				rval = 1;
8183348528dm		}
8197c478bdstevel@tonic-gate	}
8207c478bdstevel@tonic-gate	return (rval);
8217c478bdstevel@tonic-gate}
822