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: #include "primpl.h" michael@0: michael@0: /************************************************************************/ michael@0: michael@0: /* michael@0: * Notifies just get posted to the monitor. The actual notification is done michael@0: * when the monitor is fully exited so that MP systems don't contend for a michael@0: * monitor that they can't enter. michael@0: */ michael@0: static void _PR_PostNotifyToMonitor(PRMonitor *mon, PRBool broadcast) michael@0: { michael@0: PR_ASSERT(mon != NULL); michael@0: PR_ASSERT_CURRENT_THREAD_IN_MONITOR(mon); michael@0: michael@0: /* mon->notifyTimes is protected by the monitor, so we don't need to michael@0: * acquire mon->lock. michael@0: */ michael@0: if (broadcast) michael@0: mon->notifyTimes = -1; michael@0: else if (mon->notifyTimes != -1) michael@0: mon->notifyTimes += 1; michael@0: } michael@0: michael@0: static void _PR_PostNotifiesFromMonitor(PRCondVar *cv, PRIntn times) michael@0: { michael@0: PRStatus rv; michael@0: michael@0: /* michael@0: * Time to actually notify any waits that were affected while the monitor michael@0: * was entered. michael@0: */ michael@0: PR_ASSERT(cv != NULL); michael@0: PR_ASSERT(times != 0); michael@0: if (times == -1) { michael@0: rv = PR_NotifyAllCondVar(cv); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: } else { michael@0: while (times-- > 0) { michael@0: rv = PR_NotifyCondVar(cv); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* michael@0: ** Create a new monitor. michael@0: */ michael@0: PR_IMPLEMENT(PRMonitor*) PR_NewMonitor() michael@0: { michael@0: PRMonitor *mon; michael@0: PRStatus rv; michael@0: michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: michael@0: mon = PR_NEWZAP(PRMonitor); michael@0: if (mon == NULL) { michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); michael@0: return NULL; michael@0: } michael@0: michael@0: rv = _PR_InitLock(&mon->lock); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: if (rv != PR_SUCCESS) michael@0: goto error1; michael@0: michael@0: mon->owner = NULL; michael@0: michael@0: rv = _PR_InitCondVar(&mon->entryCV, &mon->lock); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: if (rv != PR_SUCCESS) michael@0: goto error2; michael@0: michael@0: rv = _PR_InitCondVar(&mon->waitCV, &mon->lock); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: if (rv != PR_SUCCESS) michael@0: goto error3; michael@0: michael@0: mon->notifyTimes = 0; michael@0: mon->entryCount = 0; michael@0: mon->name = NULL; michael@0: return mon; michael@0: michael@0: error3: michael@0: _PR_FreeCondVar(&mon->entryCV); michael@0: error2: michael@0: _PR_FreeLock(&mon->lock); michael@0: error1: michael@0: PR_Free(mon); michael@0: return NULL; 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: mon->name = name; michael@0: return mon; michael@0: } michael@0: michael@0: /* michael@0: ** Destroy a monitor. There must be no thread waiting on the monitor's michael@0: ** condition variable. The caller is responsible for guaranteeing that the michael@0: ** monitor is no longer in use. michael@0: */ michael@0: PR_IMPLEMENT(void) PR_DestroyMonitor(PRMonitor *mon) michael@0: { michael@0: PR_ASSERT(mon != NULL); michael@0: _PR_FreeCondVar(&mon->waitCV); michael@0: _PR_FreeCondVar(&mon->entryCV); michael@0: _PR_FreeLock(&mon->lock); michael@0: #if defined(DEBUG) michael@0: memset(mon, 0xaf, sizeof(PRMonitor)); michael@0: #endif michael@0: PR_Free(mon); michael@0: } michael@0: michael@0: /* michael@0: ** Enter the lock associated with the monitor. michael@0: */ michael@0: PR_IMPLEMENT(void) PR_EnterMonitor(PRMonitor *mon) michael@0: { michael@0: PRThread *me = _PR_MD_CURRENT_THREAD(); michael@0: PRStatus rv; michael@0: michael@0: PR_ASSERT(mon != NULL); michael@0: PR_Lock(&mon->lock); michael@0: if (mon->entryCount != 0) { michael@0: if (mon->owner == me) michael@0: goto done; michael@0: while (mon->entryCount != 0) { michael@0: rv = PR_WaitCondVar(&mon->entryCV, PR_INTERVAL_NO_TIMEOUT); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: } michael@0: } michael@0: /* and now I have the monitor */ michael@0: PR_ASSERT(mon->notifyTimes == 0); michael@0: PR_ASSERT(mon->owner == NULL); michael@0: mon->owner = me; michael@0: michael@0: done: michael@0: mon->entryCount += 1; michael@0: rv = PR_Unlock(&mon->lock); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: } michael@0: michael@0: /* michael@0: ** Test and then enter the lock associated with the monitor if it's not michael@0: ** already entered by some other thread. Return PR_FALSE if some other michael@0: ** thread owned the lock at the time of the call. michael@0: */ michael@0: PR_IMPLEMENT(PRBool) PR_TestAndEnterMonitor(PRMonitor *mon) michael@0: { michael@0: PRThread *me = _PR_MD_CURRENT_THREAD(); michael@0: PRStatus rv; michael@0: michael@0: PR_ASSERT(mon != NULL); michael@0: PR_Lock(&mon->lock); michael@0: if (mon->entryCount != 0) { michael@0: if (mon->owner == me) michael@0: goto done; michael@0: rv = PR_Unlock(&mon->lock); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: return PR_FALSE; michael@0: } michael@0: /* and now I have the monitor */ michael@0: PR_ASSERT(mon->notifyTimes == 0); michael@0: PR_ASSERT(mon->owner == NULL); michael@0: mon->owner = me; michael@0: michael@0: done: michael@0: mon->entryCount += 1; michael@0: rv = PR_Unlock(&mon->lock); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: return PR_TRUE; michael@0: } michael@0: michael@0: /* michael@0: ** Exit the lock associated with the monitor once. michael@0: */ michael@0: PR_IMPLEMENT(PRStatus) PR_ExitMonitor(PRMonitor *mon) michael@0: { michael@0: PRThread *me = _PR_MD_CURRENT_THREAD(); michael@0: PRStatus rv; michael@0: michael@0: PR_ASSERT(mon != NULL); michael@0: PR_Lock(&mon->lock); michael@0: /* the entries should be > 0 and we'd better be the owner */ michael@0: PR_ASSERT(mon->entryCount > 0); michael@0: PR_ASSERT(mon->owner == me); michael@0: if (mon->entryCount == 0 || mon->owner != me) michael@0: { michael@0: rv = PR_Unlock(&mon->lock); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: return PR_FAILURE; michael@0: } michael@0: michael@0: mon->entryCount -= 1; /* reduce by one */ michael@0: if (mon->entryCount == 0) michael@0: { michael@0: /* and if it transitioned to zero - notify an entry waiter */ michael@0: /* make the owner unknown */ michael@0: mon->owner = NULL; michael@0: if (mon->notifyTimes != 0) { michael@0: _PR_PostNotifiesFromMonitor(&mon->waitCV, mon->notifyTimes); michael@0: mon->notifyTimes = 0; michael@0: } michael@0: rv = PR_NotifyCondVar(&mon->entryCV); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: } michael@0: rv = PR_Unlock(&mon->lock); michael@0: PR_ASSERT(rv == PR_SUCCESS); 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) PR_GetMonitorEntryCount(PRMonitor *mon) michael@0: { michael@0: PRThread *me = _PR_MD_CURRENT_THREAD(); michael@0: PRStatus rv; michael@0: PRIntn count = 0; michael@0: michael@0: PR_Lock(&mon->lock); michael@0: if (mon->owner == me) michael@0: count = mon->entryCount; michael@0: rv = PR_Unlock(&mon->lock); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: return count; michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) PR_AssertCurrentThreadInMonitor(PRMonitor *mon) michael@0: { michael@0: #if defined(DEBUG) || defined(FORCE_PR_ASSERT) michael@0: PRStatus rv; michael@0: michael@0: PR_Lock(&mon->lock); michael@0: PR_ASSERT(mon->entryCount != 0 && michael@0: mon->owner == _PR_MD_CURRENT_THREAD()); michael@0: rv = PR_Unlock(&mon->lock); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: #endif michael@0: } michael@0: michael@0: /* michael@0: ** Wait for a notify on the condition variable. Sleep for "ticks" amount michael@0: ** of time (if "tick" is 0 then the sleep is indefinite). While michael@0: ** the thread is waiting it exits the monitors lock (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" elapses. michael@0: ** michael@0: ** Returns PR_FAILURE if the caller has not locked the lock associated michael@0: ** with the condition variable. michael@0: ** This routine can return PR_PENDING_INTERRUPT_ERROR if the waiting thread michael@0: ** has been interrupted. michael@0: */ michael@0: PR_IMPLEMENT(PRStatus) PR_Wait(PRMonitor *mon, PRIntervalTime ticks) michael@0: { michael@0: PRStatus rv; michael@0: PRUint32 saved_entries; michael@0: PRThread *saved_owner; michael@0: michael@0: PR_ASSERT(mon != NULL); michael@0: PR_Lock(&mon->lock); michael@0: /* the entries better be positive */ michael@0: PR_ASSERT(mon->entryCount > 0); michael@0: /* and it better be owned by us */ michael@0: PR_ASSERT(mon->owner == _PR_MD_CURRENT_THREAD()); /* XXX return failure */ michael@0: michael@0: /* tuck these away 'till later */ michael@0: saved_entries = mon->entryCount; michael@0: mon->entryCount = 0; michael@0: saved_owner = mon->owner; michael@0: mon->owner = NULL; michael@0: /* If we have pending notifies, post them now. */ michael@0: if (mon->notifyTimes != 0) { michael@0: _PR_PostNotifiesFromMonitor(&mon->waitCV, mon->notifyTimes); michael@0: mon->notifyTimes = 0; michael@0: } michael@0: rv = PR_NotifyCondVar(&mon->entryCV); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: michael@0: rv = PR_WaitCondVar(&mon->waitCV, ticks); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: michael@0: while (mon->entryCount != 0) { michael@0: rv = PR_WaitCondVar(&mon->entryCV, PR_INTERVAL_NO_TIMEOUT); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: } michael@0: PR_ASSERT(mon->notifyTimes == 0); michael@0: /* reinstate the interesting information */ michael@0: mon->entryCount = saved_entries; michael@0: mon->owner = saved_owner; michael@0: michael@0: rv = PR_Unlock(&mon->lock); michael@0: PR_ASSERT(rv == PR_SUCCESS); michael@0: return rv; michael@0: } michael@0: michael@0: /* michael@0: ** Notify the highest priority thread waiting on the condition michael@0: ** variable. If a thread is waiting on the condition variable (using michael@0: ** PR_Wait) then it is awakened and begins waiting on the monitor's lock. michael@0: */ michael@0: PR_IMPLEMENT(PRStatus) PR_Notify(PRMonitor *mon) michael@0: { michael@0: _PR_PostNotifyToMonitor(mon, PR_FALSE); michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: /* michael@0: ** Notify all of the threads waiting on the condition variable. All of michael@0: ** threads are notified in turn. The highest priority thread will michael@0: ** probably acquire the monitor first when the monitor is exited. michael@0: */ michael@0: PR_IMPLEMENT(PRStatus) PR_NotifyAll(PRMonitor *mon) michael@0: { michael@0: _PR_PostNotifyToMonitor(mon, PR_TRUE); michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: /************************************************************************/ michael@0: michael@0: PRUint32 _PR_MonitorToString(PRMonitor *mon, char *buf, PRUint32 buflen) michael@0: { michael@0: PRUint32 nb; michael@0: michael@0: if (mon->owner) { michael@0: nb = PR_snprintf(buf, buflen, "[%p] owner=%d[%p] count=%ld", michael@0: mon, mon->owner->id, mon->owner, mon->entryCount); michael@0: } else { michael@0: nb = PR_snprintf(buf, buflen, "[%p]", mon); michael@0: } michael@0: return nb; michael@0: }