]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/sleep.9
mdoc(7) police:
[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 December 17, 1998
29 .Os
30 .Dt SLEEP 9
31 .Sh NAME
32 .Nm sleep ,
33 .Nm msleep ,
34 .Nm tsleep ,
35 .Nm asleep ,
36 .Nm await ,
37 .Nm wakeup
38 .Nd wait for events
39 .Sh SYNOPSIS
40 .Fd #include <sys/param.h>
41 .Fd #include <sys/systm.h>
42 .Fd #include <sys/proc.h>
43 .Ft int
44 .Fn tsleep "void *ident" "int priority" "const char *wmesg" "int timo"
45 .Ft int
46 .Fn msleep "void *ident" "struct mtx *mtx" "int priority" "const char *wmesg" "int timo"
47 .Ft int
48 .Fn asleep "void *ident" "int priority" "const char *wmesg" "int timo"
49 .Ft int
50 .Fn await "int priority" "int timo"
51 .Ft void
52 .Fn wakeup "void *ident"
53 .Ft void
54 .Fn wakeup_one "void *ident"
55 .Sh DESCRIPTION
56 The functions
57 .Fn tsleep
58 and
59 .Fn wakeup
60 handle event-based process blocking.  If a process must wait for an
61 external event, it is put on sleep by
62 .Nm tsleep .
63 The parameter
64 .Ar ident
65 is an arbitrary address that uniquely identifies the event on which
66 the process is being asleep.  All processes sleeping on a single
67 .Ar ident
68 are woken up later by
69 .Nm wakeup ,
70 often called from inside an interrupt routine, to indicate that the
71 resource the process was blocking on is available now.
72 .Pp
73 The parameter
74 .Ar wmesg
75 is a string describing the sleep condition for tools like
76 .Xr ps 1 .
77 Due to the limited space of those programs to display arbitrary strings,
78 this message should not be longer than 6 characters.
79 .Pp
80 The
81 .Fn wakeup_one
82 function is used to make the first process in the queue that is
83 sleeping on the parameter
84 .Fa ident
85 runnable.  This can prevent the system from becoming saturated
86 when a large number of processes are sleeping on the same address,
87 but only one of them can actually do any useful work when made
88 runnable.
89 .Pp
90 .Nm Tsleep
91 is the general sleep call.  Suspends the current process until a wakeup is
92 performed on the specified identifier.  The process will then be made
93 runnable with the specified
94 .Ar priority .
95 Sleeps at most
96 .Ar timo
97 \&/ hz seconds (0 means no timeout).  If
98 .Ar pri
99 includes the
100 .Dv PCATCH
101 flag, signals are checked before and after sleeping, else signals are
102 not checked.  Returns 0 if awakened,
103 .Er EWOULDBLOCK
104 if the timeout expires.  If
105 .Dv PCATCH
106 is set and a signal needs to be delivered,
107 .Er ERESTART
108 is returned if the current system call should be restarted if
109 possible, and
110 .Er EINTR
111 is returned if the system call should be interrupted by the signal
112 (return
113 .Er EINTR ) .
114 .Pp
115 .Nm Msleep
116 is a variation on tsleep.  The parameter
117 .Ar mtx
118 is a mutex, which will be exited before sleeping, and entered before
119 .Nm msleep
120 returns.  If
121 .Ar pri
122 includes the
123 .Dv PDROP
124 flag, the
125 .Ar mtx
126 parameter will not be entered before returning.  The mutex is
127 used to ensure that a condition can be checked atomicly, and
128 that the current process can be suspended without missing a
129 change to the condition, or an associated wakeup.
130 .Pp
131 .Nm Asleep
132 implements the new asynchronous sleep function.  It takes the same arguments
133 as
134 .Fn tsleep
135 and places the process on the appropriate wait queue, but
136 .Fn asleep
137 leaves the process runnable and returns immediately.  The caller is then
138 expected to, at some point in the future, call
139 .Fn await
140 to actually wait for the previously queued wait condition.
141 If
142 .Fn asleep
143 is called several times, only the most recent call is effective.
144 .Fn asleep
145 may be called with an
146 .Ar ident
147 value of NULL
148 to remove any previously queued condition.
149 .Pp
150 .Nm Await
151 implements the new asynchronous wait function.  When
152 .Fn asleep
153 is called on an identifier it associates the process with that
154 identifier but does not block.
155 .Fn await
156 will actually block the process until
157 .Fn wakeup
158 is called on that identifier any time after the
159 .Fn asleep .
160 If
161 .Fn wakeup
162 is called after you
163 .Fn asleep
164 but before you
165 .Fn await
166 then the
167 .Fn await
168 call is effectively a NOP.
169 If
170 .Fn await
171 is called multiple times without an intervening
172 .Fn asleep ,
173 the
174 .Fn await
175 is effectively a NOP but will also call
176 .Fn mi_switch
177 for safety.  The
178 .Fn await
179 function allows you to override the priority and timeout values to be used.
180 If the value -1 is specified for an argument, the value is taken from the
181 previous
182 .Fn asleep
183 call.  If -1 is passed for the priority you must be prepared to catch signal
184 conditions if the prior call to
185 .Fn asleep
186 specified it in its priority.  If -1 is passed for the timeout you must be
187 prepared to catch a timeout condition if the prior call to
188 .Fn asleep
189 specified a timeout.  When you use -1, it is usually a good idea to not make
190 assumptions as to the arguments used by the prior
191 .Fn asleep
192 call.
193 .Pp
194 The
195 .Fn asleep
196 and
197 .Fn await
198 functions are mainly used by the kernel to shift the burden of blocking
199 away from extremely low level routines and to push it onto their callers.
200 This in turn allows more complex interlocking code to
201 .Em backout
202 of a temporary resource failure
203 (such as lack of memory) in order to release major locks prior to actually
204 blocking, and to then retry the operation on wakeup.  This key feature is
205 expected to be heavily used in SMP situations in order to allow code to make
206 better use of spinlocks.  A spinlock, by its very nature, cannot be used
207 around code that might block.  It is hoped that these capabilities will
208 make it easier to migrate the SMP master locks deeper into the kernel.
209 .Pp
210 These routines may also be used to avoid nasty spl*() calls to get around
211 race conditions with simple conditional test/wait interlocks.  You simply
212 call
213 .Fn asleep
214 prior to your test, then conditionally
215 .Fn await
216 only if the test fails.  It is usually a good idea to cancel an
217 .Fn asleep
218 if you wind up never calling the related
219 .Fn await ,
220 but it is not required.  If you do not want to waste cpu calling
221 .Fn asleep
222 unnecessarily, you can surround the whole thing with a second test.  The
223 race condition is still handled by the inside
224 .Fn asleep
225 call.
226 .Sh RETURN VALUES
227 See above.
228 .Sh SEE ALSO
229 .Xr ps 1 ,
230 .Xr malloc 9 ,
231 .Xr mi_switch 9
232 .Sh HISTORY
233 The sleep/wakeup process synchronization mechanism is very old.  It
234 appeared in a very early version of Unix.
235 .Pp
236 .Nm Tsleep
237 appeared in
238 .Bx 4.4 .
239 .Pp
240 .Nm Asleep Ns / Ns Nm await
241 first appeared in
242 .Fx 3.0
243 and is designed to shift the burden of blocking
244 away from extremely low level routines and push it up to their callers.
245 .Pp
246 .Nm Sleep
247 used to be the traditional form.  It doesn't let you specify a timeout or a
248 .Ar wmesg ,
249 hence it has been discontinued.
250 .Sh AUTHORS
251 .An -nosplit
252 This man page was written by
253 .An J\(:org Wunsch .
254 .Nm Asleep
255 and
256 .Nm await
257 were designed and written by
258 .An Matthew Dillon .