aboutsummaryrefslogtreecommitdiff
path: root/share/man/man9/sleep.9
blob: bd421eaf6baa4252ddd1c12c5b0a23a14bac25ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
.\"
.\" Copyright (c) 1996 Joerg Wunsch
.\"
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $FreeBSD$
.\"
.Dd February 19, 2013
.Dt SLEEP 9
.Os
.Sh NAME
.Nm msleep ,
.Nm msleep_sbt ,
.Nm msleep_spin ,
.Nm msleep_spin_sbt ,
.Nm pause ,
.Nm pause_sbt ,
.Nm tsleep ,
.Nm tsleep_sbt ,
.Nm wakeup
.Nd wait for events
.Sh SYNOPSIS
.In sys/param.h
.In sys/systm.h
.In sys/proc.h
.Ft int
.Fn msleep "void *chan" "struct mtx *mtx" "int priority" "const char *wmesg" "int timo"
.Ft int
.Fn msleep_sbt "void *chan" "struct mtx *mtx" "int priority" \
"const char *wmesg" "sbintime_t sbt" "sbintime_t pr" "int flags"
.Ft int
.Fn msleep_spin "void *chan" "struct mtx *mtx" "const char *wmesg" "int timo"
.Ft int
.Fn msleep_spin_sbt "void *chan" "struct mtx *mtx" "const char *wmesg" \
"sbintime_t sbt" "sbintime_t pr" "int flags"
.Ft void
.Fn pause "const char *wmesg" "int timo"
.Ft void
.Fn pause_sbt "const char *wmesg" "sbintime_t sbt" "sbintime_t pr" \
 "int flags"
.Ft int
.Fn tsleep "void *chan" "int priority" "const char *wmesg" "int timo"
.Ft int
.Fn tsleep_sbt "void *chan" "int priority" "const char *wmesg" \
"sbintime_t sbt" "sbintime_t pr" "int flags"
.Ft void
.Fn wakeup "void *chan"
.Ft void
.Fn wakeup_one "void *chan"
.Sh DESCRIPTION
The functions
.Fn tsleep ,
.Fn msleep ,
.Fn msleep_spin ,
.Fn pause ,
.Fn wakeup ,
and
.Fn wakeup_one
handle event-based thread blocking.
If a thread must wait for an
external event, it is put to sleep by
.Fn tsleep ,
.Fn msleep ,
.Fn msleep_spin ,
or
.Fn pause .
Threads may also wait using one of the locking primitive sleep routines
.Xr mtx_sleep 9 ,
.Xr rw_sleep 9 ,
or
.Xr sx_sleep 9 .
.Pp
The parameter
.Fa chan
is an arbitrary address that uniquely identifies the event on which
the thread is being put to sleep.
All threads sleeping on a single
.Fa chan
are woken up later by
.Fn wakeup ,
often called from inside an interrupt routine, to indicate that the
resource the thread was blocking on is available now.
.Pp
The parameter
.Fa priority
specifies a new priority for the thread as well as some optional flags.
If the new priority is not 0,
then the thread will be made
runnable with the specified
.Fa priority
when it resumes.
.Dv PZERO
should never be used, as it is for compatibility only.
A new priority of 0 means to use the thread's current priority when
it is made runnable again.
.Pp
If
.Fa priority
includes the
.Dv PCATCH
flag, pending signals are allowed to interrupt the sleep, otherwise
pending signals are ignored during the sleep.
If
.Dv PCATCH
is set and a signal becomes pending,
.Er ERESTART
is returned if the current system call should be restarted if
possible, and
.Er EINTR
is returned if the system call should be interrupted by the signal
(return
.Er EINTR ) .
If the
.Dv PBDRY
flag is specified in addition to
.Dv PCATCH ,
then the sleeping thread is not stopped when
.Dv SIGSTOP
becomes pending
or some other stop action occurs while it is sleeping.
Instead, it is woken up, with the assumption
that the stop will occur on reaching a stop
point when returning to usermode.
The flag should be used when the sleeping thread owns resources, for instance
vnode locks, that should be released in a timely fashion.
.Pp
The parameter
.Fa wmesg
is a string describing the sleep condition for tools like
.Xr ps 1 .
Due to the limited space of those programs to display arbitrary strings,
this message should not be longer than 6 characters.
.Pp
The parameter
.Fa timo
specifies a timeout for the sleep.
If
.Fa timo
is not 0,
then the thread will sleep for at most
.Fa timo No / Va hz
seconds.
If the timeout expires,
then the sleep function will return
.Er EWOULDBLOCK .
.Pp
.Fn msleep_sbt ,
.Fn msleep_spin_sbt ,
.Fn pause_sbt
and
.Fn tsleep_sbt
functions take
.Fa sbt
parameter instead of
.Fa timo .
It allows the caller to specify relative or absolute wakeup time with higher resolution
in form of
.Vt sbintime_t .
The parameter
.Fa pr
allows the caller to specify wanted absolute event precision.
The parameter
.Fa flags
allows the caller to pass additional
.Fn callout_reset_sbt
flags.
.Pp
Several of the sleep functions including
.Fn msleep ,
.Fn msleep_spin ,
and the locking primitive sleep routines specify an additional lock
parameter.
The lock will be released before sleeping and reacquired
before the sleep routine returns.
If
.Fa priority
includes the
.Dv PDROP
flag, then
the lock will not be reacquired before returning.
The lock is used to ensure that a condition can be checked atomically,
and that the current thread can be suspended without missing a
change to the condition, or an associated wakeup.
In addition, all of the sleep routines will fully drop the
.Va Giant
mutex
(even if recursed)
while the thread is suspended and will reacquire the
.Va Giant
mutex before the function returns.
Note that the
.Va Giant
mutex may be specified as the lock to drop.
In that case, however, the
.Dv PDROP
flag is not allowed.
.Pp
To avoid lost wakeups,
either a lock should be used to protect against races,
or a timeout should be specified to place an upper bound on the delay due
to a lost wakeup.
As a result,
the
.Fn tsleep
function should only be invoked with a timeout of 0 when the
.Va Giant
mutex is held.
.Pp
The
.Fn msleep
function requires that
.Fa mtx
reference a default, i.e. non-spin, mutex.
Its use is deprecated in favor of
.Xr mtx_sleep 9
which provides identical behavior.
.Pp
The
.Fn msleep_spin
function requires that
.Fa mtx
reference a spin mutex.
The
.Fn msleep_spin
function does not accept a
.Fa priority
parameter and thus does not support changing the current thread's priority,
the
.Dv PDROP
flag,
or catching signals via the
.Dv PCATCH
flag.
.Pp
The
.Fn pause
function is a wrapper around
.Fn tsleep
that suspends execution of the current thread for the indicated timeout.
The thread can not be awakened early by signals or calls to
.Fn wakeup
or
.Fn wakeup_one .
.Pp
The
.Fn wakeup_one
function makes the first thread in the queue that is sleeping on the
parameter
.Fa chan
runnable.
This reduces the load when a large number of threads are sleeping on
the same address, but only one of them can actually do any useful work
when made runnable.
.Pp
Due to the way it works, the
.Fn wakeup_one
function requires that only related threads sleep on a specific
.Fa chan
address.
It is the programmer's responsibility to choose a unique
.Fa chan
value.
The older
.Fn wakeup
function did not require this, though it was never good practice
for threads to share a
.Fa chan
value.
When converting from
.Fn wakeup
to
.Fn wakeup_one ,
pay particular attention to ensure that no other threads wait on the
same
.Fa chan .
.Sh RETURN VALUES
When awakened by a call to
.Fn wakeup
or
.Fn wakeup_one ,
if a signal is pending and
.Dv PCATCH
is specified,
a non-zero error code is returned.
If the thread is awakened by a call to
.Fn wakeup
or
.Fn wakeup_one ,
the
.Fn msleep ,
.Fn msleep_spin ,
.Fn tsleep ,
and locking primitive sleep functions return 0.
Otherwise, a non-zero error code is returned.
.Sh ERRORS
.Fn msleep ,
.Fn msleep_spin ,
.Fn tsleep ,
and the locking primitive sleep functions will fail if:
.Bl -tag -width Er
.It Bq Er EINTR
The
.Dv PCATCH
flag was specified, a signal was caught, and the system call should be
interrupted.
.It Bq Er ERESTART
The
.Dv PCATCH
flag was specified, a signal was caught, and the system call should be
restarted.
.It Bq Er EWOULDBLOCK
A non-zero timeout was specified and the timeout expired.
.El
.Sh SEE ALSO
.Xr ps 1 ,
.Xr locking 9 ,
.Xr malloc 9 ,
.Xr mi_switch 9 ,
.Xr mtx_sleep 9 ,
.Xr rw_sleep 9 ,
.Xr sx_sleep 9 ,
.Xr timeout 9
.Sh HISTORY
The functions
.Fn sleep
and
.Fn wakeup
were present in
.At v1 .
They were probably also present in the preceding
PDP-7 version of
.Ux .
They were the basic process synchronization model.
.Pp
The
.Fn tsleep
function appeared in
.Bx 4.4
and added the parameters
.Fa wmesg
and
.Fa timo .
The
.Fn sleep
function was removed in
.Fx 2.2 .
The
.Fn wakeup_one
function appeared in
.Fx 2.2 .
The
.Fn msleep
function appeared in
.Fx 5.0 ,
and the
.Fn msleep_spin
function appeared in
.Fx 6.2 .
The
.Fn pause
function appeared in
.Fx 7.0 .
.Sh AUTHORS
.An -nosplit
This manual page was written by
.An J\(:org Wunsch Aq Mt joerg@FreeBSD.org .