Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsThreadManager_h__
8 #define nsThreadManager_h__
10 #include "mozilla/Mutex.h"
11 #include "nsIThreadManager.h"
12 #include "nsRefPtrHashtable.h"
13 #include "nsThread.h"
15 class nsIRunnable;
17 class nsThreadManager : public nsIThreadManager
18 {
19 public:
20 NS_DECL_ISUPPORTS
21 NS_DECL_NSITHREADMANAGER
23 static nsThreadManager *get() {
24 static nsThreadManager sInstance;
25 return &sInstance;
26 }
28 nsresult Init();
30 // Shutdown all threads. This function should only be called on the main
31 // thread of the application process.
32 void Shutdown();
34 // Called by nsThread to inform the ThreadManager it exists. This method
35 // must be called when the given thread is the current thread.
36 void RegisterCurrentThread(nsThread *thread);
38 // Called by nsThread to inform the ThreadManager it is going away. This
39 // method must be called when the given thread is the current thread.
40 void UnregisterCurrentThread(nsThread *thread);
42 // Returns the current thread. Returns null if OOM or if ThreadManager isn't
43 // initialized.
44 nsThread *GetCurrentThread();
46 // Returns the maximal number of threads that have been in existence
47 // simultaneously during the execution of the thread manager.
48 uint32_t GetHighestNumberOfThreads();
50 // This needs to be public in order to support static instantiation of this
51 // class with older compilers (e.g., egcs-2.91.66).
52 ~nsThreadManager() {}
54 private:
55 nsThreadManager()
56 : mCurThreadIndex(0)
57 , mMainPRThread(nullptr)
58 , mLock(nullptr)
59 , mInitialized(false)
60 , mCurrentNumberOfThreads(1)
61 , mHighestNumberOfThreads(1) {
62 }
64 nsRefPtrHashtable<nsPtrHashKey<PRThread>, nsThread> mThreadsByPRThread;
65 unsigned mCurThreadIndex; // thread-local-storage index
66 nsRefPtr<nsThread> mMainThread;
67 PRThread *mMainPRThread;
68 // This is a pointer in order to allow creating nsThreadManager from
69 // the static context in debug builds.
70 nsAutoPtr<mozilla::Mutex> mLock; // protects tables
71 bool mInitialized;
73 // The current number of threads
74 uint32_t mCurrentNumberOfThreads;
75 // The highest number of threads encountered so far during the session
76 uint32_t mHighestNumberOfThreads;
77 };
79 #define NS_THREADMANAGER_CID \
80 { /* 7a4204c6-e45a-4c37-8ebb-6709a22c917c */ \
81 0x7a4204c6, \
82 0xe45a, \
83 0x4c37, \
84 {0x8e, 0xbb, 0x67, 0x09, 0xa2, 0x2c, 0x91, 0x7c} \
85 }
87 #endif // nsThreadManager_h__