1238510fcSJason Evans /*- 2238510fcSJason Evans * Copyright (c) 2000 Jake Burkholder <jake@freebsd.org>. 3238510fcSJason Evans * All rights reserved. 4238510fcSJason Evans * 5238510fcSJason Evans * Redistribution and use in source and binary forms, with or without 6238510fcSJason Evans * modification, are permitted provided that the following conditions 7238510fcSJason Evans * are met: 8238510fcSJason Evans * 1. Redistributions of source code must retain the above copyright 9238510fcSJason Evans * notice, this list of conditions and the following disclaimer. 10238510fcSJason Evans * 2. Redistributions in binary form must reproduce the above copyright 11238510fcSJason Evans * notice, this list of conditions and the following disclaimer in the 12238510fcSJason Evans * documentation and/or other materials provided with the distribution. 13238510fcSJason Evans * 14238510fcSJason Evans * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15238510fcSJason Evans * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16238510fcSJason Evans * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17238510fcSJason Evans * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18238510fcSJason Evans * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19238510fcSJason Evans * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20238510fcSJason Evans * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21238510fcSJason Evans * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22238510fcSJason Evans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23238510fcSJason Evans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24238510fcSJason Evans * SUCH DAMAGE. 25238510fcSJason Evans */ 26238510fcSJason Evans 27677b542eSDavid E. O'Brien #include <sys/cdefs.h> 28677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 29677b542eSDavid E. O'Brien 30238510fcSJason Evans #include "opt_ktrace.h" 31238510fcSJason Evans 32238510fcSJason Evans #include <sys/param.h> 33238510fcSJason Evans #include <sys/systm.h> 34fb919e4dSMark Murray #include <sys/lock.h> 35fb919e4dSMark Murray #include <sys/mutex.h> 36238510fcSJason Evans #include <sys/proc.h> 37238510fcSJason Evans #include <sys/kernel.h> 38238510fcSJason Evans #include <sys/ktr.h> 39238510fcSJason Evans #include <sys/condvar.h> 404e997f4bSJeff Roberson #include <sys/sched.h> 41238510fcSJason Evans #include <sys/signalvar.h> 4244f3b092SJohn Baldwin #include <sys/sleepqueue.h> 43238510fcSJason Evans #include <sys/resourcevar.h> 44238510fcSJason Evans #ifdef KTRACE 45238510fcSJason Evans #include <sys/uio.h> 46238510fcSJason Evans #include <sys/ktrace.h> 47238510fcSJason Evans #endif 48238510fcSJason Evans 49238510fcSJason Evans /* 50238510fcSJason Evans * Common sanity checks for cv_wait* functions. 51238510fcSJason Evans */ 528f27b08eSJohn Baldwin #define CV_ASSERT(cvp, lock, td) do { \ 53a48740b6SDavid E. O'Brien KASSERT((td) != NULL, ("%s: curthread NULL", __func__)); \ 5471fad9fdSJulian Elischer KASSERT(TD_IS_RUNNING(td), ("%s: not TDS_RUNNING", __func__)); \ 55a48740b6SDavid E. O'Brien KASSERT((cvp) != NULL, ("%s: cvp NULL", __func__)); \ 568f27b08eSJohn Baldwin KASSERT((lock) != NULL, ("%s: lock NULL", __func__)); \ 57238510fcSJason Evans } while (0) 58238510fcSJason Evans 59238510fcSJason Evans /* 60238510fcSJason Evans * Initialize a condition variable. Must be called before use. 61238510fcSJason Evans */ 62238510fcSJason Evans void 63238510fcSJason Evans cv_init(struct cv *cvp, const char *desc) 64238510fcSJason Evans { 65238510fcSJason Evans 66238510fcSJason Evans cvp->cv_description = desc; 679000d57dSJohn Baldwin cvp->cv_waiters = 0; 68238510fcSJason Evans } 69238510fcSJason Evans 70238510fcSJason Evans /* 71238510fcSJason Evans * Destroy a condition variable. The condition variable must be re-initialized 72238510fcSJason Evans * in order to be re-used. 73238510fcSJason Evans */ 74238510fcSJason Evans void 75238510fcSJason Evans cv_destroy(struct cv *cvp) 76238510fcSJason Evans { 7744f3b092SJohn Baldwin #ifdef INVARIANTS 7844f3b092SJohn Baldwin struct sleepqueue *sq; 79238510fcSJason Evans 802ff0e645SJohn Baldwin sleepq_lock(cvp); 8144f3b092SJohn Baldwin sq = sleepq_lookup(cvp); 8244f3b092SJohn Baldwin sleepq_release(cvp); 8344f3b092SJohn Baldwin KASSERT(sq == NULL, ("%s: associated sleep queue non-empty", __func__)); 8444f3b092SJohn Baldwin #endif 85238510fcSJason Evans } 86238510fcSJason Evans 87238510fcSJason Evans /* 88b40ce416SJulian Elischer * Wait on a condition variable. The current thread is placed on the condition 89238510fcSJason Evans * variable's wait queue and suspended. A cv_signal or cv_broadcast on the same 90b40ce416SJulian Elischer * condition variable will resume the thread. The mutex is released before 91238510fcSJason Evans * sleeping and will be held on return. It is recommended that the mutex be 92238510fcSJason Evans * held when cv_signal or cv_broadcast are called. 93238510fcSJason Evans */ 94238510fcSJason Evans void 958f27b08eSJohn Baldwin _cv_wait(struct cv *cvp, struct lock_object *lock) 96238510fcSJason Evans { 978f27b08eSJohn Baldwin WITNESS_SAVE_DECL(lock_witness); 988f27b08eSJohn Baldwin struct lock_class *class; 99503916a7SJohn Baldwin struct thread *td; 1008f27b08eSJohn Baldwin int lock_state; 101238510fcSJason Evans 102503916a7SJohn Baldwin td = curthread; 103414e7679SJohn Baldwin lock_state = 0; 104503916a7SJohn Baldwin #ifdef KTRACE 105503916a7SJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 106503916a7SJohn Baldwin ktrcsw(1, 0); 107503916a7SJohn Baldwin #endif 1088f27b08eSJohn Baldwin CV_ASSERT(cvp, lock, td); 1098f27b08eSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock, 110503916a7SJohn Baldwin "Waiting on \"%s\"", cvp->cv_description); 1118f27b08eSJohn Baldwin WITNESS_SAVE(lock, lock_witness); 1128f27b08eSJohn Baldwin class = LOCK_CLASS(lock); 113238510fcSJason Evans 11444f3b092SJohn Baldwin if (cold || panicstr) { 115238510fcSJason Evans /* 116fe799533SAndrew Gallatin * During autoconfiguration, just give interrupts 117fe799533SAndrew Gallatin * a chance, then just return. Don't run any other 118fe799533SAndrew Gallatin * thread or panic below, in case this is the idle 119fe799533SAndrew Gallatin * process and already asleep. 120238510fcSJason Evans */ 121238510fcSJason Evans return; 122238510fcSJason Evans } 1234bc37205SJeffrey Hsu 124503916a7SJohn Baldwin sleepq_lock(cvp); 125503916a7SJohn Baldwin 126503916a7SJohn Baldwin cvp->cv_waiters++; 127503916a7SJohn Baldwin DROP_GIANT(); 128503916a7SJohn Baldwin 1298f27b08eSJohn Baldwin sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0); 130414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 1319fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 1329fa7ce0fSJohn Baldwin sleepq_release(cvp); 1339fa7ce0fSJohn Baldwin lock_state = class->lc_unlock(lock); 1349fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 1359fa7ce0fSJohn Baldwin sleepq_lock(cvp); 136414e7679SJohn Baldwin } 137c5aa6b58SJeff Roberson sleepq_wait(cvp, 0); 138503916a7SJohn Baldwin 139503916a7SJohn Baldwin #ifdef KTRACE 140503916a7SJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 141503916a7SJohn Baldwin ktrcsw(0, 0); 142503916a7SJohn Baldwin #endif 143503916a7SJohn Baldwin PICKUP_GIANT(); 144414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 1458f27b08eSJohn Baldwin class->lc_lock(lock, lock_state); 1468f27b08eSJohn Baldwin WITNESS_RESTORE(lock, lock_witness); 14792f44a3fSCraig Rodrigues } 148414e7679SJohn Baldwin } 14992f44a3fSCraig Rodrigues 15092f44a3fSCraig Rodrigues /* 15192f44a3fSCraig Rodrigues * Wait on a condition variable. This function differs from cv_wait by 15292f44a3fSCraig Rodrigues * not aquiring the mutex after condition variable was signaled. 15392f44a3fSCraig Rodrigues */ 15492f44a3fSCraig Rodrigues void 1558f27b08eSJohn Baldwin _cv_wait_unlock(struct cv *cvp, struct lock_object *lock) 15692f44a3fSCraig Rodrigues { 1578f27b08eSJohn Baldwin struct lock_class *class; 15892f44a3fSCraig Rodrigues struct thread *td; 15992f44a3fSCraig Rodrigues 16092f44a3fSCraig Rodrigues td = curthread; 16192f44a3fSCraig Rodrigues #ifdef KTRACE 16292f44a3fSCraig Rodrigues if (KTRPOINT(td, KTR_CSW)) 16392f44a3fSCraig Rodrigues ktrcsw(1, 0); 16492f44a3fSCraig Rodrigues #endif 1658f27b08eSJohn Baldwin CV_ASSERT(cvp, lock, td); 1668f27b08eSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock, 16792f44a3fSCraig Rodrigues "Waiting on \"%s\"", cvp->cv_description); 168414e7679SJohn Baldwin KASSERT(lock != &Giant.lock_object, 169414e7679SJohn Baldwin ("cv_wait_unlock cannot be used with Giant")); 1708f27b08eSJohn Baldwin class = LOCK_CLASS(lock); 17192f44a3fSCraig Rodrigues 17292f44a3fSCraig Rodrigues if (cold || panicstr) { 17392f44a3fSCraig Rodrigues /* 17492f44a3fSCraig Rodrigues * During autoconfiguration, just give interrupts 17592f44a3fSCraig Rodrigues * a chance, then just return. Don't run any other 17692f44a3fSCraig Rodrigues * thread or panic below, in case this is the idle 17792f44a3fSCraig Rodrigues * process and already asleep. 17892f44a3fSCraig Rodrigues */ 1798f27b08eSJohn Baldwin class->lc_unlock(lock); 18092f44a3fSCraig Rodrigues return; 18192f44a3fSCraig Rodrigues } 18292f44a3fSCraig Rodrigues 1832ff0e645SJohn Baldwin sleepq_lock(cvp); 184238510fcSJason Evans 1859000d57dSJohn Baldwin cvp->cv_waiters++; 186c86b6ff5SJohn Baldwin DROP_GIANT(); 187238510fcSJason Evans 1888f27b08eSJohn Baldwin sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0); 1899fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 1909fa7ce0fSJohn Baldwin sleepq_release(cvp); 1919fa7ce0fSJohn Baldwin class->lc_unlock(lock); 1929fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 1939fa7ce0fSJohn Baldwin sleepq_lock(cvp); 194c5aa6b58SJeff Roberson sleepq_wait(cvp, 0); 195238510fcSJason Evans 196503916a7SJohn Baldwin #ifdef KTRACE 197503916a7SJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 198503916a7SJohn Baldwin ktrcsw(0, 0); 199503916a7SJohn Baldwin #endif 200238510fcSJason Evans PICKUP_GIANT(); 201238510fcSJason Evans } 202238510fcSJason Evans 203238510fcSJason Evans /* 204238510fcSJason Evans * Wait on a condition variable, allowing interruption by signals. Return 0 if 205b40ce416SJulian Elischer * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if 206238510fcSJason Evans * a signal was caught. If ERESTART is returned the system call should be 207238510fcSJason Evans * restarted if possible. 208238510fcSJason Evans */ 209238510fcSJason Evans int 2108f27b08eSJohn Baldwin _cv_wait_sig(struct cv *cvp, struct lock_object *lock) 211238510fcSJason Evans { 2128f27b08eSJohn Baldwin WITNESS_SAVE_DECL(lock_witness); 2138f27b08eSJohn Baldwin struct lock_class *class; 214b40ce416SJulian Elischer struct thread *td; 21566f769feSPeter Wemm struct proc *p; 2168f27b08eSJohn Baldwin int lock_state, rval; 217238510fcSJason Evans 218b40ce416SJulian Elischer td = curthread; 2199ef3a985SJohn Baldwin p = td->td_proc; 220414e7679SJohn Baldwin lock_state = 0; 221238510fcSJason Evans #ifdef KTRACE 2229ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 2239ba7fe1bSJohn Baldwin ktrcsw(1, 0); 224238510fcSJason Evans #endif 2258f27b08eSJohn Baldwin CV_ASSERT(cvp, lock, td); 2268f27b08eSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock, 22726306795SJohn Baldwin "Waiting on \"%s\"", cvp->cv_description); 2288f27b08eSJohn Baldwin WITNESS_SAVE(lock, lock_witness); 2298f27b08eSJohn Baldwin class = LOCK_CLASS(lock); 230238510fcSJason Evans 231238510fcSJason Evans if (cold || panicstr) { 232238510fcSJason Evans /* 233238510fcSJason Evans * After a panic, or during autoconfiguration, just give 234238510fcSJason Evans * interrupts a chance, then just return; don't run any other 235238510fcSJason Evans * procs or panic below, in case this is the idle process and 236238510fcSJason Evans * already asleep. 237238510fcSJason Evans */ 238274f8f48SJohn Baldwin return (0); 239238510fcSJason Evans } 2404bc37205SJeffrey Hsu 2412ff0e645SJohn Baldwin sleepq_lock(cvp); 2424bc37205SJeffrey Hsu 2439000d57dSJohn Baldwin cvp->cv_waiters++; 244c86b6ff5SJohn Baldwin DROP_GIANT(); 245238510fcSJason Evans 2468f27b08eSJohn Baldwin sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR | 2476cbb70e2SKip Macy SLEEPQ_INTERRUPTIBLE, 0); 248414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 2499fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 2509fa7ce0fSJohn Baldwin sleepq_release(cvp); 2519fa7ce0fSJohn Baldwin lock_state = class->lc_unlock(lock); 2529fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 2539fa7ce0fSJohn Baldwin sleepq_lock(cvp); 254414e7679SJohn Baldwin } 255c5aa6b58SJeff Roberson rval = sleepq_wait_sig(cvp, 0); 256238510fcSJason Evans 257238510fcSJason Evans #ifdef KTRACE 2589ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 2599ba7fe1bSJohn Baldwin ktrcsw(0, 0); 260238510fcSJason Evans #endif 2619ba7fe1bSJohn Baldwin PICKUP_GIANT(); 262414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 2638f27b08eSJohn Baldwin class->lc_lock(lock, lock_state); 2648f27b08eSJohn Baldwin WITNESS_RESTORE(lock, lock_witness); 265414e7679SJohn Baldwin } 266238510fcSJason Evans 267238510fcSJason Evans return (rval); 268238510fcSJason Evans } 269238510fcSJason Evans 270238510fcSJason Evans /* 271238510fcSJason Evans * Wait on a condition variable for at most timo/hz seconds. Returns 0 if the 272238510fcSJason Evans * process was resumed by cv_signal or cv_broadcast, EWOULDBLOCK if the timeout 273238510fcSJason Evans * expires. 274238510fcSJason Evans */ 275238510fcSJason Evans int 2768f27b08eSJohn Baldwin _cv_timedwait(struct cv *cvp, struct lock_object *lock, int timo) 277238510fcSJason Evans { 2788f27b08eSJohn Baldwin WITNESS_SAVE_DECL(lock_witness); 2798f27b08eSJohn Baldwin struct lock_class *class; 280b40ce416SJulian Elischer struct thread *td; 2818f27b08eSJohn Baldwin int lock_state, rval; 282238510fcSJason Evans 283b40ce416SJulian Elischer td = curthread; 284238510fcSJason Evans rval = 0; 285414e7679SJohn Baldwin lock_state = 0; 286238510fcSJason Evans #ifdef KTRACE 2879ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 2889ba7fe1bSJohn Baldwin ktrcsw(1, 0); 289238510fcSJason Evans #endif 2908f27b08eSJohn Baldwin CV_ASSERT(cvp, lock, td); 2918f27b08eSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock, 29226306795SJohn Baldwin "Waiting on \"%s\"", cvp->cv_description); 2938f27b08eSJohn Baldwin WITNESS_SAVE(lock, lock_witness); 2948f27b08eSJohn Baldwin class = LOCK_CLASS(lock); 295238510fcSJason Evans 296238510fcSJason Evans if (cold || panicstr) { 297238510fcSJason Evans /* 298238510fcSJason Evans * After a panic, or during autoconfiguration, just give 299238510fcSJason Evans * interrupts a chance, then just return; don't run any other 300b40ce416SJulian Elischer * thread or panic below, in case this is the idle process and 301238510fcSJason Evans * already asleep. 302238510fcSJason Evans */ 303238510fcSJason Evans return 0; 304238510fcSJason Evans } 3054bc37205SJeffrey Hsu 3062ff0e645SJohn Baldwin sleepq_lock(cvp); 307238510fcSJason Evans 3089000d57dSJohn Baldwin cvp->cv_waiters++; 309c86b6ff5SJohn Baldwin DROP_GIANT(); 310238510fcSJason Evans 3118f27b08eSJohn Baldwin sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0); 3121ed3e44fSJohn Baldwin sleepq_set_timeout(cvp, timo); 313414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 3149fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 3159fa7ce0fSJohn Baldwin sleepq_release(cvp); 3169fa7ce0fSJohn Baldwin lock_state = class->lc_unlock(lock); 3179fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 3189fa7ce0fSJohn Baldwin sleepq_lock(cvp); 319414e7679SJohn Baldwin } 320c5aa6b58SJeff Roberson rval = sleepq_timedwait(cvp, 0); 321238510fcSJason Evans 322238510fcSJason Evans #ifdef KTRACE 3239ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 3249ba7fe1bSJohn Baldwin ktrcsw(0, 0); 325238510fcSJason Evans #endif 326238510fcSJason Evans PICKUP_GIANT(); 327414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 3288f27b08eSJohn Baldwin class->lc_lock(lock, lock_state); 3298f27b08eSJohn Baldwin WITNESS_RESTORE(lock, lock_witness); 330414e7679SJohn Baldwin } 331238510fcSJason Evans 332238510fcSJason Evans return (rval); 333238510fcSJason Evans } 334238510fcSJason Evans 335238510fcSJason Evans /* 336238510fcSJason Evans * Wait on a condition variable for at most timo/hz seconds, allowing 337b40ce416SJulian Elischer * interruption by signals. Returns 0 if the thread was resumed by cv_signal 338238510fcSJason Evans * or cv_broadcast, EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if 339238510fcSJason Evans * a signal was caught. 340238510fcSJason Evans */ 341238510fcSJason Evans int 3428f27b08eSJohn Baldwin _cv_timedwait_sig(struct cv *cvp, struct lock_object *lock, int timo) 343238510fcSJason Evans { 3448f27b08eSJohn Baldwin WITNESS_SAVE_DECL(lock_witness); 3458f27b08eSJohn Baldwin struct lock_class *class; 346b40ce416SJulian Elischer struct thread *td; 3479ef3a985SJohn Baldwin struct proc *p; 3488f27b08eSJohn Baldwin int lock_state, rval; 349238510fcSJason Evans 350b40ce416SJulian Elischer td = curthread; 3519ef3a985SJohn Baldwin p = td->td_proc; 352238510fcSJason Evans rval = 0; 353414e7679SJohn Baldwin lock_state = 0; 354238510fcSJason Evans #ifdef KTRACE 3559ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 3569ba7fe1bSJohn Baldwin ktrcsw(1, 0); 357238510fcSJason Evans #endif 3588f27b08eSJohn Baldwin CV_ASSERT(cvp, lock, td); 3598f27b08eSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock, 36026306795SJohn Baldwin "Waiting on \"%s\"", cvp->cv_description); 3618f27b08eSJohn Baldwin WITNESS_SAVE(lock, lock_witness); 3628f27b08eSJohn Baldwin class = LOCK_CLASS(lock); 363238510fcSJason Evans 364238510fcSJason Evans if (cold || panicstr) { 365238510fcSJason Evans /* 366238510fcSJason Evans * After a panic, or during autoconfiguration, just give 367238510fcSJason Evans * interrupts a chance, then just return; don't run any other 368b40ce416SJulian Elischer * thread or panic below, in case this is the idle process and 369238510fcSJason Evans * already asleep. 370238510fcSJason Evans */ 371238510fcSJason Evans return 0; 372238510fcSJason Evans } 3734bc37205SJeffrey Hsu 3742ff0e645SJohn Baldwin sleepq_lock(cvp); 375238510fcSJason Evans 3769000d57dSJohn Baldwin cvp->cv_waiters++; 377c86b6ff5SJohn Baldwin DROP_GIANT(); 378238510fcSJason Evans 3798f27b08eSJohn Baldwin sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR | 3806cbb70e2SKip Macy SLEEPQ_INTERRUPTIBLE, 0); 3811ed3e44fSJohn Baldwin sleepq_set_timeout(cvp, timo); 382414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 3839fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 3849fa7ce0fSJohn Baldwin sleepq_release(cvp); 3859fa7ce0fSJohn Baldwin lock_state = class->lc_unlock(lock); 3869fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 3879fa7ce0fSJohn Baldwin sleepq_lock(cvp); 388414e7679SJohn Baldwin } 389c5aa6b58SJeff Roberson rval = sleepq_timedwait_sig(cvp, 0); 390238510fcSJason Evans 391238510fcSJason Evans #ifdef KTRACE 3929ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 3939ba7fe1bSJohn Baldwin ktrcsw(0, 0); 394238510fcSJason Evans #endif 3959ba7fe1bSJohn Baldwin PICKUP_GIANT(); 396414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 3978f27b08eSJohn Baldwin class->lc_lock(lock, lock_state); 3988f27b08eSJohn Baldwin WITNESS_RESTORE(lock, lock_witness); 399414e7679SJohn Baldwin } 400238510fcSJason Evans 401238510fcSJason Evans return (rval); 402238510fcSJason Evans } 403238510fcSJason Evans 404238510fcSJason Evans /* 405b40ce416SJulian Elischer * Signal a condition variable, wakes up one waiting thread. Will also wakeup 406238510fcSJason Evans * the swapper if the process is not in memory, so that it can bring the 407b40ce416SJulian Elischer * sleeping process in. Note that this may also result in additional threads 408238510fcSJason Evans * being made runnable. Should be called with the same mutex as was passed to 409238510fcSJason Evans * cv_wait held. 410238510fcSJason Evans */ 411238510fcSJason Evans void 412238510fcSJason Evans cv_signal(struct cv *cvp) 413238510fcSJason Evans { 414da7bbd2cSJohn Baldwin int wakeup_swapper; 415238510fcSJason Evans 416da7bbd2cSJohn Baldwin wakeup_swapper = 0; 4172ff0e645SJohn Baldwin sleepq_lock(cvp); 4189000d57dSJohn Baldwin if (cvp->cv_waiters > 0) { 4199000d57dSJohn Baldwin cvp->cv_waiters--; 420da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_signal(cvp, SLEEPQ_CONDVAR, 0, 0); 421d72e80f0SJeff Roberson } 4222ff0e645SJohn Baldwin sleepq_release(cvp); 423da7bbd2cSJohn Baldwin if (wakeup_swapper) 424da7bbd2cSJohn Baldwin kick_proc0(); 4259000d57dSJohn Baldwin } 426238510fcSJason Evans 427238510fcSJason Evans /* 428b40ce416SJulian Elischer * Broadcast a signal to a condition variable. Wakes up all waiting threads. 429238510fcSJason Evans * Should be called with the same mutex as was passed to cv_wait held. 430238510fcSJason Evans */ 431238510fcSJason Evans void 432512824f8SSeigo Tanimura cv_broadcastpri(struct cv *cvp, int pri) 433238510fcSJason Evans { 434da7bbd2cSJohn Baldwin int wakeup_swapper; 435da7bbd2cSJohn Baldwin 436c5aa6b58SJeff Roberson /* 437c5aa6b58SJeff Roberson * XXX sleepq_broadcast pri argument changed from -1 meaning 438c5aa6b58SJeff Roberson * no pri to 0 meaning no pri. 439c5aa6b58SJeff Roberson */ 440da7bbd2cSJohn Baldwin wakeup_swapper = 0; 441c5aa6b58SJeff Roberson if (pri == -1) 442c5aa6b58SJeff Roberson pri = 0; 4432ff0e645SJohn Baldwin sleepq_lock(cvp); 4449000d57dSJohn Baldwin if (cvp->cv_waiters > 0) { 4459000d57dSJohn Baldwin cvp->cv_waiters = 0; 446da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri, 0); 447c5aa6b58SJeff Roberson } 4482ff0e645SJohn Baldwin sleepq_release(cvp); 449da7bbd2cSJohn Baldwin if (wakeup_swapper) 450da7bbd2cSJohn Baldwin kick_proc0(); 4519000d57dSJohn Baldwin } 452