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