michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 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: #include michael@0: michael@0: #include "primpl.h" 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: PR_IMPLEMENT(PRMonitor*) michael@0: PR_NewMonitor (void) michael@0: { michael@0: PRMonitor *mon; michael@0: PRCondVar *cvar; michael@0: PRLock *lock; michael@0: michael@0: mon = PR_NEWZAP( PRMonitor ); michael@0: if( mon ) michael@0: { michael@0: lock = PR_NewLock(); michael@0: if( !lock ) michael@0: { michael@0: PR_DELETE( mon ); michael@0: return( 0 ); michael@0: } michael@0: michael@0: cvar = PR_NewCondVar( lock ); michael@0: if( !cvar ) michael@0: { michael@0: PR_DestroyLock( lock ); michael@0: PR_DELETE( mon ); michael@0: return( 0 ); michael@0: } michael@0: michael@0: mon->cvar = cvar; michael@0: mon->name = NULL; michael@0: } michael@0: michael@0: return( mon ); michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRMonitor*) PR_NewNamedMonitor(const char* name) michael@0: { michael@0: PRMonitor* mon = PR_NewMonitor(); michael@0: if( mon ) michael@0: { michael@0: mon->name = name; michael@0: } michael@0: return mon; michael@0: } 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 michael@0: ** monitor's condition variable and that the lock is not held. michael@0: ** michael@0: */ michael@0: PR_IMPLEMENT(void) michael@0: PR_DestroyMonitor (PRMonitor *mon) michael@0: { michael@0: PR_DestroyLock( mon->cvar->lock ); michael@0: PR_DestroyCondVar( mon->cvar ); michael@0: PR_DELETE( mon ); michael@0: } 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: PR_IMPLEMENT(void) michael@0: PR_EnterMonitor (PRMonitor *mon) michael@0: { michael@0: if( mon->cvar->lock->owner == find_thread( NULL ) ) michael@0: { michael@0: mon->entryCount++; michael@0: michael@0: } else michael@0: { michael@0: PR_Lock( mon->cvar->lock ); michael@0: mon->entryCount = 1; michael@0: } michael@0: } 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: PR_IMPLEMENT(PRStatus) michael@0: PR_ExitMonitor (PRMonitor *mon) michael@0: { michael@0: if( mon->cvar->lock->owner != find_thread( NULL ) ) michael@0: { michael@0: return( PR_FAILURE ); michael@0: } michael@0: if( --mon->entryCount == 0 ) michael@0: { michael@0: return( PR_Unlock( mon->cvar->lock ) ); michael@0: } michael@0: return( PR_SUCCESS ); michael@0: } 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: PR_IMPLEMENT(PRStatus) michael@0: PR_Wait (PRMonitor *mon, PRIntervalTime ticks) michael@0: { michael@0: PRUint32 entryCount; michael@0: PRUintn status; michael@0: PRThread *meThread; michael@0: thread_id me = find_thread( NULL ); michael@0: meThread = PR_GetCurrentThread(); michael@0: michael@0: if( mon->cvar->lock->owner != me ) return( PR_FAILURE ); michael@0: michael@0: entryCount = mon->entryCount; michael@0: mon->entryCount = 0; michael@0: michael@0: status = PR_WaitCondVar( mon->cvar, ticks ); michael@0: michael@0: mon->entryCount = entryCount; michael@0: michael@0: return( status ); michael@0: } 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: PR_IMPLEMENT(PRStatus) michael@0: PR_Notify (PRMonitor *mon) michael@0: { michael@0: if( mon->cvar->lock->owner != find_thread( NULL ) ) michael@0: { michael@0: return( PR_FAILURE ); michael@0: } michael@0: michael@0: PR_NotifyCondVar( mon->cvar ); michael@0: return( PR_SUCCESS ); michael@0: } 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: PR_IMPLEMENT(PRStatus) michael@0: PR_NotifyAll (PRMonitor *mon) michael@0: { michael@0: if( mon->cvar->lock->owner != find_thread( NULL ) ) michael@0: { michael@0: return( PR_FAILURE ); michael@0: } michael@0: michael@0: PR_NotifyAllCondVar( mon->cvar ); michael@0: return( PR_SUCCESS ); michael@0: } michael@0: michael@0: /* michael@0: ** Return the number of times that the current thread has entered the michael@0: ** lock. Returns zero if the current thread has not entered the lock. michael@0: */ michael@0: PR_IMPLEMENT(PRIntn) michael@0: PR_GetMonitorEntryCount(PRMonitor *mon) michael@0: { michael@0: return( (mon->cvar->lock->owner == find_thread( NULL )) ? michael@0: mon->entryCount : 0 ); michael@0: } michael@0: michael@0: /* 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: PR_IMPLEMENT(void) michael@0: PR_AssertCurrentThreadInMonitor(PRMonitor *mon) michael@0: { michael@0: PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(mon->cvar->lock); michael@0: }