1.\" Copyright (c) 2000-2004 John H. Baldwin <jhb@FreeBSD.org> 2.\" All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 13.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 14.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 17.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23.\" 24.\" $FreeBSD$ 25.\" 26.Dd August 13, 2007 27.Dt SLEEPQUEUE 9 28.Os 29.Sh NAME 30.Nm init_sleepqueues , 31.Nm sleepq_abort , 32.Nm sleepq_add , 33.Nm sleepq_alloc , 34.Nm sleepq_broadcast , 35.Nm sleepq_calc_signal_retval , 36.Nm sleepq_catch_signals , 37.Nm sleepq_free , 38.Nm sleepq_lock , 39.Nm sleepq_lookup , 40.Nm sleepq_release , 41.Nm sleepq_remove , 42.Nm sleepq_signal , 43.Nm sleepq_set_timeout , 44.Nm sleepq_timedwait , 45.Nm sleepq_timedwait_sig , 46.Nm sleepq_wait , 47.Nm sleepq_wait_sig 48.Nd manage the queues of sleeping threads 49.Sh SYNOPSIS 50.In sys/param.h 51.In sys/sleepqueue.h 52.Ft void 53.Fn init_sleepqueues "void" 54.Ft void 55.Fn sleepq_abort "struct thread *td" 56.Ft void 57.Fn sleepq_add "void *wchan" "struct lock_object *lock" "const char *wmesg" "int flags" "int queue" 58.Ft struct sleepqueue * 59.Fn sleepq_alloc "void" 60.Ft void 61.Fn sleepq_broadcast "void *wchan" "int flags" "int pri" "int queue" 62.Ft int 63.Fn sleepq_calc_signal_retval "int sig" 64.Ft int 65.Fn sleepq_catch_signals "void *wchan" 66.Ft void 67.Fn sleepq_free "struct sleepqueue *sq" 68.Ft struct sleepqueue * 69.Fn sleepq_lookup "void *wchan" 70.Ft void 71.Fn sleepq_lock "void *wchan" 72.Ft void 73.Fn sleepq_release "void *wchan" 74.Ft void 75.Fn sleepq_remove "struct thread *td" "void *wchan" 76.Ft void 77.Fn sleepq_signal "void *wchan" "int flags" "int pri" "int queue" 78.Ft void 79.Fn sleepq_set_timeout "void *wchan" "int timo" 80.Ft int 81.Fn sleepq_timedwait "void *wchan" 82.Ft int 83.Fn sleepq_timedwait_sig "void *wchan" "int signal_caught" 84.Ft void 85.Fn sleepq_wait "void *wchan" 86.Ft int 87.Fn sleepq_wait_sig "void *wchan" 88.Sh DESCRIPTION 89Sleep queues provide a mechanism for suspending execution of a thread until 90some condition is met. 91Each queue is associated with a specific wait channel when it is active, 92and only one queue may be associated with a wait channel at any given point 93in time. 94The implementation of each wait channel splits its sleepqueue into 2 sub-queues 95in order to enable some optimizations on threads' wakeups. 96An active queue holds a list of threads that are blocked on the associated 97wait channel. 98Threads that are not blocked on a wait channel have an associated inactive 99sleep queue. 100When a thread blocks on a wait channel it donates its inactive sleep queue 101to the wait channel. 102When a thread is resumed, 103the wait channel that it was blocked on gives it an inactive sleep queue for 104later use. 105.Pp 106The 107.Fn sleepq_alloc 108function allocates an inactive sleep queue and is used to assign a 109sleep queue to a thread during thread creation. 110The 111.Fn sleepq_free 112function frees the resources associated with an inactive sleep queue and is 113used to free a queue during thread destruction. 114.Pp 115Active sleep queues are stored in a hash table hashed on the addresses pointed 116to by wait channels. 117Each bucket in the hash table contains a sleep queue chain. 118A sleep queue chain contains a spin mutex and a list of sleep queues that hash 119to that specific chain. 120Active sleep queues are protected by their chain's spin mutex. 121The 122.Fn init_sleepqueues 123function initializes the hash table of sleep queue chains. 124.Pp 125The 126.Fn sleepq_lock 127function locks the sleep queue chain associated with wait channel 128.Fa wchan . 129.Pp 130The 131.Fn sleepq_lookup 132returns a pointer to the currently active sleep queue for that wait 133channel associated with 134.Fa wchan 135or 136.Dv NULL 137if there is no active sleep queue associated with 138argument 139.Fa wchan . 140It requires the sleep queue chain associated with 141.Fa wchan 142to have been locked by a prior call to 143.Fn sleepq_lock . 144.Pp 145The 146.Fn sleepq_release 147function unlocks the sleep queue chain associated with 148.Fn wchan 149and is primarily useful when aborting a pending sleep request before one of 150the wait functions is called. 151.Pp 152The 153.Fn sleepq_add 154function places the current thread on the sleep queue associated with the 155wait channel 156.Fa wchan . 157The sleep queue chain associated with argument 158.Fa wchan 159must be locked by a prior call to 160.Fn sleepq_lock 161when this function is called. 162If a lock is specified via the 163.Fa lock 164argument, and if the kernel was compiled with 165.Cd "options INVARIANTS" , 166then the sleep queue code will perform extra checks to ensure that 167the lock is used by all threads sleeping on 168.Fa wchan . 169The 170.Fa wmesg 171parameter should be a short description of 172.Fa wchan . 173The 174.Fa flags 175parameter is a bitmask consisting of the type of sleep queue being slept on 176and zero or more optional flags. 177The 178.Fa queue 179parameter specifies the sub-queue, in which the contending thread will be 180inserted. 181.Pp 182There are currently three types of sleep queues: 183.Pp 184.Bl -tag -width ".Dv SLEEPQ_CONDVAR" -compact 185.It Dv SLEEPQ_CONDVAR 186A sleep queue used to implement condition variables. 187.It Dv SLEEPQ_SLEEP 188A sleep queue used to implement 189.Xr sleep 9 , 190.Xr wakeup 9 191and 192.Xr wakeup_one 9 . 193.It Dv SLEEPQ_PAUSE 194A sleep queue used to implement 195.Xr pause 9 . 196.El 197.Pp 198There is currently only one optional flag: 199.Pp 200.Bl -tag -width ".Dv SLEEPQ_INTERRUPTIBLE" -compact 201.It Dv SLEEPQ_INTERRUPTIBLE 202The current thread is entering an interruptible sleep. 203.El 204.Pp 205A timeout on the sleep may be specified by calling 206.Fn sleepq_set_timeout 207after 208.Fn sleepq_add . 209The 210.Fa wchan 211parameter should be the same value from the preceding call to 212.Fn sleepq_add , 213and the sleep queue chain associated with 214.Fa wchan 215must have been locked by a prior call to 216.Fn sleepq_lock . 217The 218.Fa timo 219parameter should specify the timeout value in ticks. 220.Pp 221The current thread may be marked interruptible by calling 222.Fn sleepq_catch_signals 223with 224.Fa wchan 225set to the wait channel. 226This function returns a signal number if there are any pending signals for 227the current thread and 0 if there is not a pending signal. 228The sleep queue chain associated with argument 229.Fa wchan 230should have been locked by a prior call to 231.Fn sleepq_lock . 232.Pp 233Once the thread is ready to suspend, 234one of the wait functions is called to put the current thread to sleep 235until it is awakened and to context switch to another thread. 236The 237.Fn sleepq_wait 238function is used for non-interruptible sleeps that do not have a timeout. 239The 240.Fn sleepq_timedwait 241function is used for non-interruptible sleeps that have had a timeout set via 242.Fn sleepq_set_timeout . 243The 244.Fn sleepq_wait_sig 245function is used for interruptible sleeps that do not have a timeout. 246The 247.Fn sleepq_timedwait_sig 248function is used for interruptible sleeps that do have a timeout set. 249The 250.Fa wchan 251argument to all of the wait functions is the wait channel being slept 252on. 253The sleep queue chain associated with argument 254.Fa wchan 255needs to have been locked with a prior call to 256.Fn sleepq_lock . 257The 258.Fa signal_caught 259parameter to 260.Fn sleepq_timedwait_sig 261specifies if a previous call to 262.Fn sleepq_catch_signals 263found a pending signal. 264.Pp 265When the thread is resumed, 266the wait functions return a non-zero value if the thread was awakened due to 267an interrupt other than a signal or a timeout. 268If the sleep timed out, then 269.Er EWOULDBLOCK 270is returned. 271If the sleep was interrupted by something other than a signal, 272then some other return value will be returned. 273If zero is returned after resuming from an interruptible sleep, 274then 275.Fn sleepq_calc_signal_retval 276should be called to determine if the sleep was interrupted by a signal. 277If so, 278.Fn sleepq_calc_signal_retval 279returns 280.Er ERESTART 281if the interrupting signal is restartable and 282.Er EINTR 283otherwise. 284If the sleep was not interrupted by a signal, 285.Fn sleepq_calc_signal_retval 286will return 0. 287.Pp 288A sleeping thread is normally resumed by the 289.Fn sleepq_broadcast 290and 291.Fn sleepq_signal 292functions. 293The 294.Fn sleepq_signal 295function awakens the highest priority thread sleeping on a wait channel while 296.Fn sleepq_broadcast 297awakens all of the threads sleeping on a wait channel. 298The 299.Fa wchan 300argument specifics which wait channel to awaken. 301The 302.Fa flags 303argument must match the sleep queue type contained in the 304.Fa flags 305argument passed to 306.Fn sleepq_add 307by the threads sleeping on the wait channel. 308If the 309.Fa pri 310argument does not equal \-1, 311then each thread that is awakened will have its priority raised to 312.Fa pri 313if it has a lower priority. 314The sleep queue chain associated with argument 315.Fa wchan 316must be locked by a prior call to 317.Fn sleepq_lock 318before calling any of these functions. 319The 320.Fa queue 321argument specifies the sub-queue, from which threads need to be woken up. 322.Pp 323A thread in an interruptible sleep can be interrupted by another thread via 324the 325.Fn sleepq_abort 326function. 327The 328.Fa td 329argument specifies the thread to interrupt. 330An individual thread can also be awakened from sleeping on a specific wait 331channel via the 332.Fn sleepq_remove 333function. 334The 335.Fa td 336argument specifies the thread to awaken and the 337.Fa wchan 338argument specifies the wait channel to awaken it from. 339If the thread 340.Fa td 341is not blocked on the the wait channel 342.Fa wchan 343then this function will not do anything, 344even if the thread is asleep on a different wait channel. 345This function should only be used if one of the other functions above is not 346sufficient. 347One possible use is waking up a specific thread from a widely shared sleep 348channel. 349.Pp 350The sleep queue interface is currently used to implement the 351.Xr sleep 9 352and 353.Xr condvar 9 354interfaces. 355Almost all other code in the kernel should use one of those interfaces rather 356than manipulating sleep queues directly. 357.Sh SEE ALSO 358.Xr condvar 9 , 359.Xr runqueue 9 , 360.Xr scheduler 9 , 361.Xr sleep 9 362