]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/sleep.9
This commit was generated by cvs2svn to compensate for changes in r171366,
[FreeBSD/FreeBSD.git] / share / man / man9 / sleep.9
1 .\"
2 .\" Copyright (c) 1996 Joerg Wunsch
3 .\"
4 .\" All rights reserved.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\"    notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\"    notice, this list of conditions and the following disclaimer in the
13 .\"    documentation and/or other materials provided with the distribution.
14 .\"
15 .\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
16 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 .\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 .\"
26 .\" $FreeBSD$
27 .\"
28 .Dd February 27, 2007
29 .Os
30 .Dt SLEEP 9
31 .Sh NAME
32 .Nm msleep ,
33 .Nm msleep_spin ,
34 .Nm pause ,
35 .Nm tsleep ,
36 .Nm wakeup
37 .Nd wait for events
38 .Sh SYNOPSIS
39 .In sys/param.h
40 .In sys/systm.h
41 .In sys/proc.h
42 .Ft int
43 .Fn msleep "void *chan" "struct mtx *mtx" "int priority" "const char *wmesg" "int timo"
44 .Ft int
45 .Fn msleep_spin "void *chan" "struct mtx *mtx" "const char *wmesg" "int timo"
46 .Ft void
47 .Fn pause "const char *wmesg" "int timo"
48 .Ft int
49 .Fn tsleep "void *chan" "int priority" "const char *wmesg" "int timo"
50 .Ft void
51 .Fn wakeup "void *chan"
52 .Ft void
53 .Fn wakeup_one "void *chan"
54 .Sh DESCRIPTION
55 The functions
56 .Fn tsleep ,
57 .Fn msleep ,
58 .Fn msleep_spin ,
59 .Fn pause ,
60 .Fn wakeup ,
61 and
62 .Fn wakeup_one
63 handle event-based thread blocking.
64 If a thread must wait for an
65 external event, it is put to sleep by
66 .Fn tsleep ,
67 .Fn msleep ,
68 .Fn msleep_spin ,
69 or
70 .Fn pause .
71 Threads may also wait using one of the locking primitive sleep routines
72 .Xr mtx_sleep 9 ,
73 .Xr rw_sleep 9 ,
74 or
75 .Xr sx_sleep 9 .
76 .Pp
77 The parameter
78 .Fa chan
79 is an arbitrary address that uniquely identifies the event on which
80 the thread is being put to sleep.
81 All threads sleeping on a single
82 .Fa chan
83 are woken up later by
84 .Fn wakeup ,
85 often called from inside an interrupt routine, to indicate that the
86 resource the thread was blocking on is available now.
87 .Pp
88 The parameter
89 .Fa priority
90 specifies a new priority for the thread as well as some optional flags.
91 If the new priority is not 0,
92 then the thread will be made
93 runnable with the specified
94 .Fa priority
95 when it resumes.
96 If
97 .Fa priority
98 includes the
99 .Dv PCATCH
100 flag, signals are checked before and after sleeping, otherwise signals are
101 not checked.
102 If
103 .Dv PCATCH
104 is set and a signal needs to be delivered,
105 .Er ERESTART
106 is returned if the current system call should be restarted if
107 possible, and
108 .Er EINTR
109 is returned if the system call should be interrupted by the signal
110 (return
111 .Er EINTR ) .
112 .Pp
113 The parameter
114 .Fa wmesg
115 is a string describing the sleep condition for tools like
116 .Xr ps 1 .
117 Due to the limited space of those programs to display arbitrary strings,
118 this message should not be longer than 6 characters.
119 .Pp
120 The parameter
121 .Fa timo
122 specifies a timeout for the sleep.
123 If
124 .Fa timo
125 is not 0,
126 then the thread will sleep for at most
127 .Fa timo No / Va hz
128 seconds.
129 If the timeout expires,
130 then the sleep function will return
131 .Er EWOULDBLOCK .
132 .Pp
133 Several of the sleep functions including
134 .Fn msleep ,
135 .Fn msleep_spin ,
136 and the locking primitive sleep routines specify an additional lock
137 parameter.
138 The lock will be released before sleeping and reacquired
139 before the sleep routine returns.
140 If
141 .Fa priority
142 includes the
143 .Dv PDROP
144 flag, then
145 the lock will not be reacquired before returning.
146 The lock is used to ensure that a condition can be checked atomically,
147 and that the current thread can be suspended without missing a
148 change to the condition, or an associated wakeup.
149 In addition, all of the sleep routines will fully drop the
150 .Va Giant
151 mutex
152 (even if recursed)
153 while the thread is suspended and will reacquire the
154 .Va Giant
155 mutex before the function returns.
156 .Pp
157 To avoid lost wakeups,
158 either a lock should be used to protect against races,
159 or a timeout should be specified to place an upper bound on the delay due
160 to a lost wakeup.
161 As a result,
162 the
163 .Fn tsleep
164 function should only be invoked with a timeout of 0 when the
165 .Va Giant
166 mutex is held.
167 .Pp
168 The
169 .Fn msleep
170 function requires that
171 .Fa mtx
172 reference a default, i.e. non-spin, mutex.
173 Its use is deprecated in favor of
174 .Xr mtx_sleep 9
175 which provides identical behavior.
176 .Pp
177 The
178 .Fn msleep_spin
179 function requires that
180 .Fa mtx
181 reference a spin mutex.
182 The
183 .Fn msleep_spin
184 function does not accept a
185 .Fa priority
186 parameter and thus does not support changing the current thread's priority,
187 the
188 .Dv PDROP
189 flag,
190 or catching signals via the
191 .Dv PCATCH
192 flag.
193 .Pp
194 The
195 .Fn pause
196 function is a wrapper around
197 .Fn tsleep
198 that suspends execution of the current thread for the indicated timeout.
199 The thread can not be awakened early by signals or calls to
200 .Fn wakeup
201 or
202 .Fn wakeup_one .
203 .Pp
204 The
205 .Fn wakeup_one
206 function makes the first thread in the queue that is sleeping on the
207 parameter
208 .Fa chan
209 runnable.
210 This reduces the load when a large number of threads are sleeping on
211 the same address, but only one of them can actually do any useful work
212 when made runnable.
213 .Pp
214 Due to the way it works, the
215 .Fn wakeup_one
216 function requires that only related threads sleep on a specific
217 .Fa chan
218 address.
219 It is the programmer's responsibility to choose a unique
220 .Fa chan
221 value.
222 The older
223 .Fn wakeup
224 function did not require this, though it was never good practice 
225 for threads to share a
226 .Fa chan
227 value.
228 When converting from
229 .Fn wakeup
230 to
231 .Fn wakeup_one ,
232 pay particular attention to ensure that no other threads wait on the
233 same
234 .Fa chan .
235 .Sh RETURN VALUES
236 If the thread is awakened by a call to
237 .Fn wakeup
238 or
239 .Fn wakeup_one ,
240 the
241 .Fn msleep ,
242 .Fn msleep_spin ,
243 .Fn tsleep ,
244 and locking primitive sleep functions return 0.
245 Otherwise, a non-zero error code is returned.
246 .Sh ERRORS
247 .Fn msleep ,
248 .Fn msleep_spin ,
249 .Fn tsleep ,
250 and the locking primitive sleep functions will fail if:
251 .Bl -tag -width Er
252 .It Bq Er EINTR
253 The
254 .Dv PCATCH
255 flag was specified, a signal was caught, and the system call should be
256 interrupted.
257 .It Bq Er ERESTART
258 The
259 .Dv PCATCH
260 flag was specified, a signal was caught, and the system call should be
261 restarted.
262 .It Bq Er EWOULDBLOCK
263 A non-zero timeout was specified and the timeout expired.
264 .El
265 .Sh SEE ALSO
266 .Xr ps 1 ,
267 .Xr locking 9 ,
268 .Xr malloc 9 ,
269 .Xr mi_switch 9 ,
270 .Xr mtx_sleep 9 ,
271 .Xr rw_sleep 9 ,
272 .Xr sx_sleep 9
273 .Sh HISTORY
274 The functions
275 .Fn sleep
276 and
277 .Fn wakeup
278 were present in
279 .At v1 .
280 They were probably also present in the preceding
281 PDP-7 version of
282 .Ux .
283 They were the basic process synchronization model.
284 .Pp
285 The
286 .Fn tsleep
287 function appeared in
288 .Bx 4.4
289 and added the parameters
290 .Fa wmesg
291 and
292 .Fa timo .
293 The
294 .Fn sleep
295 function was removed in
296 .Fx 2.2 .
297 The
298 .Fn wakeup_one
299 function appeared in
300 .Fx 2.2 .
301 The
302 .Fn msleep
303 function appeared in
304 .Fx 5.0 ,
305 and the
306 .Fn msleep_spin
307 function appeared in
308 .Fx 6.2 .
309 The
310 .Fn pause
311 function appeared in
312 .Fx 7.0 .
313 .Sh AUTHORS
314 .An -nosplit
315 This manual page was written by
316 .An J\(:org Wunsch Aq joerg@FreeBSD.org .