1 .\" Copyright (c) 2000-2004 John H. Baldwin <jhb@FreeBSD.org>
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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.
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.
30 .Nm init_sleepqueues ,
34 .Nm sleepq_broadcast ,
35 .Nm sleepq_calc_signal_retval ,
36 .Nm sleepq_catch_signals ,
43 .Nm sleepq_set_timeout ,
44 .Nm sleepq_set_timeout_sbt ,
46 .Nm sleepq_timedwait ,
47 .Nm sleepq_timedwait_sig ,
51 .Nd manage the queues of sleeping threads
56 .Fn init_sleepqueues "void"
58 .Fn sleepq_abort "struct thread *td"
60 .Fn sleepq_add "void *wchan" "struct lock_object *lock" "const char *wmesg" "int flags" "int queue"
61 .Ft struct sleepqueue *
62 .Fn sleepq_alloc "void"
64 .Fn sleepq_broadcast "void *wchan" "int flags" "int pri" "int queue"
66 .Fn sleepq_calc_signal_retval "int sig"
68 .Fn sleepq_catch_signals "void *wchan"
70 .Fn sleepq_free "struct sleepqueue *sq"
71 .Ft struct sleepqueue *
72 .Fn sleepq_lookup "void *wchan"
74 .Fn sleepq_lock "void *wchan"
76 .Fn sleepq_release "void *wchan"
78 .Fn sleepq_remove "struct thread *td" "void *wchan"
80 .Fn sleepq_signal "void *wchan" "int flags" "int pri" "int queue"
82 .Fn sleepq_set_timeout "void *wchan" "int timo"
84 .Fn sleepq_set_timeout_sbt "void *wchan" "sbintime_t sbt" \
85 "sbintime_t pr" "int flags"
87 .Fn sleepq_sleepcnt "void *wchan" "int queue"
89 .Fn sleepq_timedwait "void *wchan"
91 .Fn sleepq_timedwait_sig "void *wchan" "int signal_caught"
93 .Fn sleepq_type "void *wchan"
95 .Fn sleepq_wait "void *wchan"
97 .Fn sleepq_wait_sig "void *wchan"
99 Sleep queues provide a mechanism for suspending execution of a thread until
100 some condition is met.
101 Each queue is associated with a specific wait channel when it is active,
102 and only one queue may be associated with a wait channel at any given point
104 The implementation of each wait channel splits its sleepqueue into 2 sub-queues
105 in order to enable some optimizations on threads' wakeups.
106 An active queue holds a list of threads that are blocked on the associated
108 Threads that are not blocked on a wait channel have an associated inactive
110 When a thread blocks on a wait channel it donates its inactive sleep queue
112 When a thread is resumed,
113 the wait channel that it was blocked on gives it an inactive sleep queue for
118 function allocates an inactive sleep queue and is used to assign a
119 sleep queue to a thread during thread creation.
122 function frees the resources associated with an inactive sleep queue and is
123 used to free a queue during thread destruction.
125 Active sleep queues are stored in a hash table hashed on the addresses pointed
127 Each bucket in the hash table contains a sleep queue chain.
128 A sleep queue chain contains a spin mutex and a list of sleep queues that hash
129 to that specific chain.
130 Active sleep queues are protected by their chain's spin mutex.
133 function initializes the hash table of sleep queue chains.
137 function locks the sleep queue chain associated with wait channel
142 returns a pointer to the currently active sleep queue for that wait
143 channel associated with
147 if there is no active sleep queue associated with
150 It requires the sleep queue chain associated with
152 to have been locked by a prior call to
157 function unlocks the sleep queue chain associated with
159 and is primarily useful when aborting a pending sleep request before one of
160 the wait functions is called.
164 function places the current thread on the sleep queue associated with the
167 The sleep queue chain associated with argument
169 must be locked by a prior call to
171 when this function is called.
172 If a lock is specified via the
174 argument, and if the kernel was compiled with
175 .Cd "options INVARIANTS" ,
176 then the sleep queue code will perform extra checks to ensure that
177 the lock is used by all threads sleeping on
181 parameter should be a short description of
185 parameter is a bitmask consisting of the type of sleep queue being slept on
186 and zero or more optional flags.
189 parameter specifies the sub-queue, in which the contending thread will be
192 There are currently three types of sleep queues:
194 .Bl -tag -width ".Dv SLEEPQ_CONDVAR" -compact
195 .It Dv SLEEPQ_CONDVAR
196 A sleep queue used to implement condition variables.
198 A sleep queue used to implement
204 A sleep queue used to implement
208 There are currently two optional flag:
210 .Bl -tag -width ".Dv SLEEPQ_INTERRUPTIBLE" -compact
211 .It Dv SLEEPQ_INTERRUPTIBLE
212 The current thread is entering an interruptible sleep.
214 .Bl -tag -width ".Dv SLEEPQ_STOP_ON_BDRY" -compact
215 .It Dv SLEEPQ_STOP_ON_BDRY
216 When thread is entering an interruptible sleep, do not stop it upon
217 arrival of stop action, like
222 A timeout on the sleep may be specified by calling
223 .Fn sleepq_set_timeout
228 parameter should be the same value from the preceding call to
230 and the sleep queue chain associated with
232 must have been locked by a prior call to
236 parameter should specify the timeout value in ticks.
238 .Fn sleepq_set_timeout_sbt
243 It allows to specify relative or absolute wakeup time with higher resolution
248 allows to specify wanted absolute event precision.
251 allows to pass additional
252 .Fn callout_reset_sbt
255 The current thread may be marked interruptible by calling
256 .Fn sleepq_catch_signals
259 set to the wait channel.
260 This function returns a signal number if there are any pending signals for
261 the current thread and 0 if there is not a pending signal.
262 The sleep queue chain associated with argument
264 should have been locked by a prior call to
267 Once the thread is ready to suspend,
268 one of the wait functions is called to put the current thread to sleep
269 until it is awakened and to context switch to another thread.
272 function is used for non-interruptible sleeps that do not have a timeout.
275 function is used for non-interruptible sleeps that have had a timeout set via
276 .Fn sleepq_set_timeout .
279 function is used for interruptible sleeps that do not have a timeout.
281 .Fn sleepq_timedwait_sig
282 function is used for interruptible sleeps that do have a timeout set.
285 argument to all of the wait functions is the wait channel being slept
287 The sleep queue chain associated with argument
289 needs to have been locked with a prior call to
294 .Fn sleepq_timedwait_sig
295 specifies if a previous call to
296 .Fn sleepq_catch_signals
297 found a pending signal.
299 When the thread is resumed,
300 the wait functions return a non-zero value if the thread was awakened due to
301 an interrupt other than a signal or a timeout.
302 If the sleep timed out, then
305 If the sleep was interrupted by something other than a signal,
306 then some other return value will be returned.
307 If zero is returned after resuming from an interruptible sleep,
309 .Fn sleepq_calc_signal_retval
310 should be called to determine if the sleep was interrupted by a signal.
312 .Fn sleepq_calc_signal_retval
315 if the interrupting signal is restartable and
318 If the sleep was not interrupted by a signal,
319 .Fn sleepq_calc_signal_retval
322 A sleeping thread is normally resumed by the
329 function awakens the highest priority thread sleeping on a wait channel while
331 awakens all of the threads sleeping on a wait channel.
334 argument specifics which wait channel to awaken.
337 argument must match the sleep queue type contained in the
341 by the threads sleeping on the wait channel.
344 argument does not equal \-1,
345 then each thread that is awakened will have its priority raised to
347 if it has a lower priority.
348 The sleep queue chain associated with argument
350 must be locked by a prior call to
352 before calling any of these functions.
355 argument specifies the sub-queue, from which threads need to be woken up.
357 A thread in an interruptible sleep can be interrupted by another thread via
363 argument specifies the thread to interrupt.
364 An individual thread can also be awakened from sleeping on a specific wait
370 argument specifies the thread to awaken and the
372 argument specifies the wait channel to awaken it from.
375 is not blocked on the wait channel
377 then this function will not do anything,
378 even if the thread is asleep on a different wait channel.
379 This function should only be used if one of the other functions above is not
381 One possible use is waking up a specific thread from a widely shared sleep
386 function offer a simple way to retrieve the number of threads sleeping for
394 function returns the type of
396 associated to a sleepqueue.
400 .Fn sleepq_broadcast ,
403 functions all return a boolean value.
404 If the return value is true,
405 then at least one thread was resumed that is currently swapped out.
406 The caller is responsible for awakening the scheduler process so that the
407 resumed thread will be swapped back in.
408 This is done by calling the
410 function after releasing the sleep queue chain lock via a call to
413 The sleep queue interface is currently used to implement the
418 Almost all other code in the kernel should use one of those interfaces rather
419 than manipulating sleep queues directly.