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 { \ 53*0a15e5d3SAttilio Rao KASSERT((td) != NULL, ("%s: td 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)) 10688bf5036SJohn Baldwin ktrcsw(1, 0, cv_wmesg(cvp)); 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 class = LOCK_CLASS(lock); 112238510fcSJason Evans 11344f3b092SJohn Baldwin if (cold || panicstr) { 114238510fcSJason Evans /* 115fe799533SAndrew Gallatin * During autoconfiguration, just give interrupts 116fe799533SAndrew Gallatin * a chance, then just return. Don't run any other 117fe799533SAndrew Gallatin * thread or panic below, in case this is the idle 118fe799533SAndrew Gallatin * process and already asleep. 119238510fcSJason Evans */ 120238510fcSJason Evans return; 121238510fcSJason Evans } 1224bc37205SJeffrey Hsu 123503916a7SJohn Baldwin sleepq_lock(cvp); 124503916a7SJohn Baldwin 125503916a7SJohn Baldwin cvp->cv_waiters++; 1267d43ca69SJohn Baldwin if (lock == &Giant.lock_object) 1277d43ca69SJohn Baldwin mtx_assert(&Giant, MA_OWNED); 128503916a7SJohn Baldwin DROP_GIANT(); 129503916a7SJohn Baldwin 1308f27b08eSJohn Baldwin sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0); 131414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 1329fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 1339fa7ce0fSJohn Baldwin sleepq_release(cvp); 1347d43ca69SJohn Baldwin WITNESS_SAVE(lock, lock_witness); 1359fa7ce0fSJohn Baldwin lock_state = class->lc_unlock(lock); 1369fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 1379fa7ce0fSJohn Baldwin sleepq_lock(cvp); 138414e7679SJohn Baldwin } 139c5aa6b58SJeff Roberson sleepq_wait(cvp, 0); 140503916a7SJohn Baldwin 141503916a7SJohn Baldwin #ifdef KTRACE 142503916a7SJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 14388bf5036SJohn Baldwin ktrcsw(0, 0, cv_wmesg(cvp)); 144503916a7SJohn Baldwin #endif 145503916a7SJohn Baldwin PICKUP_GIANT(); 146414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 1478f27b08eSJohn Baldwin class->lc_lock(lock, lock_state); 1488f27b08eSJohn Baldwin WITNESS_RESTORE(lock, lock_witness); 14992f44a3fSCraig Rodrigues } 150414e7679SJohn Baldwin } 15192f44a3fSCraig Rodrigues 15292f44a3fSCraig Rodrigues /* 15392f44a3fSCraig Rodrigues * Wait on a condition variable. This function differs from cv_wait by 15492f44a3fSCraig Rodrigues * not aquiring the mutex after condition variable was signaled. 15592f44a3fSCraig Rodrigues */ 15692f44a3fSCraig Rodrigues void 1578f27b08eSJohn Baldwin _cv_wait_unlock(struct cv *cvp, struct lock_object *lock) 15892f44a3fSCraig Rodrigues { 1598f27b08eSJohn Baldwin struct lock_class *class; 16092f44a3fSCraig Rodrigues struct thread *td; 16192f44a3fSCraig Rodrigues 16292f44a3fSCraig Rodrigues td = curthread; 16392f44a3fSCraig Rodrigues #ifdef KTRACE 16492f44a3fSCraig Rodrigues if (KTRPOINT(td, KTR_CSW)) 16588bf5036SJohn Baldwin ktrcsw(1, 0, cv_wmesg(cvp)); 16692f44a3fSCraig Rodrigues #endif 1678f27b08eSJohn Baldwin CV_ASSERT(cvp, lock, td); 1688f27b08eSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock, 16992f44a3fSCraig Rodrigues "Waiting on \"%s\"", cvp->cv_description); 170414e7679SJohn Baldwin KASSERT(lock != &Giant.lock_object, 171414e7679SJohn Baldwin ("cv_wait_unlock cannot be used with Giant")); 1728f27b08eSJohn Baldwin class = LOCK_CLASS(lock); 17392f44a3fSCraig Rodrigues 17492f44a3fSCraig Rodrigues if (cold || panicstr) { 17592f44a3fSCraig Rodrigues /* 17692f44a3fSCraig Rodrigues * During autoconfiguration, just give interrupts 17792f44a3fSCraig Rodrigues * a chance, then just return. Don't run any other 17892f44a3fSCraig Rodrigues * thread or panic below, in case this is the idle 17992f44a3fSCraig Rodrigues * process and already asleep. 18092f44a3fSCraig Rodrigues */ 1818f27b08eSJohn Baldwin class->lc_unlock(lock); 18292f44a3fSCraig Rodrigues return; 18392f44a3fSCraig Rodrigues } 18492f44a3fSCraig Rodrigues 1852ff0e645SJohn Baldwin sleepq_lock(cvp); 186238510fcSJason Evans 1879000d57dSJohn Baldwin cvp->cv_waiters++; 188c86b6ff5SJohn Baldwin DROP_GIANT(); 189238510fcSJason Evans 1908f27b08eSJohn Baldwin sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0); 1919fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 1929fa7ce0fSJohn Baldwin sleepq_release(cvp); 1939fa7ce0fSJohn Baldwin class->lc_unlock(lock); 1949fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 1959fa7ce0fSJohn Baldwin sleepq_lock(cvp); 196c5aa6b58SJeff Roberson sleepq_wait(cvp, 0); 197238510fcSJason Evans 198503916a7SJohn Baldwin #ifdef KTRACE 199503916a7SJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 20088bf5036SJohn Baldwin ktrcsw(0, 0, cv_wmesg(cvp)); 201503916a7SJohn Baldwin #endif 202238510fcSJason Evans PICKUP_GIANT(); 203238510fcSJason Evans } 204238510fcSJason Evans 205238510fcSJason Evans /* 206238510fcSJason Evans * Wait on a condition variable, allowing interruption by signals. Return 0 if 207b40ce416SJulian Elischer * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if 208238510fcSJason Evans * a signal was caught. If ERESTART is returned the system call should be 209238510fcSJason Evans * restarted if possible. 210238510fcSJason Evans */ 211238510fcSJason Evans int 2128f27b08eSJohn Baldwin _cv_wait_sig(struct cv *cvp, struct lock_object *lock) 213238510fcSJason Evans { 2148f27b08eSJohn Baldwin WITNESS_SAVE_DECL(lock_witness); 2158f27b08eSJohn Baldwin struct lock_class *class; 216b40ce416SJulian Elischer struct thread *td; 2178f27b08eSJohn Baldwin int lock_state, rval; 218238510fcSJason Evans 219b40ce416SJulian Elischer td = curthread; 220414e7679SJohn Baldwin lock_state = 0; 221238510fcSJason Evans #ifdef KTRACE 2229ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 22388bf5036SJohn Baldwin ktrcsw(1, 0, cv_wmesg(cvp)); 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 class = LOCK_CLASS(lock); 229238510fcSJason Evans 230238510fcSJason Evans if (cold || panicstr) { 231238510fcSJason Evans /* 232238510fcSJason Evans * After a panic, or during autoconfiguration, just give 233238510fcSJason Evans * interrupts a chance, then just return; don't run any other 234238510fcSJason Evans * procs or panic below, in case this is the idle process and 235238510fcSJason Evans * already asleep. 236238510fcSJason Evans */ 237274f8f48SJohn Baldwin return (0); 238238510fcSJason Evans } 2394bc37205SJeffrey Hsu 2402ff0e645SJohn Baldwin sleepq_lock(cvp); 2414bc37205SJeffrey Hsu 2429000d57dSJohn Baldwin cvp->cv_waiters++; 2437d43ca69SJohn Baldwin if (lock == &Giant.lock_object) 2447d43ca69SJohn Baldwin mtx_assert(&Giant, MA_OWNED); 245c86b6ff5SJohn Baldwin DROP_GIANT(); 246238510fcSJason Evans 2478f27b08eSJohn Baldwin sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR | 2486cbb70e2SKip Macy SLEEPQ_INTERRUPTIBLE, 0); 249414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 2509fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 2519fa7ce0fSJohn Baldwin sleepq_release(cvp); 2527d43ca69SJohn Baldwin WITNESS_SAVE(lock, lock_witness); 2539fa7ce0fSJohn Baldwin lock_state = class->lc_unlock(lock); 2549fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 2559fa7ce0fSJohn Baldwin sleepq_lock(cvp); 256414e7679SJohn Baldwin } 257c5aa6b58SJeff Roberson rval = sleepq_wait_sig(cvp, 0); 258238510fcSJason Evans 259238510fcSJason Evans #ifdef KTRACE 2609ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 26188bf5036SJohn Baldwin ktrcsw(0, 0, cv_wmesg(cvp)); 262238510fcSJason Evans #endif 2639ba7fe1bSJohn Baldwin PICKUP_GIANT(); 264414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 2658f27b08eSJohn Baldwin class->lc_lock(lock, lock_state); 2668f27b08eSJohn Baldwin WITNESS_RESTORE(lock, lock_witness); 267414e7679SJohn Baldwin } 268238510fcSJason Evans 269238510fcSJason Evans return (rval); 270238510fcSJason Evans } 271238510fcSJason Evans 272238510fcSJason Evans /* 273238510fcSJason Evans * Wait on a condition variable for at most timo/hz seconds. Returns 0 if the 274238510fcSJason Evans * process was resumed by cv_signal or cv_broadcast, EWOULDBLOCK if the timeout 275238510fcSJason Evans * expires. 276238510fcSJason Evans */ 277238510fcSJason Evans int 2788f27b08eSJohn Baldwin _cv_timedwait(struct cv *cvp, struct lock_object *lock, int timo) 279238510fcSJason Evans { 2808f27b08eSJohn Baldwin WITNESS_SAVE_DECL(lock_witness); 2818f27b08eSJohn Baldwin struct lock_class *class; 282b40ce416SJulian Elischer struct thread *td; 2838f27b08eSJohn Baldwin int lock_state, rval; 284238510fcSJason Evans 285b40ce416SJulian Elischer td = curthread; 286414e7679SJohn Baldwin lock_state = 0; 287238510fcSJason Evans #ifdef KTRACE 2889ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 28988bf5036SJohn Baldwin ktrcsw(1, 0, cv_wmesg(cvp)); 290238510fcSJason Evans #endif 2918f27b08eSJohn Baldwin CV_ASSERT(cvp, lock, td); 2928f27b08eSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock, 29326306795SJohn Baldwin "Waiting on \"%s\"", cvp->cv_description); 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++; 3097d43ca69SJohn Baldwin if (lock == &Giant.lock_object) 3107d43ca69SJohn Baldwin mtx_assert(&Giant, MA_OWNED); 311c86b6ff5SJohn Baldwin DROP_GIANT(); 312238510fcSJason Evans 3138f27b08eSJohn Baldwin sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0); 3141ed3e44fSJohn Baldwin sleepq_set_timeout(cvp, timo); 315414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 3169fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 3179fa7ce0fSJohn Baldwin sleepq_release(cvp); 3187d43ca69SJohn Baldwin WITNESS_SAVE(lock, lock_witness); 3199fa7ce0fSJohn Baldwin lock_state = class->lc_unlock(lock); 3209fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 3219fa7ce0fSJohn Baldwin sleepq_lock(cvp); 322414e7679SJohn Baldwin } 323c5aa6b58SJeff Roberson rval = sleepq_timedwait(cvp, 0); 324238510fcSJason Evans 325238510fcSJason Evans #ifdef KTRACE 3269ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 32788bf5036SJohn Baldwin ktrcsw(0, 0, cv_wmesg(cvp)); 328238510fcSJason Evans #endif 329238510fcSJason Evans PICKUP_GIANT(); 330414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 3318f27b08eSJohn Baldwin class->lc_lock(lock, lock_state); 3328f27b08eSJohn Baldwin WITNESS_RESTORE(lock, lock_witness); 333414e7679SJohn Baldwin } 334238510fcSJason Evans 335238510fcSJason Evans return (rval); 336238510fcSJason Evans } 337238510fcSJason Evans 338238510fcSJason Evans /* 339238510fcSJason Evans * Wait on a condition variable for at most timo/hz seconds, allowing 340b40ce416SJulian Elischer * interruption by signals. Returns 0 if the thread was resumed by cv_signal 341238510fcSJason Evans * or cv_broadcast, EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if 342238510fcSJason Evans * a signal was caught. 343238510fcSJason Evans */ 344238510fcSJason Evans int 3458f27b08eSJohn Baldwin _cv_timedwait_sig(struct cv *cvp, struct lock_object *lock, int timo) 346238510fcSJason Evans { 3478f27b08eSJohn Baldwin WITNESS_SAVE_DECL(lock_witness); 3488f27b08eSJohn Baldwin struct lock_class *class; 349b40ce416SJulian Elischer struct thread *td; 3508f27b08eSJohn Baldwin int lock_state, rval; 351238510fcSJason Evans 352b40ce416SJulian Elischer td = curthread; 353414e7679SJohn Baldwin lock_state = 0; 354238510fcSJason Evans #ifdef KTRACE 3559ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 35688bf5036SJohn Baldwin ktrcsw(1, 0, cv_wmesg(cvp)); 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 class = LOCK_CLASS(lock); 362238510fcSJason Evans 363238510fcSJason Evans if (cold || panicstr) { 364238510fcSJason Evans /* 365238510fcSJason Evans * After a panic, or during autoconfiguration, just give 366238510fcSJason Evans * interrupts a chance, then just return; don't run any other 367b40ce416SJulian Elischer * thread or panic below, in case this is the idle process and 368238510fcSJason Evans * already asleep. 369238510fcSJason Evans */ 370238510fcSJason Evans return 0; 371238510fcSJason Evans } 3724bc37205SJeffrey Hsu 3732ff0e645SJohn Baldwin sleepq_lock(cvp); 374238510fcSJason Evans 3759000d57dSJohn Baldwin cvp->cv_waiters++; 3767d43ca69SJohn Baldwin if (lock == &Giant.lock_object) 3777d43ca69SJohn Baldwin mtx_assert(&Giant, MA_OWNED); 378c86b6ff5SJohn Baldwin DROP_GIANT(); 379238510fcSJason Evans 3808f27b08eSJohn Baldwin sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR | 3816cbb70e2SKip Macy SLEEPQ_INTERRUPTIBLE, 0); 3821ed3e44fSJohn Baldwin sleepq_set_timeout(cvp, timo); 383414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 3849fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 3859fa7ce0fSJohn Baldwin sleepq_release(cvp); 3867d43ca69SJohn Baldwin WITNESS_SAVE(lock, lock_witness); 3879fa7ce0fSJohn Baldwin lock_state = class->lc_unlock(lock); 3889fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 3899fa7ce0fSJohn Baldwin sleepq_lock(cvp); 390414e7679SJohn Baldwin } 391c5aa6b58SJeff Roberson rval = sleepq_timedwait_sig(cvp, 0); 392238510fcSJason Evans 393238510fcSJason Evans #ifdef KTRACE 3949ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 39588bf5036SJohn Baldwin ktrcsw(0, 0, cv_wmesg(cvp)); 396238510fcSJason Evans #endif 3979ba7fe1bSJohn Baldwin PICKUP_GIANT(); 398414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 3998f27b08eSJohn Baldwin class->lc_lock(lock, lock_state); 4008f27b08eSJohn Baldwin WITNESS_RESTORE(lock, lock_witness); 401414e7679SJohn Baldwin } 402238510fcSJason Evans 403238510fcSJason Evans return (rval); 404238510fcSJason Evans } 405238510fcSJason Evans 406238510fcSJason Evans /* 407b40ce416SJulian Elischer * Signal a condition variable, wakes up one waiting thread. Will also wakeup 408238510fcSJason Evans * the swapper if the process is not in memory, so that it can bring the 409b40ce416SJulian Elischer * sleeping process in. Note that this may also result in additional threads 410238510fcSJason Evans * being made runnable. Should be called with the same mutex as was passed to 411238510fcSJason Evans * cv_wait held. 412238510fcSJason Evans */ 413238510fcSJason Evans void 414238510fcSJason Evans cv_signal(struct cv *cvp) 415238510fcSJason Evans { 416da7bbd2cSJohn Baldwin int wakeup_swapper; 417238510fcSJason Evans 418da7bbd2cSJohn Baldwin wakeup_swapper = 0; 4192ff0e645SJohn Baldwin sleepq_lock(cvp); 4209000d57dSJohn Baldwin if (cvp->cv_waiters > 0) { 4219000d57dSJohn Baldwin cvp->cv_waiters--; 422da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_signal(cvp, SLEEPQ_CONDVAR, 0, 0); 423d72e80f0SJeff Roberson } 4242ff0e645SJohn Baldwin sleepq_release(cvp); 425da7bbd2cSJohn Baldwin if (wakeup_swapper) 426da7bbd2cSJohn Baldwin kick_proc0(); 4279000d57dSJohn Baldwin } 428238510fcSJason Evans 429238510fcSJason Evans /* 430b40ce416SJulian Elischer * Broadcast a signal to a condition variable. Wakes up all waiting threads. 431238510fcSJason Evans * Should be called with the same mutex as was passed to cv_wait held. 432238510fcSJason Evans */ 433238510fcSJason Evans void 434512824f8SSeigo Tanimura cv_broadcastpri(struct cv *cvp, int pri) 435238510fcSJason Evans { 436da7bbd2cSJohn Baldwin int wakeup_swapper; 437da7bbd2cSJohn Baldwin 438c5aa6b58SJeff Roberson /* 439c5aa6b58SJeff Roberson * XXX sleepq_broadcast pri argument changed from -1 meaning 440c5aa6b58SJeff Roberson * no pri to 0 meaning no pri. 441c5aa6b58SJeff Roberson */ 442da7bbd2cSJohn Baldwin wakeup_swapper = 0; 443c5aa6b58SJeff Roberson if (pri == -1) 444c5aa6b58SJeff Roberson pri = 0; 4452ff0e645SJohn Baldwin sleepq_lock(cvp); 4469000d57dSJohn Baldwin if (cvp->cv_waiters > 0) { 4479000d57dSJohn Baldwin cvp->cv_waiters = 0; 448da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri, 0); 449c5aa6b58SJeff Roberson } 4502ff0e645SJohn Baldwin sleepq_release(cvp); 451da7bbd2cSJohn Baldwin if (wakeup_swapper) 452da7bbd2cSJohn Baldwin kick_proc0(); 4539000d57dSJohn Baldwin } 454