InternalThreadPool.h (2218B)
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 /* 8 * An internal thread pool, used for the shell and when 9 * JS::SetHelperThreadTaskCallback not called. 10 */ 11 12 #ifndef vm_InternalThreadPool_h 13 #define vm_InternalThreadPool_h 14 15 #include "js/AllocPolicy.h" 16 #include "js/UniquePtr.h" 17 #include "js/Vector.h" 18 #include "threading/ConditionVariable.h" 19 #include "threading/ProtectedData.h" 20 21 namespace JS { 22 class HelperThreadTask; 23 }; 24 25 namespace js { 26 27 class AutoLockHelperThreadState; 28 class HelperThread; 29 using JS::HelperThreadTask; 30 31 using HelperThreadVector = 32 Vector<UniquePtr<HelperThread>, 0, SystemAllocPolicy>; 33 34 using HelperTaskVector = Vector<HelperThreadTask*, 0, SystemAllocPolicy>; 35 36 class InternalThreadPool { 37 public: 38 static bool Initialize(size_t threadCount, AutoLockHelperThreadState& lock); 39 static void ShutDown(AutoLockHelperThreadState& lock); 40 41 static bool IsInitialized() { return Instance; } 42 static InternalThreadPool& Get(); 43 44 bool ensureThreadCount(size_t threadCount, AutoLockHelperThreadState& lock); 45 size_t threadCount(const AutoLockHelperThreadState& lock); 46 47 size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf, 48 const AutoLockHelperThreadState& lock) const; 49 50 private: 51 static void DispatchTask(HelperThreadTask* task); 52 53 void dispatchOrQueueTask(HelperThreadTask* task); 54 void maybeDispatchQueuedTask(); 55 void shutDown(AutoLockHelperThreadState& lock); 56 57 HelperThreadVector& threads(const AutoLockHelperThreadState& lock); 58 const HelperThreadVector& threads( 59 const AutoLockHelperThreadState& lock) const; 60 61 void setThreadFree(uint32_t threadId); 62 void clearThreadFree(uint32_t threadId); 63 64 friend class HelperThread; 65 66 static InternalThreadPool* Instance; 67 68 HelperThreadLockData<HelperThreadVector> threads_; 69 HelperThreadLockData<bool> terminating; 70 HelperThreadLockData<uint32_t> freeThreadSet; 71 }; 72 73 } // namespace js 74 75 #endif /* vm_InternalThreadPool_h */