xref: /openbsd-src/share/man/man9/tsleep.9 (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1.\"	$OpenBSD: tsleep.9,v 1.11 2016/09/13 08:32:44 mpi Exp $
2.\"	$NetBSD: sleep.9,v 1.11 1999/03/24 06:15:12 mycroft Exp $
3.\"
4.\" Copyright (c) 1996 The NetBSD Foundation, Inc.
5.\" All rights reserved.
6.\"
7.\" This code is derived from software contributed to The NetBSD Foundation
8.\" by Paul Kranenburg.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29.\" POSSIBILITY OF SUCH DAMAGE.
30.\"
31.Dd $Mdocdate: September 13 2016 $
32.Dt TSLEEP 9
33.Os
34.Sh NAME
35.Nm tsleep ,
36.Nm msleep ,
37.Nm rwsleep ,
38.Nm wakeup ,
39.Nm wakeup_n ,
40.Nm wakeup_one
41.Nd process context sleep and wakeup
42.Sh SYNOPSIS
43.In sys/param.h
44.In sys/systm.h
45.Ft int
46.Fn tsleep "void *ident" "int priority" "const char *wmesg" "int timo"
47.Ft int
48.Fn msleep "void *ident" "struct mutex *mtx" "int priority" "const char *wmesg" "int timo"
49.Ft int
50.Fn rwsleep "void *ident" "struct rwlock *wl" "int priority" "const char *wmesg" "int timo"
51.Ft void
52.Fn wakeup "void *ident"
53.Ft void
54.Fn wakeup_n "void *ident" "int count"
55.Ft void
56.Fn wakeup_one "void *ident"
57.Sh DESCRIPTION
58These functions implement voluntary context switching.
59.Fn tsleep ,
60.Fn msleep
61and
62.Fn rwsleep
63are used throughout the kernel whenever processing in the current context
64cannot continue for any of the following reasons:
65.Bl -bullet -offset indent
66.It
67The current process needs to await the results of a pending I/O operation.
68.It
69The current process needs resources
70.Pq e.g. memory
71which are temporarily unavailable.
72.It
73The current process wants access to data structures which are locked by
74other processes.
75.El
76.Pp
77The
78.Fn wakeup ,
79.Fn wakeup_n ,
80and
81.Fn wakeup_one
82functions are used to notify sleeping processes of possible changes to the
83condition that caused them to go to sleep.
84Typically, an awakened process will -- after it has acquired a context
85again -- retry the action that blocked its operation to see if the
86.Dq blocking
87condition has cleared.
88.Pp
89The
90.Fn tsleep
91function takes the following arguments:
92.Bl -tag -width priority
93.It Fa ident
94An identifier of the
95.Dq wait channel
96representing the resource for which the current process needs to wait.
97This typically is the virtual address of some kernel data structure related
98to the resource for which the process is contending.
99The same identifier must be used in a call to
100.Fn wakeup
101to get the process going again.
102.Fa ident
103should not be
104.Dv NULL .
105.It Fa priority
106The process priority to be used when the process is awakened and put on
107the queue of runnable processes.
108This mechanism is used to optimize
109.Dq throughput
110of processes executing in kernel mode.
111If the flag
112.Dv PCATCH
113is OR'ed into
114.Fa priority
115the process checks for posted signals before and after sleeping.
116.It Fa wmesg
117A pointer to a character string indicating the reason a process is sleeping.
118The kernel does not use the string, but makes it available
119.Pq through the process structure field Li p_wmesg
120for user level utilities such as
121.Xr ps 1 .
122.It Fa timo
123If non-zero, the process will sleep for at most
124.Li timo/hz
125seconds.
126If this amount of time elapses and no
127.Fn wakeup "ident"
128has occurred, and no signal
129.Pq if Dv PCATCH No was set
130was posted,
131.Fn tsleep
132will return
133.Er EWOULDBLOCK .
134.El
135.Pp
136The
137.Fn msleep
138function behaves just like
139.Fn tsleep ,
140but takes an additional argument:
141.Bl -tag -width priority
142.It Fa mtx
143A mutex that will be unlocked when the process is safely
144on the sleep queue.
145The mutex will be relocked at the end of msleep unless the
146.Dv PNORELOCK
147flag is set in the
148.Fa priority
149argument.
150.El
151.Pp
152The
153.Fn rwsleep
154function behaves just like
155.Fn tsleep ,
156but takes an additional argument:
157.Bl -tag -width priority
158.It Fa wl
159A write lock that will be unlocked when the process is safely
160on the sleep queue.
161The write lock will be relocked at the end of rwsleep unless the
162.Dv PNORELOCK
163flag is set in the
164.Fa priority
165argument.
166.El
167.Pp
168The
169.Fn wakeup
170function will mark all processes which are currently sleeping on the identifier
171.Fa ident
172as runnable.
173Eventually, each of the processes will resume execution in the kernel
174context, causing a return from
175.Fn tsleep .
176Note that processes returning from sleep should always re-evaluate the
177conditions that blocked them, since a call to
178.Fn wakeup
179merely signals a
180.Em possible
181change to the blocking conditions.
182For example, when two or more processes are waiting for an exclusive lock,
183only one of them will succeed in acquiring the lock when it is released.
184All others will have to go back to sleep and wait for the next opportunity.
185.Pp
186The
187.Fn wakeup_n
188and
189.Fn wakeup_one
190functions behave similarly to
191.Fn wakeup
192except that only
193.Fa count
194or one process, respectively, is marked runnable.
195.Sh RETURN VALUES
196.Fn tsleep ,
197.Fn msleep
198and
199.Fn rwsleep
200return 0 if they return as a result of a
201.Fn wakeup .
202If they return as a result of a signal, the return value is
203.Er ERESTART
204if the signal has the
205.Dv SA_RESTART
206property
207.Pq see Xr sigaction 2 ,
208and
209.Er EINTR
210otherwise.
211If they return as a result of a timeout, the return value is
212.Er EWOULDBLOCK .
213.Sh CODE REFERENCES
214These functions are implemented in the file
215.Pa sys/kern/kern_synch.c .
216.Sh SEE ALSO
217.Xr hz 9 ,
218.Xr mi_switch 9 ,
219.Xr timeout 9
220