xref: /netbsd-src/share/man/man9/condvar.9 (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1.\"	$NetBSD: condvar.9,v 1.20 2019/04/12 11:02:22 abhinav Exp $
2.\"
3.\" Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Andrew Doran.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd November 12, 2017
31.Dt CONDVAR 9
32.Os
33.Sh NAME
34.Nm cv ,
35.Nm condvar ,
36.Nm cv_init ,
37.Nm cv_destroy ,
38.Nm cv_wait ,
39.Nm cv_wait_sig ,
40.Nm cv_timedwait ,
41.Nm cv_timedwait_sig ,
42.Nm cv_timedwaitbt ,
43.Nm cv_timedwaitbt_sig ,
44.Nm cv_signal ,
45.Nm cv_broadcast ,
46.Nm cv_has_waiters
47.Nd condition variables
48.Sh SYNOPSIS
49.In sys/condvar.h
50.Ft void
51.Fn cv_init "kcondvar_t *cv" "const char *wmesg"
52.Ft void
53.Fn cv_destroy "kcondvar_t *cv"
54.Ft void
55.Fn cv_wait "kcondvar_t *cv" "kmutex_t *mtx"
56.Ft int
57.Fn cv_wait_sig "kcondvar_t *cv" "kmutex_t *mtx"
58.Ft int
59.Fn cv_timedwait "kcondvar_t *cv" "kmutex_t *mtx" "int ticks"
60.Ft int
61.Fn cv_timedwait_sig "kcondvar_t *cv" "kmutex_t *mtx" "int ticks"
62.Ft int
63.Fn cv_timedwaitbt "kcondvar_t *cv" "kmutex_t *mtx" "struct bintime *bt" \
64"const struct bintime *epsilon"
65.Ft int
66.Fn cv_timedwaitbt_sig "kcondvar_t *cv" "kmutex_t *mtx" "struct bintime *bt" \
67"const struct bintime *epsilon"
68.Ft void
69.Fn cv_signal "kcondvar_t *cv"
70.Ft void
71.Fn cv_broadcast "kcondvar_t *cv"
72.Ft bool
73.Fn cv_has_waiters "kcondvar_t *cv"
74.Pp
75.Cd "options DIAGNOSTIC"
76.Cd "options LOCKDEBUG"
77.Sh DESCRIPTION
78Condition variables (CVs) are used in the kernel to synchronize access
79to resources that are limited (for example, memory) and to wait for
80pending I/O operations to complete.
81.Pp
82The
83.Vt kcondvar_t
84type provides storage for the CV object.
85This should be treated as an opaque object and not examined directly by
86consumers.
87.Sh OPTIONS
88.Bl -tag -width abcd
89.It Cd "options DIAGNOSTIC"
90.Pp
91Kernels compiled with the
92.Dv DIAGNOSTIC
93option perform basic sanity checks on CV operations.
94.It Cd "options LOCKDEBUG"
95.Pp
96Kernels compiled with the
97.Dv LOCKDEBUG
98option perform potentially CPU intensive sanity checks
99on CV operations.
100.El
101.Sh FUNCTIONS
102.Bl -tag -width abcd
103.It Fn cv_init "cv" "wmesg"
104.Pp
105Initialize a CV for use.
106No other operations can be performed on the CV until it has been initialized.
107.Pp
108The
109.Fa wmesg
110argument specifies a string of no more than 8 characters that describes
111the resource or condition associated with the CV.
112The kernel does not use this argument directly but makes it available for
113utilities such as
114.Xr ps 1
115to display.
116.It Fn cv_destroy "cv"
117.Pp
118Release resources used by a CV.
119The CV must not be in use when it is destroyed, and must not be used afterwards.
120.It Fn cv_wait "cv" "mtx"
121.Pp
122Cause the current LWP to wait non-interruptably for access to a resource,
123or for an I/O operation to complete.
124The LWP will resume execution when awoken by another thread using
125.Fn cv_signal
126or
127.Fn cv_broadcast .
128.Pp
129.Fa mtx
130specifies a kernel mutex to be used as an interlock, and must be held by the
131calling LWP on entry to
132.Fn cv_wait .
133It will be released once the LWP has prepared to sleep, and will be reacquired
134before
135.Fn cv_wait
136returns.
137.Pp
138A small window exists between testing for availability of a resource and
139waiting for the resource with
140.Fn cv_wait ,
141in which the resource may become available again.
142The interlock is used to guarantee that the resource will not be signalled
143as available until the calling LWP has begun to wait for it.
144.Pp
145Non-interruptable waits have the potential to deadlock the system, and so must
146be kept short (typically, under one second).
147.It Fn cv_wait_sig "cv" "mtx"
148.Pp
149As per
150.Fn cv_wait ,
151but causes the current LWP to wait interruptably.
152If the LWP receives a signal, or is interrupted by another condition such
153as its containing process exiting, the wait is ended early and an error
154code returned.
155.Pp
156If
157.Fn cv_wait_sig
158returns as a result of a signal, the return value is
159.Er ERESTART
160if the signal
161has the
162.Dv SA_RESTART
163property.
164If awoken normally, the value is zero, and
165.Er EINTR
166under all other conditions.
167.It Fn cv_timedwait "cv" "mtx" "ticks"
168.Pp
169As per
170.Fn cv_wait ,
171but will return early if a timeout specified by the
172.Fa ticks
173argument expires.
174.Pp
175.Fa ticks
176is an architecture and system dependent value related to the number of
177clock interrupts per second.
178See
179.Xr hz 9
180for details.
181The
182.Xr mstohz 9
183macro can be used to convert a timeout expressed in milliseconds to
184one suitable for
185.Fn cv_timedwait .
186If the
187.Fa ticks
188argument is zero,
189.Fn cv_timedwait
190behaves exactly like
191.Fn cv_wait .
192.Pp
193If the timeout expires before the LWP is awoken, the return value is
194.Er EWOULDBLOCK .
195If awoken normally, the return value is zero.
196.It Fn cv_timedwait_sig "cv" "mtx" "ticks"
197.Pp
198As per
199.Fn cv_wait_sig ,
200but also accepts a timeout value and will return
201.Er EWOULDBLOCK
202if the timeout expires.
203.It Fn cv_timedwaitbt "cv" "mtx" "bt" "epsilon"
204.It Fn cv_timedwaitbt_sig "cv" "mtx" "bt" "epsilon"
205.Pp
206Similar to
207.Fn cv_timedwait
208and
209.Fn cv_timedwait_sig ,
210but
211.Fa bt
212is decremented in place with the amount of time actually waited, and on
213return contains the amount of time remaining, possibly negative if the
214timeout expired.
215.Pp
216The hint
217.Fa epsilon
218requests that the wakeup not be delayed more than
219.Fa bt Li "+" Fa epsilon ,
220so that the system can coalesce multiple wakeups within their
221respective epsilons into a single high-resolution clock interrupt or
222choose to use cheaper low-resolution clock interrupts instead.
223.Pp
224However, the system is still limited by its best clock interrupt
225resolution and by scheduling competition, which may delay the wakeup by
226more than
227.Fa bt Li "+" Fa epsilon .
228.It Fn cv_signal "cv"
229.Pp
230Awaken one LWP (potentially among many) that is waiting on the specified
231condition variable.
232The mutex passed to the wait function
233.Po Fa mtx Pc
234must also be held when calling
235.Fn cv_signal .
236.Pp
237(Note that
238.Fn cv_signal
239is erroneously named in that it does not send a signal in the traditional
240sense to LWPs waiting on a CV.)
241.It Fn cv_broadcast "cv"
242.Pp
243Awaken all LWPs waiting on the specified condition variable.
244The mutex passed to the wait function
245.Po Fa mtx Pc
246must also be held when calling
247.Fn cv_broadcast .
248.It Fn cv_has_waiters "cv"
249.Pp
250Return
251.Dv true
252if one or more LWPs are waiting on the specified condition variable.
253.Pp
254.Fn cv_has_waiters
255cannot test reliably for interruptable waits.
256It should only be used to test for non-interruptable waits
257made using
258.Fn cv_wait .
259.Pp
260.Fn cv_has_waiters
261should only be used when making diagnostic assertions, and must
262be called while holding the interlocking mutex passed to
263.Fn cv_wait .
264.El
265.Sh EXAMPLES
266Consuming a resource:
267.Bd -literal
268	/*
269	 * Lock the resource.  Its mutex will also serve as the
270	 * interlock.
271	 */
272	mutex_enter(&res->mutex);
273
274	/*
275	 * Wait for the resource to become available.  Timeout after
276	 * five seconds.  If the resource is not available within the
277	 * alloted time, return an error.
278	 */
279	struct bintime timeout = { .sec = 5, .frac = 0 };
280	const struct bintime epsilon = { .sec = 1, .frac = 0 };
281	while (res->state == BUSY) {
282		error = cv_timedwaitbt(&res->condvar, \\
283		    &res->mutex, &timeout, &epsilon);
284		if (error) {
285			KASSERT(error == EWOULDBLOCK);
286			if (res->state != BUSY)
287				break;
288			mutex_exit(&res->mutex);
289			return ETIMEDOUT;
290		}
291	}
292
293	/*
294	 * It's now available to us.  Take ownership of the
295	 * resource, and consume it.
296	 */
297	res->state = BUSY;
298	mutex_exit(&res->mutex);
299	consume(res);
300.Ed
301.Pp
302Releasing a resource for the next consumer to use:
303.Bd -literal
304	mutex_enter(&res->mutex);
305	res->state = IDLE;
306	cv_signal(&res->condvar);
307	mutex_exit(&res->mutex);
308.Ed
309.Sh CODE REFERENCES
310The core of the CV implementation is in
311.Pa sys/kern/kern_condvar.c .
312.Pp
313The header file
314.Pa sys/sys/condvar.h
315describes the public interface.
316.Sh SEE ALSO
317.Xr sigaction 2 ,
318.Xr membar_ops 3 ,
319.Xr errno 9 ,
320.Xr mstohz 9 ,
321.Xr mutex 9 ,
322.Xr rwlock 9
323.Pp
324.Rs
325.%A Jim Mauro
326.%A Richard McDougall
327.%T Solaris Internals: Core Kernel Architecture
328.%I Prentice Hall
329.%D 2001
330.%O ISBN 0-13-022496-0
331.Re
332.Sh HISTORY
333The CV primitives first appeared in
334.Nx 5.0 .
335The
336.Fn cv_timedwaitbt
337and
338.Fn cv_timedwaitbt_sig
339primitives first appeared in
340.Nx 9.0 .
341