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