aboutsummaryrefslogtreecommitdiff
path: root/share/man/man9/sleep.9
blob: 1562c55aefd15515733cf0920ff1abcbb8c75b7d (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
.\"
.\" 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 December 17, 1998
.Os
.Dt SLEEP 9
.Sh NAME
.Nm msleep ,
.Nm msleep_spin ,
.Nm tsleep ,
.Nm wakeup
.Nd wait for events
.Sh SYNOPSIS
.In sys/param.h
.In sys/systm.h
.In sys/proc.h
.Ft int
.Fn tsleep "void *chan" "int priority" "const char *wmesg" "int timo"
.Ft int
.Fn msleep "void *chan" "struct mtx *mtx" "int priority" "const char *wmesg" "int timo"
.Ft int
.Fn msleep_spin "void *chan" "struct mtx *mtx" "const char *wmesg" "int timo"
.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 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 ,
or
.Fn msleep_spin .
The parameter
.Fa chan
is an arbitrary address that uniquely identifies the event on which
the thread is being asleep.
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 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
.Fn wakeup_one
function is used to make the first thread in the queue that is
sleeping on the parameter
.Fa chan
runnable.
This can prevent the system from becoming saturated
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
The
.Fn msleep
function is the general sleep call.
It suspends the current thread until a wakeup is
performed on the specified identifier.
The
.Fa mtx
parameter is a mutex which will be released before sleeping and reacquired
before
.Fn msleep
returns.
If
.Fa priority
includes the
.Dv PDROP
flag, the
.Fa mtx
parameter will not be reacquired before returning.
The mutex 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.
If
.Fa priority
is not 0,
then the thread will be made
runnable with the specified
.Fa priority
when it resumes.
If
.Fa timo
is not 0,
then the thread will sleep for at most
.Fa timo
\&/ hz seconds.
If the
.Va Giant
lock is not held and
.Fa mtx
is
.Dv NULL ,
then
.Fa timo
must be non-zero.
If
.Fa priority
includes the
.Dv PCATCH
flag, signals are checked before and after sleeping, otherwise signals are
not checked.
The
.Fn msleep
function returns 0 if awakened,
.Er EWOULDBLOCK
if the timeout expires.
If
.Dv PCATCH
is set and a signal needs to be delivered,
.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 ) .
.Pp
The
.Fn tsleep
function is a variation on
.Fn msleep .
It is identical to invoking
.Fn msleep
with a
.Dv NULL
.Fa mtx
parameter.
.Pp
The
.Fn msleep_spin
function is another variation on
.Fn msleep .
This function accepts a spin mutex rather than a default mutex for its
.Fa mtx
parameter.
It is also more limited in that it does not accept a
.Fa priority
parameter.
Thus, it will not change the priority of a sleeping thread,
and it does not support the
.Dv PDROP
and
.Dv PCATCH
flags.
.Sh RETURN VALUES
See above.
.Sh SEE ALSO
.Xr ps 1 ,
.Xr malloc 9 ,
.Xr mi_switch 9
.Sh HISTORY
The sleep/wakeup thread synchronization mechanism is very old.
It
appeared in a very early version of
.Ux .
.Pp
The
.Fn tsleep
function appeared in
.Bx 4.4 .
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 7.0 .
.Pp
The
.Fn sleep
function used to be the traditional form.
It did not let you specify a timeout or a
.Fa wmesg ,
hence it was discontinued.
.Sh AUTHORS
.An -nosplit
This manual page was written by
.An J\(:org Wunsch Aq joerg@FreeBSD.org .