Bug 724368 - Expose the maximum number of threads. r=dougt
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Fri, 19 Apr 2013 07:54:18 -0400
changeset 129323 4fb6cb5453268a3db3f97015f87e9f378cf70c64
parent 129322 39615ca8ab61c0ef1d23a0666492a4884eb72b11
child 129324 532c3e448de059f7369db809ff8ffbda1270ef1c
push idunknown
push userunknown
push dateunknown
reviewersdougt
bugs724368
milestone23.0a1
Bug 724368 - Expose the maximum number of threads. r=dougt
xpcom/threads/nsThreadManager.cpp
xpcom/threads/nsThreadManager.h
--- a/xpcom/threads/nsThreadManager.cpp
+++ b/xpcom/threads/nsThreadManager.cpp
@@ -155,29 +155,35 @@ nsThreadManager::Shutdown()
 
 void
 nsThreadManager::RegisterCurrentThread(nsThread *thread)
 {
   MOZ_ASSERT(thread->GetPRThread() == PR_GetCurrentThread(), "bad thread");
 
   MutexAutoLock lock(*mLock);
 
+  ++mCurrentNumberOfThreads;
+  if (mCurrentNumberOfThreads > mHighestNumberOfThreads) {
+    mHighestNumberOfThreads = mCurrentNumberOfThreads;
+  }
+
   mThreadsByPRThread.Put(thread->GetPRThread(), thread);  // XXX check OOM?
 
   NS_ADDREF(thread);  // for TLS entry
   PR_SetThreadPrivate(mCurThreadIndex, thread);
 }
 
 void
 nsThreadManager::UnregisterCurrentThread(nsThread *thread)
 {
   MOZ_ASSERT(thread->GetPRThread() == PR_GetCurrentThread(), "bad thread");
 
   MutexAutoLock lock(*mLock);
 
+  --mCurrentNumberOfThreads;
   mThreadsByPRThread.Remove(thread->GetPRThread());
 
   PR_SetThreadPrivate(mCurThreadIndex, nullptr);
   // Ref-count balanced via ReleaseObject
 }
 
 nsThread *
 nsThreadManager::GetCurrentThread()
@@ -274,8 +280,15 @@ nsThreadManager::GetIsMainThread(bool *r
 }
 
 NS_IMETHODIMP
 nsThreadManager::GetIsCycleCollectorThread(bool *result)
 {
   *result = bool(NS_IsCycleCollectorThread());
   return NS_OK;
 }
+
+uint32_t
+nsThreadManager::GetHighestNumberOfThreads()
+{
+  MutexAutoLock lock(*mLock);
+  return mHighestNumberOfThreads;
+}
--- a/xpcom/threads/nsThreadManager.h
+++ b/xpcom/threads/nsThreadManager.h
@@ -37,38 +37,49 @@ public:
   // Called by nsThread to inform the ThreadManager it is going away.  This
   // method must be called when the given thread is the current thread.
   void UnregisterCurrentThread(nsThread *thread);
 
   // Returns the current thread.  Returns null if OOM or if ThreadManager isn't
   // initialized.
   nsThread *GetCurrentThread();
 
+  // Returns the maximal number of threads that have been in existence
+  // simultaneously during the execution of the thread manager.
+  uint32_t GetHighestNumberOfThreads();
+
   // This needs to be public in order to support static instantiation of this
   // class with older compilers (e.g., egcs-2.91.66).
   ~nsThreadManager() {}
 
 private:
   nsThreadManager()
     : mCurThreadIndex(0)
     , mMainPRThread(nullptr)
     , mLock(nullptr)
-    , mInitialized(false) {
+    , mInitialized(false)
+    , mCurrentNumberOfThreads(1)
+    , mHighestNumberOfThreads(1) {
   }
-  
+
   static nsThreadManager sInstance;
 
   nsRefPtrHashtable<nsPtrHashKey<PRThread>, nsThread> mThreadsByPRThread;
   unsigned             mCurThreadIndex;  // thread-local-storage index
   nsRefPtr<nsThread>  mMainThread;
   PRThread           *mMainPRThread;
   // This is a pointer in order to allow creating nsThreadManager from
   // the static context in debug builds.
   nsAutoPtr<mozilla::Mutex> mLock;  // protects tables
   bool                mInitialized;
+
+   // The current number of threads
+   uint32_t           mCurrentNumberOfThreads;
+   // The highest number of threads encountered so far during the session
+   uint32_t           mHighestNumberOfThreads;
 };
 
 #define NS_THREADMANAGER_CID                       \
 { /* 7a4204c6-e45a-4c37-8ebb-6709a22c917c */       \
   0x7a4204c6,                                      \
   0xe45a,                                          \
   0x4c37,                                          \
   {0x8e, 0xbb, 0x67, 0x09, 0xa2, 0x2c, 0x91, 0x7c} \