|
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/. */ |
|
6 |
|
7 #ifndef nsThreadManager_h__ |
|
8 #define nsThreadManager_h__ |
|
9 |
|
10 #include "mozilla/Mutex.h" |
|
11 #include "nsIThreadManager.h" |
|
12 #include "nsRefPtrHashtable.h" |
|
13 #include "nsThread.h" |
|
14 |
|
15 class nsIRunnable; |
|
16 |
|
17 class nsThreadManager : public nsIThreadManager |
|
18 { |
|
19 public: |
|
20 NS_DECL_ISUPPORTS |
|
21 NS_DECL_NSITHREADMANAGER |
|
22 |
|
23 static nsThreadManager *get() { |
|
24 static nsThreadManager sInstance; |
|
25 return &sInstance; |
|
26 } |
|
27 |
|
28 nsresult Init(); |
|
29 |
|
30 // Shutdown all threads. This function should only be called on the main |
|
31 // thread of the application process. |
|
32 void Shutdown(); |
|
33 |
|
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); |
|
37 |
|
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); |
|
41 |
|
42 // Returns the current thread. Returns null if OOM or if ThreadManager isn't |
|
43 // initialized. |
|
44 nsThread *GetCurrentThread(); |
|
45 |
|
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(); |
|
49 |
|
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() {} |
|
53 |
|
54 private: |
|
55 nsThreadManager() |
|
56 : mCurThreadIndex(0) |
|
57 , mMainPRThread(nullptr) |
|
58 , mLock(nullptr) |
|
59 , mInitialized(false) |
|
60 , mCurrentNumberOfThreads(1) |
|
61 , mHighestNumberOfThreads(1) { |
|
62 } |
|
63 |
|
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; |
|
72 |
|
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 }; |
|
78 |
|
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 } |
|
86 |
|
87 #endif // nsThreadManager_h__ |