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 "js/Utility.h"
11 
12 namespace js {
13 
14 class AutoLockHelperThreadState;
15 struct ParseTask;
16 class SourceCompressionTask;
17 
18 namespace jit {
19 class IonCompileTask;
20 class IonFreeTask;
21 }  // namespace jit
22 namespace wasm {
23 struct Tier2GeneratorTask;
24 }  // namespace wasm
25 
26 template <typename T>
27 struct MapTypeToThreadType {};
28 
29 template <>
30 struct MapTypeToThreadType<jit::IonCompileTask> {
31   static const ThreadType threadType = THREAD_TYPE_ION;
32 };
33 
34 template <>
35 struct MapTypeToThreadType<wasm::Tier2GeneratorTask> {
36   static const ThreadType threadType = THREAD_TYPE_WASM_GENERATOR_TIER2;
37 };
38 
39 template <>
40 struct MapTypeToThreadType<ParseTask> {
41   static const ThreadType threadType = THREAD_TYPE_PARSE;
42 };
43 
44 template <>
45 struct MapTypeToThreadType<SourceCompressionTask> {
46   static const ThreadType threadType = THREAD_TYPE_COMPRESS;
47 };
48 
49 struct HelperThreadTask {
50   virtual void runHelperThreadTask(AutoLockHelperThreadState& locked) = 0;
51   virtual ThreadType threadType() = 0;
52   virtual ~HelperThreadTask() = default;
53 
54   template <typename T>
55   bool is() {
56     return MapTypeToThreadType<T>::threadType == threadType();
57   }
58 
59   template <typename T>
60   T* as() {
61     MOZ_ASSERT(this->is<T>());
62     return static_cast<T*>(this);
63   }
64 };
65 
66 }  // namespace js
67 
68 #endif /* vm_HelperThreadTask_h */
69