Fire assertions when guard objects are used as temporaries. (Bug 531460) r=cjones

This commit is contained in:
L. David Baron 2010-04-04 11:15:18 -07:00
Родитель 74f53c2869
Коммит eca80ee500
3 изменённых файлов: 33 добавлений и 14 удалений

Просмотреть файл

@ -173,13 +173,18 @@ namespace mozilla {
* } * }
*/ */
template <class T> template <class T>
class AutoRestore class NS_STACK_CLASS AutoRestore
{ {
private: private:
T& mLocation; T& mLocation;
T mValue; T mValue;
MOZILLA_DECL_USE_GUARD_OBJECT_NOTIFIER
public: public:
AutoRestore(T& aValue) : mLocation(aValue), mValue(aValue) {} AutoRestore(T& aValue MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM)
: mLocation(aValue), mValue(aValue)
{
MOZILLA_GUARD_OBJECT_NOTIFIER_INIT;
}
~AutoRestore() { mLocation = mValue; } ~AutoRestore() { mLocation = mValue; }
}; };

Просмотреть файл

@ -41,6 +41,7 @@
#include "prlock.h" #include "prlock.h"
#include "mozilla/AutoRestore.h"
#include "mozilla/BlockingResourceBase.h" #include "mozilla/BlockingResourceBase.h"
// //
@ -173,9 +174,10 @@ public:
* @param aLock A valid mozilla::Mutex* returned by * @param aLock A valid mozilla::Mutex* returned by
* mozilla::Mutex::NewMutex. * mozilla::Mutex::NewMutex.
**/ **/
MutexAutoLock(mozilla::Mutex& aLock) : MutexAutoLock(mozilla::Mutex& aLock MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM) :
mLock(&aLock) mLock(&aLock)
{ {
MOZILLA_GUARD_OBJECT_NOTIFIER_INIT;
NS_ASSERTION(mLock, "null mutex"); NS_ASSERTION(mLock, "null mutex");
mLock->Lock(); mLock->Lock();
} }
@ -192,6 +194,7 @@ private:
static void operator delete(void*); static void operator delete(void*);
mozilla::Mutex* mLock; mozilla::Mutex* mLock;
MOZILLA_DECL_USE_GUARD_OBJECT_NOTIFIER
}; };
@ -205,9 +208,10 @@ private:
class NS_COM_GLUE NS_STACK_CLASS MutexAutoUnlock class NS_COM_GLUE NS_STACK_CLASS MutexAutoUnlock
{ {
public: public:
MutexAutoUnlock(mozilla::Mutex& aLock) : MutexAutoUnlock(mozilla::Mutex& aLock MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM) :
mLock(&aLock) mLock(&aLock)
{ {
MOZILLA_GUARD_OBJECT_NOTIFIER_INIT;
NS_ASSERTION(mLock, "null lock"); NS_ASSERTION(mLock, "null lock");
mLock->Unlock(); mLock->Unlock();
} }
@ -225,6 +229,7 @@ private:
static void operator delete(void*); static void operator delete(void*);
mozilla::Mutex* mLock; mozilla::Mutex* mLock;
MOZILLA_DECL_USE_GUARD_OBJECT_NOTIFIER
}; };

Просмотреть файл

@ -109,13 +109,14 @@
#include "nscore.h" #include "nscore.h"
#include "prlock.h" #include "prlock.h"
#include "prlog.h" #include "prlog.h"
#include "mozilla/AutoRestore.h"
/** /**
* nsAutoLockBase * nsAutoLockBase
* This is the base class for the stack-based locking objects. * This is the base class for the stack-based locking objects.
* Clients of derived classes need not play with this superclass. * Clients of derived classes need not play with this superclass.
**/ **/
class NS_COM_GLUE nsAutoLockBase { class NS_COM_GLUE NS_STACK_CLASS nsAutoLockBase {
friend class nsAutoUnlockBase; friend class nsAutoUnlockBase;
protected: protected:
@ -146,7 +147,7 @@ protected:
* This is the base class for stack-based unlocking objects. * This is the base class for stack-based unlocking objects.
* It unlocks locking objects based on nsAutoLockBase. * It unlocks locking objects based on nsAutoLockBase.
**/ **/
class NS_COM_GLUE nsAutoUnlockBase { class NS_COM_GLUE NS_STACK_CLASS nsAutoUnlockBase {
protected: protected:
nsAutoUnlockBase() {} nsAutoUnlockBase() {}
@ -165,10 +166,11 @@ protected:
* nsAutoLock * nsAutoLock
* Stack-based locking object for PRLock. * Stack-based locking object for PRLock.
**/ **/
class NS_COM_GLUE nsAutoLock : public nsAutoLockBase { class NS_COM_GLUE NS_STACK_CLASS nsAutoLock : public nsAutoLockBase {
private: private:
PRLock* mLock; PRLock* mLock;
PRBool mLocked; PRBool mLocked;
MOZILLA_DECL_USE_GUARD_OBJECT_NOTIFIER
// Not meant to be implemented. This makes it a compiler error to // Not meant to be implemented. This makes it a compiler error to
// construct or assign an nsAutoLock object incorrectly. // construct or assign an nsAutoLock object incorrectly.
@ -208,10 +210,11 @@ public:
* @param aLock A valid PRLock* returned from the NSPR's * @param aLock A valid PRLock* returned from the NSPR's
* PR_NewLock() function. * PR_NewLock() function.
**/ **/
nsAutoLock(PRLock* aLock) nsAutoLock(PRLock* aLock MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM)
: nsAutoLockBase(aLock, eAutoLock), : nsAutoLockBase(aLock, eAutoLock),
mLock(aLock), mLock(aLock),
mLocked(PR_TRUE) { mLocked(PR_TRUE) {
MOZILLA_GUARD_OBJECT_NOTIFIER_INIT;
PR_ASSERT(mLock); PR_ASSERT(mLock);
// This will assert deep in the bowels of NSPR if you attempt // This will assert deep in the bowels of NSPR if you attempt
@ -250,16 +253,18 @@ public:
} }
}; };
class nsAutoUnlock : private nsAutoUnlockBase class NS_STACK_CLASS nsAutoUnlock : private nsAutoUnlockBase
{ {
private: private:
PRLock *mLock; PRLock *mLock;
MOZILLA_DECL_USE_GUARD_OBJECT_NOTIFIER
public: public:
nsAutoUnlock(PRLock *lock) : nsAutoUnlock(PRLock *lock MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM) :
nsAutoUnlockBase(lock), nsAutoUnlockBase(lock),
mLock(lock) mLock(lock)
{ {
MOZILLA_GUARD_OBJECT_NOTIFIER_INIT;
PR_Unlock(mLock); PR_Unlock(mLock);
} }
@ -272,7 +277,7 @@ public:
#include "nsError.h" #include "nsError.h"
#include "nsDebug.h" #include "nsDebug.h"
class NS_COM_GLUE nsAutoMonitor : public nsAutoLockBase { class NS_COM_GLUE NS_STACK_CLASS nsAutoMonitor : public nsAutoLockBase {
public: public:
/** /**
@ -295,10 +300,11 @@ public:
* @param mon A valid PRMonitor* returned from * @param mon A valid PRMonitor* returned from
* nsAutoMonitor::NewMonitor(). * nsAutoMonitor::NewMonitor().
**/ **/
nsAutoMonitor(PRMonitor* mon) nsAutoMonitor(PRMonitor* mon MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM)
: nsAutoLockBase((void*)mon, eAutoMonitor), : nsAutoLockBase((void*)mon, eAutoMonitor),
mMonitor(mon), mLockCount(0) mMonitor(mon), mLockCount(0)
{ {
MOZILLA_GUARD_OBJECT_NOTIFIER_INIT;
NS_ASSERTION(mMonitor, "null monitor"); NS_ASSERTION(mMonitor, "null monitor");
if (mMonitor) { if (mMonitor) {
PR_EnterMonitor(mMonitor); PR_EnterMonitor(mMonitor);
@ -361,6 +367,7 @@ public:
private: private:
PRMonitor* mMonitor; PRMonitor* mMonitor;
PRInt32 mLockCount; PRInt32 mLockCount;
MOZILLA_DECL_USE_GUARD_OBJECT_NOTIFIER
// Not meant to be implemented. This makes it a compiler error to // Not meant to be implemented. This makes it a compiler error to
// construct or assign an nsAutoLock object incorrectly. // construct or assign an nsAutoLock object incorrectly.
@ -386,12 +393,13 @@ private:
#include "prcmon.h" #include "prcmon.h"
#include "nsError.h" #include "nsError.h"
class NS_COM_GLUE nsAutoCMonitor : public nsAutoLockBase { class NS_COM_GLUE NS_STACK_CLASS nsAutoCMonitor : public nsAutoLockBase {
public: public:
nsAutoCMonitor(void* lockObject) nsAutoCMonitor(void* lockObject MOZILLA_GUARD_OBJECT_NOTIFIER_PARAM)
: nsAutoLockBase(lockObject, eAutoCMonitor), : nsAutoLockBase(lockObject, eAutoCMonitor),
mLockObject(lockObject), mLockCount(0) mLockObject(lockObject), mLockCount(0)
{ {
MOZILLA_GUARD_OBJECT_NOTIFIER_INIT;
NS_ASSERTION(lockObject, "null lock object"); NS_ASSERTION(lockObject, "null lock object");
PR_CEnterMonitor(mLockObject); PR_CEnterMonitor(mLockObject);
mLockCount = 1; mLockCount = 1;
@ -428,6 +436,7 @@ public:
private: private:
void* mLockObject; void* mLockObject;
PRInt32 mLockCount; PRInt32 mLockCount;
MOZILLA_DECL_USE_GUARD_OBJECT_NOTIFIER
// Not meant to be implemented. This makes it a compiler error to // Not meant to be implemented. This makes it a compiler error to
// construct or assign an nsAutoLock object incorrectly. // construct or assign an nsAutoLock object incorrectly.