.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright 1991, 1992, 1994, The X/Open Company Ltd.
.\" Copyright (c) 2001, The IEEE and The Open Group.  All Rights Reserved.
.\" Copyright (c) 2008, Sun Microsystems, Inc.  All Rights Reserved.
.\"
.TH PTHREAD_COND_WAIT 3C "Nov 11, 2008"
.SH NAME
pthread_cond_wait, pthread_cond_timedwait, pthread_cond_reltimedwait_np \- wait
on a condition
.SH SYNOPSIS
.LP
.nf
cc -mt [ \fIflag\fR... ] \fIfile\fR... -lpthread [ \fIlibrary\fR... ]
#include <pthread.h>

\fBint\fR \fBpthread_cond_wait\fR(\fBpthread_cond_t *restrict\fR \fIcond\fR,
     \fBpthread_mutex_t *restrict\fR \fImutex\fR);
.fi

.LP
.nf
\fBint\fR \fBpthread_cond_timedwait\fR(\fBpthread_cond_t *restrict\fR \fIcond\fR,
     \fBpthread_mutex_t *restrict\fR \fImutex\fR,
     \fBconst struct timespec *restrict\fR \fIabstime\fR);
.fi

.LP
.nf
\fBint\fR \fBpthread_cond_reltimedwait_np\fR(\fBpthread_cond_t *\fR\fIcond\fR,
     \fBpthread_mutex_t *\fR\fImutex\fR, \fBconst struct timespec *\fR\fIreltime\fR);
.fi

.SH DESCRIPTION
.sp
.LP
The \fBpthread_cond_wait()\fR, \fBpthread_cond_timedwait()\fR, and
\fBpthread_cond_reltimedwait_np()\fR functions are used to block on a condition
variable. They are called with \fImutex\fR locked by the calling thread or
undefined behavior will result.
.sp
.LP
These functions atomically release \fImutex\fR and cause the calling thread to
block on the condition variable \fIcond\fR. Atomically here means ``atomically
with respect to access by another  thread to the mutex and then the condition
variable." That is, if another thread is able to acquire the mutex after the
about-to-block thread has released it, then a subsequent call to
\fBpthread_cond_signal()\fR or \fBpthread_cond_broadcast()\fR in that thread
behaves as if it were issued after the about-to-block thread has blocked.
.sp
.LP
Upon successful return, the mutex has been locked and is owned by the calling
thread. If mutex is a robust mutex where an owner terminated while holding the
lock and the state is recoverable, the mutex is acquired even though the
function returns an error value.
.sp
.LP
When using condition variables there is always a boolean predicate, an
invariant, associated with each condition wait that must be true before the
thread should proceed. Spurious wakeups from the \fBpthread_cond_wait()\fR,
\fBpthread_cond_timedwait()\fR, or \fBpthread_cond_reltimedwait_np()\fR
functions could occur. Since the return from \fBpthread_cond_wait()\fR,
\fBpthread_cond_timedwait()\fR, or \fBpthread_cond_reltimedwait_np()\fR does
not imply anything about the value of this predicate, the predicate should
always be reevaluated.
.sp
.LP
The order in which blocked threads are awakened by \fBpthread_cond_signal()\fR
or \fBpthread_cond_broadcast()\fR is determined by the scheduling policy. See
\fBpthreads\fR(5).
.sp
.LP
The effect of using more than one mutex for concurrent
\fBpthread_cond_wait()\fR, \fBpthread_cond_timedwait()\fR, or
\fBpthread_cond_reltimedwait_np()\fR operations on the same condition variable
will result in undefined behavior.
.sp
.LP
A condition wait (whether timed or not) is a cancellation point. When the
cancelability enable state of a thread is set to \fBPTHREAD_CANCEL_DEFERRED\fR,
a side effect of acting upon a cancellation request while in a condition wait
is that the mutex is reacquired before calling the first cancellation cleanup
handler.
.sp
.LP
A thread that has been unblocked because it has been canceled while blocked in
a call to \fBpthread_cond_wait()\fR or \fBpthread_cond_timedwait()\fR does not
consume any condition signal that may be directed concurrently at the condition
variable if there are other threads blocked on the condition variable.
.sp
.LP
The \fBpthread_cond_timedwait()\fR function is the same as
\fBpthread_cond_wait()\fR except that an error is returned if the absolute time
specified by \fIabstime\fR passes (that is, system time equals or exceeds
\fIabstime\fR) before the condition \fIcond\fR is signaled or broadcast, or if
the absolute time specified by \fIabstime\fR has already been passed at the
time of the call. The \fIabstime\fR argument is of type \fBstruct timespec\fR,
defined in \fBtime.h\fR(3HEAD). When such time-outs occur,
\fBpthread_cond_timedwait()\fR will nonetheless release and reacquire the mutex
referenced by \fImutex\fR. The function \fBpthread_cond_timedwait()\fR is also
a cancellation point.
.sp
.LP
The \fBpthread_cond_reltimedwait_np()\fR function is a non-standard extension
provided by the Solaris version of POSIX threads as indicated by the
``\fB_np\fR'' (non-portable) suffix. The \fBpthread_cond_reltimedwait_np()\fR
function is the same as \fBpthread_cond_timedwait()\fR except that the
\fIreltime\fR argument specifies a non-negative time relative to the current
system time rather than an absolute time. The \fIreltime\fR argument is of type
\fBstruct timespec\fR, defined in \fBtime.h\fR(3HEAD). An error value is
returned if the relative time passes (that is, system time equals or exceeds
the starting system time plus the relative time) before the condition
\fIcond\fR is signaled or broadcast.  When such timeouts occur,
\fBpthread_cond_reltimedwait_np()\fR releases and reacquires the mutex
referenced by \fImutex\fR.  The \fBpthread_cond_reltimedwait_np()\fR function
is also a cancellation point.
.sp
.LP
If a signal is delivered to a thread waiting for a condition variable, upon
return from the signal handler the thread resumes waiting for the condition
variable as if it was not interrupted, or it returns \fB0\fR due to spurious
wakeup.
.SH RETURN VALUES
.sp
.LP
Except in the case of \fBETIMEDOUT\fR, \fBEOWNERDEAD\fR, or
\fBENOTRECOVERABLE\fR, all of these error checks act as if they were performed
immediately at the beginning of processing for the function and cause an error
return, in effect, prior to modifying the state of the mutex specified by
\fImutex\fR or the condition variable specified by \fIcond\fR.
.sp
.LP
Upon successful completion, \fB0\fR is returned. Otherwise, an error value is
returned to indicate the error.
.SH ERRORS
.sp
.LP
These functions will fail if:
.sp
.ne 2
.na
\fB\fBEPERM\fR\fR
.ad
.RS 9n
The mutex type is \fBPTHREAD_MUTEX_ERRORCHECK\fR or the mutex is a robust
mutex, and the current thread does not own the mutex.
.RE

.sp
.LP
The  \fBpthread_cond_timedwait()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBETIMEDOUT\fR\fR
.ad
.RS 13n
The absolute time specified by \fIabstime\fR to \fBpthread_cond_timedwait()\fR
has passed.
.RE

.sp
.LP
The \fBpthread_cond_reltimedwait_np()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 13n
The value specified by \fIreltime\fR is invalid.
.RE

.sp
.ne 2
.na
\fB\fBETIMEDOUT\fR\fR
.ad
.RS 13n
The relative time specified by \fIreltime\fR to
\fBpthread_cond_reltimedwait_np()\fR has passed.
.RE

.sp
.LP
These functions may fail if:
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
The value specified by \fIcond\fR, \fImutex\fR, \fIabstime\fR, or \fIreltime\fR
is invalid.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
Different mutexes were supplied for concurrent operations on the same condition
variable.
.RE

.sp
.LP
If the mutex specified by \fImutex\fR is a robust mutex (initialized with the
robustness attribute \fBPTHREAD_MUTEX_ROBUST\fR), the
\fBpthread_cond_wait()\fR, \fBpthread_cond_timedwait()\fR, and
\fBpthread_cond_reltimedwait_np()\fR functions will, under the specified
conditions, return the following error values.  For complete information, see
the \fBpthread_mutex_lock\fR(3C) and \fBpthread_mutexattr_setrobust\fR(3C)
manual pages.
.sp
.ne 2
.na
\fB\fBEOWNERDEAD\fR\fR
.ad
.RS 19n
The last owner of this mutex died while holding the mutex, leaving the state it
was protecting possibly inconsistent. The mutex is now owned by the caller.
.RE

.sp
.ne 2
.na
\fB\fBENOTRECOVERABLE\fR\fR
.ad
.RS 19n
The mutex was protecting state that has now been left irrecoverable. The mutex
has not been acquired.
.RE

.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Standard
_
MT-Level	MT-Safe
.TE

.SH SEE ALSO
.sp
.LP
\fBpthread_cond_signal\fR(3C), \fBpthread_cond_broadcast\fR(3C),
\fBpthread_mutex_lock\fR(3C), \fBpthread_mutexattr_getrobust\fR(3C),
\fBtime.h\fR(3HEAD), \fBattributes\fR(5), \fBcondition\fR(5),
\fBpthreads\fR(5), \fBstandards\fR(5)