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 /*
23*11640Srafael.vanoni@sun.com * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <sys/thread.h>
280Sstevel@tonic-gate #include <sys/proc.h>
290Sstevel@tonic-gate #include <sys/debug.h>
300Sstevel@tonic-gate #include <sys/cmn_err.h>
310Sstevel@tonic-gate #include <sys/systm.h>
320Sstevel@tonic-gate #include <sys/sobject.h>
330Sstevel@tonic-gate #include <sys/sleepq.h>
340Sstevel@tonic-gate #include <sys/cpuvar.h>
350Sstevel@tonic-gate #include <sys/condvar.h>
360Sstevel@tonic-gate #include <sys/condvar_impl.h>
370Sstevel@tonic-gate #include <sys/schedctl.h>
380Sstevel@tonic-gate #include <sys/procfs.h>
390Sstevel@tonic-gate #include <sys/sdt.h>
408048SMadhavan.Venkataraman@Sun.COM #include <sys/callo.h>
410Sstevel@tonic-gate
429334SMadhavan.Venkataraman@Sun.COM clock_t cv_timedwait_hires(kcondvar_t *, kmutex_t *, hrtime_t, hrtime_t, int);
439334SMadhavan.Venkataraman@Sun.COM
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate * CV_MAX_WAITERS is the maximum number of waiters we track; once
460Sstevel@tonic-gate * the number becomes higher than that, we look at the sleepq to
470Sstevel@tonic-gate * see whether there are *really* any waiters.
480Sstevel@tonic-gate */
490Sstevel@tonic-gate #define CV_MAX_WAITERS 1024 /* must be power of 2 */
500Sstevel@tonic-gate #define CV_WAITERS_MASK (CV_MAX_WAITERS - 1)
510Sstevel@tonic-gate
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate * Threads don't "own" condition variables.
540Sstevel@tonic-gate */
550Sstevel@tonic-gate /* ARGSUSED */
560Sstevel@tonic-gate static kthread_t *
cv_owner(void * cvp)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
cv_unsleep(kthread_t * t)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
cv_change_pri(kthread_t * t,pri_t pri,pri_t * t_prip)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
cv_init(kcondvar_t * cvp,char * name,kcv_type_t type,void * arg)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
cv_destroy(kcondvar_t * cvp)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
cv_block(condvar_impl_t * cvp)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
cv_wait(kcondvar_t * cvp,kmutex_t * mp)1880Sstevel@tonic-gate cv_wait(kcondvar_t *cvp, kmutex_t *mp)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate if (panicstr)
1910Sstevel@tonic-gate return;
19211084SJerry.Gilliam@Sun.COM ASSERT(!quiesce_active);
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
1950Sstevel@tonic-gate thread_lock(curthread); /* lock the thread */
1960Sstevel@tonic-gate cv_block((condvar_impl_t *)cvp);
1970Sstevel@tonic-gate thread_unlock_nopreempt(curthread); /* unlock the waiters field */
1980Sstevel@tonic-gate mutex_exit(mp);
1990Sstevel@tonic-gate swtch();
2000Sstevel@tonic-gate mutex_enter(mp);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2038048SMadhavan.Venkataraman@Sun.COM static void
cv_wakeup(void * arg)2048048SMadhavan.Venkataraman@Sun.COM cv_wakeup(void *arg)
2058048SMadhavan.Venkataraman@Sun.COM {
2068048SMadhavan.Venkataraman@Sun.COM kthread_t *t = arg;
2078048SMadhavan.Venkataraman@Sun.COM
2088048SMadhavan.Venkataraman@Sun.COM /*
2098048SMadhavan.Venkataraman@Sun.COM * This mutex is acquired and released in order to make sure that
2108048SMadhavan.Venkataraman@Sun.COM * the wakeup does not happen before the block itself happens.
2118048SMadhavan.Venkataraman@Sun.COM */
2128566SMadhavan.Venkataraman@Sun.COM mutex_enter(&t->t_wait_mutex);
2138566SMadhavan.Venkataraman@Sun.COM mutex_exit(&t->t_wait_mutex);
2148048SMadhavan.Venkataraman@Sun.COM setrun(t);
2158048SMadhavan.Venkataraman@Sun.COM }
2168048SMadhavan.Venkataraman@Sun.COM
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate * Same as cv_wait except the thread will unblock at 'tim'
2190Sstevel@tonic-gate * (an absolute time) if it hasn't already unblocked.
2200Sstevel@tonic-gate *
2210Sstevel@tonic-gate * Returns the amount of time left from the original 'tim' value
2220Sstevel@tonic-gate * when it was unblocked.
2230Sstevel@tonic-gate */
2240Sstevel@tonic-gate clock_t
cv_timedwait(kcondvar_t * cvp,kmutex_t * mp,clock_t tim)2250Sstevel@tonic-gate cv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
2260Sstevel@tonic-gate {
2279334SMadhavan.Venkataraman@Sun.COM hrtime_t hrtim;
22811066Srafael.vanoni@sun.com clock_t now = ddi_get_lbolt();
2299334SMadhavan.Venkataraman@Sun.COM
23011066Srafael.vanoni@sun.com if (tim <= now)
2319334SMadhavan.Venkataraman@Sun.COM return (-1);
2329334SMadhavan.Venkataraman@Sun.COM
23311066Srafael.vanoni@sun.com hrtim = TICK_TO_NSEC(tim - now);
2349334SMadhavan.Venkataraman@Sun.COM return (cv_timedwait_hires(cvp, mp, hrtim, nsec_per_tick, 0));
2359334SMadhavan.Venkataraman@Sun.COM }
2369334SMadhavan.Venkataraman@Sun.COM
23711066Srafael.vanoni@sun.com /*
23811066Srafael.vanoni@sun.com * Same as cv_timedwait() except that the third argument is a relative
23911066Srafael.vanoni@sun.com * timeout value, as opposed to an absolute one. There is also a fourth
24011066Srafael.vanoni@sun.com * argument that specifies how accurately the timeout must be implemented.
24111066Srafael.vanoni@sun.com */
24211066Srafael.vanoni@sun.com clock_t
cv_reltimedwait(kcondvar_t * cvp,kmutex_t * mp,clock_t delta,time_res_t res)24311066Srafael.vanoni@sun.com cv_reltimedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t delta, time_res_t res)
24411066Srafael.vanoni@sun.com {
24511066Srafael.vanoni@sun.com hrtime_t exp;
24611066Srafael.vanoni@sun.com
24711066Srafael.vanoni@sun.com ASSERT(TIME_RES_VALID(res));
24811066Srafael.vanoni@sun.com
24911066Srafael.vanoni@sun.com if (delta <= 0)
25011066Srafael.vanoni@sun.com return (-1);
25111066Srafael.vanoni@sun.com
25211066Srafael.vanoni@sun.com if ((exp = TICK_TO_NSEC(delta)) < 0)
25311066Srafael.vanoni@sun.com exp = CY_INFINITY;
25411066Srafael.vanoni@sun.com
25511066Srafael.vanoni@sun.com return (cv_timedwait_hires(cvp, mp, exp, time_res[res], 0));
25611066Srafael.vanoni@sun.com }
25711066Srafael.vanoni@sun.com
2589334SMadhavan.Venkataraman@Sun.COM clock_t
cv_timedwait_hires(kcondvar_t * cvp,kmutex_t * mp,hrtime_t tim,hrtime_t res,int flag)2599334SMadhavan.Venkataraman@Sun.COM cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
2609334SMadhavan.Venkataraman@Sun.COM hrtime_t res, int flag)
2619334SMadhavan.Venkataraman@Sun.COM {
2620Sstevel@tonic-gate kthread_t *t = curthread;
2638048SMadhavan.Venkataraman@Sun.COM callout_id_t id;
2640Sstevel@tonic-gate clock_t timeleft;
2659334SMadhavan.Venkataraman@Sun.COM hrtime_t limit;
2660Sstevel@tonic-gate int signalled;
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate if (panicstr)
2690Sstevel@tonic-gate return (-1);
27011084SJerry.Gilliam@Sun.COM ASSERT(!quiesce_active);
2710Sstevel@tonic-gate
2729334SMadhavan.Venkataraman@Sun.COM limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
2739334SMadhavan.Venkataraman@Sun.COM if (tim <= limit)
2740Sstevel@tonic-gate return (-1);
2758566SMadhavan.Venkataraman@Sun.COM mutex_enter(&t->t_wait_mutex);
2769334SMadhavan.Venkataraman@Sun.COM id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
2779334SMadhavan.Venkataraman@Sun.COM tim, res, flag);
2780Sstevel@tonic-gate thread_lock(t); /* lock the thread */
2790Sstevel@tonic-gate cv_block((condvar_impl_t *)cvp);
2800Sstevel@tonic-gate thread_unlock_nopreempt(t);
2818566SMadhavan.Venkataraman@Sun.COM mutex_exit(&t->t_wait_mutex);
2820Sstevel@tonic-gate mutex_exit(mp);
2830Sstevel@tonic-gate swtch();
2840Sstevel@tonic-gate signalled = (t->t_schedflag & TS_SIGNALLED);
2850Sstevel@tonic-gate /*
2860Sstevel@tonic-gate * Get the time left. untimeout() returns -1 if the timeout has
2870Sstevel@tonic-gate * occured or the time remaining. If the time remaining is zero,
2880Sstevel@tonic-gate * the timeout has occured between when we were awoken and
2890Sstevel@tonic-gate * we called untimeout. We will treat this as if the timeout
2900Sstevel@tonic-gate * has occured and set timeleft to -1.
2910Sstevel@tonic-gate */
2928566SMadhavan.Venkataraman@Sun.COM timeleft = untimeout_default(id, 0);
2930Sstevel@tonic-gate mutex_enter(mp);
2940Sstevel@tonic-gate if (timeleft <= 0) {
2950Sstevel@tonic-gate timeleft = -1;
2960Sstevel@tonic-gate if (signalled) /* avoid consuming the cv_signal() */
2970Sstevel@tonic-gate cv_signal(cvp);
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate return (timeleft);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate int
cv_wait_sig(kcondvar_t * cvp,kmutex_t * mp)3030Sstevel@tonic-gate cv_wait_sig(kcondvar_t *cvp, kmutex_t *mp)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate kthread_t *t = curthread;
3060Sstevel@tonic-gate proc_t *p = ttoproc(t);
3070Sstevel@tonic-gate klwp_t *lwp = ttolwp(t);
3085891Sraf int cancel_pending;
3090Sstevel@tonic-gate int rval = 1;
3100Sstevel@tonic-gate int signalled = 0;
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate if (panicstr)
3130Sstevel@tonic-gate return (rval);
31411084SJerry.Gilliam@Sun.COM ASSERT(!quiesce_active);
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate /*
31711173SJonathan.Adams@Sun.COM * Threads in system processes don't process signals. This is
31811173SJonathan.Adams@Sun.COM * true both for standard threads of system processes and for
31911173SJonathan.Adams@Sun.COM * interrupt threads which have borrowed their pinned thread's LWP.
3200Sstevel@tonic-gate */
32111173SJonathan.Adams@Sun.COM if (lwp == NULL || (p->p_flag & SSYS)) {
3220Sstevel@tonic-gate cv_wait(cvp, mp);
3230Sstevel@tonic-gate return (rval);
3240Sstevel@tonic-gate }
32511173SJonathan.Adams@Sun.COM ASSERT(t->t_intr == NULL);
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
3285891Sraf cancel_pending = schedctl_cancel_pending();
3290Sstevel@tonic-gate lwp->lwp_asleep = 1;
3300Sstevel@tonic-gate lwp->lwp_sysabort = 0;
3310Sstevel@tonic-gate thread_lock(t);
3320Sstevel@tonic-gate cv_block_sig(t, (condvar_impl_t *)cvp);
3330Sstevel@tonic-gate thread_unlock_nopreempt(t);
3340Sstevel@tonic-gate mutex_exit(mp);
3355891Sraf if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
3360Sstevel@tonic-gate setrun(t);
3370Sstevel@tonic-gate /* ASSERT(no locks are held) */
3380Sstevel@tonic-gate swtch();
3390Sstevel@tonic-gate signalled = (t->t_schedflag & TS_SIGNALLED);
3400Sstevel@tonic-gate t->t_flag &= ~T_WAKEABLE;
3410Sstevel@tonic-gate mutex_enter(mp);
3420Sstevel@tonic-gate if (ISSIG_PENDING(t, lwp, p)) {
3430Sstevel@tonic-gate mutex_exit(mp);
3440Sstevel@tonic-gate if (issig(FORREAL))
3450Sstevel@tonic-gate rval = 0;
3460Sstevel@tonic-gate mutex_enter(mp);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate if (lwp->lwp_sysabort || MUSTRETURN(p, t))
3490Sstevel@tonic-gate rval = 0;
3505891Sraf if (rval != 0 && cancel_pending) {
3515891Sraf schedctl_cancel_eintr();
3525891Sraf rval = 0;
3535891Sraf }
3540Sstevel@tonic-gate lwp->lwp_asleep = 0;
3550Sstevel@tonic-gate lwp->lwp_sysabort = 0;
3560Sstevel@tonic-gate if (rval == 0 && signalled) /* avoid consuming the cv_signal() */
3570Sstevel@tonic-gate cv_signal(cvp);
3580Sstevel@tonic-gate return (rval);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate
3618048SMadhavan.Venkataraman@Sun.COM static clock_t
cv_timedwait_sig_hires(kcondvar_t * cvp,kmutex_t * mp,hrtime_t tim,hrtime_t res,int flag)3629334SMadhavan.Venkataraman@Sun.COM cv_timedwait_sig_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
3639334SMadhavan.Venkataraman@Sun.COM hrtime_t res, int flag)
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate kthread_t *t = curthread;
3660Sstevel@tonic-gate proc_t *p = ttoproc(t);
3670Sstevel@tonic-gate klwp_t *lwp = ttolwp(t);
3685891Sraf int cancel_pending = 0;
3698048SMadhavan.Venkataraman@Sun.COM callout_id_t id;
3700Sstevel@tonic-gate clock_t rval = 1;
3719334SMadhavan.Venkataraman@Sun.COM hrtime_t limit;
3720Sstevel@tonic-gate int signalled = 0;
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate if (panicstr)
3750Sstevel@tonic-gate return (rval);
37611084SJerry.Gilliam@Sun.COM ASSERT(!quiesce_active);
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate /*
37911173SJonathan.Adams@Sun.COM * Threads in system processes don't process signals. This is
38011173SJonathan.Adams@Sun.COM * true both for standard threads of system processes and for
38111173SJonathan.Adams@Sun.COM * interrupt threads which have borrowed their pinned thread's LWP.
3820Sstevel@tonic-gate */
38311173SJonathan.Adams@Sun.COM if (lwp == NULL || (p->p_flag & SSYS))
3849334SMadhavan.Venkataraman@Sun.COM return (cv_timedwait_hires(cvp, mp, tim, res, flag));
38511173SJonathan.Adams@Sun.COM ASSERT(t->t_intr == NULL);
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate /*
3889334SMadhavan.Venkataraman@Sun.COM * If tim is less than or equal to current hrtime, then the timeout
3890Sstevel@tonic-gate * has already occured. So just check to see if there is a signal
3900Sstevel@tonic-gate * pending. If so return 0 indicating that there is a signal pending.
3910Sstevel@tonic-gate * Else return -1 indicating that the timeout occured. No need to
3920Sstevel@tonic-gate * wait on anything.
3930Sstevel@tonic-gate */
3949334SMadhavan.Venkataraman@Sun.COM limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
3959334SMadhavan.Venkataraman@Sun.COM if (tim <= limit) {
3960Sstevel@tonic-gate lwp->lwp_asleep = 1;
3970Sstevel@tonic-gate lwp->lwp_sysabort = 0;
3980Sstevel@tonic-gate rval = -1;
3990Sstevel@tonic-gate goto out;
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate /*
4030Sstevel@tonic-gate * Set the timeout and wait.
4040Sstevel@tonic-gate */
4055891Sraf cancel_pending = schedctl_cancel_pending();
4068566SMadhavan.Venkataraman@Sun.COM mutex_enter(&t->t_wait_mutex);
4078048SMadhavan.Venkataraman@Sun.COM id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
4089334SMadhavan.Venkataraman@Sun.COM tim, res, flag);
4090Sstevel@tonic-gate lwp->lwp_asleep = 1;
4100Sstevel@tonic-gate lwp->lwp_sysabort = 0;
4110Sstevel@tonic-gate thread_lock(t);
4120Sstevel@tonic-gate cv_block_sig(t, (condvar_impl_t *)cvp);
4130Sstevel@tonic-gate thread_unlock_nopreempt(t);
4148566SMadhavan.Venkataraman@Sun.COM mutex_exit(&t->t_wait_mutex);
4150Sstevel@tonic-gate mutex_exit(mp);
4168048SMadhavan.Venkataraman@Sun.COM if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
4170Sstevel@tonic-gate setrun(t);
4180Sstevel@tonic-gate /* ASSERT(no locks are held) */
4190Sstevel@tonic-gate swtch();
4200Sstevel@tonic-gate signalled = (t->t_schedflag & TS_SIGNALLED);
4210Sstevel@tonic-gate t->t_flag &= ~T_WAKEABLE;
4220Sstevel@tonic-gate
4230Sstevel@tonic-gate /*
4240Sstevel@tonic-gate * Untimeout the thread. untimeout() returns -1 if the timeout has
4250Sstevel@tonic-gate * occured or the time remaining. If the time remaining is zero,
4260Sstevel@tonic-gate * the timeout has occured between when we were awoken and
4270Sstevel@tonic-gate * we called untimeout. We will treat this as if the timeout
4280Sstevel@tonic-gate * has occured and set rval to -1.
4290Sstevel@tonic-gate */
4308566SMadhavan.Venkataraman@Sun.COM rval = untimeout_default(id, 0);
4318048SMadhavan.Venkataraman@Sun.COM mutex_enter(mp);
4320Sstevel@tonic-gate if (rval <= 0)
4330Sstevel@tonic-gate rval = -1;
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate * Check to see if a signal is pending. If so, regardless of whether
4370Sstevel@tonic-gate * or not we were awoken due to the signal, the signal is now pending
4380Sstevel@tonic-gate * and a return of 0 has the highest priority.
4390Sstevel@tonic-gate */
4400Sstevel@tonic-gate out:
4410Sstevel@tonic-gate if (ISSIG_PENDING(t, lwp, p)) {
4420Sstevel@tonic-gate mutex_exit(mp);
4430Sstevel@tonic-gate if (issig(FORREAL))
4440Sstevel@tonic-gate rval = 0;
4450Sstevel@tonic-gate mutex_enter(mp);
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate if (lwp->lwp_sysabort || MUSTRETURN(p, t))
4480Sstevel@tonic-gate rval = 0;
4495891Sraf if (rval != 0 && cancel_pending) {
4505891Sraf schedctl_cancel_eintr();
4515891Sraf rval = 0;
4525891Sraf }
4530Sstevel@tonic-gate lwp->lwp_asleep = 0;
4540Sstevel@tonic-gate lwp->lwp_sysabort = 0;
4550Sstevel@tonic-gate if (rval <= 0 && signalled) /* avoid consuming the cv_signal() */
4560Sstevel@tonic-gate cv_signal(cvp);
4570Sstevel@tonic-gate return (rval);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate /*
4618048SMadhavan.Venkataraman@Sun.COM * Returns:
4628048SMadhavan.Venkataraman@Sun.COM * Function result in order of precedence:
4638048SMadhavan.Venkataraman@Sun.COM * 0 if a signal was received
4648048SMadhavan.Venkataraman@Sun.COM * -1 if timeout occured
4658048SMadhavan.Venkataraman@Sun.COM * >0 if awakened via cv_signal() or cv_broadcast().
4668048SMadhavan.Venkataraman@Sun.COM * (returns time remaining)
4678048SMadhavan.Venkataraman@Sun.COM *
4688048SMadhavan.Venkataraman@Sun.COM * cv_timedwait_sig() is now part of the DDI.
4698048SMadhavan.Venkataraman@Sun.COM *
4709334SMadhavan.Venkataraman@Sun.COM * This function is now just a wrapper for cv_timedwait_sig_hires().
4718048SMadhavan.Venkataraman@Sun.COM */
4728048SMadhavan.Venkataraman@Sun.COM clock_t
cv_timedwait_sig(kcondvar_t * cvp,kmutex_t * mp,clock_t tim)4738048SMadhavan.Venkataraman@Sun.COM cv_timedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
4748048SMadhavan.Venkataraman@Sun.COM {
4759334SMadhavan.Venkataraman@Sun.COM hrtime_t hrtim;
4769334SMadhavan.Venkataraman@Sun.COM
47711066Srafael.vanoni@sun.com hrtim = TICK_TO_NSEC(tim - ddi_get_lbolt());
4789334SMadhavan.Venkataraman@Sun.COM return (cv_timedwait_sig_hires(cvp, mp, hrtim, nsec_per_tick, 0));
4798048SMadhavan.Venkataraman@Sun.COM }
4808048SMadhavan.Venkataraman@Sun.COM
4818048SMadhavan.Venkataraman@Sun.COM /*
48211066Srafael.vanoni@sun.com * Same as cv_timedwait_sig() except that the third argument is a relative
48311066Srafael.vanoni@sun.com * timeout value, as opposed to an absolute one. There is also a fourth
48411066Srafael.vanoni@sun.com * argument that specifies how accurately the timeout must be implemented.
48511066Srafael.vanoni@sun.com */
48611066Srafael.vanoni@sun.com clock_t
cv_reltimedwait_sig(kcondvar_t * cvp,kmutex_t * mp,clock_t delta,time_res_t res)48711066Srafael.vanoni@sun.com cv_reltimedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t delta,
48811066Srafael.vanoni@sun.com time_res_t res)
48911066Srafael.vanoni@sun.com {
490*11640Srafael.vanoni@sun.com hrtime_t exp = 0;
49111066Srafael.vanoni@sun.com
49211066Srafael.vanoni@sun.com ASSERT(TIME_RES_VALID(res));
49311066Srafael.vanoni@sun.com
494*11640Srafael.vanoni@sun.com if (delta > 0) {
495*11640Srafael.vanoni@sun.com if ((exp = TICK_TO_NSEC(delta)) < 0)
496*11640Srafael.vanoni@sun.com exp = CY_INFINITY;
497*11640Srafael.vanoni@sun.com }
49811066Srafael.vanoni@sun.com
49911066Srafael.vanoni@sun.com return (cv_timedwait_sig_hires(cvp, mp, exp, time_res[res], 0));
50011066Srafael.vanoni@sun.com }
50111066Srafael.vanoni@sun.com
50211066Srafael.vanoni@sun.com /*
5030Sstevel@tonic-gate * Like cv_wait_sig_swap but allows the caller to indicate (with a
5040Sstevel@tonic-gate * non-NULL sigret) that they will take care of signalling the cv
5050Sstevel@tonic-gate * after wakeup, if necessary. This is a vile hack that should only
5060Sstevel@tonic-gate * be used when no other option is available; almost all callers
5070Sstevel@tonic-gate * should just use cv_wait_sig_swap (which takes care of the cv_signal
5080Sstevel@tonic-gate * stuff automatically) instead.
5090Sstevel@tonic-gate */
5100Sstevel@tonic-gate int
cv_wait_sig_swap_core(kcondvar_t * cvp,kmutex_t * mp,int * sigret)5110Sstevel@tonic-gate cv_wait_sig_swap_core(kcondvar_t *cvp, kmutex_t *mp, int *sigret)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate kthread_t *t = curthread;
5140Sstevel@tonic-gate proc_t *p = ttoproc(t);
5150Sstevel@tonic-gate klwp_t *lwp = ttolwp(t);
5165891Sraf int cancel_pending;
5170Sstevel@tonic-gate int rval = 1;
5180Sstevel@tonic-gate int signalled = 0;
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate if (panicstr)
5210Sstevel@tonic-gate return (rval);
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate /*
52411173SJonathan.Adams@Sun.COM * Threads in system processes don't process signals. This is
52511173SJonathan.Adams@Sun.COM * true both for standard threads of system processes and for
52611173SJonathan.Adams@Sun.COM * interrupt threads which have borrowed their pinned thread's LWP.
5270Sstevel@tonic-gate */
52811173SJonathan.Adams@Sun.COM if (lwp == NULL || (p->p_flag & SSYS)) {
5290Sstevel@tonic-gate cv_wait(cvp, mp);
5300Sstevel@tonic-gate return (rval);
5310Sstevel@tonic-gate }
53211173SJonathan.Adams@Sun.COM ASSERT(t->t_intr == NULL);
5330Sstevel@tonic-gate
5345891Sraf cancel_pending = schedctl_cancel_pending();
5350Sstevel@tonic-gate lwp->lwp_asleep = 1;
5360Sstevel@tonic-gate lwp->lwp_sysabort = 0;
5370Sstevel@tonic-gate thread_lock(t);
5380Sstevel@tonic-gate t->t_kpri_req = 0; /* don't need kernel priority */
5390Sstevel@tonic-gate cv_block_sig(t, (condvar_impl_t *)cvp);
5400Sstevel@tonic-gate /* I can be swapped now */
5410Sstevel@tonic-gate curthread->t_schedflag &= ~TS_DONT_SWAP;
5420Sstevel@tonic-gate thread_unlock_nopreempt(t);
5430Sstevel@tonic-gate mutex_exit(mp);
5445891Sraf if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
5450Sstevel@tonic-gate setrun(t);
5460Sstevel@tonic-gate /* ASSERT(no locks are held) */
5470Sstevel@tonic-gate swtch();
5480Sstevel@tonic-gate signalled = (t->t_schedflag & TS_SIGNALLED);
5490Sstevel@tonic-gate t->t_flag &= ~T_WAKEABLE;
5500Sstevel@tonic-gate /* TS_DONT_SWAP set by disp() */
5510Sstevel@tonic-gate ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
5520Sstevel@tonic-gate mutex_enter(mp);
5530Sstevel@tonic-gate if (ISSIG_PENDING(t, lwp, p)) {
5540Sstevel@tonic-gate mutex_exit(mp);
5550Sstevel@tonic-gate if (issig(FORREAL))
5560Sstevel@tonic-gate rval = 0;
5570Sstevel@tonic-gate mutex_enter(mp);
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate if (lwp->lwp_sysabort || MUSTRETURN(p, t))
5600Sstevel@tonic-gate rval = 0;
5615891Sraf if (rval != 0 && cancel_pending) {
5625891Sraf schedctl_cancel_eintr();
5635891Sraf rval = 0;
5645891Sraf }
5650Sstevel@tonic-gate lwp->lwp_asleep = 0;
5660Sstevel@tonic-gate lwp->lwp_sysabort = 0;
5670Sstevel@tonic-gate if (rval == 0) {
5680Sstevel@tonic-gate if (sigret != NULL)
5690Sstevel@tonic-gate *sigret = signalled; /* just tell the caller */
5700Sstevel@tonic-gate else if (signalled)
5710Sstevel@tonic-gate cv_signal(cvp); /* avoid consuming the cv_signal() */
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate return (rval);
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate
5760Sstevel@tonic-gate /*
5770Sstevel@tonic-gate * Same as cv_wait_sig but the thread can be swapped out while waiting.
5780Sstevel@tonic-gate * This should only be used when we know we aren't holding any locks.
5790Sstevel@tonic-gate */
5800Sstevel@tonic-gate int
cv_wait_sig_swap(kcondvar_t * cvp,kmutex_t * mp)5810Sstevel@tonic-gate cv_wait_sig_swap(kcondvar_t *cvp, kmutex_t *mp)
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate return (cv_wait_sig_swap_core(cvp, mp, NULL));
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate void
cv_signal(kcondvar_t * cvp)5870Sstevel@tonic-gate cv_signal(kcondvar_t *cvp)
5880Sstevel@tonic-gate {
5890Sstevel@tonic-gate condvar_impl_t *cp = (condvar_impl_t *)cvp;
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate /* make sure the cv_waiters field looks sane */
5920Sstevel@tonic-gate ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
5930Sstevel@tonic-gate if (cp->cv_waiters > 0) {
5940Sstevel@tonic-gate sleepq_head_t *sqh = SQHASH(cp);
5950Sstevel@tonic-gate disp_lock_enter(&sqh->sq_lock);
5960Sstevel@tonic-gate ASSERT(CPU_ON_INTR(CPU) == 0);
5970Sstevel@tonic-gate if (cp->cv_waiters & CV_WAITERS_MASK) {
5980Sstevel@tonic-gate kthread_t *t;
5990Sstevel@tonic-gate cp->cv_waiters--;
6000Sstevel@tonic-gate t = sleepq_wakeone_chan(&sqh->sq_queue, cp);
6010Sstevel@tonic-gate /*
6020Sstevel@tonic-gate * If cv_waiters is non-zero (and less than
6030Sstevel@tonic-gate * CV_MAX_WAITERS) there should be a thread
6040Sstevel@tonic-gate * in the queue.
6050Sstevel@tonic-gate */
6060Sstevel@tonic-gate ASSERT(t != NULL);
6070Sstevel@tonic-gate } else if (sleepq_wakeone_chan(&sqh->sq_queue, cp) == NULL) {
6080Sstevel@tonic-gate cp->cv_waiters = 0;
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate disp_lock_exit(&sqh->sq_lock);
6110Sstevel@tonic-gate }
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate void
cv_broadcast(kcondvar_t * cvp)6150Sstevel@tonic-gate cv_broadcast(kcondvar_t *cvp)
6160Sstevel@tonic-gate {
6170Sstevel@tonic-gate condvar_impl_t *cp = (condvar_impl_t *)cvp;
6180Sstevel@tonic-gate
6190Sstevel@tonic-gate /* make sure the cv_waiters field looks sane */
6200Sstevel@tonic-gate ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
6210Sstevel@tonic-gate if (cp->cv_waiters > 0) {
6220Sstevel@tonic-gate sleepq_head_t *sqh = SQHASH(cp);
6230Sstevel@tonic-gate disp_lock_enter(&sqh->sq_lock);
6240Sstevel@tonic-gate ASSERT(CPU_ON_INTR(CPU) == 0);
6250Sstevel@tonic-gate sleepq_wakeall_chan(&sqh->sq_queue, cp);
6260Sstevel@tonic-gate cp->cv_waiters = 0;
6270Sstevel@tonic-gate disp_lock_exit(&sqh->sq_lock);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate
6310Sstevel@tonic-gate /*
6320Sstevel@tonic-gate * Same as cv_wait(), but wakes up (after wakeup_time milliseconds) to check
6330Sstevel@tonic-gate * for requests to stop, like cv_wait_sig() but without dealing with signals.
6340Sstevel@tonic-gate * This is a horrible kludge. It is evil. It is vile. It is swill.
6350Sstevel@tonic-gate * If your code has to call this function then your code is the same.
6360Sstevel@tonic-gate */
6370Sstevel@tonic-gate void
cv_wait_stop(kcondvar_t * cvp,kmutex_t * mp,int wakeup_time)6380Sstevel@tonic-gate cv_wait_stop(kcondvar_t *cvp, kmutex_t *mp, int wakeup_time)
6390Sstevel@tonic-gate {
6400Sstevel@tonic-gate kthread_t *t = curthread;
6410Sstevel@tonic-gate klwp_t *lwp = ttolwp(t);
6420Sstevel@tonic-gate proc_t *p = ttoproc(t);
6438048SMadhavan.Venkataraman@Sun.COM callout_id_t id;
6440Sstevel@tonic-gate clock_t tim;
6450Sstevel@tonic-gate
6460Sstevel@tonic-gate if (panicstr)
6470Sstevel@tonic-gate return;
6480Sstevel@tonic-gate
6490Sstevel@tonic-gate /*
65011173SJonathan.Adams@Sun.COM * Threads in system processes don't process signals. This is
65111173SJonathan.Adams@Sun.COM * true both for standard threads of system processes and for
65211173SJonathan.Adams@Sun.COM * interrupt threads which have borrowed their pinned thread's LWP.
6530Sstevel@tonic-gate */
65411173SJonathan.Adams@Sun.COM if (lwp == NULL || (p->p_flag & SSYS)) {
6550Sstevel@tonic-gate cv_wait(cvp, mp);
6560Sstevel@tonic-gate return;
6570Sstevel@tonic-gate }
65811173SJonathan.Adams@Sun.COM ASSERT(t->t_intr == NULL);
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate /*
6610Sstevel@tonic-gate * Wakeup in wakeup_time milliseconds, i.e., human time.
6620Sstevel@tonic-gate */
66311066Srafael.vanoni@sun.com tim = ddi_get_lbolt() + MSEC_TO_TICK(wakeup_time);
6648566SMadhavan.Venkataraman@Sun.COM mutex_enter(&t->t_wait_mutex);
6658048SMadhavan.Venkataraman@Sun.COM id = realtime_timeout_default((void (*)(void *))cv_wakeup, t,
66611066Srafael.vanoni@sun.com tim - ddi_get_lbolt());
6670Sstevel@tonic-gate thread_lock(t); /* lock the thread */
6680Sstevel@tonic-gate cv_block((condvar_impl_t *)cvp);
6690Sstevel@tonic-gate thread_unlock_nopreempt(t);
6708566SMadhavan.Venkataraman@Sun.COM mutex_exit(&t->t_wait_mutex);
6710Sstevel@tonic-gate mutex_exit(mp);
6720Sstevel@tonic-gate /* ASSERT(no locks are held); */
6730Sstevel@tonic-gate swtch();
6748566SMadhavan.Venkataraman@Sun.COM (void) untimeout_default(id, 0);
6750Sstevel@tonic-gate
6760Sstevel@tonic-gate /*
6773930Snr123932 * Check for reasons to stop, if lwp_nostop is not true.
6780Sstevel@tonic-gate * See issig_forreal() for explanations of the various stops.
6790Sstevel@tonic-gate */
6800Sstevel@tonic-gate mutex_enter(&p->p_lock);
6813930Snr123932 while (lwp->lwp_nostop == 0 && !(p->p_flag & SEXITLWPS)) {
6820Sstevel@tonic-gate /*
6830Sstevel@tonic-gate * Hold the lwp here for watchpoint manipulation.
6840Sstevel@tonic-gate */
6853930Snr123932 if (t->t_proc_flag & TP_PAUSE) {
6860Sstevel@tonic-gate stop(PR_SUSPENDED, SUSPEND_PAUSE);
6870Sstevel@tonic-gate continue;
6880Sstevel@tonic-gate }
6890Sstevel@tonic-gate /*
6900Sstevel@tonic-gate * System checkpoint.
6910Sstevel@tonic-gate */
6923930Snr123932 if (t->t_proc_flag & TP_CHKPT) {
6930Sstevel@tonic-gate stop(PR_CHECKPOINT, 0);
6940Sstevel@tonic-gate continue;
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate /*
6970Sstevel@tonic-gate * Honor fork1(), watchpoint activity (remapping a page),
6983930Snr123932 * and lwp_suspend() requests.
6990Sstevel@tonic-gate */
7003930Snr123932 if ((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
7013930Snr123932 (t->t_proc_flag & TP_HOLDLWP)) {
7020Sstevel@tonic-gate stop(PR_SUSPENDED, SUSPEND_NORMAL);
7030Sstevel@tonic-gate continue;
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate /*
7060Sstevel@tonic-gate * Honor /proc requested stop.
7070Sstevel@tonic-gate */
7083930Snr123932 if (t->t_proc_flag & TP_PRSTOP) {
7090Sstevel@tonic-gate stop(PR_REQUESTED, 0);
7100Sstevel@tonic-gate }
7110Sstevel@tonic-gate /*
7120Sstevel@tonic-gate * If some lwp in the process has already stopped
7130Sstevel@tonic-gate * showing PR_JOBCONTROL, stop in sympathy with it.
7140Sstevel@tonic-gate */
7153930Snr123932 if (p->p_stopsig && t != p->p_agenttp) {
7160Sstevel@tonic-gate stop(PR_JOBCONTROL, p->p_stopsig);
7170Sstevel@tonic-gate continue;
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate break;
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate mutex_exit(&p->p_lock);
7220Sstevel@tonic-gate mutex_enter(mp);
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate /*
7260Sstevel@tonic-gate * Like cv_timedwait_sig(), but takes an absolute hires future time
7270Sstevel@tonic-gate * rather than a future time in clock ticks. Will not return showing
7280Sstevel@tonic-gate * that a timeout occurred until the future time is passed.
7290Sstevel@tonic-gate * If 'when' is a NULL pointer, no timeout will occur.
7300Sstevel@tonic-gate * Returns:
7318048SMadhavan.Venkataraman@Sun.COM * Function result in order of precedence:
7320Sstevel@tonic-gate * 0 if a signal was received
7330Sstevel@tonic-gate * -1 if timeout occured
7340Sstevel@tonic-gate * >0 if awakened via cv_signal() or cv_broadcast()
7350Sstevel@tonic-gate * or by a spurious wakeup.
7360Sstevel@tonic-gate * (might return time remaining)
7374123Sdm120769 * As a special test, if someone abruptly resets the system time
7384123Sdm120769 * (but not through adjtime(2); drifting of the clock is allowed and
7394123Sdm120769 * expected [see timespectohz_adj()]), then we force a return of -1
7404123Sdm120769 * so the caller can return a premature timeout to the calling process
7414123Sdm120769 * so it can reevaluate the situation in light of the new system time.
7424123Sdm120769 * (The system clock has been reset if timecheck != timechanged.)
7430Sstevel@tonic-gate */
7440Sstevel@tonic-gate int
cv_waituntil_sig(kcondvar_t * cvp,kmutex_t * mp,timestruc_t * when,int timecheck)7454123Sdm120769 cv_waituntil_sig(kcondvar_t *cvp, kmutex_t *mp,
7464123Sdm120769 timestruc_t *when, int timecheck)
7470Sstevel@tonic-gate {
7480Sstevel@tonic-gate timestruc_t now;
7493346Svb160487 timestruc_t delta;
7509334SMadhavan.Venkataraman@Sun.COM hrtime_t interval;
7510Sstevel@tonic-gate int rval;
7520Sstevel@tonic-gate
7530Sstevel@tonic-gate if (when == NULL)
7540Sstevel@tonic-gate return (cv_wait_sig_swap(cvp, mp));
7550Sstevel@tonic-gate
7567982SDonghai.Qiao@Sun.COM gethrestime(&now);
7573346Svb160487 delta = *when;
7583346Svb160487 timespecsub(&delta, &now);
7593346Svb160487 if (delta.tv_sec < 0 || (delta.tv_sec == 0 && delta.tv_nsec == 0)) {
7600Sstevel@tonic-gate /*
7610Sstevel@tonic-gate * We have already reached the absolute future time.
7620Sstevel@tonic-gate * Call cv_timedwait_sig() just to check for signals.
7630Sstevel@tonic-gate * We will return immediately with either 0 or -1.
7640Sstevel@tonic-gate */
7659334SMadhavan.Venkataraman@Sun.COM rval = cv_timedwait_sig_hires(cvp, mp, 0, 1, 0);
7660Sstevel@tonic-gate } else {
7674123Sdm120769 if (timecheck == timechanged) {
7689334SMadhavan.Venkataraman@Sun.COM /*
7699334SMadhavan.Venkataraman@Sun.COM * Make sure that the interval is atleast one tick.
7709334SMadhavan.Venkataraman@Sun.COM * This is to prevent a user from flooding the system
7719334SMadhavan.Venkataraman@Sun.COM * with very small, high resolution timers.
7729334SMadhavan.Venkataraman@Sun.COM */
7739334SMadhavan.Venkataraman@Sun.COM interval = ts2hrt(&delta);
7749334SMadhavan.Venkataraman@Sun.COM if (interval < nsec_per_tick)
7759334SMadhavan.Venkataraman@Sun.COM interval = nsec_per_tick;
7769334SMadhavan.Venkataraman@Sun.COM rval = cv_timedwait_sig_hires(cvp, mp, interval, 1,
7778048SMadhavan.Venkataraman@Sun.COM CALLOUT_FLAG_HRESTIME);
7784123Sdm120769 } else {
7794123Sdm120769 /*
7804123Sdm120769 * Someone reset the system time;
7814123Sdm120769 * just force an immediate timeout.
7824123Sdm120769 */
7834123Sdm120769 rval = -1;
7844123Sdm120769 }
7854123Sdm120769 if (rval == -1 && timecheck == timechanged) {
7864123Sdm120769 /*
7874123Sdm120769 * Even though cv_timedwait_sig() returned showing a
7884123Sdm120769 * timeout, the future time may not have passed yet.
7894123Sdm120769 * If not, change rval to indicate a normal wakeup.
7904123Sdm120769 */
7914123Sdm120769 gethrestime(&now);
7924123Sdm120769 delta = *when;
7934123Sdm120769 timespecsub(&delta, &now);
7944123Sdm120769 if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
7954123Sdm120769 delta.tv_nsec > 0))
7960Sstevel@tonic-gate rval = 1;
7974123Sdm120769 }
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate return (rval);
8000Sstevel@tonic-gate }
801