1 .\" Copyright (c) 2000-2004 John H. Baldwin <jhb@FreeBSD.org>
3 .\" Redistribution and use in source and binary forms, with or without
4 .\" modification, are permitted provided that the following conditions
6 .\" 1. Redistributions of source code must retain the above copyright
7 .\" notice, this list of conditions and the following disclaimer.
8 .\" 2. Redistributions in binary form must reproduce the above copyright
9 .\" notice, this list of conditions and the following disclaimer in the
10 .\" documentation and/or other materials provided with the distribution.
12 .\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
13 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
15 .\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
16 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
17 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
18 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
19 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 .Nm init_sleepqueues ,
33 .Nm sleepq_broadcast ,
40 .Nm sleepq_set_timeout ,
41 .Nm sleepq_set_timeout_sbt ,
43 .Nm sleepq_timedwait ,
44 .Nm sleepq_timedwait_sig ,
48 .Nd manage the queues of sleeping threads
53 .Fn init_sleepqueues "void"
55 .Fn sleepq_abort "struct thread *td"
57 .Fn sleepq_add "const void *wchan" "struct lock_object *lock" "const char *wmesg" "int flags" "int queue"
58 .Ft struct sleepqueue *
59 .Fn sleepq_alloc "void"
61 .Fn sleepq_broadcast "const void *wchan" "int flags" "int pri" "int queue"
63 .Fn sleepq_free "struct sleepqueue *sq"
64 .Ft struct sleepqueue *
65 .Fn sleepq_lookup "const void *wchan"
67 .Fn sleepq_lock "const void *wchan"
69 .Fn sleepq_release "const void *wchan"
71 .Fn sleepq_remove "struct thread *td" "const void *wchan"
73 .Fn sleepq_signal "const void *wchan" "int flags" "int pri" "int queue"
75 .Fn sleepq_set_timeout "const void *wchan" "int timo"
77 .Fn sleepq_set_timeout_sbt "const void *wchan" "sbintime_t sbt" \
78 "sbintime_t pr" "int flags"
80 .Fn sleepq_sleepcnt "const void *wchan" "int queue"
82 .Fn sleepq_timedwait "const void *wchan" "int pri"
84 .Fn sleepq_timedwait_sig "const void *wchan" "int pri"
86 .Fn sleepq_type "const void *wchan"
88 .Fn sleepq_wait "const void *wchan" "int pri"
90 .Fn sleepq_wait_sig "const void *wchan" "int pri"
92 Sleep queues provide a mechanism for suspending execution of a thread until
93 some condition is met.
94 Each queue is associated with a specific wait channel when it is active,
95 and only one queue may be associated with a wait channel at any given point
97 The implementation of each wait channel splits its sleepqueue into 2 sub-queues
98 in order to enable some optimizations on threads' wakeups.
99 An active queue holds a list of threads that are blocked on the associated
101 Threads that are not blocked on a wait channel have an associated inactive
103 When a thread blocks on a wait channel it donates its inactive sleep queue
105 When a thread is resumed,
106 the wait channel that it was blocked on gives it an inactive sleep queue for
111 function allocates an inactive sleep queue and is used to assign a
112 sleep queue to a thread during thread creation.
115 function frees the resources associated with an inactive sleep queue and is
116 used to free a queue during thread destruction.
118 Active sleep queues are stored in a hash table hashed on the addresses pointed
120 Each bucket in the hash table contains a sleep queue chain.
121 A sleep queue chain contains a spin mutex and a list of sleep queues that hash
122 to that specific chain.
123 Active sleep queues are protected by their chain's spin mutex.
126 function initializes the hash table of sleep queue chains.
130 function locks the sleep queue chain associated with wait channel
135 returns a pointer to the currently active sleep queue for that wait
136 channel associated with
140 if there is no active sleep queue associated with
143 It requires the sleep queue chain associated with
145 to have been locked by a prior call to
150 function unlocks the sleep queue chain associated with
152 and is primarily useful when aborting a pending sleep request before one of
153 the wait functions is called.
157 function places the current thread on the sleep queue associated with the
160 The sleep queue chain associated with argument
162 must be locked by a prior call to
164 when this function is called.
165 If a lock is specified via the
167 argument, and if the kernel was compiled with
168 .Cd "options INVARIANTS" ,
169 then the sleep queue code will perform extra checks to ensure that
170 the lock is used by all threads sleeping on
174 parameter should be a short description of
178 parameter is a bitmask consisting of the type of sleep queue being slept on
179 and zero or more optional flags.
182 parameter specifies the sub-queue, in which the contending thread will be
185 There are currently three types of sleep queues:
187 .Bl -tag -width ".Dv SLEEPQ_CONDVAR" -compact
188 .It Dv SLEEPQ_CONDVAR
189 A sleep queue used to implement condition variables.
191 A sleep queue used to implement
197 A sleep queue used to implement
201 There are currently two optional flag:
203 .Bl -tag -width ".Dv SLEEPQ_INTERRUPTIBLE" -compact
204 .It Dv SLEEPQ_INTERRUPTIBLE
205 The current thread is entering an interruptible sleep.
207 .Bl -tag -width ".Dv SLEEPQ_STOP_ON_BDRY" -compact
208 .It Dv SLEEPQ_STOP_ON_BDRY
209 When thread is entering an interruptible sleep, do not stop it upon
210 arrival of stop action, like
215 A timeout on the sleep may be specified by calling
216 .Fn sleepq_set_timeout
221 parameter should be the same value from the preceding call to
223 and the sleep queue chain associated with
225 must have been locked by a prior call to
229 parameter should specify the timeout value in ticks.
231 .Fn sleepq_set_timeout_sbt
236 It allows to specify relative or absolute wakeup time with higher resolution
241 allows to specify wanted absolute event precision.
244 allows to pass additional
245 .Fn callout_reset_sbt
248 Once the thread is ready to suspend,
249 one of the wait functions is called to put the current thread to sleep
250 until it is awakened and to context switch to another thread.
253 function is used for non-interruptible sleeps that do not have a timeout.
256 function is used for non-interruptible sleeps that have had a timeout set via
257 .Fn sleepq_set_timeout .
260 function is used for interruptible sleeps that do not have a timeout.
262 .Fn sleepq_timedwait_sig
263 function is used for interruptible sleeps that do have a timeout set.
266 argument to all of the wait functions is the wait channel being slept
268 The sleep queue chain associated with argument
270 needs to have been locked with a prior call to
274 argument is used to set the priority of the thread when it is awakened.
275 If it is set to zero, the thread's priority is left alone.
277 When the thread is resumed,
278 the wait functions return a non-zero value if the thread was awakened due to
279 an interrupt other than a signal or a timeout.
280 If the sleep timed out, then
283 If the sleep was interrupted by something other than a signal,
284 then some other return value will be returned.
286 A sleeping thread is normally resumed by the
293 function awakens the highest priority thread sleeping on a wait channel
294 (if SLEEPQ_UNFAIR flag is set, thread that went to sleep recently) while
296 awakens all of the threads sleeping on a wait channel.
299 argument specifics which wait channel to awaken.
302 argument must match the sleep queue type contained in the
306 by the threads sleeping on the wait channel.
309 argument does not equal \-1,
310 then each thread that is awakened will have its priority raised to
312 if it has a lower priority.
313 The sleep queue chain associated with argument
315 must be locked by a prior call to
317 before calling any of these functions.
320 argument specifies the sub-queue, from which threads need to be woken up.
322 A thread in an interruptible sleep can be interrupted by another thread via
328 argument specifies the thread to interrupt.
329 An individual thread can also be awakened from sleeping on a specific wait
335 argument specifies the thread to awaken and the
337 argument specifies the wait channel to awaken it from.
340 is not blocked on the wait channel
342 then this function will not do anything,
343 even if the thread is asleep on a different wait channel.
344 This function should only be used if one of the other functions above is not
346 One possible use is waking up a specific thread from a widely shared sleep
351 function offer a simple way to retrieve the number of threads sleeping for
359 function returns the type of
361 associated to a sleepqueue.
365 .Fn sleepq_broadcast ,
368 functions all return a boolean value.
369 If the return value is true,
370 then at least one thread was resumed that is currently swapped out.
371 The caller is responsible for awakening the scheduler process so that the
372 resumed thread will be swapped back in.
373 This is done by calling the
375 function after releasing the sleep queue chain lock via a call to
378 The sleep queue interface is currently used to implement the
383 Almost all other code in the kernel should use one of those interfaces rather
384 than manipulating sleep queues directly.