NetBSD/share/man/man9/condvar.9

254 lines
7.3 KiB
Groff

.\" $NetBSD: condvar.9,v 1.3 2006/12/04 13:16:59 wiz Exp $
.\"
.\" Copyright (c) 2006 The NetBSD Foundation, Inc.
.\" 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.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the NetBSD
.\" Foundation, Inc. and its contributors.
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
.\" contributors may be used to endorse or promote products derived
.\" from this software without specific prior written permission.
.\"
.\" 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.
.\"
.Dd November 13, 2006
.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 ,
.Nm cv_broadcast
.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"
.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.
.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
.Xr ps 1
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
A small window exists between testing for availability of a resource and
waiting for the resource with
.Fn cv_wait ,
in which the resource may become available again.
The interlock is used to guarentee that the resource will not be signalled
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.
If the LWP recieves a signal, or is interrupted by another condition such
as its containing process exiting, the wait is ended early and an error
code returned.
.Pp
If
.Fn cv_wait_sig
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.
.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 .
.Pp
If the timeout expires before the LWP is awoken, the return value is
.Er EWOULDBLOCK .
If awoken normally, the return value is zero.
.It Fn cv_timedwait_sig "cv" "mtx" "ticks"
.Pp
As per
.Fn cv_wait_sig ,
but also accepts a timeout value and will return
.Er EWOULDBLOCK
if the timeout expires.
.It Fn cv_signal "cv"
.Pp
Awaken one LWP (potentially among many) that is waiting on the specified
condition variable.
.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.
.El
.Sh EXAMPLES
.Bd -literal
Consuming a resource:
/*
* Lock the resource. Its mutex will also serve as the
* interlock.
*/
mutex_enter(\*[Am]res-\*[Gt]mutex);
/*
* Wait for the resource to become available, and take
* ownership of it.
*/
while (res-\*[Gt]state == BUSY)
cv_wait(\*[Am]res-\*[Gt]condvar, \*[Am]res-\*[Gt]mutex);
res-\*[Gt]state = BUSY;
/*
* It's now available to us.
*/
mutex_exit(\*[Am]res-\*[Gt]mutex);
consume(res);
Releasing a resource for the next consumer to use:
mutex_enter(\*[Am]res-\*[Gt]mutex);
res-\*[Gt]state = IDLE;
cv_signal(\*[Am]res-\*[Gt]condvar);
mutex_exit(\*[Am]res-\*[Gt]mutex);
.Ed
.Sh CODE REFERENCES
This section describes places within the
.Nx
source tree where code implementing condition variables can be found.
All pathnames are relative to
.Pa /usr/src .
.Pp
The core of the CV implementation is in
.Pa sys/kern/kern_condvar.c .
.Pp
The header file
.Pa sys/sys/condvar.h
describes the public interface.
.Sh SEE ALSO
.Xr sigaction 2 ,
.Xr errno 9 ,
.Xr mstohz 9 ,
.Xr mutex 9 ,
.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
.Sh HISTORY
The CV primatives first appeared in
.Nx 5.0 .
They are modelled after similar primatives implemented in
Sun Solaris, and have been extended for
.Nx .