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
21namespace panda::ecmascript {
22class JSThread;
23
24enum class SharedMarkStatus : uint8_t {
25    READY_TO_CONCURRENT_MARK,
26    CONCURRENT_MARKING_OR_FINISHED,
27};
28
29enum 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*/
39enum class DaemonTaskGroup : uint32_t {
40    NONE = 0,
41    GC_GROUP = 1 << 0,
42    TERMINATE_GROUP = 1 << 1,
43};
44
45using TaskRunner = void(*)();
46
47class DaemonTask {
48public:
49    explicit DaemonTask(JSThread *thread, DaemonTaskType taskType, DaemonTaskGroup taskGroup, TaskRunner runner)
50        : thread_(thread), taskType_(taskType), taskGroup_(taskGroup), runner_(runner) {}
51    ~DaemonTask() = default;
52
53    JSThread *GetJSThread() const
54    {
55        return thread_;
56    }
57
58    DaemonTaskType GetTaskType() const
59    {
60        return taskType_;
61    }
62
63    DaemonTaskGroup GetTaskGroup() const
64    {
65        return taskGroup_;
66    }
67
68    void Run()
69    {
70        runner_();
71    }
72
73private:
74    JSThread *thread_;
75    DaemonTaskType taskType_;
76    DaemonTaskGroup taskGroup_;
77    TaskRunner runner_;
78};
79
80template<TriggerGCType gcType, GCReason gcReason>
81class TriggerConcurrentMarkTask : public DaemonTask {
82public:
83    explicit TriggerConcurrentMarkTask(JSThread *thread);
84};
85
86template<TriggerGCType gcType, GCReason gcReason>
87class TriggerCollectGarbageTask : public DaemonTask {
88public:
89    explicit TriggerCollectGarbageTask(JSThread *thread);
90};
91
92class TerminateDaemonTask : public DaemonTask {
93public:
94    explicit TerminateDaemonTask(JSThread *thread);
95};
96}  // namespace panda::ecmascript
97#endif //ECMASCRIPT_DAEMON_TASK_H