Bug 619096 - nsMemoryReporterManager should be threadsafe. r=vlad a=mfinkle
authorDoug Turner <dougt@dougt.org>
Wed, 15 Dec 2010 09:47:16 -0800
changeset 59197 398c0e73be649e1310b9164f1a75aa43a4dca739
parent 59196 e952221c325176496f920ceb1004424a79237cd6
child 59198 06802b3064902aae1264f64e991468af547e981a
child 59205 6ee595cd73323e508255eace564e8ed5b23c21e9
push id17579
push userdougt@mozilla.com
push dateWed, 15 Dec 2010 17:48:10 +0000
treeherdermozilla-central@398c0e73be64 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvlad, mfinkle
bugs619096
milestone2.0b9pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 619096 - nsMemoryReporterManager should be threadsafe. r=vlad a=mfinkle
xpcom/base/nsMemoryReporterManager.cpp
xpcom/base/nsMemoryReporterManager.h
--- a/xpcom/base/nsMemoryReporterManager.cpp
+++ b/xpcom/base/nsMemoryReporterManager.cpp
@@ -34,17 +34,16 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "nsCOMPtr.h"
 #include "nsServiceManagerUtils.h"
 #include "nsMemoryReporterManager.h"
-
 #include "nsArrayEnumerator.h"
 
 /**
  ** memory reporter implementation for jemalloc and OSX malloc,
  ** to obtain info on total memory in use (that we know about,
  ** at least -- on OSX, there are sometimes other zones in use).
  **/
 
@@ -205,17 +204,17 @@ NS_MEMORY_REPORTER_IMPLEMENT(Win32Privat
                              GetWin32PrivateBytes,
                              nsnull);
 #endif
 
 /**
  ** nsMemoryReporterManager implementation
  **/
 
-NS_IMPL_ISUPPORTS1(nsMemoryReporterManager, nsIMemoryReporterManager)
+NS_IMPL_THREADSAFE_ISUPPORTS1(nsMemoryReporterManager, nsIMemoryReporterManager)
 
 NS_IMETHODIMP
 nsMemoryReporterManager::Init()
 {
 #if HAVE_JEMALLOC_STATS && defined(XP_LINUX)
     if (!jemalloc_stats)
         return NS_ERROR_FAILURE;
 #endif
@@ -245,35 +244,49 @@ nsMemoryReporterManager::Init()
     REGISTER(Win32PrivateBytes);
 #endif
     REGISTER(Win32WorkingSetSize);
 #endif
 
     return NS_OK;
 }
 
+nsMemoryReporterManager::nsMemoryReporterManager()
+  : mMutex("nsMemoryReporterManager::mMutex")
+{
+}
+
+nsMemoryReporterManager::~nsMemoryReporterManager()
+{
+}
+
 NS_IMETHODIMP
 nsMemoryReporterManager::EnumerateReporters(nsISimpleEnumerator **result)
 {
-    return NS_NewArrayEnumerator(result, mReporters);
+    nsresult rv;
+    mozilla::MutexAutoLock autoLock(mMutex); 
+    rv = NS_NewArrayEnumerator(result, mReporters);
+    return rv;
 }
 
 NS_IMETHODIMP
 nsMemoryReporterManager::RegisterReporter(nsIMemoryReporter *reporter)
 {
+    mozilla::MutexAutoLock autoLock(mMutex); 
     if (mReporters.IndexOf(reporter) != -1)
         return NS_ERROR_FAILURE;
 
     mReporters.AppendObject(reporter);
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsMemoryReporterManager::UnregisterReporter(nsIMemoryReporter *reporter)
 {
+    mozilla::MutexAutoLock autoLock(mMutex); 
     if (!mReporters.RemoveObject(reporter))
         return NS_ERROR_FAILURE;
 
     return NS_OK;
 }
 
 NS_COM nsresult
 NS_RegisterMemoryReporter (nsIMemoryReporter *reporter)
--- a/xpcom/base/nsMemoryReporterManager.h
+++ b/xpcom/base/nsMemoryReporterManager.h
@@ -1,17 +1,24 @@
 
 #include "nsIMemoryReporter.h"
 #include "nsCOMArray.h"
+#include "mozilla/Mutex.h"
+
+using mozilla::Mutex;
 
 class nsMemoryReporterManager : public nsIMemoryReporterManager
 {
 public:
     NS_DECL_ISUPPORTS
     NS_DECL_NSIMEMORYREPORTERMANAGER
 
+    nsMemoryReporterManager();
+    virtual ~nsMemoryReporterManager();
+
 private:
     nsCOMArray<nsIMemoryReporter> mReporters;
+    Mutex                         mMutex;
 };
 
 #define NS_MEMORY_REPORTER_MANAGER_CID \
 { 0xfb97e4f5, 0x32dd, 0x497a, \
 { 0xba, 0xa2, 0x7d, 0x1e, 0x55, 0x7, 0x99, 0x10 } }