michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef prcvar_h___ michael@0: #define prcvar_h___ michael@0: michael@0: #include "prlock.h" michael@0: #include "prinrval.h" michael@0: michael@0: PR_BEGIN_EXTERN_C michael@0: michael@0: typedef struct PRCondVar PRCondVar; michael@0: michael@0: /* michael@0: ** Create a new condition variable. michael@0: ** michael@0: ** "lock" is the lock used to protect the condition variable. michael@0: ** michael@0: ** Condition variables are synchronization objects that threads can use michael@0: ** to wait for some condition to occur. michael@0: ** michael@0: ** This may fail if memory is tight or if some operating system resource michael@0: ** is low. In such cases, a NULL will be returned. michael@0: */ michael@0: NSPR_API(PRCondVar*) PR_NewCondVar(PRLock *lock); michael@0: michael@0: /* michael@0: ** Destroy a condition variable. There must be no thread michael@0: ** waiting on the condvar. The caller is responsible for guaranteeing michael@0: ** that the condvar is no longer in use. michael@0: ** michael@0: */ michael@0: NSPR_API(void) PR_DestroyCondVar(PRCondVar *cvar); michael@0: michael@0: /* michael@0: ** The thread that waits on a condition is blocked in a "waiting on michael@0: ** condition" state until another thread notifies the condition or a michael@0: ** caller specified amount of time expires. The lock associated with michael@0: ** the condition variable will be released, which must have be held michael@0: ** prior to the call to wait. michael@0: ** michael@0: ** Logically a notified thread is moved from the "waiting on condition" michael@0: ** state and made "ready." When scheduled, it will attempt to reacquire michael@0: ** the lock that it held when wait was called. michael@0: ** michael@0: ** The timeout has two well known values, PR_INTERVAL_NO_TIMEOUT and michael@0: ** PR_INTERVAL_NO_WAIT. The former value requires that a condition be michael@0: ** notified (or the thread interrupted) before it will resume from the michael@0: ** wait. If the timeout has a value of PR_INTERVAL_NO_WAIT, the effect michael@0: ** is to release the lock, possibly causing a rescheduling within the michael@0: ** runtime, then immediately attempting to reacquire the lock and resume. michael@0: ** michael@0: ** Any other value for timeout will cause the thread to be rescheduled michael@0: ** either due to explicit notification or an expired interval. The latter michael@0: ** must be determined by treating time as one part of the monitored data michael@0: ** being protected by the lock and tested explicitly for an expired michael@0: ** interval. michael@0: ** michael@0: ** Returns PR_FAILURE if the caller has not locked the lock associated michael@0: ** with the condition variable or the thread was interrupted (PR_Interrupt()). michael@0: ** The particular reason can be extracted with PR_GetError(). michael@0: */ michael@0: NSPR_API(PRStatus) PR_WaitCondVar(PRCondVar *cvar, PRIntervalTime timeout); michael@0: michael@0: /* michael@0: ** Notify ONE thread that is currently waiting on 'cvar'. Which thread is michael@0: ** dependent on the implementation of the runtime. Common sense would dictate michael@0: ** that all threads waiting on a single condition have identical semantics, michael@0: ** therefore which one gets notified is not significant. michael@0: ** michael@0: ** The calling thead must hold the lock that protects the condition, as michael@0: ** well as the invariants that are tightly bound to the condition, when michael@0: ** notify is called. michael@0: ** michael@0: ** Returns PR_FAILURE if the caller has not locked the lock associated michael@0: ** with the condition variable. michael@0: */ michael@0: NSPR_API(PRStatus) PR_NotifyCondVar(PRCondVar *cvar); michael@0: michael@0: /* michael@0: ** Notify all of the threads waiting on the condition variable. The order michael@0: ** that the threads are notified is indeterminant. The lock that protects michael@0: ** the condition must be held. michael@0: ** michael@0: ** Returns PR_FAILURE if the caller has not locked the lock associated michael@0: ** with the condition variable. michael@0: */ michael@0: NSPR_API(PRStatus) PR_NotifyAllCondVar(PRCondVar *cvar); michael@0: michael@0: PR_END_EXTERN_C michael@0: michael@0: #endif /* prcvar_h___ */