Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "task_processor.h" |
| 18 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 19 | #include "base/time_utils.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 20 | #include "scoped_thread_state_change-inl.h" |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 21 | |
| 22 | namespace art { |
| 23 | namespace gc { |
| 24 | |
| 25 | TaskProcessor::TaskProcessor() |
Vladimir Marko | 103fa6e | 2017-11-09 17:18:05 +0000 | [diff] [blame] | 26 | : lock_("Task processor lock", kReferenceProcessorLock), |
| 27 | cond_("Task processor condition", lock_), |
| 28 | is_running_(false), |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 29 | running_thread_(nullptr) { |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | TaskProcessor::~TaskProcessor() { |
Vladimir Marko | 103fa6e | 2017-11-09 17:18:05 +0000 | [diff] [blame] | 33 | if (!tasks_.empty()) { |
| 34 | LOG(WARNING) << "TaskProcessor: Finalizing " << tasks_.size() << " unprocessed tasks."; |
| 35 | for (HeapTask* task : tasks_) { |
| 36 | task->Finalize(); |
| 37 | } |
| 38 | } |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void TaskProcessor::AddTask(Thread* self, HeapTask* task) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 42 | ScopedThreadStateChange tsc(self, kWaitingForTaskProcessor); |
Vladimir Marko | 103fa6e | 2017-11-09 17:18:05 +0000 | [diff] [blame] | 43 | MutexLock mu(self, lock_); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 44 | tasks_.insert(task); |
Vladimir Marko | 103fa6e | 2017-11-09 17:18:05 +0000 | [diff] [blame] | 45 | cond_.Signal(self); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | HeapTask* TaskProcessor::GetTask(Thread* self) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 49 | ScopedThreadStateChange tsc(self, kWaitingForTaskProcessor); |
Vladimir Marko | 103fa6e | 2017-11-09 17:18:05 +0000 | [diff] [blame] | 50 | MutexLock mu(self, lock_); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 51 | while (true) { |
| 52 | if (tasks_.empty()) { |
| 53 | if (!is_running_) { |
| 54 | return nullptr; |
| 55 | } |
Vladimir Marko | 103fa6e | 2017-11-09 17:18:05 +0000 | [diff] [blame] | 56 | cond_.Wait(self); // Empty queue, wait until we are signalled. |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 57 | } else { |
| 58 | // Non empty queue, look at the top element and see if we are ready to run it. |
| 59 | const uint64_t current_time = NanoTime(); |
| 60 | HeapTask* task = *tasks_.begin(); |
| 61 | // If we are shutting down, return the task right away without waiting. Otherwise return the |
| 62 | // task if it is late enough. |
| 63 | uint64_t target_time = task->GetTargetRunTime(); |
| 64 | if (!is_running_ || target_time <= current_time) { |
| 65 | tasks_.erase(tasks_.begin()); |
| 66 | return task; |
| 67 | } |
| 68 | DCHECK_GT(target_time, current_time); |
Vladimir Marko | 103fa6e | 2017-11-09 17:18:05 +0000 | [diff] [blame] | 69 | // Wait until we hit the target run time. |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 70 | const uint64_t delta_time = target_time - current_time; |
| 71 | const uint64_t ms_delta = NsToMs(delta_time); |
| 72 | const uint64_t ns_delta = delta_time - MsToNs(ms_delta); |
Vladimir Marko | 103fa6e | 2017-11-09 17:18:05 +0000 | [diff] [blame] | 73 | cond_.TimedWait(self, static_cast<int64_t>(ms_delta), static_cast<int32_t>(ns_delta)); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | UNREACHABLE(); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void TaskProcessor::UpdateTargetRunTime(Thread* self, HeapTask* task, uint64_t new_target_time) { |
Vladimir Marko | 103fa6e | 2017-11-09 17:18:05 +0000 | [diff] [blame] | 80 | MutexLock mu(self, lock_); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 81 | // Find the task. |
| 82 | auto range = tasks_.equal_range(task); |
| 83 | for (auto it = range.first; it != range.second; ++it) { |
| 84 | if (*it == task) { |
| 85 | // Check if the target time was updated, if so re-insert then wait. |
| 86 | if (new_target_time != task->GetTargetRunTime()) { |
| 87 | tasks_.erase(it); |
| 88 | task->SetTargetRunTime(new_target_time); |
| 89 | tasks_.insert(task); |
| 90 | // If we became the first task then we may need to signal since we changed the task that we |
| 91 | // are sleeping on. |
| 92 | if (*tasks_.begin() == task) { |
Vladimir Marko | 103fa6e | 2017-11-09 17:18:05 +0000 | [diff] [blame] | 93 | cond_.Signal(self); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 94 | } |
| 95 | return; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | bool TaskProcessor::IsRunning() const { |
Vladimir Marko | 103fa6e | 2017-11-09 17:18:05 +0000 | [diff] [blame] | 102 | MutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 103 | return is_running_; |
| 104 | } |
| 105 | |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 106 | Thread* TaskProcessor::GetRunningThread() const { |
Vladimir Marko | 103fa6e | 2017-11-09 17:18:05 +0000 | [diff] [blame] | 107 | MutexLock mu(Thread::Current(), lock_); |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 108 | return running_thread_; |
| 109 | } |
| 110 | |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 111 | void TaskProcessor::Stop(Thread* self) { |
Vladimir Marko | 103fa6e | 2017-11-09 17:18:05 +0000 | [diff] [blame] | 112 | MutexLock mu(self, lock_); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 113 | is_running_ = false; |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 114 | running_thread_ = nullptr; |
Vladimir Marko | 103fa6e | 2017-11-09 17:18:05 +0000 | [diff] [blame] | 115 | cond_.Broadcast(self); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void TaskProcessor::Start(Thread* self) { |
Vladimir Marko | 103fa6e | 2017-11-09 17:18:05 +0000 | [diff] [blame] | 119 | MutexLock mu(self, lock_); |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 120 | is_running_ = true; |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 121 | running_thread_ = self; |
Mathieu Chartier | a5eae69 | 2014-12-17 17:56:03 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void TaskProcessor::RunAllTasks(Thread* self) { |
| 125 | while (true) { |
| 126 | // Wait and get a task, may be interrupted. |
| 127 | HeapTask* task = GetTask(self); |
| 128 | if (task != nullptr) { |
| 129 | task->Run(self); |
| 130 | task->Finalize(); |
| 131 | } else if (!IsRunning()) { |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | } // namespace gc |
| 138 | } // namespace art |