NSPR Reference Previous Contents Next |
Condition Variable Type
Condition Variable Functions
Conditions are closely associated with a single monitor, which typically consists of a mutex, one or more condition variables, and the monitored data. The association between a condition and a monitor is established when a condition variable is created, and the association persists for its life. In addition, a static association exists between the condition and some data within the monitor. This data is what will be manipulated by the program under the protection of the monitor.
A call to PR_WaitCondVar
causes a thread to block until a specified condition
variable receives notification of a change of state in its associated monitored data.
Other threads may notify the condition variable when changes occur.
For an introduction to NSPR thread synchronization, including locks and condition variables, see Chapter 1 "Introduction to NSPR"
For reference information on NSPR locks, see Chapter 5 "Locks"
NSPR provides a special type, PRMonitor
, for use with Java. Unlike a mutex of
type PRLock
, which can have multiple associated condition variables of type
PRCondVar
, a mutex of type PRMonitor
has a single, implicitly associated condition
variable. For information about PRMonitor
, see Chapter 7 "Monitors"
#include <prcvar.h>
typedef struct PRCondVar PRCondVar;
PR_NewCondVar
PR_DestroyCondVar
PR_WaitCondVar
PR_NotifyCondVar
PR_NotifyAllCondVar
#include <prcvar.h>
PRCondVar* PR_NewCondVar(PRLock *lock);
PR_NewCondVar
has one parameter:
lock |
The identity of the mutex that protects the monitored data, including this
condition variable.
|
If unsuccessful (for example, if system resources are unavailable), NULL
.
#include <prcvar.h>
void PR_DestroyCondVar(PRCondVar *cvar);
PR_DestroyCondVar
has one parameter:
cvar |
A pointer to the condition variable object to be destroyed.
|
PR_DestroyCondVar
, the caller is responsible for ensuring that the
condition variable is no longer in use.
#include <prcvar.h>
PRStatus PR_WaitCondVar(
PRCondVar *cvar,
PRIntervalTime timeout);
PR_WaitCondVar
has the following parameters:
PR_SUCCESS
.
If unsuccessful (for example, if the caller has not locked the lock associated
with the condition variable or the thread was interrupted with PR_Interrupt
),
PR_FAILURE.
The details can be determined with PR_GetError
.
PR_WaitCondVar
, the lock associated with the condition variable
must be held by the calling thread. After a call to PR_WaitCondVar
, the lock is
released and the thread is blocked in a "waiting on condition" state until another
thread notifies the condition or a caller-specified amount of time expires.
When the condition variable is notified, a thread waiting on that condition moves
from the "waiting on condition" state to the "ready" state. When scheduled, the
thread attemps to reacquire the lock that it held when PR_WaitCondVar
was called.
Any value other than PR_INTERVAL_NO_TIMEOUT
or PR_INTERVAL_NO_WAIT
for the
timeout parameter will cause the thread to be rescheduled due to either explicit
notification or the expiration of the specified interval. The latter must be
determined by treating time as one part of the monitored data being protected by
the lock and tested explicitly for an expired interval.
#include <prcvar.h>
PRStatus PR_NotifyCondVar(PRCondVar *cvar);
PR_NotifyCondVar
has one parameter:
cvar |
The condition variable to notify.
|
PR_SUCCESS
.
If unsuccessful (for example, if the caller has not locked the lock associated
with the condition variable), PR_FAILURE
.
Notification of a condition variable signals a change of state in some monitored data. When the notification occurs, the runtime promotes a thread that is waiting on the condition variable to a ready state. If more than one thread is waiting, the selection of which thread gets promoted cannot be predicted. This implies that all threads waiting on a single condition variable must have the same semantics. If no thread is waiting on the condition variable, the notify operation is a no-op.
#include <prcvar.h>
PRStatus PR_NotifyAllCondVar(PRCondVar *cvar);
PR_SUCCESS
.
If unsuccessful (for example, if the caller has not locked the lock associated
with the condition variable), PR_FAILURE
A call to PR_NotifyAllCondVar
causes all of the threads waiting on the specified
condition variable to be promoted to a ready state. If no threads are waiting, the
operation is no-op.
Last Updated May 18, 2001