1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
cc / raster / single_thread_task_graph_runner.cc [blame]
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/raster/single_thread_task_graph_runner.h"
#include <stdint.h>
#include <memory>
#include <string>
#include <utility>
#include "base/ranges/algorithm.h"
#include "base/threading/simple_thread.h"
#include "base/trace_event/base_tracing.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/typed_macros.h"
namespace cc {
SingleThreadTaskGraphRunner::SingleThreadTaskGraphRunner()
: lock_(),
has_ready_to_run_tasks_cv_(&lock_),
has_namespaces_with_finished_running_tasks_cv_(&lock_),
is_idle_cv_(&lock_) {
has_ready_to_run_tasks_cv_.declare_only_used_while_idle();
}
SingleThreadTaskGraphRunner::~SingleThreadTaskGraphRunner() = default;
void SingleThreadTaskGraphRunner::Start(
const std::string& thread_name,
const base::SimpleThread::Options& thread_options) {
thread_ = std::make_unique<base::DelegateSimpleThread>(this, thread_name,
thread_options);
thread_->StartAsync();
}
void SingleThreadTaskGraphRunner::Shutdown() {
{
base::AutoLock lock(lock_);
DCHECK(!work_queue_.HasReadyToRunTasks());
DCHECK(!work_queue_.HasAnyNamespaces());
DCHECK(!shutdown_);
shutdown_ = true;
// Wake up the worker so it knows it should exit.
has_ready_to_run_tasks_cv_.Signal();
}
thread_->Join();
}
NamespaceToken SingleThreadTaskGraphRunner::GenerateNamespaceToken() {
base::AutoLock lock(lock_);
return work_queue_.GenerateNamespaceToken();
}
void SingleThreadTaskGraphRunner::ScheduleTasks(NamespaceToken token,
TaskGraph* graph) {
TRACE_EVENT2("cc", "SingleThreadTaskGraphRunner::ScheduleTasks", "num_nodes",
graph->nodes.size(), "num_edges", graph->edges.size());
DCHECK(token.IsValid());
DCHECK(!TaskGraphWorkQueue::DependencyMismatch(graph));
{
base::AutoLock lock(lock_);
DCHECK(!shutdown_);
work_queue_.ScheduleTasks(token, graph);
// If there is more work available, wake up the worker thread.
if (work_queue_.HasReadyToRunTasks())
has_ready_to_run_tasks_cv_.Signal();
}
}
void SingleThreadTaskGraphRunner::ExternalDependencyCompletedForTask(
NamespaceToken token,
scoped_refptr<Task> task) {
base::AutoLock lock(lock_);
if (work_queue_.ExternalDependencyCompletedForTask(token, std::move(task))) {
has_ready_to_run_tasks_cv_.Signal();
}
}
void SingleThreadTaskGraphRunner::WaitForTasksToFinishRunning(
NamespaceToken token) {
TRACE_EVENT0("cc",
"SingleThreadTaskGraphRunner::WaitForTasksToFinishRunning");
DCHECK(token.IsValid());
{
base::AutoLock lock(lock_);
auto* task_namespace = work_queue_.GetNamespaceForToken(token);
if (!task_namespace)
return;
while (!work_queue_.HasFinishedRunningTasksInNamespace(task_namespace))
has_namespaces_with_finished_running_tasks_cv_.Wait();
// There may be other namespaces that have finished running tasks, so wake
// up another origin thread.
has_namespaces_with_finished_running_tasks_cv_.Signal();
}
}
void SingleThreadTaskGraphRunner::CollectCompletedTasks(
NamespaceToken token,
Task::Vector* completed_tasks) {
TRACE_EVENT0("cc", "SingleThreadTaskGraphRunner::CollectCompletedTasks");
DCHECK(token.IsValid());
{
base::AutoLock lock(lock_);
work_queue_.CollectCompletedTasks(token, completed_tasks);
}
}
void SingleThreadTaskGraphRunner::RunTasksUntilIdleForTest() {
base::AutoLock lock(lock_);
while (work_queue_.HasReadyToRunTasks() ||
work_queue_.NumRunningTasks() > 0) {
is_idle_cv_.Wait();
}
}
void SingleThreadTaskGraphRunner::Run() {
base::AutoLock lock(lock_);
while (true) {
if (!RunTaskWithLockAcquired()) {
is_idle_cv_.Signal();
// Exit when shutdown is set and no more tasks are pending.
if (shutdown_)
break;
// Wait for more tasks.
has_ready_to_run_tasks_cv_.Wait();
continue;
}
}
}
bool SingleThreadTaskGraphRunner::RunTaskWithLockAcquired() {
TRACE_EVENT0("toplevel",
"SingleThreadTaskGraphRunner::RunTaskWithLockAcquired");
// Find the first category with any tasks to run. This task graph runner
// treats categories as an additional priority.
const auto& ready_to_run_namespaces = work_queue_.ready_to_run_namespaces();
auto found = base::ranges::find_if_not(
ready_to_run_namespaces,
&TaskGraphWorkQueue::TaskNamespace::Vector::empty,
&TaskGraphWorkQueue::ReadyNamespaces::value_type::second);
if (found == ready_to_run_namespaces.cend()) {
return false;
}
const uint16_t category = found->first;
auto prioritized_task = work_queue_.GetNextTaskToRun(category);
{
TRACE_EVENT("toplevel", "cc::SingleThreadTaskGraphRunner::RunTask",
perfetto::TerminatingFlow::Global(
prioritized_task.task->trace_task_id()));
base::AutoUnlock unlock(lock_);
prioritized_task.task->RunOnWorkerThread();
}
auto* task_namespace = prioritized_task.task_namespace.get();
work_queue_.CompleteTask(std::move(prioritized_task));
// If namespace has finished running all tasks, wake up origin thread.
if (work_queue_.HasFinishedRunningTasksInNamespace(task_namespace))
has_namespaces_with_finished_running_tasks_cv_.Signal();
return true;
}
} // namespace cc