10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52498Sstevel * Common Development and Distribution License (the "License"). 62498Sstevel * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 215891Sraf 220Sstevel@tonic-gate /* 238566SMadhavan.Venkataraman@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #include <sys/thread.h> 280Sstevel@tonic-gate #include <sys/proc.h> 290Sstevel@tonic-gate #include <sys/debug.h> 300Sstevel@tonic-gate #include <sys/cmn_err.h> 310Sstevel@tonic-gate #include <sys/systm.h> 320Sstevel@tonic-gate #include <sys/sobject.h> 330Sstevel@tonic-gate #include <sys/sleepq.h> 340Sstevel@tonic-gate #include <sys/cpuvar.h> 350Sstevel@tonic-gate #include <sys/condvar.h> 360Sstevel@tonic-gate #include <sys/condvar_impl.h> 370Sstevel@tonic-gate #include <sys/schedctl.h> 380Sstevel@tonic-gate #include <sys/procfs.h> 390Sstevel@tonic-gate #include <sys/sdt.h> 408048SMadhavan.Venkataraman@Sun.COM #include <sys/callo.h> 410Sstevel@tonic-gate 429334SMadhavan.Venkataraman@Sun.COM clock_t cv_timedwait_hires(kcondvar_t *, kmutex_t *, hrtime_t, hrtime_t, int); 439334SMadhavan.Venkataraman@Sun.COM 440Sstevel@tonic-gate /* 450Sstevel@tonic-gate * CV_MAX_WAITERS is the maximum number of waiters we track; once 460Sstevel@tonic-gate * the number becomes higher than that, we look at the sleepq to 470Sstevel@tonic-gate * see whether there are *really* any waiters. 480Sstevel@tonic-gate */ 490Sstevel@tonic-gate #define CV_MAX_WAITERS 1024 /* must be power of 2 */ 500Sstevel@tonic-gate #define CV_WAITERS_MASK (CV_MAX_WAITERS - 1) 510Sstevel@tonic-gate 520Sstevel@tonic-gate /* 530Sstevel@tonic-gate * Threads don't "own" condition variables. 540Sstevel@tonic-gate */ 550Sstevel@tonic-gate /* ARGSUSED */ 560Sstevel@tonic-gate static kthread_t * 570Sstevel@tonic-gate cv_owner(void *cvp) 580Sstevel@tonic-gate { 590Sstevel@tonic-gate return (NULL); 600Sstevel@tonic-gate } 610Sstevel@tonic-gate 620Sstevel@tonic-gate /* 630Sstevel@tonic-gate * Unsleep a thread that's blocked on a condition variable. 640Sstevel@tonic-gate */ 650Sstevel@tonic-gate static void 660Sstevel@tonic-gate cv_unsleep(kthread_t *t) 670Sstevel@tonic-gate { 680Sstevel@tonic-gate condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan; 690Sstevel@tonic-gate sleepq_head_t *sqh = SQHASH(cvp); 700Sstevel@tonic-gate 710Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); 720Sstevel@tonic-gate 730Sstevel@tonic-gate if (cvp == NULL) 747240Srh87107 panic("cv_unsleep: thread %p not on sleepq %p", 757240Srh87107 (void *)t, (void *)sqh); 762498Sstevel DTRACE_SCHED1(wakeup, kthread_t *, t); 770Sstevel@tonic-gate sleepq_unsleep(t); 780Sstevel@tonic-gate if (cvp->cv_waiters != CV_MAX_WAITERS) 790Sstevel@tonic-gate cvp->cv_waiters--; 800Sstevel@tonic-gate disp_lock_exit_high(&sqh->sq_lock); 810Sstevel@tonic-gate CL_SETRUN(t); 820Sstevel@tonic-gate } 830Sstevel@tonic-gate 840Sstevel@tonic-gate /* 850Sstevel@tonic-gate * Change the priority of a thread that's blocked on a condition variable. 860Sstevel@tonic-gate */ 870Sstevel@tonic-gate static void 880Sstevel@tonic-gate cv_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip) 890Sstevel@tonic-gate { 900Sstevel@tonic-gate condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan; 910Sstevel@tonic-gate sleepq_t *sqp = t->t_sleepq; 920Sstevel@tonic-gate 930Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); 940Sstevel@tonic-gate ASSERT(&SQHASH(cvp)->sq_queue == sqp); 950Sstevel@tonic-gate 960Sstevel@tonic-gate if (cvp == NULL) 977240Srh87107 panic("cv_change_pri: %p not on sleep queue", (void *)t); 980Sstevel@tonic-gate sleepq_dequeue(t); 990Sstevel@tonic-gate *t_prip = pri; 1000Sstevel@tonic-gate sleepq_insert(sqp, t); 1010Sstevel@tonic-gate } 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate /* 1040Sstevel@tonic-gate * The sobj_ops vector exports a set of functions needed when a thread 1050Sstevel@tonic-gate * is asleep on a synchronization object of this type. 1060Sstevel@tonic-gate */ 1070Sstevel@tonic-gate static sobj_ops_t cv_sobj_ops = { 1080Sstevel@tonic-gate SOBJ_CV, cv_owner, cv_unsleep, cv_change_pri 1090Sstevel@tonic-gate }; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate /* ARGSUSED */ 1120Sstevel@tonic-gate void 1130Sstevel@tonic-gate cv_init(kcondvar_t *cvp, char *name, kcv_type_t type, void *arg) 1140Sstevel@tonic-gate { 1150Sstevel@tonic-gate ((condvar_impl_t *)cvp)->cv_waiters = 0; 1160Sstevel@tonic-gate } 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate /* 1190Sstevel@tonic-gate * cv_destroy is not currently needed, but is part of the DDI. 1200Sstevel@tonic-gate * This is in case cv_init ever needs to allocate something for a cv. 1210Sstevel@tonic-gate */ 1220Sstevel@tonic-gate /* ARGSUSED */ 1230Sstevel@tonic-gate void 1240Sstevel@tonic-gate cv_destroy(kcondvar_t *cvp) 1250Sstevel@tonic-gate { 1260Sstevel@tonic-gate ASSERT((((condvar_impl_t *)cvp)->cv_waiters & CV_WAITERS_MASK) == 0); 1270Sstevel@tonic-gate } 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate /* 1300Sstevel@tonic-gate * The cv_block() function blocks a thread on a condition variable 1310Sstevel@tonic-gate * by putting it in a hashed sleep queue associated with the 1320Sstevel@tonic-gate * synchronization object. 1330Sstevel@tonic-gate * 1340Sstevel@tonic-gate * Threads are taken off the hashed sleep queues via calls to 1350Sstevel@tonic-gate * cv_signal(), cv_broadcast(), or cv_unsleep(). 1360Sstevel@tonic-gate */ 1370Sstevel@tonic-gate static void 1380Sstevel@tonic-gate cv_block(condvar_impl_t *cvp) 1390Sstevel@tonic-gate { 1400Sstevel@tonic-gate kthread_t *t = curthread; 1410Sstevel@tonic-gate klwp_t *lwp = ttolwp(t); 1420Sstevel@tonic-gate sleepq_head_t *sqh; 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); 1450Sstevel@tonic-gate ASSERT(t != CPU->cpu_idle_thread); 1460Sstevel@tonic-gate ASSERT(CPU_ON_INTR(CPU) == 0); 1470Sstevel@tonic-gate ASSERT(t->t_wchan0 == NULL && t->t_wchan == NULL); 1480Sstevel@tonic-gate ASSERT(t->t_state == TS_ONPROC); 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate t->t_schedflag &= ~TS_SIGNALLED; 1510Sstevel@tonic-gate CL_SLEEP(t); /* assign kernel priority */ 1520Sstevel@tonic-gate t->t_wchan = (caddr_t)cvp; 1530Sstevel@tonic-gate t->t_sobj_ops = &cv_sobj_ops; 1540Sstevel@tonic-gate DTRACE_SCHED(sleep); 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate /* 1570Sstevel@tonic-gate * The check for t_intr is to avoid doing the 1580Sstevel@tonic-gate * account for an interrupt thread on the still-pinned 1590Sstevel@tonic-gate * lwp's statistics. 1600Sstevel@tonic-gate */ 1610Sstevel@tonic-gate if (lwp != NULL && t->t_intr == NULL) { 1620Sstevel@tonic-gate lwp->lwp_ru.nvcsw++; 1630Sstevel@tonic-gate (void) new_mstate(t, LMS_SLEEP); 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate sqh = SQHASH(cvp); 1670Sstevel@tonic-gate disp_lock_enter_high(&sqh->sq_lock); 1680Sstevel@tonic-gate if (cvp->cv_waiters < CV_MAX_WAITERS) 1690Sstevel@tonic-gate cvp->cv_waiters++; 1700Sstevel@tonic-gate ASSERT(cvp->cv_waiters <= CV_MAX_WAITERS); 1710Sstevel@tonic-gate THREAD_SLEEP(t, &sqh->sq_lock); 1720Sstevel@tonic-gate sleepq_insert(&sqh->sq_queue, t); 1730Sstevel@tonic-gate /* 1740Sstevel@tonic-gate * THREAD_SLEEP() moves curthread->t_lockp to point to the 1750Sstevel@tonic-gate * lock sqh->sq_lock. This lock is later released by the caller 1760Sstevel@tonic-gate * when it calls thread_unlock() on curthread. 1770Sstevel@tonic-gate */ 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate #define cv_block_sig(t, cvp) \ 1810Sstevel@tonic-gate { (t)->t_flag |= T_WAKEABLE; cv_block(cvp); } 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate /* 1840Sstevel@tonic-gate * Block on the indicated condition variable and release the 1850Sstevel@tonic-gate * associated kmutex while blocked. 1860Sstevel@tonic-gate */ 1870Sstevel@tonic-gate void 1880Sstevel@tonic-gate cv_wait(kcondvar_t *cvp, kmutex_t *mp) 1890Sstevel@tonic-gate { 1900Sstevel@tonic-gate if (panicstr) 1910Sstevel@tonic-gate return; 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 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 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 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 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 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 /* 317*11173SJonathan.Adams@Sun.COM * Threads in system processes don't process signals. This is 318*11173SJonathan.Adams@Sun.COM * true both for standard threads of system processes and for 319*11173SJonathan.Adams@Sun.COM * interrupt threads which have borrowed their pinned thread's LWP. 3200Sstevel@tonic-gate */ 321*11173SJonathan.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 } 325*11173SJonathan.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 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 /* 379*11173SJonathan.Adams@Sun.COM * Threads in system processes don't process signals. This is 380*11173SJonathan.Adams@Sun.COM * true both for standard threads of system processes and for 381*11173SJonathan.Adams@Sun.COM * interrupt threads which have borrowed their pinned thread's LWP. 3820Sstevel@tonic-gate */ 383*11173SJonathan.Adams@Sun.COM if (lwp == NULL || (p->p_flag & SSYS)) 3849334SMadhavan.Venkataraman@Sun.COM return (cv_timedwait_hires(cvp, mp, tim, res, flag)); 385*11173SJonathan.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 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 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 { 49011066Srafael.vanoni@sun.com hrtime_t exp; 49111066Srafael.vanoni@sun.com 49211066Srafael.vanoni@sun.com ASSERT(TIME_RES_VALID(res)); 49311066Srafael.vanoni@sun.com 49411066Srafael.vanoni@sun.com if ((exp = TICK_TO_NSEC(delta)) < 0) 49511066Srafael.vanoni@sun.com exp = CY_INFINITY; 49611066Srafael.vanoni@sun.com 49711066Srafael.vanoni@sun.com return (cv_timedwait_sig_hires(cvp, mp, exp, time_res[res], 0)); 49811066Srafael.vanoni@sun.com } 49911066Srafael.vanoni@sun.com 50011066Srafael.vanoni@sun.com /* 5010Sstevel@tonic-gate * Like cv_wait_sig_swap but allows the caller to indicate (with a 5020Sstevel@tonic-gate * non-NULL sigret) that they will take care of signalling the cv 5030Sstevel@tonic-gate * after wakeup, if necessary. This is a vile hack that should only 5040Sstevel@tonic-gate * be used when no other option is available; almost all callers 5050Sstevel@tonic-gate * should just use cv_wait_sig_swap (which takes care of the cv_signal 5060Sstevel@tonic-gate * stuff automatically) instead. 5070Sstevel@tonic-gate */ 5080Sstevel@tonic-gate int 5090Sstevel@tonic-gate cv_wait_sig_swap_core(kcondvar_t *cvp, kmutex_t *mp, int *sigret) 5100Sstevel@tonic-gate { 5110Sstevel@tonic-gate kthread_t *t = curthread; 5120Sstevel@tonic-gate proc_t *p = ttoproc(t); 5130Sstevel@tonic-gate klwp_t *lwp = ttolwp(t); 5145891Sraf int cancel_pending; 5150Sstevel@tonic-gate int rval = 1; 5160Sstevel@tonic-gate int signalled = 0; 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate if (panicstr) 5190Sstevel@tonic-gate return (rval); 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate /* 522*11173SJonathan.Adams@Sun.COM * Threads in system processes don't process signals. This is 523*11173SJonathan.Adams@Sun.COM * true both for standard threads of system processes and for 524*11173SJonathan.Adams@Sun.COM * interrupt threads which have borrowed their pinned thread's LWP. 5250Sstevel@tonic-gate */ 526*11173SJonathan.Adams@Sun.COM if (lwp == NULL || (p->p_flag & SSYS)) { 5270Sstevel@tonic-gate cv_wait(cvp, mp); 5280Sstevel@tonic-gate return (rval); 5290Sstevel@tonic-gate } 530*11173SJonathan.Adams@Sun.COM ASSERT(t->t_intr == NULL); 5310Sstevel@tonic-gate 5325891Sraf cancel_pending = schedctl_cancel_pending(); 5330Sstevel@tonic-gate lwp->lwp_asleep = 1; 5340Sstevel@tonic-gate lwp->lwp_sysabort = 0; 5350Sstevel@tonic-gate thread_lock(t); 5360Sstevel@tonic-gate t->t_kpri_req = 0; /* don't need kernel priority */ 5370Sstevel@tonic-gate cv_block_sig(t, (condvar_impl_t *)cvp); 5380Sstevel@tonic-gate /* I can be swapped now */ 5390Sstevel@tonic-gate curthread->t_schedflag &= ~TS_DONT_SWAP; 5400Sstevel@tonic-gate thread_unlock_nopreempt(t); 5410Sstevel@tonic-gate mutex_exit(mp); 5425891Sraf if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending) 5430Sstevel@tonic-gate setrun(t); 5440Sstevel@tonic-gate /* ASSERT(no locks are held) */ 5450Sstevel@tonic-gate swtch(); 5460Sstevel@tonic-gate signalled = (t->t_schedflag & TS_SIGNALLED); 5470Sstevel@tonic-gate t->t_flag &= ~T_WAKEABLE; 5480Sstevel@tonic-gate /* TS_DONT_SWAP set by disp() */ 5490Sstevel@tonic-gate ASSERT(curthread->t_schedflag & TS_DONT_SWAP); 5500Sstevel@tonic-gate mutex_enter(mp); 5510Sstevel@tonic-gate if (ISSIG_PENDING(t, lwp, p)) { 5520Sstevel@tonic-gate mutex_exit(mp); 5530Sstevel@tonic-gate if (issig(FORREAL)) 5540Sstevel@tonic-gate rval = 0; 5550Sstevel@tonic-gate mutex_enter(mp); 5560Sstevel@tonic-gate } 5570Sstevel@tonic-gate if (lwp->lwp_sysabort || MUSTRETURN(p, t)) 5580Sstevel@tonic-gate rval = 0; 5595891Sraf if (rval != 0 && cancel_pending) { 5605891Sraf schedctl_cancel_eintr(); 5615891Sraf rval = 0; 5625891Sraf } 5630Sstevel@tonic-gate lwp->lwp_asleep = 0; 5640Sstevel@tonic-gate lwp->lwp_sysabort = 0; 5650Sstevel@tonic-gate if (rval == 0) { 5660Sstevel@tonic-gate if (sigret != NULL) 5670Sstevel@tonic-gate *sigret = signalled; /* just tell the caller */ 5680Sstevel@tonic-gate else if (signalled) 5690Sstevel@tonic-gate cv_signal(cvp); /* avoid consuming the cv_signal() */ 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate return (rval); 5720Sstevel@tonic-gate } 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate /* 5750Sstevel@tonic-gate * Same as cv_wait_sig but the thread can be swapped out while waiting. 5760Sstevel@tonic-gate * This should only be used when we know we aren't holding any locks. 5770Sstevel@tonic-gate */ 5780Sstevel@tonic-gate int 5790Sstevel@tonic-gate cv_wait_sig_swap(kcondvar_t *cvp, kmutex_t *mp) 5800Sstevel@tonic-gate { 5810Sstevel@tonic-gate return (cv_wait_sig_swap_core(cvp, mp, NULL)); 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate void 5850Sstevel@tonic-gate cv_signal(kcondvar_t *cvp) 5860Sstevel@tonic-gate { 5870Sstevel@tonic-gate condvar_impl_t *cp = (condvar_impl_t *)cvp; 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate /* make sure the cv_waiters field looks sane */ 5900Sstevel@tonic-gate ASSERT(cp->cv_waiters <= CV_MAX_WAITERS); 5910Sstevel@tonic-gate if (cp->cv_waiters > 0) { 5920Sstevel@tonic-gate sleepq_head_t *sqh = SQHASH(cp); 5930Sstevel@tonic-gate disp_lock_enter(&sqh->sq_lock); 5940Sstevel@tonic-gate ASSERT(CPU_ON_INTR(CPU) == 0); 5950Sstevel@tonic-gate if (cp->cv_waiters & CV_WAITERS_MASK) { 5960Sstevel@tonic-gate kthread_t *t; 5970Sstevel@tonic-gate cp->cv_waiters--; 5980Sstevel@tonic-gate t = sleepq_wakeone_chan(&sqh->sq_queue, cp); 5990Sstevel@tonic-gate /* 6000Sstevel@tonic-gate * If cv_waiters is non-zero (and less than 6010Sstevel@tonic-gate * CV_MAX_WAITERS) there should be a thread 6020Sstevel@tonic-gate * in the queue. 6030Sstevel@tonic-gate */ 6040Sstevel@tonic-gate ASSERT(t != NULL); 6050Sstevel@tonic-gate } else if (sleepq_wakeone_chan(&sqh->sq_queue, cp) == NULL) { 6060Sstevel@tonic-gate cp->cv_waiters = 0; 6070Sstevel@tonic-gate } 6080Sstevel@tonic-gate disp_lock_exit(&sqh->sq_lock); 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate } 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate void 6130Sstevel@tonic-gate cv_broadcast(kcondvar_t *cvp) 6140Sstevel@tonic-gate { 6150Sstevel@tonic-gate condvar_impl_t *cp = (condvar_impl_t *)cvp; 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate /* make sure the cv_waiters field looks sane */ 6180Sstevel@tonic-gate ASSERT(cp->cv_waiters <= CV_MAX_WAITERS); 6190Sstevel@tonic-gate if (cp->cv_waiters > 0) { 6200Sstevel@tonic-gate sleepq_head_t *sqh = SQHASH(cp); 6210Sstevel@tonic-gate disp_lock_enter(&sqh->sq_lock); 6220Sstevel@tonic-gate ASSERT(CPU_ON_INTR(CPU) == 0); 6230Sstevel@tonic-gate sleepq_wakeall_chan(&sqh->sq_queue, cp); 6240Sstevel@tonic-gate cp->cv_waiters = 0; 6250Sstevel@tonic-gate disp_lock_exit(&sqh->sq_lock); 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate } 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate /* 6300Sstevel@tonic-gate * Same as cv_wait(), but wakes up (after wakeup_time milliseconds) to check 6310Sstevel@tonic-gate * for requests to stop, like cv_wait_sig() but without dealing with signals. 6320Sstevel@tonic-gate * This is a horrible kludge. It is evil. It is vile. It is swill. 6330Sstevel@tonic-gate * If your code has to call this function then your code is the same. 6340Sstevel@tonic-gate */ 6350Sstevel@tonic-gate void 6360Sstevel@tonic-gate cv_wait_stop(kcondvar_t *cvp, kmutex_t *mp, int wakeup_time) 6370Sstevel@tonic-gate { 6380Sstevel@tonic-gate kthread_t *t = curthread; 6390Sstevel@tonic-gate klwp_t *lwp = ttolwp(t); 6400Sstevel@tonic-gate proc_t *p = ttoproc(t); 6418048SMadhavan.Venkataraman@Sun.COM callout_id_t id; 6420Sstevel@tonic-gate clock_t tim; 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate if (panicstr) 6450Sstevel@tonic-gate return; 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate /* 648*11173SJonathan.Adams@Sun.COM * Threads in system processes don't process signals. This is 649*11173SJonathan.Adams@Sun.COM * true both for standard threads of system processes and for 650*11173SJonathan.Adams@Sun.COM * interrupt threads which have borrowed their pinned thread's LWP. 6510Sstevel@tonic-gate */ 652*11173SJonathan.Adams@Sun.COM if (lwp == NULL || (p->p_flag & SSYS)) { 6530Sstevel@tonic-gate cv_wait(cvp, mp); 6540Sstevel@tonic-gate return; 6550Sstevel@tonic-gate } 656*11173SJonathan.Adams@Sun.COM ASSERT(t->t_intr == NULL); 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate /* 6590Sstevel@tonic-gate * Wakeup in wakeup_time milliseconds, i.e., human time. 6600Sstevel@tonic-gate */ 66111066Srafael.vanoni@sun.com tim = ddi_get_lbolt() + MSEC_TO_TICK(wakeup_time); 6628566SMadhavan.Venkataraman@Sun.COM mutex_enter(&t->t_wait_mutex); 6638048SMadhavan.Venkataraman@Sun.COM id = realtime_timeout_default((void (*)(void *))cv_wakeup, t, 66411066Srafael.vanoni@sun.com tim - ddi_get_lbolt()); 6650Sstevel@tonic-gate thread_lock(t); /* lock the thread */ 6660Sstevel@tonic-gate cv_block((condvar_impl_t *)cvp); 6670Sstevel@tonic-gate thread_unlock_nopreempt(t); 6688566SMadhavan.Venkataraman@Sun.COM mutex_exit(&t->t_wait_mutex); 6690Sstevel@tonic-gate mutex_exit(mp); 6700Sstevel@tonic-gate /* ASSERT(no locks are held); */ 6710Sstevel@tonic-gate swtch(); 6728566SMadhavan.Venkataraman@Sun.COM (void) untimeout_default(id, 0); 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate /* 6753930Snr123932 * Check for reasons to stop, if lwp_nostop is not true. 6760Sstevel@tonic-gate * See issig_forreal() for explanations of the various stops. 6770Sstevel@tonic-gate */ 6780Sstevel@tonic-gate mutex_enter(&p->p_lock); 6793930Snr123932 while (lwp->lwp_nostop == 0 && !(p->p_flag & SEXITLWPS)) { 6800Sstevel@tonic-gate /* 6810Sstevel@tonic-gate * Hold the lwp here for watchpoint manipulation. 6820Sstevel@tonic-gate */ 6833930Snr123932 if (t->t_proc_flag & TP_PAUSE) { 6840Sstevel@tonic-gate stop(PR_SUSPENDED, SUSPEND_PAUSE); 6850Sstevel@tonic-gate continue; 6860Sstevel@tonic-gate } 6870Sstevel@tonic-gate /* 6880Sstevel@tonic-gate * System checkpoint. 6890Sstevel@tonic-gate */ 6903930Snr123932 if (t->t_proc_flag & TP_CHKPT) { 6910Sstevel@tonic-gate stop(PR_CHECKPOINT, 0); 6920Sstevel@tonic-gate continue; 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate /* 6950Sstevel@tonic-gate * Honor fork1(), watchpoint activity (remapping a page), 6963930Snr123932 * and lwp_suspend() requests. 6970Sstevel@tonic-gate */ 6983930Snr123932 if ((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) || 6993930Snr123932 (t->t_proc_flag & TP_HOLDLWP)) { 7000Sstevel@tonic-gate stop(PR_SUSPENDED, SUSPEND_NORMAL); 7010Sstevel@tonic-gate continue; 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate /* 7040Sstevel@tonic-gate * Honor /proc requested stop. 7050Sstevel@tonic-gate */ 7063930Snr123932 if (t->t_proc_flag & TP_PRSTOP) { 7070Sstevel@tonic-gate stop(PR_REQUESTED, 0); 7080Sstevel@tonic-gate } 7090Sstevel@tonic-gate /* 7100Sstevel@tonic-gate * If some lwp in the process has already stopped 7110Sstevel@tonic-gate * showing PR_JOBCONTROL, stop in sympathy with it. 7120Sstevel@tonic-gate */ 7133930Snr123932 if (p->p_stopsig && t != p->p_agenttp) { 7140Sstevel@tonic-gate stop(PR_JOBCONTROL, p->p_stopsig); 7150Sstevel@tonic-gate continue; 7160Sstevel@tonic-gate } 7170Sstevel@tonic-gate break; 7180Sstevel@tonic-gate } 7190Sstevel@tonic-gate mutex_exit(&p->p_lock); 7200Sstevel@tonic-gate mutex_enter(mp); 7210Sstevel@tonic-gate } 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate /* 7240Sstevel@tonic-gate * Like cv_timedwait_sig(), but takes an absolute hires future time 7250Sstevel@tonic-gate * rather than a future time in clock ticks. Will not return showing 7260Sstevel@tonic-gate * that a timeout occurred until the future time is passed. 7270Sstevel@tonic-gate * If 'when' is a NULL pointer, no timeout will occur. 7280Sstevel@tonic-gate * Returns: 7298048SMadhavan.Venkataraman@Sun.COM * Function result in order of precedence: 7300Sstevel@tonic-gate * 0 if a signal was received 7310Sstevel@tonic-gate * -1 if timeout occured 7320Sstevel@tonic-gate * >0 if awakened via cv_signal() or cv_broadcast() 7330Sstevel@tonic-gate * or by a spurious wakeup. 7340Sstevel@tonic-gate * (might return time remaining) 7354123Sdm120769 * As a special test, if someone abruptly resets the system time 7364123Sdm120769 * (but not through adjtime(2); drifting of the clock is allowed and 7374123Sdm120769 * expected [see timespectohz_adj()]), then we force a return of -1 7384123Sdm120769 * so the caller can return a premature timeout to the calling process 7394123Sdm120769 * so it can reevaluate the situation in light of the new system time. 7404123Sdm120769 * (The system clock has been reset if timecheck != timechanged.) 7410Sstevel@tonic-gate */ 7420Sstevel@tonic-gate int 7434123Sdm120769 cv_waituntil_sig(kcondvar_t *cvp, kmutex_t *mp, 7444123Sdm120769 timestruc_t *when, int timecheck) 7450Sstevel@tonic-gate { 7460Sstevel@tonic-gate timestruc_t now; 7473346Svb160487 timestruc_t delta; 7489334SMadhavan.Venkataraman@Sun.COM hrtime_t interval; 7490Sstevel@tonic-gate int rval; 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate if (when == NULL) 7520Sstevel@tonic-gate return (cv_wait_sig_swap(cvp, mp)); 7530Sstevel@tonic-gate 7547982SDonghai.Qiao@Sun.COM gethrestime(&now); 7553346Svb160487 delta = *when; 7563346Svb160487 timespecsub(&delta, &now); 7573346Svb160487 if (delta.tv_sec < 0 || (delta.tv_sec == 0 && delta.tv_nsec == 0)) { 7580Sstevel@tonic-gate /* 7590Sstevel@tonic-gate * We have already reached the absolute future time. 7600Sstevel@tonic-gate * Call cv_timedwait_sig() just to check for signals. 7610Sstevel@tonic-gate * We will return immediately with either 0 or -1. 7620Sstevel@tonic-gate */ 7639334SMadhavan.Venkataraman@Sun.COM rval = cv_timedwait_sig_hires(cvp, mp, 0, 1, 0); 7640Sstevel@tonic-gate } else { 7654123Sdm120769 if (timecheck == timechanged) { 7669334SMadhavan.Venkataraman@Sun.COM /* 7679334SMadhavan.Venkataraman@Sun.COM * Make sure that the interval is atleast one tick. 7689334SMadhavan.Venkataraman@Sun.COM * This is to prevent a user from flooding the system 7699334SMadhavan.Venkataraman@Sun.COM * with very small, high resolution timers. 7709334SMadhavan.Venkataraman@Sun.COM */ 7719334SMadhavan.Venkataraman@Sun.COM interval = ts2hrt(&delta); 7729334SMadhavan.Venkataraman@Sun.COM if (interval < nsec_per_tick) 7739334SMadhavan.Venkataraman@Sun.COM interval = nsec_per_tick; 7749334SMadhavan.Venkataraman@Sun.COM rval = cv_timedwait_sig_hires(cvp, mp, interval, 1, 7758048SMadhavan.Venkataraman@Sun.COM CALLOUT_FLAG_HRESTIME); 7764123Sdm120769 } else { 7774123Sdm120769 /* 7784123Sdm120769 * Someone reset the system time; 7794123Sdm120769 * just force an immediate timeout. 7804123Sdm120769 */ 7814123Sdm120769 rval = -1; 7824123Sdm120769 } 7834123Sdm120769 if (rval == -1 && timecheck == timechanged) { 7844123Sdm120769 /* 7854123Sdm120769 * Even though cv_timedwait_sig() returned showing a 7864123Sdm120769 * timeout, the future time may not have passed yet. 7874123Sdm120769 * If not, change rval to indicate a normal wakeup. 7884123Sdm120769 */ 7894123Sdm120769 gethrestime(&now); 7904123Sdm120769 delta = *when; 7914123Sdm120769 timespecsub(&delta, &now); 7924123Sdm120769 if (delta.tv_sec > 0 || (delta.tv_sec == 0 && 7934123Sdm120769 delta.tv_nsec > 0)) 7940Sstevel@tonic-gate rval = 1; 7954123Sdm120769 } 7960Sstevel@tonic-gate } 7970Sstevel@tonic-gate return (rval); 7980Sstevel@tonic-gate } 799