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