HelperThreadTask.h (2604B)
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 vm_HelperThreadTask_h 8 #define vm_HelperThreadTask_h 9 10 #include "mozilla/TimeStamp.h" 11 12 #include "js/Utility.h" 13 14 namespace js { 15 16 class AutoHelperTaskQueue; 17 class AutoLockHelperThreadState; 18 struct DelazifyTask; 19 struct FreeDelazifyTask; 20 class GlobalHelperThreadState; 21 class SourceCompressionTask; 22 23 namespace jit { 24 class BaselineCompileTask; 25 class IonCompileTask; 26 class IonFreeTask; 27 } // namespace jit 28 namespace wasm { 29 struct CompleteTier2GeneratorTask; 30 struct PartialTier2CompileTask; 31 } // namespace wasm 32 33 template <typename T> 34 struct MapTypeToThreadType {}; 35 36 template <> 37 struct MapTypeToThreadType<jit::BaselineCompileTask> { 38 static const ThreadType threadType = THREAD_TYPE_BASELINE; 39 }; 40 41 template <> 42 struct MapTypeToThreadType<jit::IonCompileTask> { 43 static const ThreadType threadType = THREAD_TYPE_ION; 44 }; 45 46 template <> 47 struct MapTypeToThreadType<wasm::CompleteTier2GeneratorTask> { 48 static const ThreadType threadType = 49 THREAD_TYPE_WASM_GENERATOR_COMPLETE_TIER2; 50 }; 51 52 template <> 53 struct MapTypeToThreadType<wasm::PartialTier2CompileTask> { 54 static const ThreadType threadType = THREAD_TYPE_WASM_COMPILE_PARTIAL_TIER2; 55 }; 56 57 template <> 58 struct MapTypeToThreadType<DelazifyTask> { 59 static const ThreadType threadType = THREAD_TYPE_DELAZIFY; 60 }; 61 62 template <> 63 struct MapTypeToThreadType<FreeDelazifyTask> { 64 static const ThreadType threadType = THREAD_TYPE_DELAZIFY_FREE; 65 }; 66 67 template <> 68 struct MapTypeToThreadType<SourceCompressionTask> { 69 static const ThreadType threadType = THREAD_TYPE_COMPRESS; 70 }; 71 72 } // namespace js 73 74 namespace JS { 75 76 class HelperThreadTask { 77 public: 78 virtual void runHelperThreadTask(js::AutoLockHelperThreadState& locked) = 0; 79 virtual js::ThreadType threadType() = 0; 80 virtual ~HelperThreadTask() = default; 81 82 virtual const char* getName() = 0; 83 84 template <typename T> 85 bool is() { 86 return js::MapTypeToThreadType<T>::threadType == threadType(); 87 } 88 89 template <typename T> 90 T* as() { 91 MOZ_ASSERT(this->is<T>()); 92 return static_cast<T*>(this); 93 } 94 95 protected: 96 // Called when this task is dispatched to the thread pool. 97 virtual void onThreadPoolDispatch() {} 98 friend class js::AutoHelperTaskQueue; 99 }; 100 101 } // namespace JS 102 103 namespace js { 104 using JS::HelperThreadTask; 105 } // namespace js 106 107 #endif /* vm_HelperThreadTask_h */