xref: /freebsd/share/man/man9/sleep.9 (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
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
99.Fa priority
100includes the
101.Dv PCATCH
102flag, signals are checked before and after sleeping, else signals are
103not checked.
104Returns 0 if awakened,
105.Er EWOULDBLOCK
106if the timeout expires.
107If
108.Dv PCATCH
109is set and a signal needs to be delivered,
110.Er ERESTART
111is returned if the current system call should be restarted if
112possible, and
113.Er EINTR
114is returned if the system call should be interrupted by the signal
115(return
116.Er EINTR ) .
117.Pp
118The
119.Fn msleep
120function is a variation on tsleep.
121The parameter
122.Fa mtx
123is a mutex which will be released before sleeping and reacquired before
124.Fn msleep
125returns.
126If
127.Fa priority
128includes the
129.Dv PDROP
130flag, the
131.Fa mtx
132parameter will not be reacquired before returning.
133The mutex is
134used to ensure that a condition can be checked atomically, and
135that the current process can be suspended without missing a
136change to the condition, or an associated wakeup.
137.Sh RETURN VALUES
138See above.
139.Sh SEE ALSO
140.Xr ps 1 ,
141.Xr malloc 9 ,
142.Xr mi_switch 9
143.Sh HISTORY
144The sleep/wakeup process synchronization mechanism is very old.
145It
146appeared in a very early version of
147.Ux .
148.Pp
149The
150.Fn tsleep
151function appeared in
152.Bx 4.4 .
153.Pp
154The
155.Fn sleep
156function used to be the traditional form.
157It did not let you specify a timeout or a
158.Fa wmesg ,
159hence it was discontinued.
160.Sh AUTHORS
161.An -nosplit
162This man page was written by
163.An J\(:org Wunsch Aq joerg@FreeBSD.org .
164