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/. */
7 #ifndef nsThreadManager_h__
8 #define nsThreadManager_h__
10 #include "mozilla/Mutex.h"
11 #include "nsIThreadManager.h"
12 #include "nsRefPtrHashtable.h"
18 class ReentrantMonitor
;
21 class nsThreadManager
: public nsIThreadManager
24 #ifdef MOZ_NUWA_PROCESS
25 struct ThreadStatusInfo
;
26 class AllThreadsWereIdleListener
{
28 NS_INLINE_DECL_REFCOUNTING(AllThreadsWereIdleListener
);
29 virtual void OnAllThreadsWereIdle() = 0;
31 virtual ~AllThreadsWereIdleListener()
35 #endif // MOZ_NUWA_PROCESS
38 NS_DECL_NSITHREADMANAGER
40 static nsThreadManager
* get()
42 static nsThreadManager sInstance
;
48 // Shutdown all threads. This function should only be called on the main
49 // thread of the application process.
52 // Called by nsThread to inform the ThreadManager it exists. This method
53 // must be called when the given thread is the current thread.
54 void RegisterCurrentThread(nsThread
* aThread
);
56 // Called by nsThread to inform the ThreadManager it is going away. This
57 // method must be called when the given thread is the current thread.
58 void UnregisterCurrentThread(nsThread
* aThread
);
60 // Returns the current thread. Returns null if OOM or if ThreadManager isn't
62 nsThread
* GetCurrentThread();
64 // Returns the maximal number of threads that have been in existence
65 // simultaneously during the execution of the thread manager.
66 uint32_t GetHighestNumberOfThreads();
68 // This needs to be public in order to support static instantiation of this
69 // class with older compilers (e.g., egcs-2.91.66).
74 #ifdef MOZ_NUWA_PROCESS
75 void SetIgnoreThreadStatus();
77 // |SetThreadWorking| and |SetThreadIdle| set status of thread that is
78 // currently running. They get thread status information from TLS and pass
79 // the information to |SetThreadIsWorking|.
80 void SetThreadIdle(nsIRunnable
** aReturnRunnable
);
81 void SetThreadWorking();
83 // |SetThreadIsWorking| is where is status actually changed. Thread status
84 // information is passed as a argument so caller must obtain the structure
85 // by itself. If this method is invoked on main thread, |aReturnRunnable|
86 // should be provided to receive the runnable of notifying listeners.
87 // |ResetIsDispatchingToMainThread| should be invoked after caller on main
88 // thread dispatched the task to main thread's queue.
89 void SetThreadIsWorking(ThreadStatusInfo
* aInfo
,
91 nsIRunnable
** aReturnRunnable
);
92 void ResetIsDispatchingToMainThread();
94 void AddAllThreadsWereIdleListener(AllThreadsWereIdleListener
*listener
);
95 void RemoveAllThreadsWereIdleListener(AllThreadsWereIdleListener
*listener
);
96 ThreadStatusInfo
* GetCurrentThreadStatusInfo();
97 #endif // MOZ_NUWA_PROCESS
102 , mMainPRThread(nullptr)
103 , mLock("nsThreadManager.mLock")
104 , mInitialized(false)
105 , mCurrentNumberOfThreads(1)
106 , mHighestNumberOfThreads(1)
107 #ifdef MOZ_NUWA_PROCESS
109 , mMainThreadStatusInfo(nullptr)
110 , mDispatchingToMainThread(nullptr)
115 nsRefPtrHashtable
<nsPtrHashKey
<PRThread
>, nsThread
> mThreadsByPRThread
;
116 unsigned mCurThreadIndex
; // thread-local-storage index
117 nsRefPtr
<nsThread
> mMainThread
;
118 PRThread
* mMainPRThread
;
119 mozilla::OffTheBooksMutex mLock
; // protects tables
120 mozilla::Atomic
<bool> mInitialized
;
122 // The current number of threads
123 uint32_t mCurrentNumberOfThreads
;
124 // The highest number of threads encountered so far during the session
125 uint32_t mHighestNumberOfThreads
;
127 #ifdef MOZ_NUWA_PROCESS
128 static void DeleteThreadStatusInfo(void *aData
);
129 unsigned mThreadStatusInfoIndex
;
130 nsTArray
<nsRefPtr
<AllThreadsWereIdleListener
>> mThreadsIdledListeners
;
131 nsTArray
<ThreadStatusInfo
*> mThreadStatusInfos
;
132 mozilla::UniquePtr
<mozilla::ReentrantMonitor
> mMonitor
;
133 ThreadStatusInfo
* mMainThreadStatusInfo
;
134 // |mDispatchingToMainThread| is set when all thread are found to be idle
135 // before task of notifying all listeners are dispatched to main thread.
136 // The flag is protected by |mMonitor|.
137 bool mDispatchingToMainThread
;
138 #endif // MOZ_NUWA_PROCESS
141 #define NS_THREADMANAGER_CID \
142 { /* 7a4204c6-e45a-4c37-8ebb-6709a22c917c */ \
146 {0x8e, 0xbb, 0x67, 0x09, 0xa2, 0x2c, 0x91, 0x7c} \
149 #endif // nsThreadManager_h__