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 prmon_h___ michael@0: #define prmon_h___ michael@0: michael@0: #include "prtypes.h" michael@0: #include "prinrval.h" michael@0: michael@0: PR_BEGIN_EXTERN_C michael@0: michael@0: typedef struct PRMonitor PRMonitor; michael@0: michael@0: /* michael@0: ** Create a new monitor. Monitors are re-entrant locks with a single built-in michael@0: ** condition variable. michael@0: ** michael@0: ** This may fail if memory is tight or if some operating system resource michael@0: ** is low. michael@0: */ michael@0: NSPR_API(PRMonitor*) PR_NewMonitor(void); michael@0: michael@0: /* michael@0: ** Destroy a monitor. The caller is responsible for guaranteeing that the michael@0: ** monitor is no longer in use. There must be no thread waiting on the monitor's michael@0: ** condition variable and that the lock is not held. michael@0: ** michael@0: */ michael@0: NSPR_API(void) PR_DestroyMonitor(PRMonitor *mon); michael@0: michael@0: /* michael@0: ** Enter the lock associated with the monitor. If the calling thread currently michael@0: ** is in the monitor, the call to enter will silently succeed. In either case, michael@0: ** it will increment the entry count by one. michael@0: */ michael@0: NSPR_API(void) PR_EnterMonitor(PRMonitor *mon); michael@0: michael@0: /* michael@0: ** Decrement the entry count associated with the monitor. If the decremented michael@0: ** entry count is zero, the monitor is exited. Returns PR_FAILURE if the michael@0: ** calling thread has not entered the monitor. michael@0: */ michael@0: NSPR_API(PRStatus) PR_ExitMonitor(PRMonitor *mon); michael@0: michael@0: /* michael@0: ** Wait for a notify on the monitor's condition variable. Sleep for "ticks" michael@0: ** amount of time (if "ticks" is PR_INTERVAL_NO_TIMEOUT then the sleep is michael@0: ** indefinite). michael@0: ** michael@0: ** While the thread is waiting it exits the monitor (as if it called michael@0: ** PR_ExitMonitor as many times as it had called PR_EnterMonitor). When michael@0: ** the wait has finished the thread regains control of the monitors lock michael@0: ** with the same entry count as before the wait began. michael@0: ** michael@0: ** The thread waiting on the monitor will be resumed when the monitor is michael@0: ** notified (assuming the thread is the next in line to receive the michael@0: ** notify) or when the "ticks" timeout elapses. michael@0: ** michael@0: ** Returns PR_FAILURE if the caller has not entered the monitor. michael@0: */ michael@0: NSPR_API(PRStatus) PR_Wait(PRMonitor *mon, PRIntervalTime ticks); michael@0: michael@0: /* michael@0: ** Notify a thread waiting on the monitor's condition variable. If a thread michael@0: ** is waiting on the condition variable (using PR_Wait) then it is awakened michael@0: ** and attempts to reenter the monitor. michael@0: */ michael@0: NSPR_API(PRStatus) PR_Notify(PRMonitor *mon); michael@0: michael@0: /* michael@0: ** Notify all of the threads waiting on the monitor's condition variable. michael@0: ** All of threads waiting on the condition are scheduled to reenter the michael@0: ** monitor. michael@0: */ michael@0: NSPR_API(PRStatus) PR_NotifyAll(PRMonitor *mon); michael@0: michael@0: /* michael@0: ** PR_ASSERT_CURRENT_THREAD_IN_MONITOR michael@0: ** If the current thread is in |mon|, this assertion is guaranteed to michael@0: ** succeed. Otherwise, the behavior of this function is undefined. michael@0: */ michael@0: #if defined(DEBUG) || defined(FORCE_PR_ASSERT) michael@0: #define PR_ASSERT_CURRENT_THREAD_IN_MONITOR(/* PRMonitor* */ mon) \ michael@0: PR_AssertCurrentThreadInMonitor(mon) michael@0: #else michael@0: #define PR_ASSERT_CURRENT_THREAD_IN_MONITOR(/* PRMonitor* */ mon) michael@0: #endif michael@0: michael@0: /* Don't call this function directly. */ michael@0: NSPR_API(void) PR_AssertCurrentThreadInMonitor(PRMonitor *mon); michael@0: michael@0: PR_END_EXTERN_C michael@0: michael@0: #endif /* prmon_h___ */