xref: /freebsd/share/man/man9/sleep.9 (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
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 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 *ident" "int priority" "const char *wmesg" "int timo"
43.Ft int
44.Fn msleep "void *ident" "struct mtx *mtx" "int priority" "const char *wmesg" "int timo"
45.Ft void
46.Fn wakeup "void *ident"
47.Ft void
48.Fn wakeup_one "void *ident"
49.Sh DESCRIPTION
50The functions
51.Fn tsleep
52and
53.Fn wakeup
54handle event-based process blocking.
55If a process must wait for an
56external event, it is put on sleep by
57.Fn tsleep .
58The parameter
59.Fa ident
60is an arbitrary address that uniquely identifies the event on which
61the process is being asleep.
62All processes sleeping on a single
63.Fa ident
64are woken up later by
65.Fn wakeup ,
66often called from inside an interrupt routine, to indicate that the
67resource the process was blocking on is available now.
68.Pp
69The parameter
70.Fa wmesg
71is a string describing the sleep condition for tools like
72.Xr ps 1 .
73Due to the limited space of those programs to display arbitrary strings,
74this message should not be longer than 6 characters.
75.Pp
76The
77.Fn wakeup_one
78function is used to make the first process in the queue that is
79sleeping on the parameter
80.Fa ident
81runnable.
82This can prevent the system from becoming saturated
83when a large number of processes are sleeping on the same address,
84but only one of them can actually do any useful work when made
85runnable.
86.Pp
87The
88.Fn tsleep
89function is the general sleep call.
90Suspends the current process until a wakeup is
91performed on the specified identifier.
92The process will then be made
93runnable with the specified
94.Fa priority .
95Sleeps at most
96.Fa timo
97\&/ hz seconds (0 means no timeout).
98If the
99.Va Giant
100lock is not held, then
101.Fa timo
102must be non-zero.
103If
104.Fa priority
105includes the
106.Dv PCATCH
107flag, signals are checked before and after sleeping, else signals are
108not checked.
109Returns 0 if awakened,
110.Er EWOULDBLOCK
111if the timeout expires.
112If
113.Dv PCATCH
114is set and a signal needs to be delivered,
115.Er ERESTART
116is returned if the current system call should be restarted if
117possible, and
118.Er EINTR
119is returned if the system call should be interrupted by the signal
120(return
121.Er EINTR ) .
122.Pp
123The
124.Fn msleep
125function is a variation on tsleep.
126The parameter
127.Fa mtx
128is a mutex which will be released before sleeping and reacquired before
129.Fn msleep
130returns.
131If
132.Fa priority
133includes the
134.Dv PDROP
135flag, the
136.Fa mtx
137parameter will not be reacquired before returning.
138The mutex is
139used to ensure that a condition can be checked atomically, and
140that the current process can be suspended without missing a
141change to the condition, or an associated wakeup.
142.Sh RETURN VALUES
143See above.
144.Sh SEE ALSO
145.Xr ps 1 ,
146.Xr malloc 9 ,
147.Xr mi_switch 9
148.Sh HISTORY
149The sleep/wakeup process synchronization mechanism is very old.
150It
151appeared in a very early version of
152.Ux .
153.Pp
154The
155.Fn tsleep
156function appeared in
157.Bx 4.4 .
158.Pp
159The
160.Fn sleep
161function used to be the traditional form.
162It did not let you specify a timeout or a
163.Fa wmesg ,
164hence it was discontinued.
165.Sh AUTHORS
166.An -nosplit
167This man page was written by
168.An J\(:org Wunsch Aq joerg@FreeBSD.org .
169