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