1 .\" $NetBSD: condvar.9,v 1.12 2008/06/04 11:24:36 ad Exp $
3 .\" Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
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.
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.
41 .Nm cv_timedwait_sig ,
45 .Nd condition variables
49 .Fn cv_init "kcondvar_t *cv" "const char *wmesg"
51 .Fn cv_destroy "kcondvar_t *cv"
53 .Fn cv_wait "kcondvar_t *cv" "kmutex_t *mtx"
55 .Fn cv_wait_sig "kcondvar_t *cv" "kmutex_t *mtx"
57 .Fn cv_timedwait "kcondvar_t *cv" "kmutex_t *mtx" "int ticks"
59 .Fn cv_timedwait_sig "kcondvar_t *cv" "kmutex_t *mtx" "int ticks"
61 .Fn cv_signal "kcondvar_t *cv"
63 .Fn cv_broadcast "kcondvar_t *cv"
65 .Fn cv_has_waiters "kcondvar_t *cv"
67 .Cd "options DIAGNOSTIC"
68 .Cd "options LOCKDEBUG"
70 Condition variables (CVs) are used in the kernel to synchronize access
71 to resources that are limited (for example, memory) and to wait for
72 pending I/O operations to complete.
76 type provides storage for the CV object.
77 This should be treated as an opaque object and not examined directly by
81 .It Cd "options DIAGNOSTIC"
83 Kernels compiled with the
85 option perform basic sanity checks on CV operations.
86 .It Cd "options LOCKDEBUG"
88 Kernels compiled with the
90 option perform potentially CPU intensive sanity checks
95 .It Fn cv_init "cv" "wmesg"
97 Initialize a CV for use.
98 No other operations can be performed on the CV until it has been initialized.
102 argument specifies a string of no more than 8 characters that describes
103 the resource or condition associated with the CV.
104 The kernel does not use this argument directly but makes it available for
108 .It Fn cv_destroy "cv"
110 Release resources used by a CV.
111 The CV must not be in use when it is destroyed, and must not be used afterwards.
112 .It Fn cv_wait "cv" "mtx"
114 Cause the current LWP to wait non-interruptably for access to a resource,
115 or for an I/O operation to complete.
116 The LWP will resume execution when awoken by another thread using
122 specifies a kernel mutex to be used as an interlock, and must be held by the
123 calling LWP on entry to
125 It will be released once the LWP has prepared to sleep, and will be reacquired
130 A small window exists between testing for availability of a resource and
131 waiting for the resource with
133 in which the resource may become available again.
134 The interlock is used to guarantee that the resource will not be signalled
135 as available until the calling LWP has begun to wait for it.
137 Non-interruptable waits have the potential to deadlock the system, and so must
138 be kept short (typically, under one second).
139 .It Fn cv_wait_sig "cv" "mtx"
143 but causes the current LWP to wait interruptably.
144 If the LWP receives a signal, or is interrupted by another condition such
145 as its containing process exiting, the wait is ended early and an error
150 returns as a result of a signal, the return value is
156 If awoken normally, the value is zero, and
158 under all other conditions.
159 .It Fn cv_timedwait "cv" "mtx" "ticks"
163 but will return early if a timeout specified by the
168 is an architecture and system dependent value related to the number of
169 clock interrupts per second.
175 macro can be used to convert a timeout expressed in milliseconds to
185 If the timeout expires before the LWP is awoken, the return value is
187 If awoken normally, the return value is zero.
188 .It Fn cv_timedwait_sig "cv" "mtx" "ticks"
192 but also accepts a timeout value and will return
194 if the timeout expires.
195 .It Fn cv_signal "cv"
197 Awaken one LWP (potentially among many) that is waiting on the specified
199 The mutex passed to the wait function
201 must also be held when calling
206 is erroneously named in that it does not send a signal in the traditional
207 sense to LWPs waiting on a CV.)
208 .It Fn cv_broadcast "cv"
210 Awaken all LWPs waiting on the specified condition variable.
211 The mutex passed to the wait function
213 must also be held when calling
215 .It Fn cv_has_waiters "cv"
219 if one or more LWPs are waiting on the specified condition variable.
222 cannot test reliably for interruptable waits.
223 It should only be used to test for non-interruptable waits
228 should only be used when making diagnostic assertions, and must
229 be called while holding the interlocking mutex passed to
234 Consuming a resource:
237 * Lock the resource. Its mutex will also serve as the
240 mutex_enter(\*[Am]res-\*[Gt]mutex);
243 * Wait for the resource to become available.
245 while (res-\*[Gt]state == BUSY)
246 cv_wait(\*[Am]res-\*[Gt]condvar, \*[Am]res-\*[Gt]mutex);
249 * It's now available to us. Take ownership of the
250 * resource, and consume it.
252 res-\*[Gt]state = BUSY;
253 mutex_exit(\*[Am]res-\*[Gt]mutex);
256 Releasing a resource for the next consumer to use:
258 mutex_enter(\*[Am]res-\*[Gt]mutex);
259 res-\*[Gt]state = IDLE;
260 cv_signal(\*[Am]res-\*[Gt]condvar);
261 mutex_exit(\*[Am]res-\*[Gt]mutex);
264 This section describes places within the
266 source tree where code implementing condition variables can be found.
267 All pathnames are relative to
270 The core of the CV implementation is in
271 .Pa sys/kern/kern_condvar.c .
274 .Pa sys/sys/condvar.h
275 describes the public interface.
286 .%A Richard McDougall
287 .%T Solaris Internals: Core Kernel Architecture
290 .%O ISBN 0-13-022496-0
293 The CV primitives first appeared in