xref: /freebsd/share/man/man9/sleep.9 (revision 2bc6540439d0932b38067c9cc321fa0e2a61f264)
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, then
103.Fa timo
104must be non-zero.
105If
106.Fa priority
107includes the
108.Dv PCATCH
109flag, signals are checked before and after sleeping, otherwise signals are
110not checked.
111Returns 0 if awakened,
112.Er EWOULDBLOCK
113if the timeout expires.
114If
115.Dv PCATCH
116is set and a signal needs to be delivered,
117.Er ERESTART
118is returned if the current system call should be restarted if
119possible, and
120.Er EINTR
121is returned if the system call should be interrupted by the signal
122(return
123.Er EINTR ) .
124.Pp
125The
126.Fn msleep
127function is a variation on tsleep.
128The parameter
129.Fa mtx
130is a mutex which will be released before sleeping and reacquired before
131.Fn msleep
132returns.
133If
134.Fa priority
135includes the
136.Dv PDROP
137flag, the
138.Fa mtx
139parameter will not be reacquired before returning.
140The mutex is
141used to ensure that a condition can be checked atomically, and
142that the current process can be suspended without missing a
143change to the condition, or an associated wakeup.
144.Pp
145The
146.Fn msleep_spin
147function is another variation on
148.Fn tsleep
149similar to
150.Fn msleep .
151Unlike
152.Fn msleep ,
153this function accepts a spin mutex rather than a default mutex for its
154.Fa mtx
155parameter.
156It is also more limited in that it does not accept a
157.Fa priority
158parameter.
159Thus, it will not change the priority of a sleeping thread,
160and it does not support the
161.Dv PDROP
162and
163.Dv PCATCH
164flags.
165.Sh RETURN VALUES
166See above.
167.Sh SEE ALSO
168.Xr ps 1 ,
169.Xr malloc 9 ,
170.Xr mi_switch 9
171.Sh HISTORY
172The sleep/wakeup process synchronization mechanism is very old.
173It
174appeared in a very early version of
175.Ux .
176.Pp
177The
178.Fn tsleep
179function appeared in
180.Bx 4.4 .
181The
182.Fn msleep
183function appeared in
184.Fx 5.0 ,
185and the
186.Fn msleep_spin
187function appeared in
188.Fx 7.0 .
189.Pp
190The
191.Fn sleep
192function used to be the traditional form.
193It did not let you specify a timeout or a
194.Fa wmesg ,
195hence it was discontinued.
196.Sh AUTHORS
197.An -nosplit
198This manual page was written by
199.An J\(:org Wunsch Aq joerg@FreeBSD.org .
200