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 { \ 530a15e5d3SAttilio 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; 1007faf4d90SDavide Italiano uintptr_t 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; 2177faf4d90SDavide Italiano uintptr_t lock_state; 2187faf4d90SDavide Italiano int rval; 219238510fcSJason Evans 220b40ce416SJulian Elischer td = curthread; 221414e7679SJohn Baldwin lock_state = 0; 222238510fcSJason Evans #ifdef KTRACE 2239ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 22488bf5036SJohn Baldwin ktrcsw(1, 0, cv_wmesg(cvp)); 225238510fcSJason Evans #endif 2268f27b08eSJohn Baldwin CV_ASSERT(cvp, lock, td); 2278f27b08eSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock, 22826306795SJohn Baldwin "Waiting on \"%s\"", cvp->cv_description); 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++; 2447d43ca69SJohn Baldwin if (lock == &Giant.lock_object) 2457d43ca69SJohn Baldwin mtx_assert(&Giant, MA_OWNED); 246c86b6ff5SJohn Baldwin DROP_GIANT(); 247238510fcSJason Evans 2488f27b08eSJohn Baldwin sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR | 2496cbb70e2SKip Macy SLEEPQ_INTERRUPTIBLE, 0); 250414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 2519fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 2529fa7ce0fSJohn Baldwin sleepq_release(cvp); 2537d43ca69SJohn Baldwin WITNESS_SAVE(lock, lock_witness); 2549fa7ce0fSJohn Baldwin lock_state = class->lc_unlock(lock); 2559fa7ce0fSJohn Baldwin if (class->lc_flags & LC_SLEEPABLE) 2569fa7ce0fSJohn Baldwin sleepq_lock(cvp); 257414e7679SJohn Baldwin } 258c5aa6b58SJeff Roberson rval = sleepq_wait_sig(cvp, 0); 259238510fcSJason Evans 260238510fcSJason Evans #ifdef KTRACE 2619ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 26288bf5036SJohn Baldwin ktrcsw(0, 0, cv_wmesg(cvp)); 263238510fcSJason Evans #endif 2649ba7fe1bSJohn Baldwin PICKUP_GIANT(); 265414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 2668f27b08eSJohn Baldwin class->lc_lock(lock, lock_state); 2678f27b08eSJohn Baldwin WITNESS_RESTORE(lock, lock_witness); 268414e7679SJohn Baldwin } 269238510fcSJason Evans 270238510fcSJason Evans return (rval); 271238510fcSJason Evans } 272238510fcSJason Evans 273238510fcSJason Evans /* 27446153735SDavide Italiano * Wait on a condition variable for (at most) the value specified in sbt 27546153735SDavide Italiano * argument. Returns 0 if the process was resumed by cv_signal or cv_broadcast, 27646153735SDavide Italiano * EWOULDBLOCK if the timeout expires. 277238510fcSJason Evans */ 278238510fcSJason Evans int 27946153735SDavide Italiano _cv_timedwait_sbt(struct cv *cvp, struct lock_object *lock, sbintime_t sbt, 28046153735SDavide Italiano sbintime_t pr, int flags) 281238510fcSJason Evans { 2828f27b08eSJohn Baldwin WITNESS_SAVE_DECL(lock_witness); 2838f27b08eSJohn Baldwin struct lock_class *class; 284b40ce416SJulian Elischer struct thread *td; 2858f27b08eSJohn Baldwin int lock_state, rval; 286238510fcSJason Evans 287b40ce416SJulian Elischer td = curthread; 288414e7679SJohn Baldwin lock_state = 0; 289238510fcSJason Evans #ifdef KTRACE 2909ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 29188bf5036SJohn Baldwin ktrcsw(1, 0, cv_wmesg(cvp)); 292238510fcSJason Evans #endif 2938f27b08eSJohn Baldwin CV_ASSERT(cvp, lock, td); 2948f27b08eSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock, 29526306795SJohn Baldwin "Waiting on \"%s\"", cvp->cv_description); 2968f27b08eSJohn Baldwin class = LOCK_CLASS(lock); 297238510fcSJason Evans 298238510fcSJason Evans if (cold || panicstr) { 299238510fcSJason Evans /* 300238510fcSJason Evans * After a panic, or during autoconfiguration, just give 301238510fcSJason Evans * interrupts a chance, then just return; don't run any other 302b40ce416SJulian Elischer * thread or panic below, in case this is the idle process and 303238510fcSJason Evans * already asleep. 304238510fcSJason Evans */ 305238510fcSJason Evans return 0; 306238510fcSJason Evans } 3074bc37205SJeffrey Hsu 3082ff0e645SJohn Baldwin sleepq_lock(cvp); 309238510fcSJason Evans 3109000d57dSJohn Baldwin cvp->cv_waiters++; 3117d43ca69SJohn Baldwin if (lock == &Giant.lock_object) 3127d43ca69SJohn Baldwin mtx_assert(&Giant, MA_OWNED); 313c86b6ff5SJohn Baldwin DROP_GIANT(); 314238510fcSJason Evans 3158f27b08eSJohn Baldwin sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0); 316*1a26c3c0SHans Petter Selasky sleepq_release(cvp); 31746153735SDavide Italiano sleepq_set_timeout_sbt(cvp, sbt, pr, flags); 318414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 3197d43ca69SJohn Baldwin WITNESS_SAVE(lock, lock_witness); 3209fa7ce0fSJohn Baldwin lock_state = class->lc_unlock(lock); 321414e7679SJohn Baldwin } 322*1a26c3c0SHans Petter Selasky sleepq_lock(cvp); 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 /* 33946153735SDavide Italiano * Wait on a condition variable for (at most) the value specified in sbt 34046153735SDavide Italiano * argument, allowing interruption by signals. 34146153735SDavide Italiano * Returns 0 if the thread was resumed by cv_signal or cv_broadcast, 34246153735SDavide Italiano * EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if a signal 34346153735SDavide Italiano * was caught. 344238510fcSJason Evans */ 345238510fcSJason Evans int 34646153735SDavide Italiano _cv_timedwait_sig_sbt(struct cv *cvp, struct lock_object *lock, 34746153735SDavide Italiano sbintime_t sbt, sbintime_t pr, int flags) 348238510fcSJason Evans { 3498f27b08eSJohn Baldwin WITNESS_SAVE_DECL(lock_witness); 3508f27b08eSJohn Baldwin struct lock_class *class; 351b40ce416SJulian Elischer struct thread *td; 3528f27b08eSJohn Baldwin int lock_state, rval; 353238510fcSJason Evans 354b40ce416SJulian Elischer td = curthread; 355414e7679SJohn Baldwin lock_state = 0; 356238510fcSJason Evans #ifdef KTRACE 3579ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 35888bf5036SJohn Baldwin ktrcsw(1, 0, cv_wmesg(cvp)); 359238510fcSJason Evans #endif 3608f27b08eSJohn Baldwin CV_ASSERT(cvp, lock, td); 3618f27b08eSJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock, 36226306795SJohn Baldwin "Waiting on \"%s\"", cvp->cv_description); 3638f27b08eSJohn Baldwin class = LOCK_CLASS(lock); 364238510fcSJason Evans 365238510fcSJason Evans if (cold || panicstr) { 366238510fcSJason Evans /* 367238510fcSJason Evans * After a panic, or during autoconfiguration, just give 368238510fcSJason Evans * interrupts a chance, then just return; don't run any other 369b40ce416SJulian Elischer * thread or panic below, in case this is the idle process and 370238510fcSJason Evans * already asleep. 371238510fcSJason Evans */ 372238510fcSJason Evans return 0; 373238510fcSJason Evans } 3744bc37205SJeffrey Hsu 3752ff0e645SJohn Baldwin sleepq_lock(cvp); 376238510fcSJason Evans 3779000d57dSJohn Baldwin cvp->cv_waiters++; 3787d43ca69SJohn Baldwin if (lock == &Giant.lock_object) 3797d43ca69SJohn Baldwin mtx_assert(&Giant, MA_OWNED); 380c86b6ff5SJohn Baldwin DROP_GIANT(); 381238510fcSJason Evans 3828f27b08eSJohn Baldwin sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR | 3836cbb70e2SKip Macy SLEEPQ_INTERRUPTIBLE, 0); 384*1a26c3c0SHans Petter Selasky sleepq_release(cvp); 38546153735SDavide Italiano sleepq_set_timeout_sbt(cvp, sbt, pr, flags); 386414e7679SJohn Baldwin if (lock != &Giant.lock_object) { 3877d43ca69SJohn Baldwin WITNESS_SAVE(lock, lock_witness); 3889fa7ce0fSJohn Baldwin lock_state = class->lc_unlock(lock); 389414e7679SJohn Baldwin } 390*1a26c3c0SHans Petter Selasky sleepq_lock(cvp); 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