1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ECMASCRIPT_DAEMON_TASK_H 17 #define ECMASCRIPT_DAEMON_TASK_H 18 19 #include "ecmascript/common.h" 20 21 namespace panda::ecmascript { 22 class JSThread; 23 24 enum class SharedMarkStatus : uint8_t { 25 READY_TO_CONCURRENT_MARK, 26 CONCURRENT_MARKING_OR_FINISHED, 27 }; 28 29 enum class DaemonTaskType : uint32_t { 30 TRIGGER_CONCURRENT_MARK, 31 TRIGGER_COLLECT_GARBAGE, 32 TRIGGER_UNIFIED_GC_MARK, 33 TERMINATE_DAEMON, 34 }; 35 36 /** 37 * Tasks in the same TaskGroup do not need to be posted repeatedly, 38 * e.g. if a TRIGGER_CONCURRENT_MARK is posted, TRIGGER_COLLECT_GARBAGE is not needed. 39 */ 40 enum class DaemonTaskGroup : uint32_t { 41 NONE = 0, 42 GC_GROUP = 1 << 0, 43 TERMINATE_GROUP = 1 << 1, 44 }; 45 46 using TaskRunner = void(*)(); 47 48 class DaemonTask { 49 public: DaemonTask(JSThread * thread,DaemonTaskType taskType,DaemonTaskGroup taskGroup,TaskRunner runner)50 explicit DaemonTask(JSThread *thread, DaemonTaskType taskType, DaemonTaskGroup taskGroup, TaskRunner runner) 51 : thread_(thread), taskType_(taskType), taskGroup_(taskGroup), runner_(runner) {} 52 ~DaemonTask() = default; 53 GetJSThread()54 JSThread *GetJSThread() const 55 { 56 return thread_; 57 } 58 GetTaskType()59 DaemonTaskType GetTaskType() const 60 { 61 return taskType_; 62 } 63 GetTaskGroup()64 DaemonTaskGroup GetTaskGroup() const 65 { 66 return taskGroup_; 67 } 68 Run()69 void Run() 70 { 71 runner_(); 72 } 73 74 private: 75 JSThread *thread_; 76 DaemonTaskType taskType_; 77 DaemonTaskGroup taskGroup_; 78 TaskRunner runner_; 79 }; 80 81 template<TriggerGCType gcType, MarkReason markReason> 82 class TriggerConcurrentMarkTask : public DaemonTask { 83 public: 84 explicit TriggerConcurrentMarkTask(JSThread *thread); 85 }; 86 87 template<TriggerGCType gcType, GCReason gcReason> 88 class TriggerCollectGarbageTask : public DaemonTask { 89 public: 90 explicit TriggerCollectGarbageTask(JSThread *thread); 91 }; 92 template<TriggerGCType gcType, GCReason gcReason> 93 class TriggerUnifiedGCMarkTask : public DaemonTask { 94 public: 95 explicit TriggerUnifiedGCMarkTask(JSThread *thread); 96 }; 97 98 class TerminateDaemonTask : public DaemonTask { 99 public: 100 explicit TerminateDaemonTask(JSThread *thread); 101 }; 102 } // namespace panda::ecmascript 103 #endif //ECMASCRIPT_DAEMON_TASK_H