• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_CONCURRENT_SWEEPER_H
17 #define ECMASCRIPT_MEM_CONCURRENT_SWEEPER_H
18 
19 #include <array>
20 #include <atomic>
21 
22 #include "ecmascript/mem/space.h"
23 #include "ecmascript/platform/task.h"
24 #include "os/mutex.h"
25 
26 namespace panda::ecmascript {
27 class ConcurrentSweeper {
28 public:
29     ConcurrentSweeper(Heap *heap, bool concurrentSweep);
30     ~ConcurrentSweeper() = default;
31 
32     NO_COPY_SEMANTIC(ConcurrentSweeper);
33     NO_MOVE_SEMANTIC(ConcurrentSweeper);
34 
35     void SweepPhases(bool fullGC = false);
36 
37     void WaitAllTaskFinished();
38     // Help to finish sweeping task. It can be called through js thread
39     void EnsureAllTaskFinished();
40     // Ensure task finish
41     void EnsureTaskFinished(MemSpaceType type);
42 
43     void PostConcurrentSweepTasks(bool fullGC = false);
44 private:
45     class SweeperTask : public Task {
46     public:
SweeperTask(ConcurrentSweeper * sweeper,MemSpaceType type)47         SweeperTask(ConcurrentSweeper *sweeper, MemSpaceType type) : sweeper_(sweeper), type_(type) {};
48         ~SweeperTask() override = default;
49         bool Run(uint32_t threadIndex) override;
50 
51         NO_COPY_SEMANTIC(SweeperTask);
52         NO_MOVE_SEMANTIC(SweeperTask);
53 
54     private:
55         ConcurrentSweeper *sweeper_;
56         MemSpaceType type_;
57     };
58 
59     void AsyncSweepSpace(MemSpaceType type, bool isMain);
60     void FinishSweeping(MemSpaceType type);
61 
62     void WaitingTaskFinish(MemSpaceType type);
63 
64     std::array<os::memory::Mutex, FREE_LIST_NUM> mutexs_;
65     std::array<os::memory::ConditionVariable, FREE_LIST_NUM> cvs_;
66     std::array<std::atomic_int, FREE_LIST_NUM> remainderTaskNum_ = {0, 0, 0};
67 
68     Heap *heap_;
69     bool concurrentSweep_ {false};
70     bool isSweeping_ {false};
71     MemSpaceType startSpaceType_ = MemSpaceType::OLD_SPACE;
72 };
73 }  // namespace panda::ecmascript
74 #endif  // ECMASCRIPT_MEM_CONCURRENT_SWEEPER_H
75