2008-08-06 11:08:31 +04:00
|
|
|
.\" $NetBSD: condvar.9,v 1.13 2008/08/06 07:08:31 skrll Exp $
|
2006-11-13 19:22:11 +03:00
|
|
|
.\"
|
2008-01-26 17:16:27 +03:00
|
|
|
.\" Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
|
2006-11-13 19:22:11 +03:00
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
.\" by Andrew Doran.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
.\" POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
.\"
|
2008-06-04 15:24:36 +04:00
|
|
|
.Dd June 4, 2008
|
2006-11-13 19:22:11 +03:00
|
|
|
.Dt CONDVAR 9
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm cv ,
|
|
|
|
.Nm condvar ,
|
|
|
|
.Nm cv_init ,
|
|
|
|
.Nm cv_destroy ,
|
|
|
|
.Nm cv_wait ,
|
|
|
|
.Nm cv_wait_sig ,
|
|
|
|
.Nm cv_timedwait ,
|
|
|
|
.Nm cv_timedwait_sig ,
|
|
|
|
.Nm cv_signal ,
|
2007-02-03 19:39:53 +03:00
|
|
|
.Nm cv_broadcast ,
|
2007-02-03 19:49:11 +03:00
|
|
|
.Nm cv_has_waiters
|
2006-11-13 19:22:11 +03:00
|
|
|
.Nd condition variables
|
|
|
|
.Sh SYNOPSIS
|
|
|
|
.In sys/condvar.h
|
|
|
|
.Ft void
|
|
|
|
.Fn cv_init "kcondvar_t *cv" "const char *wmesg"
|
|
|
|
.Ft void
|
|
|
|
.Fn cv_destroy "kcondvar_t *cv"
|
|
|
|
.Ft void
|
|
|
|
.Fn cv_wait "kcondvar_t *cv" "kmutex_t *mtx"
|
|
|
|
.Ft int
|
|
|
|
.Fn cv_wait_sig "kcondvar_t *cv" "kmutex_t *mtx"
|
|
|
|
.Ft int
|
|
|
|
.Fn cv_timedwait "kcondvar_t *cv" "kmutex_t *mtx" "int ticks"
|
|
|
|
.Ft int
|
|
|
|
.Fn cv_timedwait_sig "kcondvar_t *cv" "kmutex_t *mtx" "int ticks"
|
|
|
|
.Ft void
|
|
|
|
.Fn cv_signal "kcondvar_t *cv"
|
|
|
|
.Ft void
|
|
|
|
.Fn cv_broadcast "kcondvar_t *cv"
|
2007-03-29 21:40:36 +04:00
|
|
|
.Ft bool
|
2007-02-03 19:49:11 +03:00
|
|
|
.Fn cv_has_waiters "kcondvar_t *cv"
|
2008-06-04 15:24:36 +04:00
|
|
|
.Pp
|
|
|
|
.Cd "options DIAGNOSTIC"
|
|
|
|
.Cd "options LOCKDEBUG"
|
2006-11-13 19:22:11 +03:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
Condition variables (CVs) are used in the kernel to synchronize access
|
|
|
|
to resources that are limited (for example, memory) and to wait for
|
|
|
|
pending I/O operations to complete.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Vt kcondvar_t
|
|
|
|
type provides storage for the CV object.
|
|
|
|
This should be treated as an opaque object and not examined directly by
|
|
|
|
consumers.
|
2008-06-04 15:24:36 +04:00
|
|
|
.Sh OPTIONS
|
|
|
|
.Bl -tag -width abcd
|
|
|
|
.It Cd "options DIAGNOSTIC"
|
|
|
|
.Pp
|
|
|
|
Kernels compiled with the
|
|
|
|
.Dv DIAGNOSTIC
|
|
|
|
option perform basic sanity checks on CV operations.
|
|
|
|
.It Cd "options LOCKDEBUG"
|
|
|
|
.Pp
|
|
|
|
Kernels compiled with the
|
|
|
|
.Dv LOCKDEBUG
|
|
|
|
option perform potentially CPU intensive sanity checks
|
|
|
|
on CV operations.
|
|
|
|
.El
|
2006-11-13 19:22:11 +03:00
|
|
|
.Sh FUNCTIONS
|
|
|
|
.Bl -tag -width abcd
|
|
|
|
.It Fn cv_init "cv" "wmesg"
|
|
|
|
.Pp
|
|
|
|
Initialize a CV for use.
|
|
|
|
No other operations can be performed on the CV until it has been initialized.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fa wmesg
|
|
|
|
argument specifies a string of no more than 8 characters that describes
|
|
|
|
the resource or condition associated with the CV.
|
|
|
|
The kernel does not use this argument directly but makes it available for
|
|
|
|
utilities such as
|
2006-12-04 16:16:59 +03:00
|
|
|
.Xr ps 1
|
2006-11-13 19:22:11 +03:00
|
|
|
to display.
|
|
|
|
.It Fn cv_destroy "cv"
|
|
|
|
.Pp
|
|
|
|
Release resources used by a CV.
|
|
|
|
The CV must not be in use when it is destroyed, and must not be used afterwards.
|
|
|
|
.It Fn cv_wait "cv" "mtx"
|
|
|
|
.Pp
|
|
|
|
Cause the current LWP to wait non-interruptably for access to a resource,
|
|
|
|
or for an I/O operation to complete.
|
|
|
|
The LWP will resume execution when awoken by another thread using
|
|
|
|
.Fn cv_signal
|
|
|
|
or
|
|
|
|
.Fn cv_broadcast .
|
|
|
|
.Pp
|
|
|
|
.Fa mtx
|
|
|
|
specifies a kernel mutex to be used as an interlock, and must be held by the
|
|
|
|
calling LWP on entry to
|
|
|
|
.Fn cv_wait .
|
|
|
|
It will be released once the LWP has prepared to sleep, and will be reacquired
|
|
|
|
before
|
|
|
|
.Fn cv_wait
|
|
|
|
returns.
|
|
|
|
.Pp
|
2006-12-04 16:16:59 +03:00
|
|
|
A small window exists between testing for availability of a resource and
|
2006-11-13 19:22:11 +03:00
|
|
|
waiting for the resource with
|
|
|
|
.Fn cv_wait ,
|
|
|
|
in which the resource may become available again.
|
2008-08-06 11:08:31 +04:00
|
|
|
The interlock is used to guarantee that the resource will not be signalled
|
2006-11-13 19:22:11 +03:00
|
|
|
as available until the calling LWP has begun to wait for it.
|
|
|
|
.Pp
|
|
|
|
Non-interruptable waits have the potential to deadlock the system, and so must
|
|
|
|
be kept short (typically, under one second).
|
|
|
|
.It Fn cv_wait_sig "cv" "mtx"
|
|
|
|
.Pp
|
|
|
|
As per
|
|
|
|
.Fn cv_wait ,
|
|
|
|
but causes the current LWP to wait interruptably.
|
2008-08-06 11:08:31 +04:00
|
|
|
If the LWP receives a signal, or is interrupted by another condition such
|
2006-11-13 19:22:11 +03:00
|
|
|
as its containing process exiting, the wait is ended early and an error
|
|
|
|
code returned.
|
|
|
|
.Pp
|
|
|
|
If
|
|
|
|
.Fn cv_wait_sig
|
2006-12-04 16:16:59 +03:00
|
|
|
returns as a result of a signal, the return value is
|
|
|
|
.Er ERESTART
|
|
|
|
if the signal
|
|
|
|
has the
|
|
|
|
.Dv SA_RESTART
|
|
|
|
property.
|
|
|
|
If awoken normally, the value is zero, and
|
|
|
|
.Er EINTR
|
|
|
|
under all other conditions.
|
2006-11-13 19:22:11 +03:00
|
|
|
.It Fn cv_timedwait "cv" "mtx" "ticks"
|
|
|
|
.Pp
|
|
|
|
As per
|
|
|
|
.Fn cv_wait ,
|
|
|
|
but will return early if a timeout specified by the
|
|
|
|
.Fa ticks
|
|
|
|
argument expires.
|
|
|
|
.Pp
|
|
|
|
.Fa ticks
|
|
|
|
is an architecture and system dependent value related to the number of
|
|
|
|
clock interrupts per second.
|
|
|
|
See
|
|
|
|
.Xr hz 9
|
|
|
|
for details.
|
|
|
|
The
|
|
|
|
.Xr mstohz 9
|
|
|
|
macro can be used to convert a timeout expressed in milliseconds to
|
|
|
|
one suitable for
|
|
|
|
.Fn cv_timedwait .
|
2008-01-26 17:16:27 +03:00
|
|
|
If the
|
|
|
|
.Fa ticks
|
|
|
|
argument is zero,
|
|
|
|
.Fn cv_timedwait
|
|
|
|
behaves exactly like
|
|
|
|
.Fn cv_wait .
|
2006-11-13 19:22:11 +03:00
|
|
|
.Pp
|
|
|
|
If the timeout expires before the LWP is awoken, the return value is
|
2006-12-04 16:16:59 +03:00
|
|
|
.Er EWOULDBLOCK .
|
2006-11-13 19:22:11 +03:00
|
|
|
If awoken normally, the return value is zero.
|
|
|
|
.It Fn cv_timedwait_sig "cv" "mtx" "ticks"
|
|
|
|
.Pp
|
|
|
|
As per
|
|
|
|
.Fn cv_wait_sig ,
|
2006-12-04 16:16:59 +03:00
|
|
|
but also accepts a timeout value and will return
|
|
|
|
.Er EWOULDBLOCK
|
|
|
|
if the timeout expires.
|
2006-11-13 19:22:11 +03:00
|
|
|
.It Fn cv_signal "cv"
|
|
|
|
.Pp
|
|
|
|
Awaken one LWP (potentially among many) that is waiting on the specified
|
|
|
|
condition variable.
|
2007-02-03 19:39:53 +03:00
|
|
|
The mutex passed to the wait function
|
|
|
|
.Po Fa mtx Pc
|
|
|
|
must also be held when calling
|
|
|
|
.Fn cv_signal .
|
2006-11-13 19:22:11 +03:00
|
|
|
.Pp
|
|
|
|
(Note that
|
|
|
|
.Fn cv_signal
|
|
|
|
is erroneously named in that it does not send a signal in the traditional
|
|
|
|
sense to LWPs waiting on a CV.)
|
|
|
|
.It Fn cv_broadcast "cv"
|
|
|
|
.Pp
|
|
|
|
Awaken all LWPs waiting on the specified condition variable.
|
2007-02-03 19:39:53 +03:00
|
|
|
The mutex passed to the wait function
|
|
|
|
.Po Fa mtx Pc
|
|
|
|
must also be held when calling
|
|
|
|
.Fn cv_broadcast .
|
2007-02-03 19:49:11 +03:00
|
|
|
.It Fn cv_has_waiters "cv"
|
|
|
|
.Pp
|
2007-03-29 21:40:36 +04:00
|
|
|
Return
|
|
|
|
.Dv true
|
|
|
|
if one or more LWPs are waiting on the specified condition variable.
|
|
|
|
.Pp
|
|
|
|
.Fn cv_has_waiters
|
|
|
|
cannot test reliably for interruptable waits.
|
|
|
|
It should only be used to test for non-interruptable waits
|
|
|
|
made using
|
|
|
|
.Fn cv_wait .
|
|
|
|
.Pp
|
2007-02-03 19:49:11 +03:00
|
|
|
.Fn cv_has_waiters
|
2007-03-29 21:40:36 +04:00
|
|
|
should only be used when making diagnostic assertions, and must
|
|
|
|
be called while holding the interlocking mutex passed to
|
|
|
|
.Fn cv_wait .
|
2006-11-13 21:28:15 +03:00
|
|
|
.El
|
2006-11-13 19:22:11 +03:00
|
|
|
.Sh EXAMPLES
|
|
|
|
.Bd -literal
|
|
|
|
Consuming a resource:
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock the resource. Its mutex will also serve as the
|
|
|
|
* interlock.
|
|
|
|
*/
|
2006-12-04 16:16:59 +03:00
|
|
|
mutex_enter(\*[Am]res-\*[Gt]mutex);
|
2006-11-13 19:22:11 +03:00
|
|
|
|
|
|
|
/*
|
2007-03-29 21:40:36 +04:00
|
|
|
* Wait for the resource to become available.
|
2006-11-13 19:22:11 +03:00
|
|
|
*/
|
2006-12-04 16:16:59 +03:00
|
|
|
while (res-\*[Gt]state == BUSY)
|
|
|
|
cv_wait(\*[Am]res-\*[Gt]condvar, \*[Am]res-\*[Gt]mutex);
|
2006-11-13 19:22:11 +03:00
|
|
|
|
|
|
|
/*
|
2007-03-29 21:40:36 +04:00
|
|
|
* It's now available to us. Take ownership of the
|
|
|
|
* resource, and consume it.
|
2006-11-13 19:22:11 +03:00
|
|
|
*/
|
2007-03-29 21:40:36 +04:00
|
|
|
res-\*[Gt]state = BUSY;
|
2006-12-04 16:16:59 +03:00
|
|
|
mutex_exit(\*[Am]res-\*[Gt]mutex);
|
2006-11-13 21:28:15 +03:00
|
|
|
consume(res);
|
2006-11-13 19:22:11 +03:00
|
|
|
|
|
|
|
Releasing a resource for the next consumer to use:
|
|
|
|
|
2006-12-04 16:16:59 +03:00
|
|
|
mutex_enter(\*[Am]res-\*[Gt]mutex);
|
|
|
|
res-\*[Gt]state = IDLE;
|
|
|
|
cv_signal(\*[Am]res-\*[Gt]condvar);
|
|
|
|
mutex_exit(\*[Am]res-\*[Gt]mutex);
|
2006-11-13 19:22:11 +03:00
|
|
|
.Ed
|
|
|
|
.Sh CODE REFERENCES
|
|
|
|
This section describes places within the
|
|
|
|
.Nx
|
2006-11-13 21:28:15 +03:00
|
|
|
source tree where code implementing condition variables can be found.
|
2006-11-13 19:22:11 +03:00
|
|
|
All pathnames are relative to
|
|
|
|
.Pa /usr/src .
|
|
|
|
.Pp
|
2006-12-04 16:16:59 +03:00
|
|
|
The core of the CV implementation is in
|
2006-11-13 19:22:11 +03:00
|
|
|
.Pa sys/kern/kern_condvar.c .
|
|
|
|
.Pp
|
|
|
|
The header file
|
|
|
|
.Pa sys/sys/condvar.h
|
2006-11-13 21:28:15 +03:00
|
|
|
describes the public interface.
|
2006-11-13 19:22:11 +03:00
|
|
|
.Sh SEE ALSO
|
2006-12-04 16:16:59 +03:00
|
|
|
.Xr sigaction 2 ,
|
2006-11-13 19:22:11 +03:00
|
|
|
.Xr errno 9 ,
|
2007-02-02 06:40:07 +03:00
|
|
|
.Xr mb 9 ,
|
2007-02-02 10:36:09 +03:00
|
|
|
.Xr mstohz 9 ,
|
2006-11-13 19:22:11 +03:00
|
|
|
.Xr mutex 9 ,
|
2006-12-04 16:16:59 +03:00
|
|
|
.Xr rwlock 9
|
|
|
|
.Pp
|
|
|
|
.Rs
|
|
|
|
.%A Jim Mauro
|
|
|
|
.%A Richard McDougall
|
|
|
|
.%T Solaris Internals: Core Kernel Architecture
|
|
|
|
.%I Prentice Hall
|
|
|
|
.%D 2001
|
|
|
|
.%O ISBN 0-13-022496-0
|
|
|
|
.Re
|
2006-11-13 19:22:11 +03:00
|
|
|
.Sh HISTORY
|
2007-02-02 06:40:07 +03:00
|
|
|
The CV primitives first appeared in
|
2006-11-13 19:22:11 +03:00
|
|
|
.Nx 5.0 .
|