kern_condvar.c revision 109862
171088Sjasone/*- 271088Sjasone * Copyright (c) 2000 Jake Burkholder <jake@freebsd.org>. 371088Sjasone * All rights reserved. 471088Sjasone * 571088Sjasone * Redistribution and use in source and binary forms, with or without 671088Sjasone * modification, are permitted provided that the following conditions 771088Sjasone * are met: 871088Sjasone * 1. Redistributions of source code must retain the above copyright 971088Sjasone * notice, this list of conditions and the following disclaimer. 1071088Sjasone * 2. Redistributions in binary form must reproduce the above copyright 1171088Sjasone * notice, this list of conditions and the following disclaimer in the 1271088Sjasone * documentation and/or other materials provided with the distribution. 1371088Sjasone * 1471088Sjasone * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1571088Sjasone * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1671088Sjasone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1771088Sjasone * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1871088Sjasone * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1971088Sjasone * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2071088Sjasone * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2171088Sjasone * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2271088Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2371088Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2471088Sjasone * SUCH DAMAGE. 2571088Sjasone * 2671088Sjasone * $FreeBSD: head/sys/kern/kern_condvar.c 109862 2003-01-26 04:00:39Z jeff $ 2771088Sjasone */ 2871088Sjasone 2971088Sjasone#include "opt_ktrace.h" 3071088Sjasone 3171088Sjasone#include <sys/param.h> 3271088Sjasone#include <sys/systm.h> 3376166Smarkm#include <sys/lock.h> 3476166Smarkm#include <sys/mutex.h> 3571088Sjasone#include <sys/proc.h> 3671088Sjasone#include <sys/kernel.h> 3771088Sjasone#include <sys/ktr.h> 3871088Sjasone#include <sys/condvar.h> 39109862Sjeff#include <sys/sched.h> 4071088Sjasone#include <sys/signalvar.h> 4171088Sjasone#include <sys/resourcevar.h> 4271088Sjasone#ifdef KTRACE 4371088Sjasone#include <sys/uio.h> 4471088Sjasone#include <sys/ktrace.h> 4571088Sjasone#endif 4671088Sjasone 4771088Sjasone/* 4871088Sjasone * Common sanity checks for cv_wait* functions. 4971088Sjasone */ 5083366Sjulian#define CV_ASSERT(cvp, mp, td) do { \ 5187594Sobrien KASSERT((td) != NULL, ("%s: curthread NULL", __func__)); \ 52103216Sjulian KASSERT(TD_IS_RUNNING(td), ("%s: not TDS_RUNNING", __func__)); \ 5387594Sobrien KASSERT((cvp) != NULL, ("%s: cvp NULL", __func__)); \ 5487594Sobrien KASSERT((mp) != NULL, ("%s: mp NULL", __func__)); \ 5571088Sjasone mtx_assert((mp), MA_OWNED | MA_NOTRECURSED); \ 5671088Sjasone} while (0) 5771088Sjasone 5893408Sdan#ifdef INVARIANTS 5971088Sjasone#define CV_WAIT_VALIDATE(cvp, mp) do { \ 6071088Sjasone if (TAILQ_EMPTY(&(cvp)->cv_waitq)) { \ 6171088Sjasone /* Only waiter. */ \ 6271088Sjasone (cvp)->cv_mtx = (mp); \ 6371088Sjasone } else { \ 6471088Sjasone /* \ 6571088Sjasone * Other waiter; assert that we're using the \ 6671088Sjasone * same mutex. \ 6771088Sjasone */ \ 6871088Sjasone KASSERT((cvp)->cv_mtx == (mp), \ 6987594Sobrien ("%s: Multiple mutexes", __func__)); \ 7071088Sjasone } \ 7171088Sjasone} while (0) 72103216Sjulian 7371088Sjasone#define CV_SIGNAL_VALIDATE(cvp) do { \ 7471088Sjasone if (!TAILQ_EMPTY(&(cvp)->cv_waitq)) { \ 7571088Sjasone KASSERT(mtx_owned((cvp)->cv_mtx), \ 76103216Sjulian ("%s: Mutex not owned", __func__)); \ 7771088Sjasone } \ 7871088Sjasone} while (0) 79103216Sjulian 8071088Sjasone#else 8171088Sjasone#define CV_WAIT_VALIDATE(cvp, mp) 8271088Sjasone#define CV_SIGNAL_VALIDATE(cvp) 8371088Sjasone#endif 8471088Sjasone 8571088Sjasonestatic void cv_timedwait_end(void *arg); 8671088Sjasone 8771088Sjasone/* 8871088Sjasone * Initialize a condition variable. Must be called before use. 8971088Sjasone */ 9071088Sjasonevoid 9171088Sjasonecv_init(struct cv *cvp, const char *desc) 9271088Sjasone{ 9371088Sjasone 9471088Sjasone TAILQ_INIT(&cvp->cv_waitq); 9571088Sjasone cvp->cv_mtx = NULL; 9671088Sjasone cvp->cv_description = desc; 9771088Sjasone} 9871088Sjasone 9971088Sjasone/* 10071088Sjasone * Destroy a condition variable. The condition variable must be re-initialized 10171088Sjasone * in order to be re-used. 10271088Sjasone */ 10371088Sjasonevoid 10471088Sjasonecv_destroy(struct cv *cvp) 10571088Sjasone{ 10671088Sjasone 10787594Sobrien KASSERT(cv_waitq_empty(cvp), ("%s: cv_waitq non-empty", __func__)); 10871088Sjasone} 10971088Sjasone 11071088Sjasone/* 11171088Sjasone * Common code for cv_wait* functions. All require sched_lock. 11271088Sjasone */ 11371088Sjasone 11471088Sjasone/* 115105911Sjulian * Switch context. 11699072Sjulian */ 117105911Sjulianstatic __inline void 118105911Sjuliancv_switch(struct thread *td) 11999072Sjulian{ 120103216Sjulian TD_SET_SLEEPING(td); 12183366Sjulian td->td_proc->p_stats->p_ru.ru_nvcsw++; 12271088Sjasone mi_switch(); 12383650Sjhb CTR3(KTR_PROC, "cv_switch: resume thread %p (pid %d, %s)", td, 12483650Sjhb td->td_proc->p_pid, td->td_proc->p_comm); 12571088Sjasone} 12671088Sjasone 12771088Sjasone/* 12871088Sjasone * Switch context, catching signals. 12971088Sjasone */ 13071088Sjasonestatic __inline int 13183366Sjuliancv_switch_catch(struct thread *td) 13271088Sjasone{ 13383650Sjhb struct proc *p; 13471088Sjasone int sig; 13571088Sjasone 13671088Sjasone /* 13771088Sjasone * We put ourselves on the sleep queue and start our timeout before 13897526Sjulian * calling cursig, as we could stop there, and a wakeup or a SIGCONT (or 13971088Sjasone * both) could occur while we were stopped. A SIGCONT would cause us to 14099072Sjulian * be marked as TDS_SLP without resuming us, thus we must be ready for 14197526Sjulian * sleep when cursig is called. If the wakeup happens while we're 142103216Sjulian * stopped, td->td_wchan will be 0 upon return from cursig, 143103216Sjulian * and TD_ON_SLEEPQ() will return false. 14471088Sjasone */ 14583366Sjulian td->td_flags |= TDF_SINTR; 14672200Sbmilekic mtx_unlock_spin(&sched_lock); 14783650Sjhb p = td->td_proc; 14883650Sjhb PROC_LOCK(p); 149103216Sjulian sig = cursig(td); 15099072Sjulian if (thread_suspend_check(1)) 15199072Sjulian sig = SIGSTOP; 15272200Sbmilekic mtx_lock_spin(&sched_lock); 15388900Sjhb PROC_UNLOCK(p); 15471088Sjasone if (sig != 0) { 155103216Sjulian if (TD_ON_SLEEPQ(td)) 15683366Sjulian cv_waitq_remove(td); 157103216Sjulian TD_SET_RUNNING(td); 158103216Sjulian } else if (TD_ON_SLEEPQ(td)) { 15983366Sjulian cv_switch(td); 16071088Sjasone } 16183366Sjulian td->td_flags &= ~TDF_SINTR; 16271088Sjasone 16371088Sjasone return sig; 16471088Sjasone} 16571088Sjasone 16671088Sjasone/* 16783366Sjulian * Add a thread to the wait queue of a condition variable. 16871088Sjasone */ 16971088Sjasonestatic __inline void 17083366Sjuliancv_waitq_add(struct cv *cvp, struct thread *td) 17171088Sjasone{ 17271088Sjasone 17383366Sjulian td->td_flags |= TDF_CVWAITQ; 174103216Sjulian TD_SET_ON_SLEEPQ(td); 17583366Sjulian td->td_wchan = cvp; 17683366Sjulian td->td_wmesg = cvp->cv_description; 17783366Sjulian CTR3(KTR_PROC, "cv_waitq_add: thread %p (pid %d, %s)", td, 17883650Sjhb td->td_proc->p_pid, td->td_proc->p_comm); 17983366Sjulian TAILQ_INSERT_TAIL(&cvp->cv_waitq, td, td_slpq); 180109862Sjeff sched_sleep(td, td->td_priority); 18171088Sjasone} 18271088Sjasone 18371088Sjasone/* 18483366Sjulian * Wait on a condition variable. The current thread is placed on the condition 18571088Sjasone * variable's wait queue and suspended. A cv_signal or cv_broadcast on the same 18683366Sjulian * condition variable will resume the thread. The mutex is released before 18771088Sjasone * sleeping and will be held on return. It is recommended that the mutex be 18871088Sjasone * held when cv_signal or cv_broadcast are called. 18971088Sjasone */ 19071088Sjasonevoid 19171088Sjasonecv_wait(struct cv *cvp, struct mtx *mp) 19271088Sjasone{ 19383366Sjulian struct thread *td; 19471088Sjasone WITNESS_SAVE_DECL(mp); 19571088Sjasone 19683366Sjulian td = curthread; 19771088Sjasone#ifdef KTRACE 19897995Sjhb if (KTRPOINT(td, KTR_CSW)) 19997995Sjhb ktrcsw(1, 0); 20071088Sjasone#endif 20183366Sjulian CV_ASSERT(cvp, mp, td); 20274920Sjhb WITNESS_SLEEP(0, &mp->mtx_object); 20374912Sjhb WITNESS_SAVE(&mp->mtx_object, mp); 20471088Sjasone 205100209Sgallatin if (cold ) { 20671088Sjasone /* 207100209Sgallatin * During autoconfiguration, just give interrupts 208100209Sgallatin * a chance, then just return. Don't run any other 209100209Sgallatin * thread or panic below, in case this is the idle 210100209Sgallatin * process and already asleep. 21171088Sjasone */ 21271088Sjasone return; 21371088Sjasone } 21495322Shsu 21595322Shsu mtx_lock_spin(&sched_lock); 21695322Shsu 21771088Sjasone CV_WAIT_VALIDATE(cvp, mp); 21871088Sjasone 21988900Sjhb DROP_GIANT(); 22088900Sjhb mtx_unlock(mp); 22171088Sjasone 22283366Sjulian cv_waitq_add(cvp, td); 22383366Sjulian cv_switch(td); 22471088Sjasone 22572200Sbmilekic mtx_unlock_spin(&sched_lock); 22671088Sjasone#ifdef KTRACE 22797995Sjhb if (KTRPOINT(td, KTR_CSW)) 22897995Sjhb ktrcsw(0, 0); 22971088Sjasone#endif 23071088Sjasone PICKUP_GIANT(); 23172200Sbmilekic mtx_lock(mp); 23274912Sjhb WITNESS_RESTORE(&mp->mtx_object, mp); 23371088Sjasone} 23471088Sjasone 23571088Sjasone/* 23671088Sjasone * Wait on a condition variable, allowing interruption by signals. Return 0 if 23783366Sjulian * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if 23871088Sjasone * a signal was caught. If ERESTART is returned the system call should be 23971088Sjasone * restarted if possible. 24071088Sjasone */ 24171088Sjasoneint 24271088Sjasonecv_wait_sig(struct cv *cvp, struct mtx *mp) 24371088Sjasone{ 24483366Sjulian struct thread *td; 24583658Speter struct proc *p; 24671088Sjasone int rval; 24771088Sjasone int sig; 24871088Sjasone WITNESS_SAVE_DECL(mp); 24971088Sjasone 25083366Sjulian td = curthread; 25183650Sjhb p = td->td_proc; 25271088Sjasone rval = 0; 25371088Sjasone#ifdef KTRACE 25497995Sjhb if (KTRPOINT(td, KTR_CSW)) 25597995Sjhb ktrcsw(1, 0); 25671088Sjasone#endif 25783366Sjulian CV_ASSERT(cvp, mp, td); 25874920Sjhb WITNESS_SLEEP(0, &mp->mtx_object); 25974912Sjhb WITNESS_SAVE(&mp->mtx_object, mp); 26071088Sjasone 26171088Sjasone if (cold || panicstr) { 26271088Sjasone /* 26371088Sjasone * After a panic, or during autoconfiguration, just give 26471088Sjasone * interrupts a chance, then just return; don't run any other 26571088Sjasone * procs or panic below, in case this is the idle process and 26671088Sjasone * already asleep. 26771088Sjasone */ 26871088Sjasone return 0; 26971088Sjasone } 27095322Shsu 27195322Shsu mtx_lock_spin(&sched_lock); 27295322Shsu 27371088Sjasone CV_WAIT_VALIDATE(cvp, mp); 27471088Sjasone 27588900Sjhb DROP_GIANT(); 27688900Sjhb mtx_unlock(mp); 27771088Sjasone 27883366Sjulian cv_waitq_add(cvp, td); 27983366Sjulian sig = cv_switch_catch(td); 28071088Sjasone 28172200Sbmilekic mtx_unlock_spin(&sched_lock); 28271088Sjasone 28383650Sjhb PROC_LOCK(p); 28471088Sjasone if (sig == 0) 28599072Sjulian sig = cursig(td); /* XXXKSE */ 28671088Sjasone if (sig != 0) { 28783650Sjhb if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig)) 28871088Sjasone rval = EINTR; 28971088Sjasone else 29071088Sjasone rval = ERESTART; 29171088Sjasone } 29283650Sjhb PROC_UNLOCK(p); 29399072Sjulian if (p->p_flag & P_WEXIT) 29499072Sjulian rval = EINTR; 29571088Sjasone 29671088Sjasone#ifdef KTRACE 29797995Sjhb if (KTRPOINT(td, KTR_CSW)) 29897995Sjhb ktrcsw(0, 0); 29971088Sjasone#endif 30097995Sjhb PICKUP_GIANT(); 30172200Sbmilekic mtx_lock(mp); 30274912Sjhb WITNESS_RESTORE(&mp->mtx_object, mp); 30371088Sjasone 30471088Sjasone return (rval); 30571088Sjasone} 30671088Sjasone 30771088Sjasone/* 30871088Sjasone * Wait on a condition variable for at most timo/hz seconds. Returns 0 if the 30971088Sjasone * process was resumed by cv_signal or cv_broadcast, EWOULDBLOCK if the timeout 31071088Sjasone * expires. 31171088Sjasone */ 31271088Sjasoneint 31371088Sjasonecv_timedwait(struct cv *cvp, struct mtx *mp, int timo) 31471088Sjasone{ 31583366Sjulian struct thread *td; 31671088Sjasone int rval; 31771088Sjasone WITNESS_SAVE_DECL(mp); 31871088Sjasone 31983366Sjulian td = curthread; 32071088Sjasone rval = 0; 32171088Sjasone#ifdef KTRACE 32297995Sjhb if (KTRPOINT(td, KTR_CSW)) 32397995Sjhb ktrcsw(1, 0); 32471088Sjasone#endif 32583366Sjulian CV_ASSERT(cvp, mp, td); 32674920Sjhb WITNESS_SLEEP(0, &mp->mtx_object); 32774912Sjhb WITNESS_SAVE(&mp->mtx_object, mp); 32871088Sjasone 32971088Sjasone if (cold || panicstr) { 33071088Sjasone /* 33171088Sjasone * After a panic, or during autoconfiguration, just give 33271088Sjasone * interrupts a chance, then just return; don't run any other 33383366Sjulian * thread or panic below, in case this is the idle process and 33471088Sjasone * already asleep. 33571088Sjasone */ 33671088Sjasone return 0; 33771088Sjasone } 33895322Shsu 33995322Shsu mtx_lock_spin(&sched_lock); 34095322Shsu 34171088Sjasone CV_WAIT_VALIDATE(cvp, mp); 34271088Sjasone 34388900Sjhb DROP_GIANT(); 34488900Sjhb mtx_unlock(mp); 34571088Sjasone 34683366Sjulian cv_waitq_add(cvp, td); 34783366Sjulian callout_reset(&td->td_slpcallout, timo, cv_timedwait_end, td); 34883366Sjulian cv_switch(td); 34971088Sjasone 35083366Sjulian if (td->td_flags & TDF_TIMEOUT) { 35183366Sjulian td->td_flags &= ~TDF_TIMEOUT; 35271088Sjasone rval = EWOULDBLOCK; 35383366Sjulian } else if (td->td_flags & TDF_TIMOFAIL) 35483366Sjulian td->td_flags &= ~TDF_TIMOFAIL; 35583366Sjulian else if (callout_stop(&td->td_slpcallout) == 0) { 35682085Sjhb /* 35782085Sjhb * Work around race with cv_timedwait_end similar to that 35882085Sjhb * between msleep and endtsleep. 35999247Sjulian * Go back to sleep. 36082085Sjhb */ 361103216Sjulian TD_SET_SLEEPING(td); 36283366Sjulian td->td_proc->p_stats->p_ru.ru_nivcsw++; 36382085Sjhb mi_switch(); 364103216Sjulian td->td_flags &= ~TDF_TIMOFAIL; 36582085Sjhb } 36671088Sjasone 36799072Sjulian if (td->td_proc->p_flag & P_WEXIT) 36899072Sjulian rval = EWOULDBLOCK; 36972200Sbmilekic mtx_unlock_spin(&sched_lock); 37071088Sjasone#ifdef KTRACE 37197995Sjhb if (KTRPOINT(td, KTR_CSW)) 37297995Sjhb ktrcsw(0, 0); 37371088Sjasone#endif 37471088Sjasone PICKUP_GIANT(); 37572200Sbmilekic mtx_lock(mp); 37674912Sjhb WITNESS_RESTORE(&mp->mtx_object, mp); 37771088Sjasone 37871088Sjasone return (rval); 37971088Sjasone} 38071088Sjasone 38171088Sjasone/* 38271088Sjasone * Wait on a condition variable for at most timo/hz seconds, allowing 38383366Sjulian * interruption by signals. Returns 0 if the thread was resumed by cv_signal 38471088Sjasone * or cv_broadcast, EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if 38571088Sjasone * a signal was caught. 38671088Sjasone */ 38771088Sjasoneint 38871088Sjasonecv_timedwait_sig(struct cv *cvp, struct mtx *mp, int timo) 38971088Sjasone{ 39083366Sjulian struct thread *td; 39183650Sjhb struct proc *p; 39271088Sjasone int rval; 39371088Sjasone int sig; 39471088Sjasone WITNESS_SAVE_DECL(mp); 39571088Sjasone 39683366Sjulian td = curthread; 39783650Sjhb p = td->td_proc; 39871088Sjasone rval = 0; 39971088Sjasone#ifdef KTRACE 40097995Sjhb if (KTRPOINT(td, KTR_CSW)) 40197995Sjhb ktrcsw(1, 0); 40271088Sjasone#endif 40383366Sjulian CV_ASSERT(cvp, mp, td); 40474920Sjhb WITNESS_SLEEP(0, &mp->mtx_object); 40574912Sjhb WITNESS_SAVE(&mp->mtx_object, mp); 40671088Sjasone 40771088Sjasone if (cold || panicstr) { 40871088Sjasone /* 40971088Sjasone * After a panic, or during autoconfiguration, just give 41071088Sjasone * interrupts a chance, then just return; don't run any other 41183366Sjulian * thread or panic below, in case this is the idle process and 41271088Sjasone * already asleep. 41371088Sjasone */ 41471088Sjasone return 0; 41571088Sjasone } 41695322Shsu 41795322Shsu mtx_lock_spin(&sched_lock); 41895322Shsu 41971088Sjasone CV_WAIT_VALIDATE(cvp, mp); 42071088Sjasone 42188900Sjhb DROP_GIANT(); 42288900Sjhb mtx_unlock(mp); 42371088Sjasone 42483366Sjulian cv_waitq_add(cvp, td); 42583366Sjulian callout_reset(&td->td_slpcallout, timo, cv_timedwait_end, td); 42683366Sjulian sig = cv_switch_catch(td); 42771088Sjasone 42883366Sjulian if (td->td_flags & TDF_TIMEOUT) { 42983366Sjulian td->td_flags &= ~TDF_TIMEOUT; 43071088Sjasone rval = EWOULDBLOCK; 43183366Sjulian } else if (td->td_flags & TDF_TIMOFAIL) 43283366Sjulian td->td_flags &= ~TDF_TIMOFAIL; 43383366Sjulian else if (callout_stop(&td->td_slpcallout) == 0) { 43482085Sjhb /* 43582085Sjhb * Work around race with cv_timedwait_end similar to that 43682085Sjhb * between msleep and endtsleep. 43799247Sjulian * Go back to sleep. 43882085Sjhb */ 439103216Sjulian TD_SET_SLEEPING(td); 44083366Sjulian td->td_proc->p_stats->p_ru.ru_nivcsw++; 44182085Sjhb mi_switch(); 442103216Sjulian td->td_flags &= ~TDF_TIMOFAIL; 44382085Sjhb } 44472200Sbmilekic mtx_unlock_spin(&sched_lock); 44571088Sjasone 44683650Sjhb PROC_LOCK(p); 44771088Sjasone if (sig == 0) 44899072Sjulian sig = cursig(td); 44971088Sjasone if (sig != 0) { 45083650Sjhb if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig)) 45171088Sjasone rval = EINTR; 45271088Sjasone else 45371088Sjasone rval = ERESTART; 45471088Sjasone } 45583650Sjhb PROC_UNLOCK(p); 45671088Sjasone 45799072Sjulian if (p->p_flag & P_WEXIT) 45899072Sjulian rval = EINTR; 45999072Sjulian 46071088Sjasone#ifdef KTRACE 46197995Sjhb if (KTRPOINT(td, KTR_CSW)) 46297995Sjhb ktrcsw(0, 0); 46371088Sjasone#endif 46497995Sjhb PICKUP_GIANT(); 46572200Sbmilekic mtx_lock(mp); 46674912Sjhb WITNESS_RESTORE(&mp->mtx_object, mp); 46771088Sjasone 46871088Sjasone return (rval); 46971088Sjasone} 47071088Sjasone 47171088Sjasone/* 47271088Sjasone * Common code for signal and broadcast. Assumes waitq is not empty. Must be 47371088Sjasone * called with sched_lock held. 47471088Sjasone */ 47571088Sjasonestatic __inline void 47671088Sjasonecv_wakeup(struct cv *cvp) 47771088Sjasone{ 47883366Sjulian struct thread *td; 47971088Sjasone 48071557Sjhb mtx_assert(&sched_lock, MA_OWNED); 48183366Sjulian td = TAILQ_FIRST(&cvp->cv_waitq); 48287594Sobrien KASSERT(td->td_wchan == cvp, ("%s: bogus wchan", __func__)); 48387594Sobrien KASSERT(td->td_flags & TDF_CVWAITQ, ("%s: not on waitq", __func__)); 484103216Sjulian cv_waitq_remove(td); 485103216Sjulian TD_CLR_SLEEPING(td); 486103216Sjulian setrunnable(td); 48771088Sjasone} 48871088Sjasone 48971088Sjasone/* 49083366Sjulian * Signal a condition variable, wakes up one waiting thread. Will also wakeup 49171088Sjasone * the swapper if the process is not in memory, so that it can bring the 49283366Sjulian * sleeping process in. Note that this may also result in additional threads 49371088Sjasone * being made runnable. Should be called with the same mutex as was passed to 49471088Sjasone * cv_wait held. 49571088Sjasone */ 49671088Sjasonevoid 49771088Sjasonecv_signal(struct cv *cvp) 49871088Sjasone{ 49971088Sjasone 50087594Sobrien KASSERT(cvp != NULL, ("%s: cvp NULL", __func__)); 50172200Sbmilekic mtx_lock_spin(&sched_lock); 50271088Sjasone if (!TAILQ_EMPTY(&cvp->cv_waitq)) { 50371088Sjasone CV_SIGNAL_VALIDATE(cvp); 50471088Sjasone cv_wakeup(cvp); 50571088Sjasone } 50672200Sbmilekic mtx_unlock_spin(&sched_lock); 50771088Sjasone} 50871088Sjasone 50971088Sjasone/* 51083366Sjulian * Broadcast a signal to a condition variable. Wakes up all waiting threads. 51171088Sjasone * Should be called with the same mutex as was passed to cv_wait held. 51271088Sjasone */ 51371088Sjasonevoid 51471088Sjasonecv_broadcast(struct cv *cvp) 51571088Sjasone{ 51671088Sjasone 51787594Sobrien KASSERT(cvp != NULL, ("%s: cvp NULL", __func__)); 51872200Sbmilekic mtx_lock_spin(&sched_lock); 51971088Sjasone CV_SIGNAL_VALIDATE(cvp); 52071088Sjasone while (!TAILQ_EMPTY(&cvp->cv_waitq)) 52171088Sjasone cv_wakeup(cvp); 52272200Sbmilekic mtx_unlock_spin(&sched_lock); 52371088Sjasone} 52471088Sjasone 52571088Sjasone/* 52683366Sjulian * Remove a thread from the wait queue of its condition variable. This may be 52771088Sjasone * called externally. 52871088Sjasone */ 52971088Sjasonevoid 53083366Sjuliancv_waitq_remove(struct thread *td) 53171088Sjasone{ 53271088Sjasone struct cv *cvp; 53371088Sjasone 534103216Sjulian mtx_assert(&sched_lock, MA_OWNED); 53583366Sjulian if ((cvp = td->td_wchan) != NULL && td->td_flags & TDF_CVWAITQ) { 53683366Sjulian TAILQ_REMOVE(&cvp->cv_waitq, td, td_slpq); 53783366Sjulian td->td_flags &= ~TDF_CVWAITQ; 538103216Sjulian TD_CLR_ON_SLEEPQ(td); 53971088Sjasone } 54071088Sjasone} 54171088Sjasone 54271088Sjasone/* 54383366Sjulian * Timeout function for cv_timedwait. Put the thread on the runqueue and set 54471088Sjasone * its timeout flag. 54571088Sjasone */ 54671088Sjasonestatic void 54771088Sjasonecv_timedwait_end(void *arg) 54871088Sjasone{ 54983366Sjulian struct thread *td; 55071088Sjasone 55183366Sjulian td = arg; 552103216Sjulian CTR3(KTR_PROC, "cv_timedwait_end: thread %p (pid %d, %s)", 553103216Sjulian td, td->td_proc->p_pid, td->td_proc->p_comm); 55472200Sbmilekic mtx_lock_spin(&sched_lock); 555103216Sjulian if (TD_ON_SLEEPQ(td)) { 556103216Sjulian cv_waitq_remove(td); 55783366Sjulian td->td_flags |= TDF_TIMEOUT; 558103216Sjulian } else { 55983366Sjulian td->td_flags |= TDF_TIMOFAIL; 560103216Sjulian } 561103216Sjulian TD_CLR_SLEEPING(td); 562103216Sjulian setrunnable(td); 56372200Sbmilekic mtx_unlock_spin(&sched_lock); 56471088Sjasone} 56599072Sjulian 56699072Sjulian/* 56799072Sjulian * For now only abort interruptable waits. 56899072Sjulian * The others will have to either complete on their own or have a timeout. 56999072Sjulian */ 57099072Sjulianvoid 57199072Sjuliancv_abort(struct thread *td) 57299072Sjulian{ 57399072Sjulian 57499072Sjulian CTR3(KTR_PROC, "cv_abort: thread %p (pid %d, %s)", td, 575103216Sjulian td->td_proc->p_pid, td->td_proc->p_comm); 57699072Sjulian mtx_lock_spin(&sched_lock); 57799072Sjulian if ((td->td_flags & (TDF_SINTR|TDF_TIMEOUT)) == TDF_SINTR) { 578103216Sjulian if (TD_ON_SLEEPQ(td)) { 579103216Sjulian cv_waitq_remove(td); 58099072Sjulian } 581103216Sjulian TD_CLR_SLEEPING(td); 582103216Sjulian setrunnable(td); 58399072Sjulian } 58499072Sjulian mtx_unlock_spin(&sched_lock); 58599072Sjulian} 58699072Sjulian 587