• 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 #include "runtime/mem/gc/gc_queue.h"
17 
18 #include "libpandabase/utils/time.h"
19 #include "runtime/mem/gc/gc.h"
20 
21 namespace ark::mem {
22 
23 constexpr int64_t NANOSECONDS_PER_MILLISEC = 1000000;
24 
GetTask(bool needWaitTask)25 PandaUniquePtr<GCTask> GCQueueWithTime::GetTask(bool needWaitTask)
26 {
27     os::memory::LockHolder lock(lock_);
28     while (queue_.empty()) {
29         if (!gc_->IsGCRunning()) {
30             LOG(DEBUG, GC) << "GetTask() Return INVALID_CAUSE";
31             return nullptr;
32         }
33         if (needWaitTask) {
34             LOG(DEBUG, GC) << "Empty " << queueName_ << ", waiting...";
35             condVar_.Wait(&lock_);
36         } else {
37             LOG(DEBUG, GC) << "Empty " << queueName_ << ", return nullptr";
38             return nullptr;
39         }
40     }
41     GCTask *task = queue_.top().get();
42     auto currentTime = time::GetCurrentTimeInNanos();
43     while (gc_->IsGCRunning() && (task->GetTargetTime() >= currentTime)) {
44         auto delta = task->GetTargetTime() - currentTime;
45         uint64_t ms = delta / NANOSECONDS_PER_MILLISEC;
46         uint64_t ns = delta % NANOSECONDS_PER_MILLISEC;
47         LOG(DEBUG, GC) << "GetTask TimedWait";
48         condVar_.TimedWait(&lock_, ms, ns);
49         task = queue_.top().get();
50         currentTime = time::GetCurrentTimeInNanos();
51     }
52     PandaUniquePtr<GCTask> returnedTask = std::move(*const_cast<PandaUniquePtr<GCTask> *>(&queue_.top()));
53     queue_.pop();
54     LOG(DEBUG, GC) << "Extract a task from a " << queueName_;
55     return returnedTask;
56 }
57 
AddTask(PandaUniquePtr<GCTask> task)58 bool GCQueueWithTime::AddTask(PandaUniquePtr<GCTask> task)
59 {
60     if (task == nullptr) {
61         return false;
62     }
63     os::memory::LockHolder lock(lock_);
64     if (!queue_.empty()) {
65         auto *lastElem = queue_.top().get();
66         if (lastElem->reason == task->reason) {
67             // do not duplicate GC task with the same reason.
68             return false;
69         }
70     }
71     LOG(DEBUG, GC) << "Add task to a " << queueName_;
72     queue_.push(std::move(task));
73     condVar_.Signal();
74     return true;
75 }
76 
77 }  // namespace ark::mem
78