• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 PANDA_RUNTIME_GC_QUEUE_H
17 #define PANDA_RUNTIME_GC_QUEUE_H
18 
19 #include <memory>
20 
21 #include "runtime/include/gc_task.h"
22 #include "runtime/include/mem/panda_containers.h"
23 #include "runtime/include/mem/panda_smart_pointers.h"
24 
25 namespace ark::mem {
26 
27 constexpr uint64_t GC_WAIT_TIMEOUT = 500U;
28 
29 class GCQueueInterface {
30 public:
31     GCQueueInterface() = default;
32     virtual ~GCQueueInterface() = default;
33 
34     virtual PandaUniquePtr<GCTask> GetTask(bool needWaitTask) = 0;
35 
36     virtual bool AddTask(PandaUniquePtr<GCTask> task) = 0;
37 
38     virtual bool IsEmpty() const = 0;
39 
40     virtual void Signal() = 0;
41 
42     virtual bool WaitForGCTask() = 0;
43 
44     NO_COPY_SEMANTIC(GCQueueInterface);
45     NO_MOVE_SEMANTIC(GCQueueInterface);
46 };
47 
48 /// @class GCQueueWithTime is an ascending priority queue ordered by target time.
49 class GCQueueWithTime : public GCQueueInterface {
50 public:
GCQueueWithTime(GC * gc)51     explicit GCQueueWithTime(GC *gc) : gc_(gc) {}
52 
53     PandaUniquePtr<GCTask> GetTask(bool needWaitTask) override;
54 
55     bool AddTask(PandaUniquePtr<GCTask> task) override;
56 
IsEmpty()57     bool IsEmpty() const override
58     {
59         os::memory::LockHolder lock(lock_);
60         return queue_.empty();
61     }
62 
Signal()63     void Signal() override
64     {
65         os::memory::LockHolder lock(lock_);
66         condVar_.Signal();
67     }
68 
WaitForGCTask()69     bool WaitForGCTask() override
70     {
71         os::memory::LockHolder lock(lock_);
72         return condVar_.TimedWait(&lock_, GC_WAIT_TIMEOUT);
73     }
74 
75 private:
76     class CompareByTime {
77     public:
operator()78         bool operator()(const PandaUniquePtr<GCTask> &left, const PandaUniquePtr<GCTask> &right) const
79         {
80             return left->GetTargetTime() > right->GetTargetTime();
81         }
82     };
83 
84     GC *gc_;
85     mutable os::memory::Mutex lock_;
86     PandaPriorityQueue<PandaUniquePtr<GCTask>, PandaVector<PandaUniquePtr<GCTask>>, CompareByTime> queue_
87         GUARDED_BY(lock_);
88     os::memory::ConditionVariable condVar_;
89     const char *queueName_ = "GC queue ordered by time";
90 };
91 
92 }  // namespace ark::mem
93 
94 #endif  // PANDA_RUNTIME_GC_QUEUE_H
95