From 8d14a63fbaf1dcd8e438de28bb90e0066e5ae3e4 Mon Sep 17 00:00:00 2001 From: Justin Lebar Date: Fri, 24 May 2013 13:10:47 -0400 Subject: [PATCH] Bug 832609 - Add mozilla::OffTheBooksMutex, which is just like mozilla::Mutex, except it's not leak-checked. r=khuey --HG-- extra : rebase_source : 2c06334a5db4c8b57c9a3480a83e543dd98a9b20 --- xpcom/glue/BlockingResourceBase.cpp | 6 +-- xpcom/glue/Mutex.h | 62 +++++++++++++++++++---------- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/xpcom/glue/BlockingResourceBase.cpp b/xpcom/glue/BlockingResourceBase.cpp index 07024bbbd9a7..b08ff23f5fe0 100644 --- a/xpcom/glue/BlockingResourceBase.cpp +++ b/xpcom/glue/BlockingResourceBase.cpp @@ -218,9 +218,9 @@ BlockingResourceBase::PrintCycle(const DDT::ResourceAcquisitionArray* aCycle, // -// Debug implementation of Mutex +// Debug implementation of (OffTheBooks)Mutex void -Mutex::Lock() +OffTheBooksMutex::Lock() { CallStack callContext = CallStack(); @@ -230,7 +230,7 @@ Mutex::Lock() } void -Mutex::Unlock() +OffTheBooksMutex::Unlock() { Release(); // protected by mLock PRStatus status = PR_Unlock(mLock); diff --git a/xpcom/glue/Mutex.h b/xpcom/glue/Mutex.h index c395fc349f56..2edd53c59cb6 100644 --- a/xpcom/glue/Mutex.h +++ b/xpcom/glue/Mutex.h @@ -20,48 +20,44 @@ // locked and unlocked // - MutexAutoUnlock, complementary sibling to MutexAutoLock // -// Using MutexAutoLock/MutexAutoUnlock is MUCH preferred to making bare -// calls to Mutex.Lock and Unlock. +// - OffTheBooksMutex, a non-recursive mutex that doesn't do leak checking +// - OffTheBooksMutexAuto{Lock,Unlock} - Like MutexAuto{Lock,Unlock}, but for +// an OffTheBooksMutex. +// +// Using MutexAutoLock/MutexAutoUnlock etc. is MUCH preferred to making bare +// calls to Lock and Unlock. // namespace mozilla { - /** - * Mutex - * When possible, use MutexAutoLock/MutexAutoUnlock to lock/unlock this - * mutex within a scope, instead of calling Lock/Unlock directly. - **/ - -class NS_COM_GLUE Mutex : BlockingResourceBase + * OffTheBooksMutex is identical to Mutex, except that OffTheBooksMutex doesn't + * include leak checking. Sometimes you want to intentionally "leak" a mutex + * until shutdown; in these cases, OffTheBooksMutex is for you. + */ +class NS_COM_GLUE OffTheBooksMutex : BlockingResourceBase { public: /** - * Mutex * @param name A name which can reference this lock * @returns If failure, nullptr * If success, a valid Mutex* which must be destroyed * by Mutex::DestroyMutex() **/ - Mutex(const char* name) : + OffTheBooksMutex(const char* name) : BlockingResourceBase(name, eMutex) { - MOZ_COUNT_CTOR(Mutex); mLock = PR_NewLock(); if (!mLock) NS_RUNTIMEABORT("Can't allocate mozilla::Mutex"); } - /** - * ~Mutex - **/ - ~Mutex() + ~OffTheBooksMutex() { NS_ASSERTION(mLock, "improperly constructed Lock or double free"); // NSPR does consistency checks for us PR_DestroyLock(mLock); mLock = 0; - MOZ_COUNT_DTOR(Mutex); } #ifndef DEBUG @@ -116,15 +112,39 @@ public: #endif // ifndef DEBUG private: - Mutex(); - Mutex(const Mutex&); - Mutex& operator=(const Mutex&); + OffTheBooksMutex(); + OffTheBooksMutex(const OffTheBooksMutex&); + OffTheBooksMutex& operator=(const OffTheBooksMutex&); PRLock* mLock; friend class CondVar; }; +/** + * Mutex + * When possible, use MutexAutoLock/MutexAutoUnlock to lock/unlock this + * mutex within a scope, instead of calling Lock/Unlock directly. + */ +class NS_COM_GLUE Mutex : public OffTheBooksMutex +{ +public: + Mutex(const char* name) + : OffTheBooksMutex(name) + { + MOZ_COUNT_CTOR(Mutex); + } + + ~Mutex() + { + MOZ_COUNT_DTOR(Mutex); + } + +private: + Mutex(); + Mutex(const Mutex&); + Mutex& operator=(const Mutex&); +}; /** * MutexAutoLock @@ -169,6 +189,7 @@ private: }; typedef BaseAutoLock MutexAutoLock; +typedef BaseAutoLock OffTheBooksMutexAutoLock; /** * MutexAutoUnlock @@ -206,6 +227,7 @@ private: }; typedef BaseAutoUnlock MutexAutoUnlock; +typedef BaseAutoUnlock OffTheBooksMutexAutoUnlock; } // namespace mozilla