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 | |
| 19 | #include "scoped_thread_state_change.h" |
| 20 | |
| 21 | namespace art { |
| 22 | namespace gc { |
| 23 | |
| 24 | TaskProcessor::TaskProcessor() |
| 25 | : lock_(new Mutex("Task processor lock", kReferenceProcessorLock)), is_running_(false) { |
| 26 | // Piggyback off the reference processor lock level. |
| 27 | cond_.reset(new ConditionVariable("Task processor condition", *lock_)); |
| 28 | } |
| 29 | |
| 30 | TaskProcessor::~TaskProcessor() { |
| 31 | delete lock_; |
| 32 | } |
| 33 | |
| 34 | void TaskProcessor::AddTask(Thread* self, HeapTask* task) { |
| 35 | ScopedThreadStateChange tsc(self, kBlocked); |
| 36 | MutexLock mu(self, *lock_); |
| 37 | tasks_.insert(task); |
| 38 | cond_->Signal(self); |
| 39 | } |
| 40 | |
| 41 | HeapTask* TaskProcessor::GetTask(Thread* self) { |
| 42 | ScopedThreadStateChange tsc(self, kBlocked); |
| 43 | MutexLock mu(self, *lock_); |
| 44 | while (true) { |
| 45 | if (tasks_.empty()) { |
| 46 | if (!is_running_) { |
| 47 | return nullptr; |
| 48 | } |
| 49 | cond_->Wait(self); // Empty queue, wait until we are signalled. |
| 50 | } else { |
| 51 | // Non empty queue, look at the top element and see if we are ready to run it. |
| 52 | const uint64_t current_time = NanoTime(); |
| 53 | HeapTask* task = *tasks_.begin(); |
| 54 | // If we are shutting down, return the task right away without waiting. Otherwise return the |
| 55 | // task if it is late enough. |
| 56 | uint64_t target_time = task->GetTargetRunTime(); |
| 57 | if (!is_running_ || target_time <= current_time) { |
| 58 | tasks_.erase(tasks_.begin()); |
| 59 | return task; |
| 60 | } |
| 61 | DCHECK_GT(target_time, current_time); |
| 62 | // Wait untl we hit the target run time. |
| 63 | const uint64_t delta_time = target_time - current_time; |
| 64 | const uint64_t ms_delta = NsToMs(delta_time); |
| 65 | const uint64_t ns_delta = delta_time - MsToNs(ms_delta); |
| 66 | cond_->TimedWait(self, static_cast<int64_t>(ms_delta), static_cast<int32_t>(ns_delta)); |
| 67 | } |
| 68 | } |
| 69 | UNREACHABLE(); |
| 70 | return nullptr; |
| 71 | } |
| 72 | |
| 73 | void TaskProcessor::UpdateTargetRunTime(Thread* self, HeapTask* task, uint64_t new_target_time) { |
| 74 | MutexLock mu(self, *lock_); |
| 75 | // Find the task. |
| 76 | auto range = tasks_.equal_range(task); |
| 77 | for (auto it = range.first; it != range.second; ++it) { |
| 78 | if (*it == task) { |
| 79 | // Check if the target time was updated, if so re-insert then wait. |
| 80 | if (new_target_time != task->GetTargetRunTime()) { |
| 81 | tasks_.erase(it); |
| 82 | task->SetTargetRunTime(new_target_time); |
| 83 | tasks_.insert(task); |
| 84 | // If we became the first task then we may need to signal since we changed the task that we |
| 85 | // are sleeping on. |
| 86 | if (*tasks_.begin() == task) { |
| 87 | cond_->Signal(self); |
| 88 | } |
| 89 | return; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | bool TaskProcessor::IsRunning() const { |
| 96 | MutexLock mu(Thread::Current(), *lock_); |
| 97 | return is_running_; |
| 98 | } |
| 99 | |
| 100 | void TaskProcessor::Stop(Thread* self) { |
| 101 | MutexLock mu(self, *lock_); |
| 102 | is_running_ = false; |
| 103 | cond_->Broadcast(self); |
| 104 | } |
| 105 | |
| 106 | void TaskProcessor::Start(Thread* self) { |
| 107 | MutexLock mu(self, *lock_); |
| 108 | is_running_ = true; |
| 109 | } |
| 110 | |
| 111 | void TaskProcessor::RunAllTasks(Thread* self) { |
| 112 | while (true) { |
| 113 | // Wait and get a task, may be interrupted. |
| 114 | HeapTask* task = GetTask(self); |
| 115 | if (task != nullptr) { |
| 116 | task->Run(self); |
| 117 | task->Finalize(); |
| 118 | } else if (!IsRunning()) { |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | } // namespace gc |
| 125 | } // namespace art |