blob: 64e8322a0db1898a582e25297d44bbabf598ede9 [file] [log] [blame]
Mathieu Chartiera5eae692014-12-17 17:56:03 -08001/*
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 Marko80afd022015-05-19 18:08:00 +010019#include "base/time_utils.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070020#include "scoped_thread_state_change-inl.h"
Mathieu Chartiera5eae692014-12-17 17:56:03 -080021
22namespace art {
23namespace gc {
24
25TaskProcessor::TaskProcessor()
Vladimir Marko103fa6e2017-11-09 17:18:05 +000026 : lock_("Task processor lock", kReferenceProcessorLock),
27 cond_("Task processor condition", lock_),
28 is_running_(false),
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -070029 running_thread_(nullptr) {
Mathieu Chartiera5eae692014-12-17 17:56:03 -080030}
31
32TaskProcessor::~TaskProcessor() {
Vladimir Marko103fa6e2017-11-09 17:18:05 +000033 if (!tasks_.empty()) {
34 LOG(WARNING) << "TaskProcessor: Finalizing " << tasks_.size() << " unprocessed tasks.";
35 for (HeapTask* task : tasks_) {
36 task->Finalize();
37 }
38 }
Mathieu Chartiera5eae692014-12-17 17:56:03 -080039}
40
41void TaskProcessor::AddTask(Thread* self, HeapTask* task) {
Alex Light77fee872017-09-05 14:51:49 -070042 ScopedThreadStateChange tsc(self, kWaitingForTaskProcessor);
Vladimir Marko103fa6e2017-11-09 17:18:05 +000043 MutexLock mu(self, lock_);
Mathieu Chartiera5eae692014-12-17 17:56:03 -080044 tasks_.insert(task);
Vladimir Marko103fa6e2017-11-09 17:18:05 +000045 cond_.Signal(self);
Mathieu Chartiera5eae692014-12-17 17:56:03 -080046}
47
48HeapTask* TaskProcessor::GetTask(Thread* self) {
Alex Light77fee872017-09-05 14:51:49 -070049 ScopedThreadStateChange tsc(self, kWaitingForTaskProcessor);
Vladimir Marko103fa6e2017-11-09 17:18:05 +000050 MutexLock mu(self, lock_);
Mathieu Chartiera5eae692014-12-17 17:56:03 -080051 while (true) {
52 if (tasks_.empty()) {
53 if (!is_running_) {
54 return nullptr;
55 }
Vladimir Marko103fa6e2017-11-09 17:18:05 +000056 cond_.Wait(self); // Empty queue, wait until we are signalled.
Mathieu Chartiera5eae692014-12-17 17:56:03 -080057 } 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 Marko103fa6e2017-11-09 17:18:05 +000069 // Wait until we hit the target run time.
Mathieu Chartiera5eae692014-12-17 17:56:03 -080070 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 Marko103fa6e2017-11-09 17:18:05 +000073 cond_.TimedWait(self, static_cast<int64_t>(ms_delta), static_cast<int32_t>(ns_delta));
Mathieu Chartiera5eae692014-12-17 17:56:03 -080074 }
75 }
76 UNREACHABLE();
Mathieu Chartiera5eae692014-12-17 17:56:03 -080077}
78
79void TaskProcessor::UpdateTargetRunTime(Thread* self, HeapTask* task, uint64_t new_target_time) {
Vladimir Marko103fa6e2017-11-09 17:18:05 +000080 MutexLock mu(self, lock_);
Mathieu Chartiera5eae692014-12-17 17:56:03 -080081 // 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 Marko103fa6e2017-11-09 17:18:05 +000093 cond_.Signal(self);
Mathieu Chartiera5eae692014-12-17 17:56:03 -080094 }
95 return;
96 }
97 }
98 }
99}
100
101bool TaskProcessor::IsRunning() const {
Vladimir Marko103fa6e2017-11-09 17:18:05 +0000102 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800103 return is_running_;
104}
105
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -0700106Thread* TaskProcessor::GetRunningThread() const {
Vladimir Marko103fa6e2017-11-09 17:18:05 +0000107 MutexLock mu(Thread::Current(), lock_);
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -0700108 return running_thread_;
109}
110
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800111void TaskProcessor::Stop(Thread* self) {
Vladimir Marko103fa6e2017-11-09 17:18:05 +0000112 MutexLock mu(self, lock_);
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800113 is_running_ = false;
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -0700114 running_thread_ = nullptr;
Vladimir Marko103fa6e2017-11-09 17:18:05 +0000115 cond_.Broadcast(self);
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800116}
117
118void TaskProcessor::Start(Thread* self) {
Vladimir Marko103fa6e2017-11-09 17:18:05 +0000119 MutexLock mu(self, lock_);
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800120 is_running_ = true;
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -0700121 running_thread_ = self;
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800122}
123
124void 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