blob: ef34c68cc4190458de0498bed17f561f37a828fb [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
19#include "scoped_thread_state_change.h"
20
21namespace art {
22namespace gc {
23
24TaskProcessor::TaskProcessor()
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -070025 : lock_(new Mutex("Task processor lock", kReferenceProcessorLock)), is_running_(false),
26 running_thread_(nullptr) {
Mathieu Chartiera5eae692014-12-17 17:56:03 -080027 // Piggyback off the reference processor lock level.
28 cond_.reset(new ConditionVariable("Task processor condition", *lock_));
29}
30
31TaskProcessor::~TaskProcessor() {
32 delete lock_;
33}
34
35void TaskProcessor::AddTask(Thread* self, HeapTask* task) {
36 ScopedThreadStateChange tsc(self, kBlocked);
37 MutexLock mu(self, *lock_);
38 tasks_.insert(task);
39 cond_->Signal(self);
40}
41
42HeapTask* TaskProcessor::GetTask(Thread* self) {
43 ScopedThreadStateChange tsc(self, kBlocked);
44 MutexLock mu(self, *lock_);
45 while (true) {
46 if (tasks_.empty()) {
47 if (!is_running_) {
48 return nullptr;
49 }
50 cond_->Wait(self); // Empty queue, wait until we are signalled.
51 } else {
52 // Non empty queue, look at the top element and see if we are ready to run it.
53 const uint64_t current_time = NanoTime();
54 HeapTask* task = *tasks_.begin();
55 // If we are shutting down, return the task right away without waiting. Otherwise return the
56 // task if it is late enough.
57 uint64_t target_time = task->GetTargetRunTime();
58 if (!is_running_ || target_time <= current_time) {
59 tasks_.erase(tasks_.begin());
60 return task;
61 }
62 DCHECK_GT(target_time, current_time);
63 // Wait untl we hit the target run time.
64 const uint64_t delta_time = target_time - current_time;
65 const uint64_t ms_delta = NsToMs(delta_time);
66 const uint64_t ns_delta = delta_time - MsToNs(ms_delta);
67 cond_->TimedWait(self, static_cast<int64_t>(ms_delta), static_cast<int32_t>(ns_delta));
68 }
69 }
70 UNREACHABLE();
Mathieu Chartiera5eae692014-12-17 17:56:03 -080071}
72
73void 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
95bool TaskProcessor::IsRunning() const {
96 MutexLock mu(Thread::Current(), *lock_);
97 return is_running_;
98}
99
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -0700100Thread* TaskProcessor::GetRunningThread() const {
101 MutexLock mu(Thread::Current(), *lock_);
102 return running_thread_;
103}
104
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800105void TaskProcessor::Stop(Thread* self) {
106 MutexLock mu(self, *lock_);
107 is_running_ = false;
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -0700108 running_thread_ = nullptr;
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800109 cond_->Broadcast(self);
110}
111
112void TaskProcessor::Start(Thread* self) {
113 MutexLock mu(self, *lock_);
114 is_running_ = true;
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -0700115 running_thread_ = self;
Mathieu Chartiera5eae692014-12-17 17:56:03 -0800116}
117
118void TaskProcessor::RunAllTasks(Thread* self) {
119 while (true) {
120 // Wait and get a task, may be interrupted.
121 HeapTask* task = GetTask(self);
122 if (task != nullptr) {
123 task->Run(self);
124 task->Finalize();
125 } else if (!IsRunning()) {
126 break;
127 }
128 }
129}
130
131} // namespace gc
132} // namespace art