OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "cc/raster/single_thread_task_graph_runner.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/threading/simple_thread.h" | |
10 #include "base/threading/thread_restrictions.h" | |
11 #include "base/trace_event/trace_event.h" | |
12 | |
13 namespace cc { | |
14 | |
15 SingleThreadTaskGraphRunner::SingleThreadTaskGraphRunner() | |
16 : lock_(), | |
17 has_ready_to_run_tasks_cv_(&lock_), | |
18 has_namespaces_with_finished_running_tasks_cv_(&lock_), | |
19 shutdown_(false) {} | |
20 | |
21 SingleThreadTaskGraphRunner::~SingleThreadTaskGraphRunner() {} | |
22 | |
23 void SingleThreadTaskGraphRunner::Start(const std::string& thread_name) { | |
24 raster_thread_.reset(new base::DelegateSimpleThread(this, thread_name)); | |
25 raster_thread_->Start(); | |
26 } | |
27 | |
28 void SingleThreadTaskGraphRunner::Shutdown() { | |
29 // Shutdown raster threads. | |
reveman
2015/11/19 16:05:46
nit: "Shutdown threads" or maybe just remove this
ericrk
2015/11/23 18:43:37
Done.
| |
30 { | |
31 base::AutoLock lock(lock_); | |
32 | |
33 DCHECK(!work_queue_.HasReadyToRunTasks()); | |
34 | |
35 DCHECK(!shutdown_); | |
36 shutdown_ = true; | |
37 | |
38 // Wake up the worker so it knows it should exit. | |
39 has_ready_to_run_tasks_cv_.Signal(); | |
40 } | |
41 raster_thread_->Join(); | |
42 } | |
43 | |
44 void SingleThreadTaskGraphRunner::ScheduleTasks(NamespaceToken token, | |
45 TaskGraph* graph) { | |
46 TRACE_EVENT2("cc", "SingleThreadTaskGraphRunner::ScheduleTasks", "num_nodes", | |
47 graph->nodes.size(), "num_edges", graph->edges.size()); | |
48 | |
49 DCHECK(token.IsValid()); | |
50 DCHECK(!DependencyMismatch(graph)); | |
51 | |
52 { | |
53 base::AutoLock lock(lock_); | |
54 | |
55 DCHECK(!shutdown_); | |
56 | |
57 work_queue_.ScheduleTasks(token, graph); | |
58 | |
59 // If there is more work available, wake up the worker thread. | |
60 if (work_queue_.HasReadyToRunTasks()) | |
61 has_ready_to_run_tasks_cv_.Signal(); | |
62 } | |
63 } | |
64 | |
65 void SingleThreadTaskGraphRunner::WaitForTasksToFinishRunning( | |
66 NamespaceToken token) { | |
67 TRACE_EVENT0("cc", | |
68 "SingleThreadTaskGraphRunner::WaitForTasksToFinishRunning"); | |
69 | |
70 DCHECK(token.IsValid()); | |
71 | |
72 { | |
73 base::AutoLock lock(lock_); | |
74 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
75 | |
76 auto* task_namespace = work_queue_.GetNamespaceForToken(token); | |
77 | |
78 if (!task_namespace) | |
79 return; | |
80 | |
81 while (!work_queue_.HasFinishedRunningTasksInNamespace(task_namespace)) | |
82 has_namespaces_with_finished_running_tasks_cv_.Wait(); | |
83 | |
84 // There may be other namespaces that have finished running tasks, so wake | |
85 // up another origin thread. | |
86 has_namespaces_with_finished_running_tasks_cv_.Signal(); | |
87 } | |
88 } | |
89 | |
90 void SingleThreadTaskGraphRunner::CollectCompletedTasks( | |
91 NamespaceToken token, | |
92 Task::Vector* completed_tasks) { | |
93 TRACE_EVENT0("cc", "SingleThreadTaskGraphRunner::CollectCompletedTasks"); | |
94 | |
95 DCHECK(token.IsValid()); | |
96 | |
97 { | |
98 base::AutoLock lock(lock_); | |
99 work_queue_.CollectCompletedTasks(token, completed_tasks); | |
100 } | |
101 } | |
102 | |
103 void SingleThreadTaskGraphRunner::Run() { | |
104 base::AutoLock lock(lock_); | |
105 | |
106 while (true) { | |
107 if (!work_queue_.HasReadyToRunTasks()) { | |
108 // Exit when shutdown is set and no more tasks are pending. | |
109 if (shutdown_) | |
110 break; | |
111 | |
112 // Wait for more tasks. | |
113 has_ready_to_run_tasks_cv_.Wait(); | |
114 continue; | |
115 } | |
116 | |
117 RunTaskWithLockAcquired(); | |
118 } | |
119 } | |
120 | |
121 void SingleThreadTaskGraphRunner::RunTaskWithLockAcquired() { | |
122 TRACE_EVENT0("toplevel", | |
123 "SingleThreadTaskGraphRunner::RunTaskWithLockAcquired"); | |
124 | |
125 lock_.AssertAcquired(); | |
126 | |
127 auto prioritized_task = work_queue_.GetNextTaskToRun(); | |
128 Task* task = prioritized_task.task; | |
129 | |
130 // Call WillRun() before releasing |lock_| and running task. | |
131 task->WillRun(); | |
132 | |
133 { | |
134 base::AutoUnlock unlock(lock_); | |
135 task->RunOnWorkerThread(); | |
136 } | |
137 | |
138 // This will mark task as finished running. | |
139 task->DidRun(); | |
140 | |
141 work_queue_.CompleteTask(prioritized_task); | |
142 | |
143 // If namespace has finished running all tasks, wake up origin thread. | |
144 if (work_queue_.HasFinishedRunningTasksInNamespace( | |
145 prioritized_task.task_namespace)) | |
146 has_namespaces_with_finished_running_tasks_cv_.Signal(); | |
147 } | |
148 | |
149 } // namespace cc | |
OLD | NEW |