xref: /freebsd/share/man/man9/sleep.9 (revision 87569f75a91f298c52a71823c04d41cf53c88889)
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 December 17, 1998
29.Os
30.Dt SLEEP 9
31.Sh NAME
32.Nm msleep ,
33.Nm msleep_spin ,
34.Nm tsleep ,
35.Nm wakeup
36.Nd wait for events
37.Sh SYNOPSIS
38.In sys/param.h
39.In sys/systm.h
40.In sys/proc.h
41.Ft int
42.Fn tsleep "void *chan" "int priority" "const char *wmesg" "int timo"
43.Ft int
44.Fn msleep "void *chan" "struct mtx *mtx" "int priority" "const char *wmesg" "int timo"
45.Ft int
46.Fn msleep_spin "void *chan" "struct mtx *mtx" "const char *wmesg" "int timo"
47.Ft void
48.Fn wakeup "void *chan"
49.Ft void
50.Fn wakeup_one "void *chan"
51.Sh DESCRIPTION
52The functions
53.Fn tsleep
54and
55.Fn wakeup
56handle event-based process blocking.
57If a process must wait for an
58external event, it is put on sleep by
59.Fn tsleep .
60The parameter
61.Fa chan
62is an arbitrary address that uniquely identifies the event on which
63the process is being asleep.
64All processes sleeping on a single
65.Fa chan
66are woken up later by
67.Fn wakeup ,
68often called from inside an interrupt routine, to indicate that the
69resource the process was blocking on is available now.
70.Pp
71The parameter
72.Fa wmesg
73is a string describing the sleep condition for tools like
74.Xr ps 1 .
75Due to the limited space of those programs to display arbitrary strings,
76this message should not be longer than 6 characters.
77.Pp
78The
79.Fn wakeup_one
80function is used to make the first process in the queue that is
81sleeping on the parameter
82.Fa chan
83runnable.
84This can prevent the system from becoming saturated
85when a large number of processes are sleeping on the same address,
86but only one of them can actually do any useful work when made
87runnable.
88.Pp
89The
90.Fn tsleep
91function is the general sleep call.
92Suspends the current process until a wakeup is
93performed on the specified identifier.
94The process will then be made
95runnable with the specified
96.Fa priority .
97Sleeps at most
98.Fa timo
99\&/ hz seconds (0 means no timeout).
100If the
101.Va Giant
102lock is not held and
103.Fa mtx
104is
105.Dv NULL ,
106then
107.Fa timo
108must be non-zero.
109If
110.Fa priority
111includes the
112.Dv PCATCH
113flag, signals are checked before and after sleeping, otherwise signals are
114not checked.
115Returns 0 if awakened,
116.Er EWOULDBLOCK
117if the timeout expires.
118If
119.Dv PCATCH
120is set and a signal needs to be delivered,
121.Er ERESTART
122is returned if the current system call should be restarted if
123possible, and
124.Er EINTR
125is returned if the system call should be interrupted by the signal
126(return
127.Er EINTR ) .
128.Pp
129The
130.Fn msleep
131function is a variation on tsleep.
132The parameter
133.Fa mtx
134is a mutex which will be released before sleeping and reacquired before
135.Fn msleep
136returns.
137If
138.Fa priority
139includes the
140.Dv PDROP
141flag, the
142.Fa mtx
143parameter will not be reacquired before returning.
144The mutex is
145used to ensure that a condition can be checked atomically, and
146that the current process can be suspended without missing a
147change to the condition, or an associated wakeup.
148.Pp
149The
150.Fn msleep_spin
151function is another variation on
152.Fn tsleep
153similar to
154.Fn msleep .
155Unlike
156.Fn msleep ,
157this function accepts a spin mutex rather than a default mutex for its
158.Fa mtx
159parameter.
160It is also more limited in that it does not accept a
161.Fa priority
162parameter.
163Thus, it will not change the priority of a sleeping thread,
164and it does not support the
165.Dv PDROP
166and
167.Dv PCATCH
168flags.
169.Sh RETURN VALUES
170See above.
171.Sh SEE ALSO
172.Xr ps 1 ,
173.Xr malloc 9 ,
174.Xr mi_switch 9
175.Sh HISTORY
176The sleep/wakeup process synchronization mechanism is very old.
177It
178appeared in a very early version of
179.Ux .
180.Pp
181The
182.Fn tsleep
183function appeared in
184.Bx 4.4 .
185The
186.Fn msleep
187function appeared in
188.Fx 5.0 ,
189and the
190.Fn msleep_spin
191function appeared in
192.Fx 7.0 .
193.Pp
194The
195.Fn sleep
196function used to be the traditional form.
197It did not let you specify a timeout or a
198.Fa wmesg ,
199hence it was discontinued.
200.Sh AUTHORS
201.An -nosplit
202This manual page was written by
203.An J\(:org Wunsch Aq joerg@FreeBSD.org .
204