]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - share/man/man9/sleepqueue.9
MFC r271992
[FreeBSD/stable/9.git] / share / man / man9 / sleepqueue.9
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 September 22, 2014
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_free ,
36 .Nm sleepq_lock ,
37 .Nm sleepq_lookup ,
38 .Nm sleepq_release ,
39 .Nm sleepq_remove ,
40 .Nm sleepq_signal ,
41 .Nm sleepq_set_timeout ,
42 .Nm sleepq_sleepcnt ,
43 .Nm sleepq_timedwait ,
44 .Nm sleepq_timedwait_sig ,
45 .Nm sleepq_type ,
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 int
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 int
61 .Fn sleepq_broadcast "void *wchan" "int flags" "int pri" "int queue"
62 .Ft void
63 .Fn sleepq_free "struct sleepqueue *sq"
64 .Ft struct sleepqueue *
65 .Fn sleepq_lookup "void *wchan"
66 .Ft void
67 .Fn sleepq_lock "void *wchan"
68 .Ft void
69 .Fn sleepq_release "void *wchan"
70 .Ft void
71 .Fn sleepq_remove "struct thread *td" "void *wchan"
72 .Ft int
73 .Fn sleepq_signal "void *wchan" "int flags" "int pri" "int queue"
74 .Ft void
75 .Fn sleepq_set_timeout "void *wchan" "int timo"
76 .Ft u_int
77 .Fn sleepq_sleepcnt "void *wchan" "int queue"
78 .Ft int
79 .Fn sleepq_timedwait "void *wchan" "int pri"
80 .Ft int
81 .Fn sleepq_timedwait_sig "void *wchan" "int pri"
82 .Ft int
83 .Fn sleepq_type "void *wchan"
84 .Ft void
85 .Fn sleepq_wait "void *wchan" "int pri"
86 .Ft int
87 .Fn sleepq_wait_sig "void *wchan" "int pri"
88 .Sh DESCRIPTION
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
93 in time.
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
97 wait channel.
98 Threads that are not blocked on a wait channel have an associated inactive
99 sleep queue.
100 When a thread blocks on a wait channel it donates its inactive sleep queue
101 to the wait channel.
102 When a thread is resumed,
103 the wait channel that it was blocked on gives it an inactive sleep queue for
104 later use.
105 .Pp
106 The
107 .Fn sleepq_alloc
108 function allocates an inactive sleep queue and is used to assign a
109 sleep queue to a thread during thread creation.
110 The
111 .Fn sleepq_free
112 function frees the resources associated with an inactive sleep queue and is
113 used to free a queue during thread destruction.
114 .Pp
115 Active sleep queues are stored in a hash table hashed on the addresses pointed
116 to by wait channels.
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.
121 The
122 .Fn init_sleepqueues
123 function initializes the hash table of sleep queue chains.
124 .Pp
125 The
126 .Fn sleepq_lock
127 function locks the sleep queue chain associated with wait channel
128 .Fa wchan .
129 .Pp
130 The
131 .Fn sleepq_lookup
132 returns a pointer to the currently active sleep queue for that wait
133 channel associated with
134 .Fa wchan
135 or
136 .Dv NULL
137 if there is no active sleep queue associated with
138 argument
139 .Fa wchan .
140 It requires the sleep queue chain associated with
141 .Fa wchan
142 to have been locked by a prior call to
143 .Fn sleepq_lock .
144 .Pp
145 The
146 .Fn sleepq_release
147 function unlocks the sleep queue chain associated with
148 .Fn wchan
149 and is primarily useful when aborting a pending sleep request before one of
150 the wait functions is called.
151 .Pp
152 The
153 .Fn sleepq_add
154 function places the current thread on the sleep queue associated with the
155 wait channel
156 .Fa wchan .
157 The sleep queue chain associated with argument
158 .Fa wchan
159 must be locked by a prior call to
160 .Fn sleepq_lock
161 when this function is called.
162 If a lock is specified via the
163 .Fa lock
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
168 .Fa wchan .
169 The
170 .Fa wmesg
171 parameter should be a short description of
172 .Fa wchan .
173 The
174 .Fa flags
175 parameter is a bitmask consisting of the type of sleep queue being slept on
176 and zero or more optional flags.
177 The
178 .Fa queue
179 parameter specifies the sub-queue, in which the contending thread will be
180 inserted.
181 .Pp
182 There are currently three types of sleep queues:
183 .Pp
184 .Bl -tag -width ".Dv SLEEPQ_CONDVAR" -compact
185 .It Dv SLEEPQ_CONDVAR
186 A sleep queue used to implement condition variables.
187 .It Dv SLEEPQ_SLEEP
188 A sleep queue used to implement
189 .Xr sleep 9 ,
190 .Xr wakeup 9
191 and
192 .Xr wakeup_one 9 .
193 .It Dv SLEEPQ_PAUSE
194 A sleep queue used to implement
195 .Xr pause 9 .
196 .El
197 .Pp
198 There are currently two optional flag:
199 .Pp
200 .Bl -tag -width ".Dv SLEEPQ_INTERRUPTIBLE" -compact
201 .It Dv SLEEPQ_INTERRUPTIBLE
202 The current thread is entering an interruptible sleep.
203 .El
204 .Bl -tag -width ".Dv SLEEPQ_STOP_ON_BDRY" -compact
205 .It Dv SLEEPQ_STOP_ON_BDRY
206 When thread is entering an interruptible sleep, do not stop it upon
207 arrival of stop action, like
208 .Dv SIGSTOP .
209 Wake it up instead.
210 .El
211 .Pp
212 A timeout on the sleep may be specified by calling
213 .Fn sleepq_set_timeout
214 after
215 .Fn sleepq_add .
216 The
217 .Fa wchan
218 parameter should be the same value from the preceding call to
219 .Fn sleepq_add ,
220 and the sleep queue chain associated with
221 .Fa wchan
222 must have been locked by a prior call to
223 .Fn sleepq_lock .
224 The
225 .Fa timo
226 parameter should specify the timeout value in ticks.
227 .Pp
228 .Pp
229 Once the thread is ready to suspend,
230 one of the wait functions is called to put the current thread to sleep
231 until it is awakened and to context switch to another thread.
232 The
233 .Fn sleepq_wait
234 function is used for non-interruptible sleeps that do not have a timeout.
235 The
236 .Fn sleepq_timedwait
237 function is used for non-interruptible sleeps that have had a timeout set via
238 .Fn sleepq_set_timeout .
239 The
240 .Fn sleepq_wait_sig
241 function is used for interruptible sleeps that do not have a timeout.
242 The
243 .Fn sleepq_timedwait_sig
244 function is used for interruptible sleeps that do have a timeout set.
245 The
246 .Fa wchan
247 argument to all of the wait functions is the wait channel being slept
248 on.
249 The sleep queue chain associated with argument
250 .Fa wchan
251 needs to have been locked with a prior call to
252 .Fn sleepq_lock .
253 The
254 .Fa pri
255 argument is used to set the priority of the thread when it is awakened.
256 If it is set to zero, the thread's priority is left alone.
257 .Pp
258 When the thread is resumed,
259 the wait functions return a non-zero value if the thread was awakened due to
260 an interrupt other than a signal or a timeout.
261 If the sleep timed out, then
262 .Er EWOULDBLOCK
263 is returned.
264 If the sleep was interrupted by something other than a signal,
265 then some other return value will be returned.
266 .Pp
267 A sleeping thread is normally resumed by the
268 .Fn sleepq_broadcast
269 and
270 .Fn sleepq_signal
271 functions.
272 The
273 .Fn sleepq_signal
274 function awakens the highest priority thread sleeping on a wait channel while
275 .Fn sleepq_broadcast
276 awakens all of the threads sleeping on a wait channel.
277 The
278 .Fa wchan
279 argument specifics which wait channel to awaken.
280 The
281 .Fa flags
282 argument must match the sleep queue type contained in the
283 .Fa flags
284 argument passed to
285 .Fn sleepq_add
286 by the threads sleeping on the wait channel.
287 If the
288 .Fa pri
289 argument does not equal \-1,
290 then each thread that is awakened will have its priority raised to
291 .Fa pri
292 if it has a lower priority.
293 The sleep queue chain associated with argument
294 .Fa wchan
295 must be locked by a prior call to
296 .Fn sleepq_lock
297 before calling any of these functions.
298 The
299 .Fa queue
300 argument specifies the sub-queue, from which threads need to be woken up.
301 .Pp
302 A thread in an interruptible sleep can be interrupted by another thread via
303 the
304 .Fn sleepq_abort
305 function.
306 The
307 .Fa td
308 argument specifies the thread to interrupt.
309 An individual thread can also be awakened from sleeping on a specific wait
310 channel via the
311 .Fn sleepq_remove
312 function.
313 The
314 .Fa td
315 argument specifies the thread to awaken and the
316 .Fa wchan
317 argument specifies the wait channel to awaken it from.
318 If the thread
319 .Fa td
320 is not blocked on the wait channel
321 .Fa wchan
322 then this function will not do anything,
323 even if the thread is asleep on a different wait channel.
324 This function should only be used if one of the other functions above is not
325 sufficient.
326 One possible use is waking up a specific thread from a widely shared sleep
327 channel.
328 .Pp
329 The
330 .Fn sleepq_sleepcnt
331 function offer a simple way to retrieve the number of threads sleeping for
332 the specified
333 .Fa queue ,
334 given a
335 .Fa wchan .
336 .Pp
337 The
338 .Fn sleepq_type
339 function returns the type of
340 .Fa wchan
341 associated to a sleepqueue.
342 .Pp
343 The
344 .Fn sleepq_abort ,
345 .Fn sleepq_broadcast ,
346 and
347 .Fn sleepq_signal
348 functions all return a boolean value.
349 If the return value is true,
350 then at least one thread was resumed that is currently swapped out.
351 The caller is responsible for awakening the scheduler process so that the
352 resumed thread will be swapped back in.
353 This is done by calling the
354 .Fn kick_proc0
355 function after releasing the sleep queue chain lock via a call to
356 .Fn sleepq_release .
357 .Pp
358 The sleep queue interface is currently used to implement the
359 .Xr sleep 9
360 and
361 .Xr condvar 9
362 interfaces.
363 Almost all other code in the kernel should use one of those interfaces rather
364 than manipulating sleep queues directly.
365 .Sh SEE ALSO
366 .Xr condvar 9 ,
367 .Xr runqueue 9 ,
368 .Xr scheduler 9 ,
369 .Xr sleep 9