Index: head/sys/kern/kern_condvar.c =================================================================== --- head/sys/kern/kern_condvar.c (revision 131248) +++ head/sys/kern/kern_condvar.c (revision 131249) @@ -1,367 +1,367 @@ /*- * Copyright (c) 2000 Jake Burkholder . * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include "opt_ktrace.h" #include #include #include #include #include #include #include #include #include #include #include #include #ifdef KTRACE #include #include #endif /* * Common sanity checks for cv_wait* functions. */ #define CV_ASSERT(cvp, mp, td) do { \ KASSERT((td) != NULL, ("%s: curthread NULL", __func__)); \ KASSERT(TD_IS_RUNNING(td), ("%s: not TDS_RUNNING", __func__)); \ KASSERT((cvp) != NULL, ("%s: cvp NULL", __func__)); \ KASSERT((mp) != NULL, ("%s: mp NULL", __func__)); \ mtx_assert((mp), MA_OWNED | MA_NOTRECURSED); \ } while (0) /* * Initialize a condition variable. Must be called before use. */ void cv_init(struct cv *cvp, const char *desc) { cvp->cv_description = desc; cvp->cv_waiters = 0; } /* * Destroy a condition variable. The condition variable must be re-initialized * in order to be re-used. */ void cv_destroy(struct cv *cvp) { #ifdef INVARIANTS struct sleepqueue *sq; sq = sleepq_lookup(cvp); sleepq_release(cvp); KASSERT(sq == NULL, ("%s: associated sleep queue non-empty", __func__)); #endif } /* * Wait on a condition variable. The current thread is placed on the condition * variable's wait queue and suspended. A cv_signal or cv_broadcast on the same * condition variable will resume the thread. The mutex is released before * sleeping and will be held on return. It is recommended that the mutex be * held when cv_signal or cv_broadcast are called. */ void cv_wait(struct cv *cvp, struct mtx *mp) { struct sleepqueue *sq; struct thread *td; WITNESS_SAVE_DECL(mp); td = curthread; #ifdef KTRACE if (KTRPOINT(td, KTR_CSW)) ktrcsw(1, 0); #endif CV_ASSERT(cvp, mp, td); WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object, "Waiting on \"%s\"", cvp->cv_description); WITNESS_SAVE(&mp->mtx_object, mp); if (cold || panicstr) { /* * During autoconfiguration, just give interrupts * a chance, then just return. Don't run any other * thread or panic below, in case this is the idle * process and already asleep. */ return; } sq = sleepq_lookup(cvp); cvp->cv_waiters++; DROP_GIANT(); mtx_unlock(mp); sleepq_add(sq, cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR); sleepq_wait(cvp); #ifdef KTRACE if (KTRPOINT(td, KTR_CSW)) ktrcsw(0, 0); #endif PICKUP_GIANT(); mtx_lock(mp); WITNESS_RESTORE(&mp->mtx_object, mp); } /* * Wait on a condition variable, allowing interruption by signals. Return 0 if * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if * a signal was caught. If ERESTART is returned the system call should be * restarted if possible. */ int cv_wait_sig(struct cv *cvp, struct mtx *mp) { struct sleepqueue *sq; struct thread *td; struct proc *p; int rval, sig; WITNESS_SAVE_DECL(mp); td = curthread; p = td->td_proc; rval = 0; #ifdef KTRACE if (KTRPOINT(td, KTR_CSW)) ktrcsw(1, 0); #endif CV_ASSERT(cvp, mp, td); WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object, "Waiting on \"%s\"", cvp->cv_description); WITNESS_SAVE(&mp->mtx_object, mp); if (cold || panicstr) { /* * After a panic, or during autoconfiguration, just give * interrupts a chance, then just return; don't run any other * procs or panic below, in case this is the idle process and * already asleep. */ return 0; } sq = sleepq_lookup(cvp); /* XXX: Missing the threading checks from msleep! */ cvp->cv_waiters++; DROP_GIANT(); mtx_unlock(mp); sleepq_add(sq, cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR); sig = sleepq_catch_signals(cvp); /* * XXX: Missing magic return value handling for no signal * caught but thread woken up during check. */ rval = sleepq_wait_sig(cvp); if (rval == 0) rval = sleepq_calc_signal_retval(sig); /* XXX: Part of missing threading checks? */ PROC_LOCK(p); if (p->p_flag & P_WEXIT) rval = EINTR; PROC_UNLOCK(p); #ifdef KTRACE if (KTRPOINT(td, KTR_CSW)) ktrcsw(0, 0); #endif PICKUP_GIANT(); mtx_lock(mp); WITNESS_RESTORE(&mp->mtx_object, mp); return (rval); } /* * Wait on a condition variable for at most timo/hz seconds. Returns 0 if the * process was resumed by cv_signal or cv_broadcast, EWOULDBLOCK if the timeout * expires. */ int cv_timedwait(struct cv *cvp, struct mtx *mp, int timo) { struct sleepqueue *sq; struct thread *td; int rval; WITNESS_SAVE_DECL(mp); td = curthread; rval = 0; #ifdef KTRACE if (KTRPOINT(td, KTR_CSW)) ktrcsw(1, 0); #endif CV_ASSERT(cvp, mp, td); WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object, "Waiting on \"%s\"", cvp->cv_description); WITNESS_SAVE(&mp->mtx_object, mp); if (cold || panicstr) { /* * After a panic, or during autoconfiguration, just give * interrupts a chance, then just return; don't run any other * thread or panic below, in case this is the idle process and * already asleep. */ return 0; } sq = sleepq_lookup(cvp); cvp->cv_waiters++; DROP_GIANT(); mtx_unlock(mp); sleepq_add(sq, cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR); sleepq_set_timeout(cvp, timo); - rval = sleepq_timedwait(cvp, 0); + rval = sleepq_timedwait(cvp); #ifdef KTRACE if (KTRPOINT(td, KTR_CSW)) ktrcsw(0, 0); #endif PICKUP_GIANT(); mtx_lock(mp); WITNESS_RESTORE(&mp->mtx_object, mp); return (rval); } /* * Wait on a condition variable for at most timo/hz seconds, allowing * interruption by signals. Returns 0 if the thread was resumed by cv_signal * or cv_broadcast, EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if * a signal was caught. */ int cv_timedwait_sig(struct cv *cvp, struct mtx *mp, int timo) { struct sleepqueue *sq; struct thread *td; struct proc *p; int rval; int sig; WITNESS_SAVE_DECL(mp); td = curthread; p = td->td_proc; rval = 0; #ifdef KTRACE if (KTRPOINT(td, KTR_CSW)) ktrcsw(1, 0); #endif CV_ASSERT(cvp, mp, td); WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object, "Waiting on \"%s\"", cvp->cv_description); WITNESS_SAVE(&mp->mtx_object, mp); if (cold || panicstr) { /* * After a panic, or during autoconfiguration, just give * interrupts a chance, then just return; don't run any other * thread or panic below, in case this is the idle process and * already asleep. */ return 0; } sq = sleepq_lookup(cvp); cvp->cv_waiters++; DROP_GIANT(); mtx_unlock(mp); sleepq_add(sq, cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR); sleepq_set_timeout(cvp, timo); sig = sleepq_catch_signals(cvp); /* * XXX: Missing magic return value handling for no signal * caught but thread woken up during check. */ rval = sleepq_timedwait_sig(cvp, sig != 0); if (rval == 0) rval = sleepq_calc_signal_retval(sig); /* XXX: Part of missing threading checks? */ PROC_LOCK(p); if (p->p_flag & P_WEXIT) rval = EINTR; PROC_UNLOCK(p); #ifdef KTRACE if (KTRPOINT(td, KTR_CSW)) ktrcsw(0, 0); #endif PICKUP_GIANT(); mtx_lock(mp); WITNESS_RESTORE(&mp->mtx_object, mp); return (rval); } /* * Signal a condition variable, wakes up one waiting thread. Will also wakeup * the swapper if the process is not in memory, so that it can bring the * sleeping process in. Note that this may also result in additional threads * being made runnable. Should be called with the same mutex as was passed to * cv_wait held. */ void cv_signal(struct cv *cvp) { if (cvp->cv_waiters > 0) { cvp->cv_waiters--; sleepq_signal(cvp, SLEEPQ_CONDVAR, -1); } } /* * Broadcast a signal to a condition variable. Wakes up all waiting threads. * Should be called with the same mutex as was passed to cv_wait held. */ void cv_broadcastpri(struct cv *cvp, int pri) { if (cvp->cv_waiters > 0) { cvp->cv_waiters = 0; sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri); } } Index: head/sys/kern/kern_synch.c =================================================================== --- head/sys/kern/kern_synch.c (revision 131248) +++ head/sys/kern/kern_synch.c (revision 131249) @@ -1,475 +1,475 @@ /*- * Copyright (c) 1982, 1986, 1990, 1991, 1993 * The Regents of the University of California. All rights reserved. * (c) UNIX System Laboratories, Inc. * All or some portions of this file are derived from material licensed * to the University of California by American Telephone and Telegraph * Co. or Unix System Laboratories, Inc. and are reproduced herein with * the permission of UNIX System Laboratories, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)kern_synch.c 8.9 (Berkeley) 5/19/95 */ #include __FBSDID("$FreeBSD$"); #include "opt_ddb.h" #include "opt_ktrace.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef DDB #include #endif #ifdef KTRACE #include #include #endif #include static void synch_setup(void *dummy); SYSINIT(synch_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, synch_setup, NULL) int hogticks; int lbolt; static struct callout loadav_callout; static struct callout lbolt_callout; struct loadavg averunnable = { {0, 0, 0}, FSCALE }; /* load average, of runnable procs */ /* * Constants for averages over 1, 5, and 15 minutes * when sampling at 5 second intervals. */ static fixpt_t cexp[3] = { 0.9200444146293232 * FSCALE, /* exp(-1/12) */ 0.9834714538216174 * FSCALE, /* exp(-1/60) */ 0.9944598480048967 * FSCALE, /* exp(-1/180) */ }; /* kernel uses `FSCALE', userland (SHOULD) use kern.fscale */ static int fscale __unused = FSCALE; SYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, 0, FSCALE, ""); static void loadav(void *arg); static void lboltcb(void *arg); void sleepinit(void) { hogticks = (hz / 10) * 2; /* Default only. */ init_sleepqueues(); } /* * General sleep call. Suspends the current process until a wakeup is * performed on the specified identifier. The process will then be made * runnable with the specified priority. Sleeps at most timo/hz seconds * (0 means no timeout). If pri includes PCATCH flag, signals are checked * before and after sleeping, else signals are not checked. Returns 0 if * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a * signal needs to be delivered, ERESTART is returned if the current system * call should be restarted if possible, and EINTR is returned if the system * call should be interrupted by the signal (return EINTR). * * The mutex argument is exited before the caller is suspended, and * entered before msleep returns. If priority includes the PDROP * flag the mutex is not entered before returning. */ int msleep(ident, mtx, priority, wmesg, timo) void *ident; struct mtx *mtx; int priority, timo; const char *wmesg; { struct sleepqueue *sq; struct thread *td; struct proc *p; int catch, rval, sig; WITNESS_SAVE_DECL(mtx); td = curthread; p = td->td_proc; #ifdef KTRACE if (KTRPOINT(td, KTR_CSW)) ktrcsw(1, 0); #endif WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, mtx == NULL ? NULL : &mtx->mtx_object, "Sleeping on \"%s\"", wmesg); KASSERT(timo != 0 || mtx_owned(&Giant) || mtx != NULL, ("sleeping without a mutex")); KASSERT(p != NULL, ("msleep1")); KASSERT(ident != NULL && TD_IS_RUNNING(td), ("msleep")); if (cold) { /* * During autoconfiguration, just return; * don't run any other threads or panic below, * in case this is the idle thread and already asleep. * XXX: this used to do "s = splhigh(); splx(safepri); * splx(s);" to give interrupts a chance, but there is * no way to give interrupts a chance now. */ if (mtx != NULL && priority & PDROP) mtx_unlock(mtx); return (0); } catch = priority & PCATCH; rval = 0; /* * If we are already on a sleep queue, then remove us from that * sleep queue first. We have to do this to handle recursive * sleeps. */ if (TD_ON_SLEEPQ(td)) sleepq_remove(td, td->td_wchan); sq = sleepq_lookup(ident); mtx_lock_spin(&sched_lock); if (p->p_flag & P_SA || p->p_numthreads > 1) { /* * Just don't bother if we are exiting * and not the exiting thread or thread was marked as * interrupted. */ if (catch) { if ((p->p_flag & P_WEXIT) && p->p_singlethread != td) { mtx_unlock_spin(&sched_lock); sleepq_release(ident); return (EINTR); } if (td->td_flags & TDF_INTERRUPT) { mtx_unlock_spin(&sched_lock); sleepq_release(ident); return (td->td_intrval); } } } mtx_unlock_spin(&sched_lock); CTR5(KTR_PROC, "msleep: thread %p (pid %ld, %s) on %s (%p)", (void *)td, (long)p->p_pid, p->p_comm, wmesg, ident); DROP_GIANT(); if (mtx != NULL) { mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED); WITNESS_SAVE(&mtx->mtx_object, mtx); mtx_unlock(mtx); } /* * We put ourselves on the sleep queue and start our timeout * before calling thread_suspend_check, as we could stop there, * and a wakeup or a SIGCONT (or both) could occur while we were * stopped without resuming us. Thus, we must be ready for sleep * when cursig() is called. If the wakeup happens while we're * stopped, then td will no longer be on a sleep queue upon * return from cursig(). */ sleepq_add(sq, ident, mtx, wmesg, 0); if (timo) sleepq_set_timeout(ident, timo); if (catch) { sig = sleepq_catch_signals(ident); if (sig == 0 && !TD_ON_SLEEPQ(td)) { mtx_lock_spin(&sched_lock); td->td_flags &= ~TDF_SINTR; mtx_unlock_spin(&sched_lock); catch = 0; } } else sig = 0; /* * Adjust this thread's priority. * * XXX: do we need to save priority in td_base_pri? */ mtx_lock_spin(&sched_lock); sched_prio(td, priority & PRIMASK); mtx_unlock_spin(&sched_lock); if (timo && catch) rval = sleepq_timedwait_sig(ident, sig != 0); else if (timo) - rval = sleepq_timedwait(ident, sig != 0); + rval = sleepq_timedwait(ident); else if (catch) rval = sleepq_wait_sig(ident); else { sleepq_wait(ident); rval = 0; } if (rval == 0 && catch) rval = sleepq_calc_signal_retval(sig); #ifdef KTRACE if (KTRPOINT(td, KTR_CSW)) ktrcsw(0, 0); #endif PICKUP_GIANT(); if (mtx != NULL && !(priority & PDROP)) { mtx_lock(mtx); WITNESS_RESTORE(&mtx->mtx_object, mtx); } return (rval); } /* * Make all threads sleeping on the specified identifier runnable. */ void wakeup(ident) register void *ident; { sleepq_broadcast(ident, 0, -1); } /* * Make a thread sleeping on the specified identifier runnable. * May wake more than one thread if a target thread is currently * swapped out. */ void wakeup_one(ident) register void *ident; { sleepq_signal(ident, 0, -1); } /* * The machine independent parts of context switching. */ void mi_switch(int flags) { struct bintime new_switchtime; struct thread *td; struct proc *p; mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED); td = curthread; /* XXX */ p = td->td_proc; /* XXX */ KASSERT(!TD_ON_RUNQ(td), ("mi_switch: called by old code")); #ifdef INVARIANTS if (!TD_ON_LOCK(td) && !TD_IS_RUNNING(td)) mtx_assert(&Giant, MA_NOTOWNED); #endif KASSERT(td->td_critnest == 1, ("mi_switch: switch in a critical section")); KASSERT((flags & (SW_INVOL | SW_VOL)) != 0, ("mi_switch: switch must be voluntary or involuntary")); if (flags & SW_VOL) p->p_stats->p_ru.ru_nvcsw++; else p->p_stats->p_ru.ru_nivcsw++; /* * Compute the amount of time during which the current * process was running, and add that to its total so far. */ binuptime(&new_switchtime); bintime_add(&p->p_runtime, &new_switchtime); bintime_sub(&p->p_runtime, PCPU_PTR(switchtime)); td->td_generation++; /* bump preempt-detect counter */ #ifdef DDB /* * Don't perform context switches from the debugger. */ if (db_active) { mtx_unlock_spin(&sched_lock); db_print_backtrace(); db_error("Context switches not allowed in the debugger"); } #endif /* * Check if the process exceeds its cpu resource allocation. If * over max, arrange to kill the process in ast(). */ if (p->p_cpulimit != RLIM_INFINITY && p->p_runtime.sec > p->p_cpulimit) { p->p_sflag |= PS_XCPU; td->td_flags |= TDF_ASTPENDING; } /* * Finish up stats for outgoing thread. */ cnt.v_swtch++; PCPU_SET(switchtime, new_switchtime); PCPU_SET(switchticks, ticks); CTR3(KTR_PROC, "mi_switch: old thread %p (pid %ld, %s)", (void *)td, (long)p->p_pid, p->p_comm); if (td->td_proc->p_flag & P_SA) thread_switchout(td); sched_switch(td); CTR3(KTR_PROC, "mi_switch: new thread %p (pid %ld, %s)", (void *)td, (long)p->p_pid, p->p_comm); /* * If the last thread was exiting, finish cleaning it up. */ if ((td = PCPU_GET(deadthread))) { PCPU_SET(deadthread, NULL); thread_stash(td); } } /* * Change process state to be runnable, * placing it on the run queue if it is in memory, * and awakening the swapper if it isn't in memory. */ void setrunnable(struct thread *td) { struct proc *p; p = td->td_proc; mtx_assert(&sched_lock, MA_OWNED); switch (p->p_state) { case PRS_ZOMBIE: panic("setrunnable(1)"); default: break; } switch (td->td_state) { case TDS_RUNNING: case TDS_RUNQ: return; case TDS_INHIBITED: /* * If we are only inhibited because we are swapped out * then arange to swap in this process. Otherwise just return. */ if (td->td_inhibitors != TDI_SWAPPED) return; /* XXX: intentional fall-through ? */ case TDS_CAN_RUN: break; default: printf("state is 0x%x", td->td_state); panic("setrunnable(2)"); } if ((p->p_sflag & PS_INMEM) == 0) { if ((p->p_sflag & PS_SWAPPINGIN) == 0) { p->p_sflag |= PS_SWAPINREQ; wakeup(&proc0); } } else sched_wakeup(td); } /* * Compute a tenex style load average of a quantity on * 1, 5 and 15 minute intervals. * XXXKSE Needs complete rewrite when correct info is available. * Completely Bogus.. only works with 1:1 (but compiles ok now :-) */ static void loadav(void *arg) { int i, nrun; struct loadavg *avg; nrun = sched_load(); avg = &averunnable; for (i = 0; i < 3; i++) avg->ldavg[i] = (cexp[i] * avg->ldavg[i] + nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT; /* * Schedule the next update to occur after 5 seconds, but add a * random variation to avoid synchronisation with processes that * run at regular intervals. */ callout_reset(&loadav_callout, hz * 4 + (int)(random() % (hz * 2 + 1)), loadav, NULL); } static void lboltcb(void *arg) { wakeup(&lbolt); callout_reset(&lbolt_callout, hz, lboltcb, NULL); } /* ARGSUSED */ static void synch_setup(dummy) void *dummy; { callout_init(&loadav_callout, CALLOUT_MPSAFE); callout_init(&lbolt_callout, CALLOUT_MPSAFE); /* Kick off timeout driven events by calling first time. */ loadav(NULL); lboltcb(NULL); } /* * General purpose yield system call */ int yield(struct thread *td, struct yield_args *uap) { struct ksegrp *kg; kg = td->td_ksegrp; mtx_assert(&Giant, MA_NOTOWNED); mtx_lock_spin(&sched_lock); sched_prio(td, PRI_MAX_TIMESHARE); mi_switch(SW_VOL); mtx_unlock_spin(&sched_lock); td->td_retval[0] = 0; return (0); } Index: head/sys/kern/subr_sleepqueue.c =================================================================== --- head/sys/kern/subr_sleepqueue.c (revision 131248) +++ head/sys/kern/subr_sleepqueue.c (revision 131249) @@ -1,798 +1,795 @@ /* * Copyright (c) 2004 John Baldwin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Implementation of sleep queues used to hold queue of threads blocked on * a wait channel. Sleep queues different from turnstiles in that wait * channels are not owned by anyone, so there is no priority propagation. * Sleep queues can also provide a timeout and can also be interrupted by * signals. That said, there are several similarities between the turnstile * and sleep queue implementations. (Note: turnstiles were implemented * first.) For example, both use a hash table of the same size where each * bucket is referred to as a "chain" that contains both a spin lock and * a linked list of queues. An individual queue is located by using a hash * to pick a chain, locking the chain, and then walking the chain searching * for the queue. This means that a wait channel object does not need to * embed it's queue head just as locks do not embed their turnstile queue * head. Threads also carry around a sleep queue that they lend to the * wait channel when blocking. Just as in turnstiles, the queue includes * a free list of the sleep queues of other threads blocked on the same * wait channel in the case of multiple waiters. * * Some additional functionality provided by sleep queues include the * ability to set a timeout. The timeout is managed using a per-thread * callout that resumes a thread if it is asleep. A thread may also * catch signals while it is asleep (aka an interruptible sleep). The * signal code uses sleepq_abort() to interrupt a sleeping thread. Finally, * sleep queues also provide some extra assertions. One is not allowed to * mix the sleep/wakeup and cv APIs for a given wait channel. Also, one * must consistently use the same lock to synchronize with a wait channel, * though this check is currently only a warning for sleep/wakeup due to * pre-existing abuse of that API. The same lock must also be held when * awakening threads, though that is currently only enforced for condition * variables. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include /* * Constants for the hash table of sleep queue chains. These constants are * the same ones that 4BSD (and possibly earlier versions of BSD) used. * Basically, we ignore the lower 8 bits of the address since most wait * channel pointers are aligned and only look at the next 7 bits for the * hash. SC_TABLESIZE must be a power of two for SC_MASK to work properly. */ #define SC_TABLESIZE 128 /* Must be power of 2. */ #define SC_MASK (SC_TABLESIZE - 1) #define SC_SHIFT 8 #define SC_HASH(wc) (((uintptr_t)(wc) >> SC_SHIFT) & SC_MASK) #define SC_LOOKUP(wc) &sleepq_chains[SC_HASH(wc)] /* * There two different lists of sleep queues. Both lists are connected * via the sq_hash entries. The first list is the sleep queue chain list * that a sleep queue is on when it is attached to a wait channel. The * second list is the free list hung off of a sleep queue that is attached * to a wait channel. * * Each sleep queue also contains the wait channel it is attached to, the * list of threads blocked on that wait channel, flags specific to the * wait channel, and the lock used to synchronize with a wait channel. * The flags are used to catch mismatches between the various consumers * of the sleep queue API (e.g. sleep/wakeup and condition variables). * The lock pointer is only used when invariants are enabled for various * debugging checks. * * Locking key: * c - sleep queue chain lock */ struct sleepqueue { TAILQ_HEAD(, thread) sq_blocked; /* (c) Blocked threads. */ LIST_ENTRY(sleepqueue) sq_hash; /* (c) Chain and free list. */ LIST_HEAD(, sleepqueue) sq_free; /* (c) Free queues. */ void *sq_wchan; /* (c) Wait channel. */ int sq_flags; /* (c) Flags. */ #ifdef INVARIANTS struct mtx *sq_lock; /* (c) Associated lock. */ #endif }; struct sleepqueue_chain { LIST_HEAD(, sleepqueue) sc_queues; /* List of sleep queues. */ struct mtx sc_lock; /* Spin lock for this chain. */ }; static struct sleepqueue_chain sleepq_chains[SC_TABLESIZE]; MALLOC_DEFINE(M_SLEEPQUEUE, "sleep queues", "sleep queues"); /* * Prototypes for non-exported routines. */ static int sleepq_check_timeout(void); static void sleepq_switch(void *wchan); static void sleepq_timeout(void *arg); static void sleepq_remove_thread(struct sleepqueue *sq, struct thread *td); static void sleepq_resume_thread(struct thread *td, int pri); /* * Early initialization of sleep queues that is called from the sleepinit() * SYSINIT. */ void init_sleepqueues(void) { int i; for (i = 0; i < SC_TABLESIZE; i++) { LIST_INIT(&sleepq_chains[i].sc_queues); mtx_init(&sleepq_chains[i].sc_lock, "sleepq chain", NULL, MTX_SPIN); } thread0.td_sleepqueue = sleepq_alloc(); } /* * Malloc and initialize a new sleep queue for a new thread. */ struct sleepqueue * sleepq_alloc(void) { struct sleepqueue *sq; sq = malloc(sizeof(struct sleepqueue), M_SLEEPQUEUE, M_WAITOK | M_ZERO); TAILQ_INIT(&sq->sq_blocked); LIST_INIT(&sq->sq_free); return (sq); } /* * Free a sleep queue when a thread is destroyed. */ void sleepq_free(struct sleepqueue *sq) { MPASS(sq != NULL); MPASS(TAILQ_EMPTY(&sq->sq_blocked)); free(sq, M_SLEEPQUEUE); } /* * Look up the sleep queue associated with a given wait channel in the hash * table locking the associated sleep queue chain. Return holdind the sleep * queue chain lock. If no queue is found in the table, NULL is returned. */ struct sleepqueue * sleepq_lookup(void *wchan) { struct sleepqueue_chain *sc; struct sleepqueue *sq; KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__)); sc = SC_LOOKUP(wchan); mtx_lock_spin(&sc->sc_lock); LIST_FOREACH(sq, &sc->sc_queues, sq_hash) if (sq->sq_wchan == wchan) return (sq); return (NULL); } /* * Unlock the sleep queue chain associated with a given wait channel. */ void sleepq_release(void *wchan) { struct sleepqueue_chain *sc; sc = SC_LOOKUP(wchan); mtx_unlock_spin(&sc->sc_lock); } /* * Places the current thread on the sleepqueue for the specified wait * channel. If INVARIANTS is enabled, then it associates the passed in * lock with the sleepq to make sure it is held when that sleep queue is * woken up. */ void sleepq_add(struct sleepqueue *sq, void *wchan, struct mtx *lock, const char *wmesg, int flags) { struct sleepqueue_chain *sc; struct thread *td, *td1; td = curthread; sc = SC_LOOKUP(wchan); mtx_assert(&sc->sc_lock, MA_OWNED); MPASS(td->td_sleepqueue != NULL); MPASS(wchan != NULL); /* If the passed in sleep queue is NULL, use this thread's queue. */ if (sq == NULL) { sq = td->td_sleepqueue; LIST_INSERT_HEAD(&sc->sc_queues, sq, sq_hash); KASSERT(TAILQ_EMPTY(&sq->sq_blocked), ("thread's sleep queue has a non-empty queue")); KASSERT(LIST_EMPTY(&sq->sq_free), ("thread's sleep queue has a non-empty free list")); KASSERT(sq->sq_wchan == NULL, ("stale sq_wchan pointer")); sq->sq_wchan = wchan; #ifdef INVARIANTS sq->sq_lock = lock; #endif sq->sq_flags = flags; TAILQ_INSERT_TAIL(&sq->sq_blocked, td, td_slpq); } else { MPASS(wchan == sq->sq_wchan); MPASS(lock == sq->sq_lock); TAILQ_FOREACH(td1, &sq->sq_blocked, td_slpq) if (td1->td_priority > td->td_priority) break; if (td1 != NULL) TAILQ_INSERT_BEFORE(td1, td, td_slpq); else TAILQ_INSERT_TAIL(&sq->sq_blocked, td, td_slpq); LIST_INSERT_HEAD(&sq->sq_free, td->td_sleepqueue, sq_hash); } td->td_sleepqueue = NULL; mtx_lock_spin(&sched_lock); td->td_wchan = wchan; td->td_wmesg = wmesg; mtx_unlock_spin(&sched_lock); } /* * Sets a timeout that will remove the current thread from the specified * sleep queue after timo ticks if the thread has not already been awakened. */ void sleepq_set_timeout(void *wchan, int timo) { struct sleepqueue_chain *sc; struct thread *td; td = curthread; sc = SC_LOOKUP(wchan); mtx_assert(&sc->sc_lock, MA_OWNED); MPASS(TD_ON_SLEEPQ(td)); MPASS(td->td_sleepqueue == NULL); MPASS(wchan != NULL); callout_reset(&td->td_slpcallout, timo, sleepq_timeout, td); } /* * Marks the pending sleep of the current thread as interruptible and * makes an initial check for pending signals before putting a thread * to sleep. */ int sleepq_catch_signals(void *wchan) { struct sleepqueue_chain *sc; struct sleepqueue *sq; struct thread *td; struct proc *p; int do_upcall; int sig; do_upcall = 0; td = curthread; p = td->td_proc; sc = SC_LOOKUP(wchan); mtx_assert(&sc->sc_lock, MA_OWNED); MPASS(td->td_sleepqueue == NULL); MPASS(wchan != NULL); CTR3(KTR_PROC, "sleepq catching signals: thread %p (pid %ld, %s)", (void *)td, (long)p->p_pid, p->p_comm); /* Mark thread as being in an interruptible sleep. */ mtx_lock_spin(&sched_lock); MPASS(TD_ON_SLEEPQ(td)); td->td_flags |= TDF_SINTR; mtx_unlock_spin(&sched_lock); sleepq_release(wchan); /* See if there are any pending signals for this thread. */ PROC_LOCK(p); mtx_lock(&p->p_sigacts->ps_mtx); sig = cursig(td); mtx_unlock(&p->p_sigacts->ps_mtx); if (sig == 0 && thread_suspend_check(1)) sig = SIGSTOP; else do_upcall = thread_upcall_check(td); PROC_UNLOCK(p); /* * If there were pending signals and this thread is still on * the sleep queue, remove it from the sleep queue. */ sq = sleepq_lookup(wchan); mtx_lock_spin(&sched_lock); if (TD_ON_SLEEPQ(td) && (sig != 0 || do_upcall != 0)) { mtx_unlock_spin(&sched_lock); sleepq_remove_thread(sq, td); } else mtx_unlock_spin(&sched_lock); return (sig); } /* * Switches to another thread if we are still asleep on a sleep queue and * drop the lock on the sleepqueue chain. Returns with sched_lock held. */ static void sleepq_switch(void *wchan) { struct sleepqueue_chain *sc; struct thread *td; td = curthread; sc = SC_LOOKUP(wchan); mtx_assert(&sc->sc_lock, MA_OWNED); /* * If we have a sleep queue, then we've already been woken up, so * just return. */ if (td->td_sleepqueue != NULL) { MPASS(!TD_ON_SLEEPQ(td)); mtx_unlock_spin(&sc->sc_lock); mtx_lock_spin(&sched_lock); return; } /* * Otherwise, actually go to sleep. */ mtx_lock_spin(&sched_lock); mtx_unlock_spin(&sc->sc_lock); sched_sleep(td); TD_SET_SLEEPING(td); mi_switch(SW_VOL); KASSERT(TD_IS_RUNNING(td), ("running but not TDS_RUNNING")); CTR3(KTR_PROC, "sleepq resume: thread %p (pid %ld, %s)", (void *)td, (long)td->td_proc->p_pid, (void *)td->td_proc->p_comm); } /* * Check to see if we timed out. */ static int sleepq_check_timeout(void) { struct thread *td; mtx_assert(&sched_lock, MA_OWNED); td = curthread; /* * If TDF_TIMEOUT is set, we timed out. */ if (td->td_flags & TDF_TIMEOUT) { td->td_flags &= ~TDF_TIMEOUT; return (EWOULDBLOCK); } /* * If TDF_TIMOFAIL is set, the timeout ran after we had * already been woken up. */ if (td->td_flags & TDF_TIMOFAIL) td->td_flags &= ~TDF_TIMOFAIL; /* * If callout_stop() fails, then the timeout is running on * another CPU, so synchronize with it to avoid having it * accidentally wake up a subsequent sleep. */ else if (callout_stop(&td->td_slpcallout) == 0) { td->td_flags |= TDF_TIMEOUT; TD_SET_SLEEPING(td); mi_switch(SW_INVOL); } return (0); } /* * Check to see if we were awoken by a signal. */ static int sleepq_check_signals(void) { struct thread *td; mtx_assert(&sched_lock, MA_OWNED); td = curthread; /* We are no longer in an interruptible sleep. */ td->td_flags &= ~TDF_SINTR; if (td->td_flags & TDF_INTERRUPT) return (td->td_intrval); return (0); } /* * If we were in an interruptible sleep and we weren't interrupted and * didn't timeout, check to see if there are any pending signals and * which return value we should use if so. The return value from an * earlier call to sleepq_catch_signals() should be passed in as the * argument. */ int sleepq_calc_signal_retval(int sig) { struct thread *td; struct proc *p; int rval; td = curthread; p = td->td_proc; PROC_LOCK(p); mtx_lock(&p->p_sigacts->ps_mtx); /* XXX: Should we always be calling cursig()? */ if (sig == 0) sig = cursig(td); if (sig != 0) { if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig)) rval = EINTR; else rval = ERESTART; } else rval = 0; mtx_unlock(&p->p_sigacts->ps_mtx); PROC_UNLOCK(p); return (rval); } /* * Block the current thread until it is awakened from its sleep queue. */ void sleepq_wait(void *wchan) { sleepq_switch(wchan); mtx_unlock_spin(&sched_lock); } /* * Block the current thread until it is awakened from its sleep queue * or it is interrupted by a signal. */ int sleepq_wait_sig(void *wchan) { int rval; sleepq_switch(wchan); rval = sleepq_check_signals(); mtx_unlock_spin(&sched_lock); return (rval); } /* * Block the current thread until it is awakened from its sleep queue * or it times out while waiting. */ int -sleepq_timedwait(void *wchan, int signal_caught) +sleepq_timedwait(void *wchan) { int rval; sleepq_switch(wchan); rval = sleepq_check_timeout(); mtx_unlock_spin(&sched_lock); - if (signal_caught) - return (0); - else - return (rval); + return (rval); } /* * Block the current thread until it is awakened from its sleep queue, * it is interrupted by a signal, or it times out waiting to be awakened. */ int sleepq_timedwait_sig(void *wchan, int signal_caught) { int rvalt, rvals; sleepq_switch(wchan); rvalt = sleepq_check_timeout(); rvals = sleepq_check_signals(); mtx_unlock_spin(&sched_lock); if (signal_caught || rvalt == 0) return (rvals); else return (rvalt); } /* * Removes a thread from a sleep queue. */ static void sleepq_remove_thread(struct sleepqueue *sq, struct thread *td) { struct sleepqueue_chain *sc; MPASS(td != NULL); MPASS(sq->sq_wchan != NULL); MPASS(td->td_wchan == sq->sq_wchan); sc = SC_LOOKUP(sq->sq_wchan); mtx_assert(&sc->sc_lock, MA_OWNED); /* Remove the thread from the queue. */ TAILQ_REMOVE(&sq->sq_blocked, td, td_slpq); /* * Get a sleep queue for this thread. If this is the last waiter, * use the queue itself and take it out of the chain, otherwise, * remove a queue from the free list. */ if (LIST_EMPTY(&sq->sq_free)) { td->td_sleepqueue = sq; #ifdef INVARIANTS sq->sq_wchan = NULL; #endif } else td->td_sleepqueue = LIST_FIRST(&sq->sq_free); LIST_REMOVE(td->td_sleepqueue, sq_hash); mtx_lock_spin(&sched_lock); td->td_wmesg = NULL; td->td_wchan = NULL; mtx_unlock_spin(&sched_lock); } /* * Resumes a thread that was asleep on a queue. */ static void sleepq_resume_thread(struct thread *td, int pri) { /* * Note that thread td might not be sleeping if it is running * sleepq_catch_signals() on another CPU or is blocked on * its proc lock to check signals. It doesn't hurt to clear * the sleeping flag if it isn't set though, so we just always * do it. However, we can't assert that it is set. */ mtx_lock_spin(&sched_lock); CTR3(KTR_PROC, "sleepq_wakeup: thread %p (pid %ld, %s)", (void *)td, (long)td->td_proc->p_pid, td->td_proc->p_comm); TD_CLR_SLEEPING(td); /* Adjust priority if requested. */ MPASS(pri == -1 || (pri >= PRI_MIN && pri <= PRI_MAX)); if (pri != -1 && td->td_priority > pri) td->td_priority = pri; setrunnable(td); mtx_unlock_spin(&sched_lock); } /* * Find the highest priority thread sleeping on a wait channel and resume it. */ void sleepq_signal(void *wchan, int flags, int pri) { struct sleepqueue *sq; struct thread *td; CTR2(KTR_PROC, "sleepq_signal(%p, %d)", wchan, flags); KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__)); sq = sleepq_lookup(wchan); if (sq == NULL) { sleepq_release(wchan); return; } KASSERT(sq->sq_flags == flags, ("%s: mismatch between sleep/wakeup and cv_*", __func__)); /* XXX: Do for all sleep queues eventually. */ if (flags & SLEEPQ_CONDVAR) mtx_assert(sq->sq_lock, MA_OWNED); /* Remove first thread from queue and awaken it. */ td = TAILQ_FIRST(&sq->sq_blocked); sleepq_remove_thread(sq, td); sleepq_release(wchan); sleepq_resume_thread(td, pri); } /* * Resume all threads sleeping on a specified wait channel. */ void sleepq_broadcast(void *wchan, int flags, int pri) { TAILQ_HEAD(, thread) list; struct sleepqueue *sq; struct thread *td; CTR2(KTR_PROC, "sleepq_broadcast(%p, %d)", wchan, flags); KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__)); sq = sleepq_lookup(wchan); if (sq == NULL) { sleepq_release(wchan); return; } KASSERT(sq->sq_flags == flags, ("%s: mismatch between sleep/wakeup and cv_*", __func__)); /* XXX: Do for all sleep queues eventually. */ if (flags & SLEEPQ_CONDVAR) mtx_assert(sq->sq_lock, MA_OWNED); /* Move blocked threads from the sleep queue to a temporary list. */ TAILQ_INIT(&list); while (!TAILQ_EMPTY(&sq->sq_blocked)) { td = TAILQ_FIRST(&sq->sq_blocked); sleepq_remove_thread(sq, td); TAILQ_INSERT_TAIL(&list, td, td_slpq); } sleepq_release(wchan); /* Resume all the threads on the temporary list. */ while (!TAILQ_EMPTY(&list)) { td = TAILQ_FIRST(&list); TAILQ_REMOVE(&list, td, td_slpq); sleepq_resume_thread(td, pri); } } /* * Time sleeping threads out. When the timeout expires, the thread is * removed from the sleep queue and made runnable if it is still asleep. */ static void sleepq_timeout(void *arg) { struct sleepqueue *sq; struct thread *td; void *wchan; td = arg; CTR3(KTR_PROC, "sleepq_timeout: thread %p (pid %ld, %s)", (void *)td, (long)td->td_proc->p_pid, (void *)td->td_proc->p_comm); /* * First, see if the thread is asleep and get the wait channel if * it is. */ mtx_lock_spin(&sched_lock); if (TD_ON_SLEEPQ(td)) { wchan = td->td_wchan; mtx_unlock_spin(&sched_lock); sq = sleepq_lookup(wchan); mtx_lock_spin(&sched_lock); } else { wchan = NULL; sq = NULL; } /* * At this point, if the thread is still on the sleep queue, * we have that sleep queue locked as it cannot migrate sleep * queues while we dropped sched_lock. If it had resumed and * was on another CPU while the lock was dropped, it would have * seen that TDF_TIMEOUT and TDF_TIMOFAIL are clear and the * call to callout_stop() to stop this routine would have failed * meaning that it would have already set TDF_TIMEOUT to * synchronize with this function. */ if (TD_ON_SLEEPQ(td)) { MPASS(td->td_wchan == wchan); MPASS(sq != NULL); td->td_flags |= TDF_TIMEOUT; mtx_unlock_spin(&sched_lock); sleepq_remove_thread(sq, td); sleepq_release(wchan); sleepq_resume_thread(td, -1); return; } else if (wchan != NULL) sleepq_release(wchan); /* * Now check for the edge cases. First, if TDF_TIMEOUT is set, * then the other thread has already yielded to us, so clear * the flag and resume it. If TDF_TIMEOUT is not set, then the * we know that the other thread is not on a sleep queue, but it * hasn't resumed execution yet. In that case, set TDF_TIMOFAIL * to let it know that the timeout has already run and doesn't * need to be canceled. */ if (td->td_flags & TDF_TIMEOUT) { MPASS(TD_IS_SLEEPING(td)); td->td_flags &= ~TDF_TIMEOUT; TD_CLR_SLEEPING(td); setrunnable(td); } else td->td_flags |= TDF_TIMOFAIL; mtx_unlock_spin(&sched_lock); } /* * Resumes a specific thread from the sleep queue associated with a specific * wait channel if it is on that queue. */ void sleepq_remove(struct thread *td, void *wchan) { struct sleepqueue *sq; /* * Look up the sleep queue for this wait channel, then re-check * that the thread is asleep on that channel, if it is not, then * bail. */ MPASS(wchan != NULL); sq = sleepq_lookup(wchan); mtx_lock_spin(&sched_lock); if (!TD_ON_SLEEPQ(td) || td->td_wchan != wchan) { mtx_unlock_spin(&sched_lock); sleepq_release(wchan); return; } mtx_unlock_spin(&sched_lock); MPASS(sq != NULL); /* Thread is asleep on sleep queue sq, so wake it up. */ sleepq_remove_thread(sq, td); sleepq_release(wchan); sleepq_resume_thread(td, -1); } /* * Abort a thread as if an interrupt had occurred. Only abort * interruptible waits (unfortunately it isn't safe to abort others). * * XXX: What in the world does the comment below mean? * Also, whatever the signal code does... */ void sleepq_abort(struct thread *td) { void *wchan; mtx_assert(&sched_lock, MA_OWNED); MPASS(TD_ON_SLEEPQ(td)); MPASS(td->td_flags & TDF_SINTR); /* * If the TDF_TIMEOUT flag is set, just leave. A * timeout is scheduled anyhow. */ if (td->td_flags & TDF_TIMEOUT) return; CTR3(KTR_PROC, "sleepq_abort: thread %p (pid %ld, %s)", (void *)td, (long)td->td_proc->p_pid, (void *)td->td_proc->p_comm); wchan = td->td_wchan; mtx_unlock_spin(&sched_lock); sleepq_remove(td, wchan); mtx_lock_spin(&sched_lock); } Index: head/sys/sys/sleepqueue.h =================================================================== --- head/sys/sys/sleepqueue.h (revision 131248) +++ head/sys/sys/sleepqueue.h (revision 131249) @@ -1,107 +1,107 @@ /* * Copyright (c) 2004 John Baldwin * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _SYS_SLEEPQUEUE_H_ #define _SYS_SLEEPQUEUE_H_ /* * Sleep queue interface. Sleep/wakeup and condition variables use a sleep * queue for the queue of threads blocked on a sleep channel. * * A thread calls sleepq_lookup() to look up the proper sleep queue in the * hash table that is associated with a specified wait channel. This * function returns a pointer to the queue and locks the associated sleep * queue chain. A thread calls sleepq_add() to add themself onto a sleep * queue and calls one of the sleepq_wait() functions to actually go to * sleep. If a thread needs to abort a sleep operation it should call * sleepq_release() to unlock the associated sleep queue chain lock. If * the thread also needs to remove itself from a queue it just enqueued * itself on, it can use sleepq_remove(). * * If the thread only wishes to sleep for a limited amount of time, it can * call sleepq_set_timeout() after sleepq_add() to setup a timeout. It * should then use one of the sleepq_timedwait() functions to block. * * If the thread wants to the sleep to be interruptible by signals, it can * call sleepq_catch_signals() after sleepq_add(). It should then use * one of the sleepq_wait_sig() functions to block. After the thread has * been resumed, it should call sleepq_calc_signal_retval() to determine * if it should return EINTR or ERESTART passing in the value returned from * the earlier call to sleepq_catch_signals(). * * A thread is normally resumed from a sleep queue by either the * sleepq_signal() or sleepq_broadcast() functions. Sleepq_signal() wakes * the thread with the highest priority that is sleeping on the specified * wait channel. Sleepq_broadcast() wakes all threads that are sleeping * on the specified wait channel. A thread sleeping in an interruptible * sleep can be interrupted by calling sleepq_abort(). A thread can also * be removed from a specified sleep queue using the sleepq_remove() * function. * * Each thread allocates a sleep queue at thread creation via sleepq_alloc() * and releases it at thread destruction via sleepq_free(). Note that * a sleep queue is not tied to a specific thread and that the sleep queue * released at thread destruction may not be the same sleep queue that the * thread allocated when it was created. * * XXX: Some other parts of the kernel such as ithread sleeping may end up * using this interface as well (death to TDI_IWAIT!) */ struct mtx; struct sleepqueue; struct thread; #ifdef _KERNEL #define SLEEPQ_CONDVAR 0x1 /* Sleep queue is a cv. */ void init_sleepqueues(void); void sleepq_abort(struct thread *td); void sleepq_add(struct sleepqueue *, void *, struct mtx *, const char *, int); struct sleepqueue *sleepq_alloc(void); void sleepq_broadcast(void *, int, int); int sleepq_calc_signal_retval(int sig); int sleepq_catch_signals(void *wchan); void sleepq_free(struct sleepqueue *); struct sleepqueue *sleepq_lookup(void *); void sleepq_release(void *); void sleepq_remove(struct thread *, void *); void sleepq_signal(void *, int, int); void sleepq_set_timeout(void *wchan, int timo); -int sleepq_timedwait(void *wchan, int signal_caught); +int sleepq_timedwait(void *wchan); int sleepq_timedwait_sig(void *wchan, int signal_caught); void sleepq_wait(void *); int sleepq_wait_sig(void *wchan); #endif /* _KERNEL */ #endif /* !_SYS_SLEEPQUEUE_H_ */