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_timedwait ,
45 .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 "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 "void *wchan" "int flags" "int pri" "int queue"
63 .Fn sleepq_calc_signal_retval "int sig"
65 .Fn sleepq_catch_signals "void *wchan"
67 .Fn sleepq_free "struct sleepqueue *sq"
68 .Ft struct sleepqueue *
69 .Fn sleepq_lookup "void *wchan"
71 .Fn sleepq_lock "void *wchan"
73 .Fn sleepq_release "void *wchan"
75 .Fn sleepq_remove "struct thread *td" "void *wchan"
77 .Fn sleepq_signal "void *wchan" "int flags" "int pri" "int queue"
79 .Fn sleepq_set_timeout "void *wchan" "int timo"
81 .Fn sleepq_timedwait "void *wchan"
83 .Fn sleepq_timedwait_sig "void *wchan" "int signal_caught"
85 .Fn sleepq_wait "void *wchan"
87 .Fn sleepq_wait_sig "void *wchan"
89 Sleep queues provide a mechanism for suspending execution of a thread until
90 some condition is met.
91 Each queue is associated with a specific wait channel when it is active,
92 and only one queue may be associated with a wait channel at any given point
94 The implementation of each wait channel splits its sleepqueue into 2 sub-queues
95 in order to enable some optimizations on threads' wakeups.
96 An active queue holds a list of threads that are blocked on the associated
98 Threads that are not blocked on a wait channel have an associated inactive
100 When a thread blocks on a wait channel it donates its inactive sleep queue
102 When a thread is resumed,
103 the wait channel that it was blocked on gives it an inactive sleep queue for
108 function allocates an inactive sleep queue and is used to assign a
109 sleep queue to a thread during thread creation.
112 function frees the resources associated with an inactive sleep queue and is
113 used to free a queue during thread destruction.
115 Active sleep queues are stored in a hash table hashed on the addresses pointed
117 Each bucket in the hash table contains a sleep queue chain.
118 A sleep queue chain contains a spin mutex and a list of sleep queues that hash
119 to that specific chain.
120 Active sleep queues are protected by their chain's spin mutex.
123 function initializes the hash table of sleep queue chains.
127 function locks the sleep queue chain associated with wait channel
132 returns a pointer to the currently active sleep queue for that wait
133 channel associated with
137 if there is no active sleep queue associated with
140 It requires the sleep queue chain associated with
142 to have been locked by a prior call to
147 function unlocks the sleep queue chain associated with
149 and is primarily useful when aborting a pending sleep request before one of
150 the wait functions is called.
154 function places the current thread on the sleep queue associated with the
157 The sleep queue chain associated with argument
159 must be locked by a prior call to
161 when this function is called.
162 If a lock is specified via the
164 argument, and if the kernel was compiled with
165 .Cd "options INVARIANTS" ,
166 then the sleep queue code will perform extra checks to ensure that
167 the lock is used by all threads sleeping on
171 parameter should be a short description of
175 parameter is a bitmask consisting of the type of sleep queue being slept on
176 and zero or more optional flags.
179 parameter specifies the sub-queue, in which the contending thread will be
182 There are currently three types of sleep queues:
184 .Bl -tag -width ".Dv SLEEPQ_CONDVAR" -compact
185 .It Dv SLEEPQ_CONDVAR
186 A sleep queue used to implement condition variables.
188 A sleep queue used to implement
194 A sleep queue used to implement
198 There is currently only one optional flag:
200 .Bl -tag -width ".Dv SLEEPQ_INTERRUPTIBLE" -compact
201 .It Dv SLEEPQ_INTERRUPTIBLE
202 The current thread is entering an interruptible sleep.
205 A timeout on the sleep may be specified by calling
206 .Fn sleepq_set_timeout
211 parameter should be the same value from the preceding call to
213 and the sleep queue chain associated with
215 must have been locked by a prior call to
219 parameter should specify the timeout value in ticks.
221 The current thread may be marked interruptible by calling
222 .Fn sleepq_catch_signals
225 set to the wait channel.
226 This function returns a signal number if there are any pending signals for
227 the current thread and 0 if there is not a pending signal.
228 The sleep queue chain associated with argument
230 should have been locked by a prior call to
233 Once the thread is ready to suspend,
234 one of the wait functions is called to put the current thread to sleep
235 until it is awakened and to context switch to another thread.
238 function is used for non-interruptible sleeps that do not have a timeout.
241 function is used for non-interruptible sleeps that have had a timeout set via
242 .Fn sleepq_set_timeout .
245 function is used for interruptible sleeps that do not have a timeout.
247 .Fn sleepq_timedwait_sig
248 function is used for interruptible sleeps that do have a timeout set.
251 argument to all of the wait functions is the wait channel being slept
253 The sleep queue chain associated with argument
255 needs to have been locked with a prior call to
260 .Fn sleepq_timedwait_sig
261 specifies if a previous call to
262 .Fn sleepq_catch_signals
263 found a pending signal.
265 When the thread is resumed,
266 the wait functions return a non-zero value if the thread was awakened due to
267 an interrupt other than a signal or a timeout.
268 If the sleep timed out, then
271 If the sleep was interrupted by something other than a signal,
272 then some other return value will be returned.
273 If zero is returned after resuming from an interruptible sleep,
275 .Fn sleepq_calc_signal_retval
276 should be called to determine if the sleep was interrupted by a signal.
278 .Fn sleepq_calc_signal_retval
281 if the interrupting signal is restartable and
284 If the sleep was not interrupted by a signal,
285 .Fn sleepq_calc_signal_retval
288 A sleeping thread is normally resumed by the
295 function awakens the highest priority thread sleeping on a wait channel while
297 awakens all of the threads sleeping on a wait channel.
300 argument specifics which wait channel to awaken.
303 argument must match the sleep queue type contained in the
307 by the threads sleeping on the wait channel.
310 argument does not equal \-1,
311 then each thread that is awakened will have its priority raised to
313 if it has a lower priority.
314 The sleep queue chain associated with argument
316 must be locked by a prior call to
318 before calling any of these functions.
321 argument specifies the sub-queue, from which threads need to be woken up.
323 A thread in an interruptible sleep can be interrupted by another thread via
329 argument specifies the thread to interrupt.
330 An individual thread can also be awakened from sleeping on a specific wait
336 argument specifies the thread to awaken and the
338 argument specifies the wait channel to awaken it from.
341 is not blocked on the the wait channel
343 then this function will not do anything,
344 even if the thread is asleep on a different wait channel.
345 This function should only be used if one of the other functions above is not
347 One possible use is waking up a specific thread from a widely shared sleep
350 The sleep queue interface is currently used to implement the
355 Almost all other code in the kernel should use one of those interfaces rather
356 than manipulating sleep queues directly.