Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 2481 : static nsThreadManager& get()
24 : {
25 2481 : static nsThreadManager sInstance;
26 2481 : return sInstance;
27 : }
28 :
29 : nsresult Init();
30 :
31 : // Shutdown all threads. This function should only be called on the main
32 : // thread of the application process.
33 : void Shutdown();
34 :
35 : // Called by nsThread to inform the ThreadManager it exists. This method
36 : // must be called when the given thread is the current thread.
37 : void RegisterCurrentThread(nsThread& aThread);
38 :
39 : // Called by nsThread to inform the ThreadManager it is going away. This
40 : // method must be called when the given thread is the current thread.
41 : void UnregisterCurrentThread(nsThread& aThread);
42 :
43 : // Returns the current thread. Returns null if OOM or if ThreadManager isn't
44 : // initialized.
45 : nsThread* GetCurrentThread();
46 :
47 : // Returns the maximal number of threads that have been in existence
48 : // simultaneously during the execution of the thread manager.
49 : uint32_t GetHighestNumberOfThreads();
50 :
51 : // This needs to be public in order to support static instantiation of this
52 : // class with older compilers (e.g., egcs-2.91.66).
53 0 : ~nsThreadManager()
54 0 : {
55 0 : }
56 :
57 : private:
58 3 : nsThreadManager()
59 3 : : mCurThreadIndex(0)
60 : , mMainPRThread(nullptr)
61 : , mLock("nsThreadManager.mLock")
62 : , mInitialized(false)
63 : , mCurrentNumberOfThreads(1)
64 3 : , mHighestNumberOfThreads(1)
65 : {
66 3 : }
67 :
68 : nsRefPtrHashtable<nsPtrHashKey<PRThread>, nsThread> mThreadsByPRThread;
69 : unsigned mCurThreadIndex; // thread-local-storage index
70 : RefPtr<nsThread> mMainThread;
71 : PRThread* mMainPRThread;
72 : mozilla::OffTheBooksMutex mLock; // protects tables
73 : mozilla::Atomic<bool> mInitialized;
74 :
75 : // The current number of threads
76 : uint32_t mCurrentNumberOfThreads;
77 : // The highest number of threads encountered so far during the session
78 : uint32_t mHighestNumberOfThreads;
79 : };
80 :
81 : #define NS_THREADMANAGER_CID \
82 : { /* 7a4204c6-e45a-4c37-8ebb-6709a22c917c */ \
83 : 0x7a4204c6, \
84 : 0xe45a, \
85 : 0x4c37, \
86 : {0x8e, 0xbb, 0x67, 0x09, 0xa2, 0x2c, 0x91, 0x7c} \
87 : }
88 :
89 : #endif // nsThreadManager_h__
|