2001-07-14 19:41:16 +00:00
|
|
|
.\"
|
2001-08-13 16:33:00 +00:00
|
|
|
.\" Copyright (C) 2000 Jason Evans <jasone@FreeBSD.org>. All rights reserved.
|
2001-07-14 19:41:16 +00:00
|
|
|
.\"
|
2001-01-16 01:00:43 +00:00
|
|
|
.\" 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(s), this list of conditions and the following disclaimer as
|
2001-07-14 19:41:16 +00:00
|
|
|
.\" the first lines of this file unmodified other than the possible
|
2001-01-16 01:00:43 +00:00
|
|
|
.\" addition of one or more copyright notices.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice(s), 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 COPYRIGHT HOLDER(S) ``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 COPYRIGHT HOLDER(S) 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.
|
|
|
|
.\"
|
|
|
|
.\" $FreeBSD$
|
|
|
|
.\"
|
2007-06-05 20:53:18 +00:00
|
|
|
.Dd June 5, 2007
|
2001-01-16 01:00:43 +00:00
|
|
|
.Dt CONDVAR 9
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm condvar ,
|
|
|
|
.Nm cv_init ,
|
|
|
|
.Nm cv_destroy ,
|
|
|
|
.Nm cv_wait ,
|
|
|
|
.Nm cv_wait_sig ,
|
2007-03-09 16:59:27 +00:00
|
|
|
.Nm cv_wait_unlock ,
|
2001-01-16 01:00:43 +00:00
|
|
|
.Nm cv_timedwait ,
|
|
|
|
.Nm cv_timedwait_sig ,
|
|
|
|
.Nm cv_signal ,
|
|
|
|
.Nm cv_broadcast ,
|
2004-03-10 15:57:25 +00:00
|
|
|
.Nm cv_broadcastpri ,
|
2001-01-16 01:00:43 +00:00
|
|
|
.Nm cv_wmesg
|
|
|
|
.Nd kernel condition variable
|
|
|
|
.Sh SYNOPSIS
|
2001-12-26 23:14:04 +00:00
|
|
|
.In sys/param.h
|
|
|
|
.In sys/proc.h
|
2001-10-01 16:09:29 +00:00
|
|
|
.In sys/condvar.h
|
2001-01-16 01:00:43 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn cv_init "struct cv *cvp" "const char *desc"
|
|
|
|
.Ft void
|
|
|
|
.Fn cv_destroy "struct cv *cvp"
|
|
|
|
.Ft void
|
2007-03-21 22:22:13 +00:00
|
|
|
.Fn cv_wait "struct cv *cvp" "lock"
|
2001-01-16 01:00:43 +00:00
|
|
|
.Ft int
|
2007-03-21 22:22:13 +00:00
|
|
|
.Fn cv_wait_sig "struct cv *cvp" "lock"
|
2007-03-09 16:59:27 +00:00
|
|
|
.Ft void
|
2007-03-21 22:22:13 +00:00
|
|
|
.Fn cv_wait_unlock "struct cv *cvp" "lock"
|
2001-01-16 01:00:43 +00:00
|
|
|
.Ft int
|
2007-03-21 22:22:13 +00:00
|
|
|
.Fn cv_timedwait "struct cv *cvp" "lock" "int timo"
|
2001-01-16 01:00:43 +00:00
|
|
|
.Ft int
|
2007-03-21 22:22:13 +00:00
|
|
|
.Fn cv_timedwait_sig "struct cv *cvp" "lock" "int timo"
|
2001-01-16 01:00:43 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn cv_signal "struct cv *cvp"
|
|
|
|
.Ft void
|
|
|
|
.Fn cv_broadcast "struct cv *cvp"
|
|
|
|
.Ft void
|
2004-03-10 15:57:25 +00:00
|
|
|
.Fn cv_broadcastpri "struct cv *cvp" "int pri"
|
2001-01-16 01:00:43 +00:00
|
|
|
.Ft const char *
|
|
|
|
.Fn cv_wmesg "struct cv *cvp"
|
|
|
|
.Sh DESCRIPTION
|
|
|
|
Condition variables are used in conjunction with mutexes to wait for conditions
|
|
|
|
to occur.
|
|
|
|
Condition variables are created with
|
|
|
|
.Fn cv_init ,
|
|
|
|
where
|
2001-01-16 08:53:04 +00:00
|
|
|
.Fa cvp
|
2001-01-16 01:00:43 +00:00
|
|
|
is a pointer to space for a
|
2001-01-16 08:53:04 +00:00
|
|
|
.Vt struct cv ,
|
2001-01-16 01:00:43 +00:00
|
|
|
and
|
2001-01-16 08:53:04 +00:00
|
|
|
.Fa desc
|
2001-01-16 01:00:43 +00:00
|
|
|
is a pointer to a null-terminated character string that describes the condition
|
|
|
|
variable.
|
|
|
|
Condition variables are destroyed with
|
|
|
|
.Fn cv_destroy .
|
|
|
|
Threads wait on condition variables by calling
|
|
|
|
.Fn cv_wait ,
|
|
|
|
.Fn cv_wait_sig ,
|
2007-03-09 16:59:27 +00:00
|
|
|
.Fn cv_wait_unlock ,
|
2001-01-16 01:00:43 +00:00
|
|
|
.Fn cv_timedwait ,
|
|
|
|
or
|
|
|
|
.Fn cv_timedwait_sig .
|
|
|
|
Threads unblock waiters by calling
|
|
|
|
.Fn cv_signal
|
|
|
|
to unblock one waiter, or
|
|
|
|
.Fn cv_broadcast
|
2004-03-10 15:57:25 +00:00
|
|
|
or
|
|
|
|
.Fn cv_broadcastpri
|
2001-01-16 01:00:43 +00:00
|
|
|
to unblock all waiters.
|
2004-03-10 15:57:25 +00:00
|
|
|
In addition to waking waiters,
|
|
|
|
.Fn cv_broadcastpri
|
2004-06-21 14:11:45 +00:00
|
|
|
ensures that all of the waiters have a priority of at least
|
2004-03-10 15:57:25 +00:00
|
|
|
.Fa pri
|
|
|
|
by raising the priority of any threads that do not.
|
2001-01-16 01:00:43 +00:00
|
|
|
.Fn cv_wmesg
|
|
|
|
returns the description string of
|
2001-01-16 08:53:04 +00:00
|
|
|
.Fa cvp ,
|
2001-01-16 01:00:43 +00:00
|
|
|
as set by the initial call to
|
|
|
|
.Fn cv_init .
|
|
|
|
.Pp
|
2007-03-21 22:22:13 +00:00
|
|
|
The
|
|
|
|
.Fa lock
|
|
|
|
argument is a pointer to either a
|
|
|
|
.Xr mutex 9 ,
|
|
|
|
.Xr rwlock 9 ,
|
|
|
|
or
|
|
|
|
.Xr sx 9
|
|
|
|
lock.
|
2007-06-05 20:45:10 +00:00
|
|
|
A
|
|
|
|
.Xr mutex 9
|
|
|
|
argument must be initialized with
|
|
|
|
.Dv MTX_DEF
|
|
|
|
and not
|
|
|
|
.Dv MTX_SPIN .
|
2001-01-16 01:00:43 +00:00
|
|
|
A thread must hold
|
2007-03-21 22:22:13 +00:00
|
|
|
.Fa lock
|
2001-01-16 01:00:43 +00:00
|
|
|
before calling
|
|
|
|
.Fn cv_wait ,
|
|
|
|
.Fn cv_wait_sig ,
|
2007-03-09 16:59:27 +00:00
|
|
|
.Fn cv_wait_unlock ,
|
2001-01-16 01:00:43 +00:00
|
|
|
.Fn cv_timedwait ,
|
|
|
|
or
|
|
|
|
.Fn cv_timedwait_sig .
|
|
|
|
When a thread waits on a condition,
|
2007-03-21 22:22:13 +00:00
|
|
|
.Fa lock
|
2007-03-09 16:59:27 +00:00
|
|
|
is atomically released before the thread is blocked, then reacquired
|
2001-01-16 01:00:43 +00:00
|
|
|
before the function call returns.
|
2007-03-09 16:59:27 +00:00
|
|
|
The
|
|
|
|
.Fn cv_wait_unlock
|
|
|
|
function does not reacquire the lock before returning.
|
2001-01-16 01:00:43 +00:00
|
|
|
All waiters must pass the same
|
2007-03-21 22:22:13 +00:00
|
|
|
.Fa lock
|
2001-01-16 01:00:43 +00:00
|
|
|
in conjunction with
|
2001-01-16 08:53:04 +00:00
|
|
|
.Fa cvp .
|
2001-01-16 01:00:43 +00:00
|
|
|
.Pp
|
|
|
|
When
|
|
|
|
.Fn cv_wait ,
|
|
|
|
.Fn cv_wait_sig ,
|
2007-03-09 16:59:27 +00:00
|
|
|
.Fn cv_wait_unlock ,
|
2001-01-16 01:00:43 +00:00
|
|
|
.Fn cv_timedwait ,
|
|
|
|
and
|
|
|
|
.Fn cv_timedwait_sig
|
|
|
|
unblock, their calling threads are made runnable.
|
|
|
|
.Fn cv_timedwait
|
|
|
|
and
|
|
|
|
.Fn cv_timedwait_sig
|
|
|
|
wait for at most
|
2001-01-16 08:53:04 +00:00
|
|
|
.Fa timo
|
2001-01-16 01:00:43 +00:00
|
|
|
/
|
|
|
|
.Dv HZ
|
|
|
|
seconds before being unblocked and returning
|
|
|
|
.Er EWOULDBLOCK ;
|
|
|
|
otherwise, they return 0.
|
|
|
|
.Fn cv_wait_sig
|
|
|
|
and
|
|
|
|
.Fn cv_timedwait_sig
|
|
|
|
return prematurely with a value of
|
|
|
|
.Er EINTR
|
|
|
|
or
|
|
|
|
.Er ERESTART
|
|
|
|
if a signal is caught, or 0 if signaled via
|
|
|
|
.Fn cv_signal
|
|
|
|
or
|
|
|
|
.Fn cv_broadcast .
|
|
|
|
.Sh RETURN VALUES
|
|
|
|
If successful,
|
|
|
|
.Fn cv_wait_sig ,
|
|
|
|
.Fn cv_timedwait ,
|
|
|
|
and
|
|
|
|
.Fn cv_timedwait_sig
|
|
|
|
return 0.
|
|
|
|
Otherwise, a non-zero error code is returned.
|
|
|
|
.Pp
|
|
|
|
.Fn cv_wmesg
|
|
|
|
returns the description string that was passed to
|
|
|
|
.Fn cv_init .
|
|
|
|
.Sh ERRORS
|
|
|
|
.Fn cv_wait_sig
|
|
|
|
and
|
|
|
|
.Fn cv_timedwait_sig
|
|
|
|
will fail if:
|
|
|
|
.Bl -tag -width Er
|
|
|
|
.It Bq Er EINTR
|
2007-03-09 17:53:16 +00:00
|
|
|
A signal was caught and the system call should be interrupted.
|
2007-03-09 17:56:05 +00:00
|
|
|
.It Bq Er ERESTART
|
|
|
|
A signal was caught and the system call should be restarted.
|
2001-01-16 01:00:43 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
.Fn cv_timedwait
|
|
|
|
and
|
|
|
|
.Fn cv_timedwait_sig
|
|
|
|
will fail if:
|
|
|
|
.Bl -tag -width Er
|
|
|
|
.It Bq Er EWOULDBLOCK
|
|
|
|
Timeout expired.
|
|
|
|
.El
|
|
|
|
.Sh SEE ALSO
|
2007-03-30 18:07:26 +00:00
|
|
|
.Xr locking 9 ,
|
2002-03-28 12:51:06 +00:00
|
|
|
.Xr mtx_pool 9 ,
|
2001-09-17 07:24:48 +00:00
|
|
|
.Xr mutex 9 ,
|
2006-02-01 20:30:55 +00:00
|
|
|
.Xr rwlock 9 ,
|
2001-09-17 07:24:48 +00:00
|
|
|
.Xr sema 9 ,
|
2007-03-09 22:41:01 +00:00
|
|
|
.Xr sleep 9 ,
|
2001-09-17 07:24:48 +00:00
|
|
|
.Xr sx 9
|