From 1fafa621b418cec760c7810bdc3df18e12933aaf Mon Sep 17 00:00:00 2001 From: "warren%netscape.com" Date: Wed, 16 Jun 1999 04:15:08 +0000 Subject: [PATCH] Changed nsAutoMonitor to nsAutoCMonitor (cached monitors). git-svn-id: svn://10.0.0.236/trunk@35645 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/xpcom/ds/nsBuffer.cpp | 4 +- mozilla/xpcom/tests/TestBuffers.cpp | 6 +-- mozilla/xpcom/threads/nsAutoLock.h | 65 ++++++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/mozilla/xpcom/ds/nsBuffer.cpp b/mozilla/xpcom/ds/nsBuffer.cpp index 27b5fbfd18c..72cfc069c3c 100644 --- a/mozilla/xpcom/ds/nsBuffer.cpp +++ b/mozilla/xpcom/ds/nsBuffer.cpp @@ -85,7 +85,7 @@ nsBuffer::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) nsresult nsBuffer::PushWriteSegment() { - nsAutoMonitor mon(this); // protect mSegments + nsAutoCMonitor mon(this); // protect mSegments if (mBufferSize >= mMaxSize) { if (mObserver) { @@ -118,7 +118,7 @@ nsresult nsBuffer::PopReadSegment() { nsresult rv; - nsAutoMonitor mon(this); // protect mSegments + nsAutoCMonitor mon(this); // protect mSegments PRCList* header = (PRCList*)mSegments.next; char* segment = (char*)header; diff --git a/mozilla/xpcom/tests/TestBuffers.cpp b/mozilla/xpcom/tests/TestBuffers.cpp index 6bb900e5d0c..676901794a3 100644 --- a/mozilla/xpcom/tests/TestBuffers.cpp +++ b/mozilla/xpcom/tests/TestBuffers.cpp @@ -34,7 +34,7 @@ public: NS_IMETHOD Run() { nsresult rv; - nsAutoMonitor mon(this); + nsAutoCMonitor mon(this); char buf[100]; PRUint32 readCnt; @@ -128,7 +128,7 @@ WriteMessages(nsIBuffer* buffer) return rv; } if (cnt == 0) { - nsAutoMonitor mon(reader); + nsAutoCMonitor mon(reader); mon.Notify(); // wake up reader mon.Wait(); // and wait for reader to read all } @@ -142,7 +142,7 @@ WriteMessages(nsIBuffer* buffer) PR_FREEIF(mem); { reader->SetEOF(); - nsAutoMonitor mon(reader); + nsAutoCMonitor mon(reader); mon.Notify(); // wake up reader } diff --git a/mozilla/xpcom/threads/nsAutoLock.h b/mozilla/xpcom/threads/nsAutoLock.h index a86aab4c870..cab0d845db9 100644 --- a/mozilla/xpcom/threads/nsAutoLock.h +++ b/mozilla/xpcom/threads/nsAutoLock.h @@ -130,14 +130,69 @@ public: class nsAutoMonitor { public: - nsAutoMonitor(void* lockObject) + nsAutoMonitor(PRMonitor* mon) + : mMonitor(mon) + { + NS_ASSERTION(mMonitor, "null lock object"); + PR_EnterMonitor(mMonitor); + } + + ~nsAutoMonitor() { + PR_ExitMonitor(mMonitor); + } + + nsresult Wait(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT) { + return PR_Wait(mMonitor, interval) == PR_SUCCESS + ? NS_OK : NS_ERROR_FAILURE; + } + + nsresult Notify() { + return PR_Notify(mMonitor) == PR_SUCCESS + ? NS_OK : NS_ERROR_FAILURE; + } + + nsresult NotifyAll() { + return PR_NotifyAll(mMonitor) == PR_SUCCESS + ? NS_OK : NS_ERROR_FAILURE; + } + +private: + PRMonitor* mMonitor; + + // Not meant to be implemented. This makes it a compiler error to + // construct or assign an nsAutoLock object incorrectly. + nsAutoMonitor(void) {} + nsAutoMonitor(nsAutoMonitor& aMon) {} + nsAutoMonitor& operator =(nsAutoMonitor& aMon) { + return *this; + } + + // Not meant to be implemented. This makes it a compiler error to + // attempt to create an nsAutoLock object on the heap. + static void* operator new(size_t size) { + return nsnull; + } + static void operator delete(void* memory) {} +}; + +//////////////////////////////////////////////////////////////////////////////// +// Once again, this time with a cache... +// (Using this avoids the need to allocate a PRMonitor, which may be useful when +// a large number of objects of the same class need associated monitors.) + +#include "prcmon.h" +#include "nsError.h" + +class nsAutoCMonitor { +public: + nsAutoCMonitor(void* lockObject) : mLockObject(lockObject) { NS_ASSERTION(lockObject, "null lock object"); PR_CEnterMonitor(mLockObject); } - ~nsAutoMonitor() { + ~nsAutoCMonitor() { PR_CExitMonitor(mLockObject); } @@ -161,9 +216,9 @@ private: // Not meant to be implemented. This makes it a compiler error to // construct or assign an nsAutoLock object incorrectly. - nsAutoMonitor(void) {} - nsAutoMonitor(nsAutoMonitor& aMon) {} - nsAutoMonitor& operator =(nsAutoMonitor& aMon) { + nsAutoCMonitor(void) {} + nsAutoCMonitor(nsAutoCMonitor& aMon) {} + nsAutoCMonitor& operator =(nsAutoCMonitor& aMon) { return *this; }