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