condvar.c revision 51b32bdd07bc63a6e416c5759f0f445147703107
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/condvar_impl.h>
#include <sys/schedctl.h>
/*
* CV_MAX_WAITERS is the maximum number of waiters we track; once
* the number becomes higher than that, we look at the sleepq to
* see whether there are *really* any waiters.
*/
/*
* Threads don't "own" condition variables.
*/
/* ARGSUSED */
static kthread_t *
{
return (NULL);
}
/*
* Unsleep a thread that's blocked on a condition variable.
*/
static void
cv_unsleep(kthread_t *t)
{
ASSERT(THREAD_LOCK_HELD(t));
panic("cv_unsleep: thread %p not on sleepq %p",
(void *)t, (void *)sqh);
sleepq_unsleep(t);
cvp->cv_waiters--;
CL_SETRUN(t);
}
/*
* Change the priority of a thread that's blocked on a condition variable.
*/
static void
{
ASSERT(THREAD_LOCK_HELD(t));
panic("cv_change_pri: %p not on sleep queue", (void *)t);
sleepq_dequeue(t);
sleepq_insert(sqp, t);
}
/*
* The sobj_ops vector exports a set of functions needed when a thread
* is asleep on a synchronization object of this type.
*/
static sobj_ops_t cv_sobj_ops = {
};
/* ARGSUSED */
void
{
}
/*
* cv_destroy is not currently needed, but is part of the DDI.
* This is in case cv_init ever needs to allocate something for a cv.
*/
/* ARGSUSED */
void
{
}
/*
* The cv_block() function blocks a thread on a condition variable
* by putting it in a hashed sleep queue associated with the
* synchronization object.
*
* Threads are taken off the hashed sleep queues via calls to
* cv_signal(), cv_broadcast(), or cv_unsleep().
*/
static void
{
ASSERT(THREAD_LOCK_HELD(t));
t->t_schedflag &= ~TS_SIGNALLED;
CL_SLEEP(t); /* assign kernel priority */
t->t_sobj_ops = &cv_sobj_ops;
/*
* The check for t_intr is to avoid doing the
* account for an interrupt thread on the still-pinned
* lwp's statistics.
*/
(void) new_mstate(t, LMS_SLEEP);
}
cvp->cv_waiters++;
/*
* THREAD_SLEEP() moves curthread->t_lockp to point to the
* lock sqh->sq_lock. This lock is later released by the caller
* when it calls thread_unlock() on curthread.
*/
}
#define cv_block_sig(t, cvp) \
/*
* Block on the indicated condition variable and release the
* associated kmutex while blocked.
*/
void
{
if (panicstr)
return;
mutex_exit(mp);
swtch();
}
static void
{
/*
* This mutex is acquired and released in order to make sure that
* the wakeup does not happen before the block itself happens.
*/
mutex_enter(&t->t_wait_mutex);
mutex_exit(&t->t_wait_mutex);
setrun(t);
}
/*
* Same as cv_wait except the thread will unblock at 'tim'
* (an absolute time) if it hasn't already unblocked.
*
* Returns the amount of time left from the original 'tim' value
* when it was unblocked.
*/
{
return (-1);
}
{
int signalled;
if (panicstr)
return (-1);
return (-1);
mutex_enter(&t->t_wait_mutex);
thread_lock(t); /* lock the thread */
mutex_exit(&t->t_wait_mutex);
mutex_exit(mp);
swtch();
/*
* 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.
*/
if (timeleft <= 0) {
timeleft = -1;
if (signalled) /* avoid consuming the cv_signal() */
}
return (timeleft);
}
int
{
int cancel_pending;
int rval = 1;
int signalled = 0;
if (panicstr)
return (rval);
/*
* The check for t_intr is to catch an interrupt thread
* that has not yet unpinned the thread underneath.
*/
return (rval);
}
lwp->lwp_sysabort = 0;
thread_lock(t);
mutex_exit(mp);
setrun(t);
/* ASSERT(no locks are held) */
swtch();
t->t_flag &= ~T_WAKEABLE;
if (ISSIG_PENDING(t, lwp, p)) {
mutex_exit(mp);
rval = 0;
}
rval = 0;
if (rval != 0 && cancel_pending) {
rval = 0;
}
lwp->lwp_asleep = 0;
lwp->lwp_sysabort = 0;
return (rval);
}
static clock_t
{
int cancel_pending = 0;
int signalled = 0;
if (panicstr)
return (rval);
/*
* If there is no lwp, then we don't need to wait for a signal.
* The check for t_intr is to catch an interrupt thread
* that has not yet unpinned the thread underneath.
*/
/*
* 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.
*/
lwp->lwp_sysabort = 0;
rval = -1;
goto out;
}
/*
* Set the timeout and wait.
*/
mutex_enter(&t->t_wait_mutex);
lwp->lwp_sysabort = 0;
thread_lock(t);
mutex_exit(&t->t_wait_mutex);
mutex_exit(mp);
setrun(t);
/* ASSERT(no locks are held) */
swtch();
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.
*/
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);
rval = 0;
}
rval = 0;
if (rval != 0 && cancel_pending) {
rval = 0;
}
lwp->lwp_asleep = 0;
lwp->lwp_sysabort = 0;
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().
*/
{
}
/*
* 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
{
int cancel_pending;
int rval = 1;
int signalled = 0;
if (panicstr)
return (rval);
/*
* The check for t_intr is to catch an interrupt thread
* that has not yet unpinned the thread underneath.
*/
return (rval);
}
lwp->lwp_sysabort = 0;
thread_lock(t);
t->t_kpri_req = 0; /* don't need kernel priority */
/* I can be swapped now */
mutex_exit(mp);
setrun(t);
/* ASSERT(no locks are held) */
swtch();
t->t_flag &= ~T_WAKEABLE;
/* TS_DONT_SWAP set by disp() */
if (ISSIG_PENDING(t, lwp, p)) {
mutex_exit(mp);
rval = 0;
}
rval = 0;
if (rval != 0 && cancel_pending) {
rval = 0;
}
lwp->lwp_asleep = 0;
lwp->lwp_sysabort = 0;
if (rval == 0) {
else if (signalled)
}
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
{
}
void
{
/* make sure the cv_waiters field looks sane */
if (cp->cv_waiters > 0) {
kthread_t *t;
cp->cv_waiters--;
/*
* If cv_waiters is non-zero (and less than
* CV_MAX_WAITERS) there should be a thread
* in the queue.
*/
cp->cv_waiters = 0;
}
}
}
void
{
/* make sure the cv_waiters field looks sane */
if (cp->cv_waiters > 0) {
cp->cv_waiters = 0;
}
}
/*
* 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
{
if (panicstr)
return;
/*
* If there is no lwp, then we don't need to eventually stop it
* The check for t_intr is to catch an interrupt thread
* that has not yet unpinned the thread underneath.
*/
return;
}
/*
* Wakeup in wakeup_time milliseconds, i.e., human time.
*/
mutex_enter(&t->t_wait_mutex);
thread_lock(t); /* lock the thread */
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);
/*
* Hold the lwp here for watchpoint manipulation.
*/
if (t->t_proc_flag & TP_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.
*/
(t->t_proc_flag & TP_HOLDLWP)) {
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.
*/
continue;
}
break;
}
mutex_exit(&p->p_lock);
}
/*
* 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
{
int rval;
gethrestime(&now);
/*
* 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.
*/
} 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.
*/
if (interval < nsec_per_tick)
} else {
/*
* Someone reset the system time;
* just force an immediate timeout.
*/
rval = -1;
}
/*
* 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);
rval = 1;
}
}
return (rval);
}