condvar.c revision fe234e7c4899705de73738b92cd6d420b63447cd
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 * See the License for the specific language governing permissions 1N/A * and limitations under the License. 1N/A * When distributing Covered Code, include this CDDL HEADER in each 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 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 1N/A * Use is subject to license terms. 1N/A/* Copyright (c) 2011 by Delphix. All rights reserved. */ 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 * Threads don't "own" condition variables. 1N/A * Unsleep a thread that's blocked on a condition variable. 1N/A panic(
"cv_unsleep: thread %p not on sleepq %p",
1N/A * Change the priority of a thread that's blocked on a condition variable. 1N/A panic(
"cv_change_pri: %p not on sleep queue", (
void *)t);
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 * 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 * 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 * Threads are taken off the hashed sleep queues via calls to 1N/A * cv_signal(), cv_broadcast(), or cv_unsleep(). 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 * 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 * Block on the indicated condition variable and release the 1N/A * associated kmutex while blocked. 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 * Same as cv_wait except the thread will unblock at 'tim' 1N/A * (an absolute time) if it hasn't already unblocked. 1N/A * Returns the amount of time left from the original 'tim' value 1N/A * when it was unblocked. 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. * 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 (
signalled)
/* avoid consuming the cv_signal() */ * 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. /* ASSERT(no locks are held) */ if (
rval == 0 &&
signalled)
/* avoid consuming the cv_signal() */ * 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 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 * Set the timeout and wait. /* ASSERT(no locks are held) */ * 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. * 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. if (
rval <= 0 &&
signalled)
/* avoid consuming the cv_signal() */ * Function result in order of precedence: * 0 if a signal was received * >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(). * 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. * Same as cv_reltimedwait_sig() except that the timeout is optional. If * there is no timeout then the function will block until woken up * If there is no timeout specified wait indefinitely for a * cv_reltimedwait_sig will wait for the relative timeout * 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. * 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. t->
t_kpri_req = 0;
/* don't need kernel priority */ /* I can be swapped now */ /* ASSERT(no locks are held) */ /* TS_DONT_SWAP set by disp() */ * 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. /* make sure the cv_waiters field looks sane */ * If cv_waiters is non-zero (and less than * CV_MAX_WAITERS) there should be a thread /* make sure the cv_waiters field looks sane */ * 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. * 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. * Wakeup in wakeup_time milliseconds, i.e., human time. /* ASSERT(no locks are held); */ * Check for reasons to stop, if lwp_nostop is not true. * See issig_forreal() for explanations of the various stops. * Hold the lwp here for watchpoint manipulation. * Honor fork1(), watchpoint activity (remapping a page), * and lwp_suspend() requests. * Honor /proc requested stop. * If some lwp in the process has already stopped * showing PR_JOBCONTROL, stop in sympathy with it. * 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. * Function result in order of precedence: * 0 if a signal was received * >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.) * 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. * 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. * Someone reset the system time; * just force an immediate timeout. * 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.