/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include"mozilla/BlockingResourceBase.h" #include"mozilla/ThreadSafety.h" #include"nsISupports.h" // // Provides: // // - ReentrantMonitor, a Java-like monitor // - ReentrantMonitorAutoEnter, an RAII class for ensuring that // ReentrantMonitors are properly entered and exited // // Using ReentrantMonitorAutoEnter is MUCH preferred to making bare calls to // ReentrantMonitor.Enter and Exit. // namespace mozilla {
/** * ReentrantMonitor * Java-like monitor. * When possible, use ReentrantMonitorAutoEnter to hold this monitor within a * scope, instead of calling Enter/Exit directly.
**/ class MOZ_CAPABILITY("reentrant monitor") ReentrantMonitor
: BlockingResourceBase { public: /** * ReentrantMonitor * @param aName A name which can reference this monitor
*/ explicit ReentrantMonitor(constchar* aName)
: BlockingResourceBase(aName, eReentrantMonitor) #ifdef DEBUG
,
mEntryCount(0) #endif
{
MOZ_COUNT_CTOR(ReentrantMonitor);
mReentrantMonitor = PR_NewMonitor(); if (!mReentrantMonitor) {
MOZ_CRASH("Can't allocate mozilla::ReentrantMonitor");
}
}
/** * ReentrantMonitorAutoEnter * Enters the ReentrantMonitor when it enters scope, and exits it when * it leaves scope. * * MUCH PREFERRED to bare calls to ReentrantMonitor.Enter and Exit.
*/ class MOZ_SCOPED_CAPABILITY MOZ_STACK_CLASS ReentrantMonitorAutoEnter { public: /** * Constructor * The constructor aquires the given lock. The destructor * releases the lock. * * @param aReentrantMonitor A valid mozilla::ReentrantMonitor*.
**/ explicit ReentrantMonitorAutoEnter(
mozilla::ReentrantMonitor& aReentrantMonitor)
MOZ_CAPABILITY_ACQUIRE(aReentrantMonitor)
: mReentrantMonitor(&aReentrantMonitor) {
NS_ASSERTION(mReentrantMonitor, "null monitor");
mReentrantMonitor->Enter();
}
/** * ReentrantMonitorAutoExit * Exit the ReentrantMonitor when it enters scope, and enters it when it leaves * scope. * * MUCH PREFERRED to bare calls to ReentrantMonitor.Exit and Enter.
*/ class MOZ_SCOPED_CAPABILITY MOZ_STACK_CLASS ReentrantMonitorAutoExit { public: /** * Constructor * The constructor releases the given lock. The destructor * acquires the lock. The lock must be held before constructing * this object! * * @param aReentrantMonitor A valid mozilla::ReentrantMonitor*. It * must be already locked.
**/ explicit ReentrantMonitorAutoExit(ReentrantMonitor& aReentrantMonitor)
MOZ_EXCLUSIVE_RELEASE(aReentrantMonitor)
: mReentrantMonitor(&aReentrantMonitor) {
NS_ASSERTION(mReentrantMonitor, "null monitor");
mReentrantMonitor->AssertCurrentThreadIn();
mReentrantMonitor->Exit();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.