michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsThreadManager_h__ michael@0: #define nsThreadManager_h__ michael@0: michael@0: #include "mozilla/Mutex.h" michael@0: #include "nsIThreadManager.h" michael@0: #include "nsRefPtrHashtable.h" michael@0: #include "nsThread.h" michael@0: michael@0: class nsIRunnable; michael@0: michael@0: class nsThreadManager : public nsIThreadManager michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSITHREADMANAGER michael@0: michael@0: static nsThreadManager *get() { michael@0: static nsThreadManager sInstance; michael@0: return &sInstance; michael@0: } michael@0: michael@0: nsresult Init(); michael@0: michael@0: // Shutdown all threads. This function should only be called on the main michael@0: // thread of the application process. michael@0: void Shutdown(); michael@0: michael@0: // Called by nsThread to inform the ThreadManager it exists. This method michael@0: // must be called when the given thread is the current thread. michael@0: void RegisterCurrentThread(nsThread *thread); michael@0: michael@0: // Called by nsThread to inform the ThreadManager it is going away. This michael@0: // method must be called when the given thread is the current thread. michael@0: void UnregisterCurrentThread(nsThread *thread); michael@0: michael@0: // Returns the current thread. Returns null if OOM or if ThreadManager isn't michael@0: // initialized. michael@0: nsThread *GetCurrentThread(); michael@0: michael@0: // Returns the maximal number of threads that have been in existence michael@0: // simultaneously during the execution of the thread manager. michael@0: uint32_t GetHighestNumberOfThreads(); michael@0: michael@0: // This needs to be public in order to support static instantiation of this michael@0: // class with older compilers (e.g., egcs-2.91.66). michael@0: ~nsThreadManager() {} michael@0: michael@0: private: michael@0: nsThreadManager() michael@0: : mCurThreadIndex(0) michael@0: , mMainPRThread(nullptr) michael@0: , mLock(nullptr) michael@0: , mInitialized(false) michael@0: , mCurrentNumberOfThreads(1) michael@0: , mHighestNumberOfThreads(1) { michael@0: } michael@0: michael@0: nsRefPtrHashtable, nsThread> mThreadsByPRThread; michael@0: unsigned mCurThreadIndex; // thread-local-storage index michael@0: nsRefPtr mMainThread; michael@0: PRThread *mMainPRThread; michael@0: // This is a pointer in order to allow creating nsThreadManager from michael@0: // the static context in debug builds. michael@0: nsAutoPtr mLock; // protects tables michael@0: bool mInitialized; michael@0: michael@0: // The current number of threads michael@0: uint32_t mCurrentNumberOfThreads; michael@0: // The highest number of threads encountered so far during the session michael@0: uint32_t mHighestNumberOfThreads; michael@0: }; michael@0: michael@0: #define NS_THREADMANAGER_CID \ michael@0: { /* 7a4204c6-e45a-4c37-8ebb-6709a22c917c */ \ michael@0: 0x7a4204c6, \ michael@0: 0xe45a, \ michael@0: 0x4c37, \ michael@0: {0x8e, 0xbb, 0x67, 0x09, 0xa2, 0x2c, 0x91, 0x7c} \ michael@0: } michael@0: michael@0: #endif // nsThreadManager_h__