xref: /freebsd/share/man/man9/sleep.9 (revision 721351876cd4d3a8a700f62d2061331fa951a488)
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 April 4, 2008
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
55The functions
56.Fn tsleep ,
57.Fn msleep ,
58.Fn msleep_spin ,
59.Fn pause ,
60.Fn wakeup ,
61and
62.Fn wakeup_one
63handle event-based thread blocking.
64If a thread must wait for an
65external event, it is put to sleep by
66.Fn tsleep ,
67.Fn msleep ,
68.Fn msleep_spin ,
69or
70.Fn pause .
71Threads may also wait using one of the locking primitive sleep routines
72.Xr mtx_sleep 9 ,
73.Xr rw_sleep 9 ,
74or
75.Xr sx_sleep 9 .
76.Pp
77The parameter
78.Fa chan
79is an arbitrary address that uniquely identifies the event on which
80the thread is being put to sleep.
81All threads sleeping on a single
82.Fa chan
83are woken up later by
84.Fn wakeup ,
85often called from inside an interrupt routine, to indicate that the
86resource the thread was blocking on is available now.
87.Pp
88The parameter
89.Fa priority
90specifies a new priority for the thread as well as some optional flags.
91If the new priority is not 0,
92then the thread will be made
93runnable with the specified
94.Fa priority
95when it resumes.
96.Dv PZERO
97should never be used, as it is for compatibility only.
98A new priority of 0 means to use the thread's current priority when
99it is made runnable again.
100If
101.Fa priority
102includes the
103.Dv PCATCH
104flag, signals are checked before and after sleeping, otherwise signals are
105not checked.
106If
107.Dv PCATCH
108is set and a signal needs to be delivered,
109.Er ERESTART
110is returned if the current system call should be restarted if
111possible, and
112.Er EINTR
113is returned if the system call should be interrupted by the signal
114(return
115.Er EINTR ) .
116.Pp
117The parameter
118.Fa wmesg
119is a string describing the sleep condition for tools like
120.Xr ps 1 .
121Due to the limited space of those programs to display arbitrary strings,
122this message should not be longer than 6 characters.
123.Pp
124The parameter
125.Fa timo
126specifies a timeout for the sleep.
127If
128.Fa timo
129is not 0,
130then the thread will sleep for at most
131.Fa timo No / Va hz
132seconds.
133If the timeout expires,
134then the sleep function will return
135.Er EWOULDBLOCK .
136.Pp
137Several of the sleep functions including
138.Fn msleep ,
139.Fn msleep_spin ,
140and the locking primitive sleep routines specify an additional lock
141parameter.
142The lock will be released before sleeping and reacquired
143before the sleep routine returns.
144If
145.Fa priority
146includes the
147.Dv PDROP
148flag, then
149the lock will not be reacquired before returning.
150The lock is used to ensure that a condition can be checked atomically,
151and that the current thread can be suspended without missing a
152change to the condition, or an associated wakeup.
153In addition, all of the sleep routines will fully drop the
154.Va Giant
155mutex
156(even if recursed)
157while the thread is suspended and will reacquire the
158.Va Giant
159mutex before the function returns.
160.Pp
161To avoid lost wakeups,
162either a lock should be used to protect against races,
163or a timeout should be specified to place an upper bound on the delay due
164to a lost wakeup.
165As a result,
166the
167.Fn tsleep
168function should only be invoked with a timeout of 0 when the
169.Va Giant
170mutex is held.
171.Pp
172The
173.Fn msleep
174function requires that
175.Fa mtx
176reference a default, i.e. non-spin, mutex.
177Its use is deprecated in favor of
178.Xr mtx_sleep 9
179which provides identical behavior.
180.Pp
181The
182.Fn msleep_spin
183function requires that
184.Fa mtx
185reference a spin mutex.
186The
187.Fn msleep_spin
188function does not accept a
189.Fa priority
190parameter and thus does not support changing the current thread's priority,
191the
192.Dv PDROP
193flag,
194or catching signals via the
195.Dv PCATCH
196flag.
197.Pp
198The
199.Fn pause
200function is a wrapper around
201.Fn tsleep
202that suspends execution of the current thread for the indicated timeout.
203The thread can not be awakened early by signals or calls to
204.Fn wakeup
205or
206.Fn wakeup_one .
207.Pp
208The
209.Fn wakeup_one
210function makes the first thread in the queue that is sleeping on the
211parameter
212.Fa chan
213runnable.
214This reduces the load when a large number of threads are sleeping on
215the same address, but only one of them can actually do any useful work
216when made runnable.
217.Pp
218Due to the way it works, the
219.Fn wakeup_one
220function requires that only related threads sleep on a specific
221.Fa chan
222address.
223It is the programmer's responsibility to choose a unique
224.Fa chan
225value.
226The older
227.Fn wakeup
228function did not require this, though it was never good practice
229for threads to share a
230.Fa chan
231value.
232When converting from
233.Fn wakeup
234to
235.Fn wakeup_one ,
236pay particular attention to ensure that no other threads wait on the
237same
238.Fa chan .
239.Sh RETURN VALUES
240If the thread is awakened by a call to
241.Fn wakeup
242or
243.Fn wakeup_one ,
244the
245.Fn msleep ,
246.Fn msleep_spin ,
247.Fn tsleep ,
248and locking primitive sleep functions return 0.
249Otherwise, a non-zero error code is returned.
250.Sh ERRORS
251.Fn msleep ,
252.Fn msleep_spin ,
253.Fn tsleep ,
254and the locking primitive sleep functions will fail if:
255.Bl -tag -width Er
256.It Bq Er EINTR
257The
258.Dv PCATCH
259flag was specified, a signal was caught, and the system call should be
260interrupted.
261.It Bq Er ERESTART
262The
263.Dv PCATCH
264flag was specified, a signal was caught, and the system call should be
265restarted.
266.It Bq Er EWOULDBLOCK
267A non-zero timeout was specified and the timeout expired.
268.El
269.Sh SEE ALSO
270.Xr ps 1 ,
271.Xr locking 9 ,
272.Xr malloc 9 ,
273.Xr mi_switch 9 ,
274.Xr mtx_sleep 9 ,
275.Xr rw_sleep 9 ,
276.Xr sx_sleep 9
277.Sh HISTORY
278The functions
279.Fn sleep
280and
281.Fn wakeup
282were present in
283.At v1 .
284They were probably also present in the preceding
285PDP-7 version of
286.Ux .
287They were the basic process synchronization model.
288.Pp
289The
290.Fn tsleep
291function appeared in
292.Bx 4.4
293and added the parameters
294.Fa wmesg
295and
296.Fa timo .
297The
298.Fn sleep
299function was removed in
300.Fx 2.2 .
301The
302.Fn wakeup_one
303function appeared in
304.Fx 2.2 .
305The
306.Fn msleep
307function appeared in
308.Fx 5.0 ,
309and the
310.Fn msleep_spin
311function appeared in
312.Fx 6.2 .
313The
314.Fn pause
315function appeared in
316.Fx 7.0 .
317.Sh AUTHORS
318.An -nosplit
319This manual page was written by
320.An J\(:org Wunsch Aq joerg@FreeBSD.org .
321