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     TERMINATE_DAEMON,
33 };
34 
35 /**
36  * Tasks in the same TaskGroup do not need to be posted repeatedly,
37  * e.g. if a TRIGGER_CONCURRENT_MARK is posted, TRIGGER_COLLECT_GARBAGE is not needed.
38 */
39 enum class DaemonTaskGroup : uint32_t {
40     NONE = 0,
41     GC_GROUP = 1 << 0,
42     TERMINATE_GROUP = 1 << 1,
43 };
44 
45 using TaskRunner = void(*)();
46 
47 class DaemonTask {
48 public:
DaemonTask(JSThread *thread, DaemonTaskType taskType, DaemonTaskGroup taskGroup, TaskRunner runner)49     explicit DaemonTask(JSThread *thread, DaemonTaskType taskType, DaemonTaskGroup taskGroup, TaskRunner runner)
50         : thread_(thread), taskType_(taskType), taskGroup_(taskGroup), runner_(runner) {}
51     ~DaemonTask() = default;
52 
GetJSThread() const53     JSThread *GetJSThread() const
54     {
55         return thread_;
56     }
57 
GetTaskType() const58     DaemonTaskType GetTaskType() const
59     {
60         return taskType_;
61     }
62 
GetTaskGroup() const63     DaemonTaskGroup GetTaskGroup() const
64     {
65         return taskGroup_;
66     }
67 
Run()68     void Run()
69     {
70         runner_();
71     }
72 
73 private:
74     JSThread *thread_;
75     DaemonTaskType taskType_;
76     DaemonTaskGroup taskGroup_;
77     TaskRunner runner_;
78 };
79 
80 template<TriggerGCType gcType, GCReason gcReason>
81 class TriggerConcurrentMarkTask : public DaemonTask {
82 public:
83     explicit TriggerConcurrentMarkTask(JSThread *thread);
84 };
85 
86 template<TriggerGCType gcType, GCReason gcReason>
87 class TriggerCollectGarbageTask : public DaemonTask {
88 public:
89     explicit TriggerCollectGarbageTask(JSThread *thread);
90 };
91 
92 class TerminateDaemonTask : public DaemonTask {
93 public:
94     explicit TerminateDaemonTask(JSThread *thread);
95 };
96 }  // namespace panda::ecmascript
97 #endif //ECMASCRIPT_DAEMON_TASK_H