condvar.c revision fe234e7c4899705de73738b92cd6d420b63447cd
1N/A/*
1N/A * CDDL HEADER START
1N/A *
1N/A * The contents of this file are subject to the terms of the
1N/A * Common Development and Distribution License (the "License").
1N/A * You may not use this file except in compliance with the License.
1N/A *
1N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1N/A * or http://www.opensolaris.org/os/licensing.
1N/A * See the License for the specific language governing permissions
1N/A * and limitations under the License.
1N/A *
1N/A * When distributing Covered Code, include this CDDL HEADER in each
1N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1N/A * If applicable, add the following below this CDDL HEADER, with the
1N/A * fields enclosed by brackets "[]" replaced with your own identifying
1N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1N/A *
1N/A * CDDL HEADER END
1N/A */
1N/A
1N/A/*
1N/A * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A */
1N/A
1N/A/* Copyright (c) 2011 by Delphix. All rights reserved. */
1N/A
1N/A#include <sys/thread.h>
1N/A#include <sys/proc.h>
1N/A#include <sys/debug.h>
1N/A#include <sys/cmn_err.h>
1N/A#include <sys/systm.h>
1N/A#include <sys/sobject.h>
1N/A#include <sys/sleepq.h>
1N/A#include <sys/cpuvar.h>
1N/A#include <sys/condvar.h>
1N/A#include <sys/condvar_impl.h>
1N/A#include <sys/schedctl.h>
1N/A#include <sys/procfs.h>
1N/A#include <sys/sdt.h>
1N/A#include <sys/callo.h>
1N/A
1N/Aclock_t cv_timedwait_hires(kcondvar_t *, kmutex_t *, hrtime_t, hrtime_t, int);
1N/A
1N/A/*
1N/A * CV_MAX_WAITERS is the maximum number of waiters we track; once
1N/A * the number becomes higher than that, we look at the sleepq to
1N/A * see whether there are *really* any waiters.
1N/A */
1N/A#define CV_MAX_WAITERS 1024 /* must be power of 2 */
1N/A#define CV_WAITERS_MASK (CV_MAX_WAITERS - 1)
1N/A
1N/A/*
1N/A * Threads don't "own" condition variables.
1N/A */
1N/A/* ARGSUSED */
1N/Astatic kthread_t *
1N/Acv_owner(void *cvp)
1N/A{
1N/A return (NULL);
1N/A}
1N/A
1N/A/*
1N/A * Unsleep a thread that's blocked on a condition variable.
1N/A */
1N/Astatic void
1N/Acv_unsleep(kthread_t *t)
1N/A{
1N/A condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
1N/A sleepq_head_t *sqh = SQHASH(cvp);
1N/A
1N/A ASSERT(THREAD_LOCK_HELD(t));
1N/A
1N/A if (cvp == NULL)
1N/A panic("cv_unsleep: thread %p not on sleepq %p",
1N/A (void *)t, (void *)sqh);
1N/A DTRACE_SCHED1(wakeup, kthread_t *, t);
1N/A sleepq_unsleep(t);
1N/A if (cvp->cv_waiters != CV_MAX_WAITERS)
1N/A cvp->cv_waiters--;
1N/A disp_lock_exit_high(&sqh->sq_lock);
1N/A CL_SETRUN(t);
1N/A}
1N/A
1N/A/*
1N/A * Change the priority of a thread that's blocked on a condition variable.
1N/A */
1N/Astatic void
1N/Acv_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip)
1N/A{
1N/A condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
1N/A sleepq_t *sqp = t->t_sleepq;
1N/A
1N/A ASSERT(THREAD_LOCK_HELD(t));
1N/A ASSERT(&SQHASH(cvp)->sq_queue == sqp);
1N/A
1N/A if (cvp == NULL)
1N/A panic("cv_change_pri: %p not on sleep queue", (void *)t);
1N/A sleepq_dequeue(t);
1N/A *t_prip = pri;
1N/A sleepq_insert(sqp, t);
1N/A}
1N/A
1N/A/*
1N/A * The sobj_ops vector exports a set of functions needed when a thread
1N/A * is asleep on a synchronization object of this type.
1N/A */
1N/Astatic sobj_ops_t cv_sobj_ops = {
1N/A SOBJ_CV, cv_owner, cv_unsleep, cv_change_pri
1N/A};
1N/A
1N/A/* ARGSUSED */
1N/Avoid
1N/Acv_init(kcondvar_t *cvp, char *name, kcv_type_t type, void *arg)
1N/A{
1N/A ((condvar_impl_t *)cvp)->cv_waiters = 0;
1N/A}
1N/A
1N/A/*
1N/A * cv_destroy is not currently needed, but is part of the DDI.
1N/A * This is in case cv_init ever needs to allocate something for a cv.
1N/A */
1N/A/* ARGSUSED */
1N/Avoid
1N/Acv_destroy(kcondvar_t *cvp)
1N/A{
1N/A ASSERT((((condvar_impl_t *)cvp)->cv_waiters & CV_WAITERS_MASK) == 0);
1N/A}
1N/A
1N/A/*
1N/A * The cv_block() function blocks a thread on a condition variable
1N/A * by putting it in a hashed sleep queue associated with the
1N/A * synchronization object.
1N/A *
1N/A * Threads are taken off the hashed sleep queues via calls to
1N/A * cv_signal(), cv_broadcast(), or cv_unsleep().
1N/A */
1N/Astatic void
1N/Acv_block(condvar_impl_t *cvp)
1N/A{
1N/A kthread_t *t = curthread;
1N/A klwp_t *lwp = ttolwp(t);
1N/A sleepq_head_t *sqh;
1N/A
1N/A ASSERT(THREAD_LOCK_HELD(t));
1N/A ASSERT(t != CPU->cpu_idle_thread);
1N/A ASSERT(CPU_ON_INTR(CPU) == 0);
1N/A ASSERT(t->t_wchan0 == NULL && t->t_wchan == NULL);
1N/A ASSERT(t->t_state == TS_ONPROC);
1N/A
1N/A t->t_schedflag &= ~TS_SIGNALLED;
1N/A CL_SLEEP(t); /* assign kernel priority */
1N/A t->t_wchan = (caddr_t)cvp;
1N/A t->t_sobj_ops = &cv_sobj_ops;
1N/A DTRACE_SCHED(sleep);
1N/A
1N/A /*
1N/A * The check for t_intr is to avoid doing the
1N/A * account for an interrupt thread on the still-pinned
1N/A * lwp's statistics.
1N/A */
1N/A if (lwp != NULL && t->t_intr == NULL) {
1N/A lwp->lwp_ru.nvcsw++;
1N/A (void) new_mstate(t, LMS_SLEEP);
1N/A }
1N/A
1N/A sqh = SQHASH(cvp);
1N/A disp_lock_enter_high(&sqh->sq_lock);
1N/A if (cvp->cv_waiters < CV_MAX_WAITERS)
1N/A cvp->cv_waiters++;
1N/A ASSERT(cvp->cv_waiters <= CV_MAX_WAITERS);
1N/A THREAD_SLEEP(t, &sqh->sq_lock);
1N/A sleepq_insert(&sqh->sq_queue, t);
1N/A /*
1N/A * THREAD_SLEEP() moves curthread->t_lockp to point to the
1N/A * lock sqh->sq_lock. This lock is later released by the caller
1N/A * when it calls thread_unlock() on curthread.
1N/A */
1N/A}
1N/A
1N/A#define cv_block_sig(t, cvp) \
1N/A { (t)->t_flag |= T_WAKEABLE; cv_block(cvp); }
1N/A
1N/A/*
1N/A * Block on the indicated condition variable and release the
1N/A * associated kmutex while blocked.
1N/A */
1N/Avoid
1N/Acv_wait(kcondvar_t *cvp, kmutex_t *mp)
1N/A{
1N/A if (panicstr)
1N/A return;
1N/A ASSERT(!quiesce_active);
1N/A
1N/A ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
1N/A thread_lock(curthread); /* lock the thread */
1N/A cv_block((condvar_impl_t *)cvp);
1N/A thread_unlock_nopreempt(curthread); /* unlock the waiters field */
1N/A mutex_exit(mp);
1N/A swtch();
1N/A mutex_enter(mp);
1N/A}
1N/A
1N/Astatic void
1N/Acv_wakeup(void *arg)
1N/A{
1N/A kthread_t *t = arg;
1N/A
1N/A /*
1N/A * This mutex is acquired and released in order to make sure that
1N/A * the wakeup does not happen before the block itself happens.
1N/A */
1N/A mutex_enter(&t->t_wait_mutex);
1N/A mutex_exit(&t->t_wait_mutex);
1N/A setrun(t);
1N/A}
1N/A
1N/A/*
1N/A * Same as cv_wait except the thread will unblock at 'tim'
1N/A * (an absolute time) if it hasn't already unblocked.
1N/A *
1N/A * Returns the amount of time left from the original 'tim' value
1N/A * when it was unblocked.
1N/A */
1N/Aclock_t
1N/Acv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
1N/A{
1N/A hrtime_t hrtim;
1N/A clock_t now = ddi_get_lbolt();
1N/A
1N/A if (tim <= now)
1N/A return (-1);
1N/A
1N/A hrtim = TICK_TO_NSEC(tim - now);
1N/A return (cv_timedwait_hires(cvp, mp, hrtim, nsec_per_tick, 0));
1N/A}
1N/A
1N/A/*
1N/A * Same as cv_timedwait() except that the third argument is a relative
1N/A * timeout value, as opposed to an absolute one. There is also a fourth
1N/A * argument that specifies how accurately the timeout must be implemented.
1N/A */
1N/Aclock_t
1N/Acv_reltimedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t delta, time_res_t res)
1N/A{
1N/A hrtime_t exp;
ASSERT(TIME_RES_VALID(res));
if (delta <= 0)
return (-1);
if ((exp = TICK_TO_NSEC(delta)) < 0)
exp = CY_INFINITY;
return (cv_timedwait_hires(cvp, mp, exp, time_res[res], 0));
}
clock_t
cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
hrtime_t res, int flag)
{
kthread_t *t = curthread;
callout_id_t id;
clock_t timeleft;
hrtime_t limit;
int signalled;
if (panicstr)
return (-1);
ASSERT(!quiesce_active);
limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
if (tim <= limit)
return (-1);
mutex_enter(&t->t_wait_mutex);
id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
tim, res, flag);
thread_lock(t); /* lock the thread */
cv_block((condvar_impl_t *)cvp);
thread_unlock_nopreempt(t);
mutex_exit(&t->t_wait_mutex);
mutex_exit(mp);
swtch();
signalled = (t->t_schedflag & TS_SIGNALLED);
/*
* Get the time left. untimeout() returns -1 if the timeout has
* occured or the time remaining. If the time remaining is zero,
* the timeout has occured between when we were awoken and
* we called untimeout. We will treat this as if the timeout
* has occured and set timeleft to -1.
*/
timeleft = untimeout_default(id, 0);
mutex_enter(mp);
if (timeleft <= 0) {
timeleft = -1;
if (signalled) /* avoid consuming the cv_signal() */
cv_signal(cvp);
}
return (timeleft);
}
int
cv_wait_sig(kcondvar_t *cvp, kmutex_t *mp)
{
kthread_t *t = curthread;
proc_t *p = ttoproc(t);
klwp_t *lwp = ttolwp(t);
int cancel_pending;
int rval = 1;
int signalled = 0;
if (panicstr)
return (rval);
ASSERT(!quiesce_active);
/*
* Threads in system processes don't process signals. This is
* true both for standard threads of system processes and for
* interrupt threads which have borrowed their pinned thread's LWP.
*/
if (lwp == NULL || (p->p_flag & SSYS)) {
cv_wait(cvp, mp);
return (rval);
}
ASSERT(t->t_intr == NULL);
ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
cancel_pending = schedctl_cancel_pending();
lwp->lwp_asleep = 1;
lwp->lwp_sysabort = 0;
thread_lock(t);
cv_block_sig(t, (condvar_impl_t *)cvp);
thread_unlock_nopreempt(t);
mutex_exit(mp);
if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
setrun(t);
/* ASSERT(no locks are held) */
swtch();
signalled = (t->t_schedflag & TS_SIGNALLED);
t->t_flag &= ~T_WAKEABLE;
mutex_enter(mp);
if (ISSIG_PENDING(t, lwp, p)) {
mutex_exit(mp);
if (issig(FORREAL))
rval = 0;
mutex_enter(mp);
}
if (lwp->lwp_sysabort || MUSTRETURN(p, t))
rval = 0;
if (rval != 0 && cancel_pending) {
schedctl_cancel_eintr();
rval = 0;
}
lwp->lwp_asleep = 0;
lwp->lwp_sysabort = 0;
if (rval == 0 && signalled) /* avoid consuming the cv_signal() */
cv_signal(cvp);
return (rval);
}
static clock_t
cv_timedwait_sig_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
hrtime_t res, int flag)
{
kthread_t *t = curthread;
proc_t *p = ttoproc(t);
klwp_t *lwp = ttolwp(t);
int cancel_pending = 0;
callout_id_t id;
clock_t rval = 1;
hrtime_t limit;
int signalled = 0;
if (panicstr)
return (rval);
ASSERT(!quiesce_active);
/*
* Threads in system processes don't process signals. This is
* true both for standard threads of system processes and for
* interrupt threads which have borrowed their pinned thread's LWP.
*/
if (lwp == NULL || (p->p_flag & SSYS))
return (cv_timedwait_hires(cvp, mp, tim, res, flag));
ASSERT(t->t_intr == NULL);
/*
* If tim is less than or equal to current hrtime, then the timeout
* has already occured. So just check to see if there is a signal
* pending. If so return 0 indicating that there is a signal pending.
* Else return -1 indicating that the timeout occured. No need to
* wait on anything.
*/
limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
if (tim <= limit) {
lwp->lwp_asleep = 1;
lwp->lwp_sysabort = 0;
rval = -1;
goto out;
}
/*
* Set the timeout and wait.
*/
cancel_pending = schedctl_cancel_pending();
mutex_enter(&t->t_wait_mutex);
id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
tim, res, flag);
lwp->lwp_asleep = 1;
lwp->lwp_sysabort = 0;
thread_lock(t);
cv_block_sig(t, (condvar_impl_t *)cvp);
thread_unlock_nopreempt(t);
mutex_exit(&t->t_wait_mutex);
mutex_exit(mp);
if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
setrun(t);
/* ASSERT(no locks are held) */
swtch();
signalled = (t->t_schedflag & TS_SIGNALLED);
t->t_flag &= ~T_WAKEABLE;
/*
* Untimeout the thread. untimeout() returns -1 if the timeout has
* occured or the time remaining. If the time remaining is zero,
* the timeout has occured between when we were awoken and
* we called untimeout. We will treat this as if the timeout
* has occured and set rval to -1.
*/
rval = untimeout_default(id, 0);
mutex_enter(mp);
if (rval <= 0)
rval = -1;
/*
* Check to see if a signal is pending. If so, regardless of whether
* or not we were awoken due to the signal, the signal is now pending
* and a return of 0 has the highest priority.
*/
out:
if (ISSIG_PENDING(t, lwp, p)) {
mutex_exit(mp);
if (issig(FORREAL))
rval = 0;
mutex_enter(mp);
}
if (lwp->lwp_sysabort || MUSTRETURN(p, t))
rval = 0;
if (rval != 0 && cancel_pending) {
schedctl_cancel_eintr();
rval = 0;
}
lwp->lwp_asleep = 0;
lwp->lwp_sysabort = 0;
if (rval <= 0 && signalled) /* avoid consuming the cv_signal() */
cv_signal(cvp);
return (rval);
}
/*
* Returns:
* Function result in order of precedence:
* 0 if a signal was received
* -1 if timeout occured
* >0 if awakened via cv_signal() or cv_broadcast().
* (returns time remaining)
*
* cv_timedwait_sig() is now part of the DDI.
*
* This function is now just a wrapper for cv_timedwait_sig_hires().
*/
clock_t
cv_timedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
{
hrtime_t hrtim;
hrtim = TICK_TO_NSEC(tim - ddi_get_lbolt());
return (cv_timedwait_sig_hires(cvp, mp, hrtim, nsec_per_tick, 0));
}
/*
* Same as cv_timedwait_sig() except that the third argument is a relative
* timeout value, as opposed to an absolute one. There is also a fourth
* argument that specifies how accurately the timeout must be implemented.
*/
clock_t
cv_reltimedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t delta,
time_res_t res)
{
hrtime_t exp = 0;
ASSERT(TIME_RES_VALID(res));
if (delta > 0) {
if ((exp = TICK_TO_NSEC(delta)) < 0)
exp = CY_INFINITY;
}
return (cv_timedwait_sig_hires(cvp, mp, exp, time_res[res], 0));
}
/*
* Same as cv_reltimedwait_sig() except that the timeout is optional. If
* there is no timeout then the function will block until woken up
* or interrupted.
*/
clock_t
cv_relwaituntil_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t *delta,
time_res_t res)
{
/*
* If there is no timeout specified wait indefinitely for a
* signal or a wakeup.
*/
if (delta == NULL) {
return (cv_wait_sig_swap(cvp, mp));
}
/*
* cv_reltimedwait_sig will wait for the relative timeout
* specified by delta.
*/
return (cv_reltimedwait_sig(cvp, mp, *delta, res));
}
/*
* Like cv_wait_sig_swap but allows the caller to indicate (with a
* non-NULL sigret) that they will take care of signalling the cv
* after wakeup, if necessary. This is a vile hack that should only
* be used when no other option is available; almost all callers
* should just use cv_wait_sig_swap (which takes care of the cv_signal
* stuff automatically) instead.
*/
int
cv_wait_sig_swap_core(kcondvar_t *cvp, kmutex_t *mp, int *sigret)
{
kthread_t *t = curthread;
proc_t *p = ttoproc(t);
klwp_t *lwp = ttolwp(t);
int cancel_pending;
int rval = 1;
int signalled = 0;
if (panicstr)
return (rval);
/*
* Threads in system processes don't process signals. This is
* true both for standard threads of system processes and for
* interrupt threads which have borrowed their pinned thread's LWP.
*/
if (lwp == NULL || (p->p_flag & SSYS)) {
cv_wait(cvp, mp);
return (rval);
}
ASSERT(t->t_intr == NULL);
cancel_pending = schedctl_cancel_pending();
lwp->lwp_asleep = 1;
lwp->lwp_sysabort = 0;
thread_lock(t);
t->t_kpri_req = 0; /* don't need kernel priority */
cv_block_sig(t, (condvar_impl_t *)cvp);
/* I can be swapped now */
curthread->t_schedflag &= ~TS_DONT_SWAP;
thread_unlock_nopreempt(t);
mutex_exit(mp);
if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
setrun(t);
/* ASSERT(no locks are held) */
swtch();
signalled = (t->t_schedflag & TS_SIGNALLED);
t->t_flag &= ~T_WAKEABLE;
/* TS_DONT_SWAP set by disp() */
ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
mutex_enter(mp);
if (ISSIG_PENDING(t, lwp, p)) {
mutex_exit(mp);
if (issig(FORREAL))
rval = 0;
mutex_enter(mp);
}
if (lwp->lwp_sysabort || MUSTRETURN(p, t))
rval = 0;
if (rval != 0 && cancel_pending) {
schedctl_cancel_eintr();
rval = 0;
}
lwp->lwp_asleep = 0;
lwp->lwp_sysabort = 0;
if (rval == 0) {
if (sigret != NULL)
*sigret = signalled; /* just tell the caller */
else if (signalled)
cv_signal(cvp); /* avoid consuming the cv_signal() */
}
return (rval);
}
/*
* Same as cv_wait_sig but the thread can be swapped out while waiting.
* This should only be used when we know we aren't holding any locks.
*/
int
cv_wait_sig_swap(kcondvar_t *cvp, kmutex_t *mp)
{
return (cv_wait_sig_swap_core(cvp, mp, NULL));
}
void
cv_signal(kcondvar_t *cvp)
{
condvar_impl_t *cp = (condvar_impl_t *)cvp;
/* make sure the cv_waiters field looks sane */
ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
if (cp->cv_waiters > 0) {
sleepq_head_t *sqh = SQHASH(cp);
disp_lock_enter(&sqh->sq_lock);
ASSERT(CPU_ON_INTR(CPU) == 0);
if (cp->cv_waiters & CV_WAITERS_MASK) {
kthread_t *t;
cp->cv_waiters--;
t = sleepq_wakeone_chan(&sqh->sq_queue, cp);
/*
* If cv_waiters is non-zero (and less than
* CV_MAX_WAITERS) there should be a thread
* in the queue.
*/
ASSERT(t != NULL);
} else if (sleepq_wakeone_chan(&sqh->sq_queue, cp) == NULL) {
cp->cv_waiters = 0;
}
disp_lock_exit(&sqh->sq_lock);
}
}
void
cv_broadcast(kcondvar_t *cvp)
{
condvar_impl_t *cp = (condvar_impl_t *)cvp;
/* make sure the cv_waiters field looks sane */
ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
if (cp->cv_waiters > 0) {
sleepq_head_t *sqh = SQHASH(cp);
disp_lock_enter(&sqh->sq_lock);
ASSERT(CPU_ON_INTR(CPU) == 0);
sleepq_wakeall_chan(&sqh->sq_queue, cp);
cp->cv_waiters = 0;
disp_lock_exit(&sqh->sq_lock);
}
}
/*
* Same as cv_wait(), but wakes up (after wakeup_time milliseconds) to check
* for requests to stop, like cv_wait_sig() but without dealing with signals.
* This is a horrible kludge. It is evil. It is vile. It is swill.
* If your code has to call this function then your code is the same.
*/
void
cv_wait_stop(kcondvar_t *cvp, kmutex_t *mp, int wakeup_time)
{
kthread_t *t = curthread;
klwp_t *lwp = ttolwp(t);
proc_t *p = ttoproc(t);
callout_id_t id;
clock_t tim;
if (panicstr)
return;
/*
* Threads in system processes don't process signals. This is
* true both for standard threads of system processes and for
* interrupt threads which have borrowed their pinned thread's LWP.
*/
if (lwp == NULL || (p->p_flag & SSYS)) {
cv_wait(cvp, mp);
return;
}
ASSERT(t->t_intr == NULL);
/*
* Wakeup in wakeup_time milliseconds, i.e., human time.
*/
tim = ddi_get_lbolt() + MSEC_TO_TICK(wakeup_time);
mutex_enter(&t->t_wait_mutex);
id = realtime_timeout_default((void (*)(void *))cv_wakeup, t,
tim - ddi_get_lbolt());
thread_lock(t); /* lock the thread */
cv_block((condvar_impl_t *)cvp);
thread_unlock_nopreempt(t);
mutex_exit(&t->t_wait_mutex);
mutex_exit(mp);
/* ASSERT(no locks are held); */
swtch();
(void) untimeout_default(id, 0);
/*
* Check for reasons to stop, if lwp_nostop is not true.
* See issig_forreal() for explanations of the various stops.
*/
mutex_enter(&p->p_lock);
while (lwp->lwp_nostop == 0 && !(p->p_flag & SEXITLWPS)) {
/*
* Hold the lwp here for watchpoint manipulation.
*/
if (t->t_proc_flag & TP_PAUSE) {
stop(PR_SUSPENDED, SUSPEND_PAUSE);
continue;
}
/*
* System checkpoint.
*/
if (t->t_proc_flag & TP_CHKPT) {
stop(PR_CHECKPOINT, 0);
continue;
}
/*
* Honor fork1(), watchpoint activity (remapping a page),
* and lwp_suspend() requests.
*/
if ((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
(t->t_proc_flag & TP_HOLDLWP)) {
stop(PR_SUSPENDED, SUSPEND_NORMAL);
continue;
}
/*
* Honor /proc requested stop.
*/
if (t->t_proc_flag & TP_PRSTOP) {
stop(PR_REQUESTED, 0);
}
/*
* If some lwp in the process has already stopped
* showing PR_JOBCONTROL, stop in sympathy with it.
*/
if (p->p_stopsig && t != p->p_agenttp) {
stop(PR_JOBCONTROL, p->p_stopsig);
continue;
}
break;
}
mutex_exit(&p->p_lock);
mutex_enter(mp);
}
/*
* Like cv_timedwait_sig(), but takes an absolute hires future time
* rather than a future time in clock ticks. Will not return showing
* that a timeout occurred until the future time is passed.
* If 'when' is a NULL pointer, no timeout will occur.
* Returns:
* Function result in order of precedence:
* 0 if a signal was received
* -1 if timeout occured
* >0 if awakened via cv_signal() or cv_broadcast()
* or by a spurious wakeup.
* (might return time remaining)
* As a special test, if someone abruptly resets the system time
* (but not through adjtime(2); drifting of the clock is allowed and
* expected [see timespectohz_adj()]), then we force a return of -1
* so the caller can return a premature timeout to the calling process
* so it can reevaluate the situation in light of the new system time.
* (The system clock has been reset if timecheck != timechanged.)
*/
int
cv_waituntil_sig(kcondvar_t *cvp, kmutex_t *mp,
timestruc_t *when, int timecheck)
{
timestruc_t now;
timestruc_t delta;
hrtime_t interval;
int rval;
if (when == NULL)
return (cv_wait_sig_swap(cvp, mp));
gethrestime(&now);
delta = *when;
timespecsub(&delta, &now);
if (delta.tv_sec < 0 || (delta.tv_sec == 0 && delta.tv_nsec == 0)) {
/*
* We have already reached the absolute future time.
* Call cv_timedwait_sig() just to check for signals.
* We will return immediately with either 0 or -1.
*/
rval = cv_timedwait_sig_hires(cvp, mp, 0, 1, 0);
} else {
if (timecheck == timechanged) {
/*
* Make sure that the interval is atleast one tick.
* This is to prevent a user from flooding the system
* with very small, high resolution timers.
*/
interval = ts2hrt(&delta);
if (interval < nsec_per_tick)
interval = nsec_per_tick;
rval = cv_timedwait_sig_hires(cvp, mp, interval, 1,
CALLOUT_FLAG_HRESTIME);
} else {
/*
* Someone reset the system time;
* just force an immediate timeout.
*/
rval = -1;
}
if (rval == -1 && timecheck == timechanged) {
/*
* Even though cv_timedwait_sig() returned showing a
* timeout, the future time may not have passed yet.
* If not, change rval to indicate a normal wakeup.
*/
gethrestime(&now);
delta = *when;
timespecsub(&delta, &now);
if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
delta.tv_nsec > 0))
rval = 1;
}
}
return (rval);
}