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