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_MEM_SHARED_HEAP_SHARED_CONCURRENT_SWEEPER_H
17#define ECMASCRIPT_MEM_SHARED_HEAP_SHARED_CONCURRENT_SWEEPER_H
18
19#include "ecmascript/mem/concurrent_sweeper.h"
20
21namespace panda::ecmascript {
22class SharedHeap;
23
24class SharedConcurrentSweeper {
25public:
26    SharedConcurrentSweeper(SharedHeap *heap, EnableConcurrentSweepType type);
27    ~SharedConcurrentSweeper() = default;
28
29    NO_COPY_SEMANTIC(SharedConcurrentSweeper);
30    NO_MOVE_SEMANTIC(SharedConcurrentSweeper);
31
32    void PostTask(bool isFullGC);
33    void Sweep(bool isFullGC);
34
35    void WaitAllTaskFinished();
36    // Help to finish sweeping task. It can be called through js thread
37    void EnsureAllTaskFinished();
38    // Ensure task finish. It can be called through js thread
39    void EnsureTaskFinished(MemSpaceType type);
40
41    void TryFillSweptRegion();
42
43    void EnableConcurrentSweep(EnableConcurrentSweepType type);
44
45    bool IsSweeping()
46    {
47        return isSweeping_;
48    }
49
50    bool ConcurrentSweepEnabled()
51    {
52        return !IsDisabled();
53    }
54
55    void ConfigConcurrentSweep(bool enabled)
56    {
57        enableType_ = enabled ? EnableConcurrentSweepType::ENABLE :
58                      EnableConcurrentSweepType::CONFIG_DISABLE;
59    }
60
61    bool IsDisabled() const
62    {
63        return enableType_ == EnableConcurrentSweepType::DISABLE ||
64            enableType_ == EnableConcurrentSweepType::CONFIG_DISABLE;
65    }
66
67    bool IsRequestDisabled() const
68    {
69        return enableType_ == EnableConcurrentSweepType::REQUEST_DISABLE;
70    }
71
72    bool IsConfigDisabled() const
73    {
74        return enableType_ == EnableConcurrentSweepType::CONFIG_DISABLE;
75    }
76private:
77    class SweeperTask : public Task {
78    public:
79        SweeperTask(int32_t id, SharedConcurrentSweeper *sweeper, MemSpaceType type)
80            : Task(id), sweeper_(sweeper), type_(type) {};
81        ~SweeperTask() override = default;
82        bool Run(uint32_t threadIndex) override;
83
84        NO_COPY_SEMANTIC(SweeperTask);
85        NO_MOVE_SEMANTIC(SweeperTask);
86
87    private:
88        SharedConcurrentSweeper *sweeper_;
89        MemSpaceType type_;
90    };
91
92    void AsyncSweepSpace(MemSpaceType type, bool isMain);
93
94    void WaitingTaskFinish(MemSpaceType type);
95
96    std::array<Mutex, SHARED_SWEEPING_SPACE_NUM> mutexs_;
97    std::array<ConditionVariable, SHARED_SWEEPING_SPACE_NUM> cvs_;
98    std::array<std::atomic_int, SHARED_SWEEPING_SPACE_NUM> remainingTaskNum_ = {0, 0};
99
100    SharedHeap *sHeap_;
101    EnableConcurrentSweepType enableType_ {EnableConcurrentSweepType::CONFIG_DISABLE};
102    bool isSweeping_ {false};
103    bool isFullGC_ {false};
104};
105}  // namespace panda::ecmascript
106#endif  // ECMASCRIPT_MEM_SHARED_HEAP_SHARED_CONCURRENT_SWEEPER_H
107