Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(381)

Side by Side Diff: cc/resources/worker_pool.cc

Issue 73923003: Shared Raster Worker Threads (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: WIP - number of raster thread code review + nits Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "cc/resources/worker_pool.h" 5 #include "cc/resources/worker_pool.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <queue> 8 #include <queue>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/containers/hash_tables.h" 11 #include "base/containers/hash_tables.h"
12 #include "base/debug/trace_event.h" 12 #include "base/debug/trace_event.h"
13 #include "base/lazy_instance.h"
14 #include "base/memory/linked_ptr.h"
13 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
14 #include "base/synchronization/condition_variable.h" 16 #include "base/synchronization/condition_variable.h"
15 #include "base/threading/simple_thread.h" 17 #include "base/threading/simple_thread.h"
16 #include "base/threading/thread_restrictions.h" 18 #include "base/threading/thread_restrictions.h"
17 #include "cc/base/scoped_ptr_deque.h" 19 #include "cc/base/scoped_ptr_deque.h"
20 #include "cc/base/switches.h"
18 21
19 namespace cc { 22 namespace cc {
20 23
21 namespace internal { 24 namespace {
22 25
23 WorkerPoolTask::WorkerPoolTask() 26 // TaskGraphRunners can process task graphs from multiple
24 : did_schedule_(false), 27 // workerpool instances. All members are guarded by |lock_|.
25 did_run_(false), 28 class TaskGraphRunner : public base::DelegateSimpleThread::Delegate {
26 did_complete_(false) { 29 public:
27 } 30 typedef WorkerPool::TaskGraph TaskGraph;
31 typedef WorkerPool::TaskVector TaskVector;
28 32
29 WorkerPoolTask::~WorkerPoolTask() { 33 TaskGraphRunner(size_t num_threads, const std::string& thread_name_prefix);
30 DCHECK_EQ(did_schedule_, did_complete_); 34 virtual ~TaskGraphRunner();
31 DCHECK(!did_run_ || did_schedule_);
32 DCHECK(!did_run_ || did_complete_);
33 }
34 35
35 void WorkerPoolTask::DidSchedule() { 36 void Register(const WorkerPool* worker_pool);
36 DCHECK(!did_complete_); 37 void Unregister(const WorkerPool* worker_pool);
37 did_schedule_ = true; 38 void WaitForTasksToFinishRunning(const WorkerPool* worker_pool);
reveman 2013/12/30 08:57:06 Please move the declaration of "WaitForTasksToFini
sohanjg 2013/12/30 09:18:55 Done.
38 }
39
40 void WorkerPoolTask::WillRun() {
41 DCHECK(did_schedule_);
42 DCHECK(!did_complete_);
43 DCHECK(!did_run_);
44 }
45
46 void WorkerPoolTask::DidRun() {
47 did_run_ = true;
48 }
49
50 void WorkerPoolTask::WillComplete() {
51 DCHECK(!did_complete_);
52 }
53
54 void WorkerPoolTask::DidComplete() {
55 DCHECK(did_schedule_);
56 DCHECK(!did_complete_);
57 did_complete_ = true;
58 }
59
60 bool WorkerPoolTask::HasFinishedRunning() const {
61 return did_run_;
62 }
63
64 bool WorkerPoolTask::HasCompleted() const {
65 return did_complete_;
66 }
67
68 GraphNode::GraphNode(internal::WorkerPoolTask* task, unsigned priority)
69 : task_(task),
70 priority_(priority),
71 num_dependencies_(0) {
72 }
73
74 GraphNode::~GraphNode() {
75 }
76
77 } // namespace internal
78
79 // Internal to the worker pool. Any data or logic that needs to be
80 // shared between threads lives in this class. All members are guarded
81 // by |lock_|.
82 class WorkerPool::Inner : public base::DelegateSimpleThread::Delegate {
83 public:
84 Inner(size_t num_threads, const std::string& thread_name_prefix);
85 virtual ~Inner();
86
87 void Shutdown();
88 39
89 // Schedule running of tasks in |graph|. Tasks previously scheduled but 40 // Schedule running of tasks in |graph|. Tasks previously scheduled but
90 // no longer needed will be canceled unless already running. Canceled 41 // no longer needed will be canceled unless already running. Canceled
91 // tasks are moved to |completed_tasks_| without being run. The result 42 // tasks are moved to |completed_tasks| without being run. The result
92 // is that once scheduled, a task is guaranteed to end up in the 43 // is that once scheduled, a task is guaranteed to end up in the
93 // |completed_tasks_| queue even if they later get canceled by another 44 // |completed_tasks| queue even if it later get canceled by another
94 // call to SetTaskGraph(). 45 // call to SetTaskGraph().
95 void SetTaskGraph(TaskGraph* graph); 46 void SetTaskGraph(const WorkerPool* worker_pool, TaskGraph* graph);
96 47
97 // Collect all completed tasks in |completed_tasks|. 48 // Collect all completed tasks in |completed_tasks|.
98 void CollectCompletedTasks(TaskVector* completed_tasks); 49 void CollectCompletedTasks(const WorkerPool* worker_pool,
50 TaskVector* completed_tasks);
99 51
100 private: 52 private:
101 class PriorityComparator { 53 class TaskPriorityComparator {
102 public: 54 public:
103 bool operator()(const internal::GraphNode* a, 55 bool operator()(internal::GraphNode* a,
104 const internal::GraphNode* b) { 56 internal::GraphNode* b) {
105 // In this system, numerically lower priority is run first. 57 // In this system, numerically lower priority is run first.
106 if (a->priority() != b->priority()) 58 if (a->priority() != b->priority())
107 return a->priority() > b->priority(); 59 return a->priority() > b->priority();
108 60
109 // Run task with most dependents first when priority is the same. 61 // Run task with most dependents first when priority is the same.
110 return a->dependents().size() < b->dependents().size(); 62 return a->dependents().size() < b->dependents().size();
111 } 63 }
112 }; 64 };
113 65
66 // Ordered set of tasks that are ready to run.
reveman 2013/12/30 08:57:06 This comment is supposed to be next to "ready_to_r
sohanjg 2013/12/30 09:18:55 Done.
67 typedef std::priority_queue<internal::GraphNode*,
68 std::vector<internal::GraphNode*>,
69 TaskPriorityComparator> TaskQueue;
70
71 struct TaskNamespace {
72 TaskGraph pending_tasks;
73 TaskGraph running_tasks;
74 TaskVector completed_tasks;
75 TaskQueue ready_to_run_tasks;
76 };
77
78 class TaskNamespacePriorityComparator {
79 public:
80 bool operator()(TaskNamespace* a,
81 TaskNamespace* b) {
82 return task_comparators_.operator()(a->ready_to_run_tasks.top(),
83 b->ready_to_run_tasks.top());
84 }
85 TaskPriorityComparator task_comparators_;
86 };
87
88 typedef std::map<const WorkerPool*, linked_ptr<TaskNamespace> >
89 TaskNamespaceMap;
90
91 // Ordered set of tasks namespaces that have ready to run tasks.
reveman 2013/12/30 08:57:06 Please move this comment above |ready_to_run_names
sohanjg 2013/12/30 09:18:55 Done.
92 typedef std::priority_queue<TaskNamespace*,
93 std::vector<TaskNamespace*>,
94 TaskNamespacePriorityComparator>
95 TaskNamespaceQueue;
96
114 // Overridden from base::DelegateSimpleThread: 97 // Overridden from base::DelegateSimpleThread:
115 virtual void Run() OVERRIDE; 98 virtual void Run() OVERRIDE;
116 99
100 inline bool has_finished_running_tasks(TaskNamespace* task_namespace) {
101 return (task_namespace->pending_tasks.empty() &&
102 task_namespace->running_tasks.empty());
103 }
104
117 // This lock protects all members of this class except 105 // This lock protects all members of this class except
118 // |worker_pool_on_origin_thread_|. Do not read or modify anything 106 // |worker_pool_on_origin_thread_|. Do not read or modify anything
119 // without holding this lock. Do not block while holding this lock. 107 // without holding this lock. Do not block while holding this lock.
120 mutable base::Lock lock_; 108 mutable base::Lock lock_;
121 109
122 // Condition variable that is waited on by worker threads until new 110 // Condition variable that is waited on by worker threads until new
123 // tasks are ready to run or shutdown starts. 111 // tasks are ready to run or shutdown starts.
124 base::ConditionVariable has_ready_to_run_tasks_cv_; 112 base::ConditionVariable has_ready_to_run_tasks_cv_;
125 113
114 // Condition variable that is waited on by origin threads until a
115 // namespace has finished running all associated tasks.
116 base::ConditionVariable has_namespaces_with_finished_running_tasks_cv_;
117
126 // Provides each running thread loop with a unique index. First thread 118 // Provides each running thread loop with a unique index. First thread
127 // loop index is 0. 119 // loop index is 0.
128 unsigned next_thread_index_; 120 unsigned next_thread_index_;
129 121
130 // Set during shutdown. Tells workers to exit when no more tasks 122 // Set during shutdown. Tells workers to exit when no more tasks
131 // are pending. 123 // are pending.
132 bool shutdown_; 124 bool shutdown_;
133 125
134 // This set contains all pending tasks.
reveman 2013/12/30 08:57:06 nit: please keep this comment by moving it to "str
sohanjg 2013/12/30 09:18:55 Done.
135 GraphNodeMap pending_tasks_;
136
137 // Ordered set of tasks that are ready to run.
138 typedef std::priority_queue<internal::GraphNode*,
139 std::vector<internal::GraphNode*>,
140 PriorityComparator> TaskQueue;
141 TaskQueue ready_to_run_tasks_;
142
143 // This set contains all currently running tasks.
reveman 2013/12/30 08:57:06 nit: please keep this comment by moving it to "str
sohanjg 2013/12/30 09:18:55 Done.
144 GraphNodeMap running_tasks_;
145
146 // Completed tasks not yet collected by origin thread.
reveman 2013/12/30 08:57:06 nit: please keep this comment by moving it to "str
sohanjg 2013/12/30 09:18:55 Done.
147 TaskVector completed_tasks_;
148
149 ScopedPtrDeque<base::DelegateSimpleThread> workers_; 126 ScopedPtrDeque<base::DelegateSimpleThread> workers_;
reveman 2013/12/30 08:57:06 nit: Let's keep this at the bottom, below |ready_t
sohanjg 2013/12/30 09:18:55 Done.
150 127
151 DISALLOW_COPY_AND_ASSIGN(Inner); 128 TaskNamespaceMap namespaces_;
reveman 2013/12/30 08:57:06 Add this comment here: // This set contains all re
sohanjg 2013/12/30 09:18:55 Done.
129
130 TaskNamespaceQueue ready_to_run_namespaces_;
131
132 DISALLOW_COPY_AND_ASSIGN(TaskGraphRunner);
152 }; 133 };
153 134
154 WorkerPool::Inner::Inner( 135 TaskGraphRunner::TaskGraphRunner(
155 size_t num_threads, const std::string& thread_name_prefix) 136 size_t num_threads, const std::string& thread_name_prefix)
156 : lock_(), 137 : lock_(),
157 has_ready_to_run_tasks_cv_(&lock_), 138 has_ready_to_run_tasks_cv_(&lock_),
139 has_namespaces_with_finished_running_tasks_cv_(&lock_),
158 next_thread_index_(0), 140 next_thread_index_(0),
159 shutdown_(false) { 141 shutdown_(false) {
160 base::AutoLock lock(lock_); 142 base::AutoLock lock(lock_);
161 143
162 while (workers_.size() < num_threads) { 144 while (workers_.size() < num_threads) {
163 scoped_ptr<base::DelegateSimpleThread> worker = make_scoped_ptr( 145 scoped_ptr<base::DelegateSimpleThread> worker = make_scoped_ptr(
164 new base::DelegateSimpleThread( 146 new base::DelegateSimpleThread(
165 this, 147 this,
166 thread_name_prefix + 148 thread_name_prefix +
167 base::StringPrintf( 149 base::StringPrintf(
168 "Worker%u", 150 "Worker%u",
169 static_cast<unsigned>(workers_.size() + 1)).c_str())); 151 static_cast<unsigned>(workers_.size() + 1)).c_str()));
170 worker->Start(); 152 worker->Start();
171 #if defined(OS_ANDROID) || defined(OS_LINUX) 153 #if defined(OS_ANDROID) || defined(OS_LINUX)
172 worker->SetThreadPriority(base::kThreadPriority_Background); 154 worker->SetThreadPriority(base::kThreadPriority_Background);
173 #endif 155 #endif
174 workers_.push_back(worker.Pass()); 156 workers_.push_back(worker.Pass());
175 } 157 }
176 } 158 }
177 159
178 WorkerPool::Inner::~Inner() { 160 TaskGraphRunner::~TaskGraphRunner() {
179 base::AutoLock lock(lock_);
180
181 DCHECK(shutdown_);
182
183 DCHECK_EQ(0u, pending_tasks_.size());
184 DCHECK_EQ(0u, ready_to_run_tasks_.size());
185 DCHECK_EQ(0u, running_tasks_.size());
186 DCHECK_EQ(0u, completed_tasks_.size());
187 }
188
189 void WorkerPool::Inner::Shutdown() {
190 { 161 {
191 base::AutoLock lock(lock_); 162 base::AutoLock lock(lock_);
192 163
164 DCHECK_EQ(0u, ready_to_run_namespaces_.size());
165 DCHECK_EQ(0u, namespaces_.size());
166
193 DCHECK(!shutdown_); 167 DCHECK(!shutdown_);
194 shutdown_ = true; 168 shutdown_ = true;
195 169
196 // Wake up a worker so it knows it should exit. This will cause all workers 170 // Wake up a worker so it knows it should exit. This will cause all workers
197 // to exit as each will wake up another worker before exiting. 171 // to exit as each will wake up another worker before exiting.
198 has_ready_to_run_tasks_cv_.Signal(); 172 has_ready_to_run_tasks_cv_.Signal();
199 } 173 }
200 174
201 while (workers_.size()) { 175 while (workers_.size()) {
202 scoped_ptr<base::DelegateSimpleThread> worker = workers_.take_front(); 176 scoped_ptr<base::DelegateSimpleThread> worker = workers_.take_front();
203 // http://crbug.com/240453 - Join() is considered IO and will block this 177 // http://crbug.com/240453 - Join() is considered IO and will block this
204 // thread. See also http://crbug.com/239423 for further ideas. 178 // thread. See also http://crbug.com/239423 for further ideas.
205 base::ThreadRestrictions::ScopedAllowIO allow_io; 179 base::ThreadRestrictions::ScopedAllowIO allow_io;
206 worker->Join(); 180 worker->Join();
207 } 181 }
208 } 182 }
209 183
210 void WorkerPool::Inner::SetTaskGraph(TaskGraph* graph) { 184 void TaskGraphRunner::Register(const WorkerPool* worker_pool) {
185 base::AutoLock lock(lock_);
186
187 DCHECK(namespaces_.find(worker_pool) == namespaces_.end());
188 linked_ptr<TaskNamespace> task_set = make_linked_ptr(new TaskNamespace());
189 namespaces_[worker_pool] = task_set;
190 }
191
192 void TaskGraphRunner::Unregister(const WorkerPool* worker_pool) {
193 base::AutoLock lock(lock_);
194
195 DCHECK(namespaces_.find(worker_pool) != namespaces_.end());
196
197 TaskNamespace* task_namespace = namespaces_[worker_pool].get();
198 DCHECK_EQ(0u, task_namespace->pending_tasks.size());
199 DCHECK_EQ(0u, task_namespace->ready_to_run_tasks.size());
200 DCHECK_EQ(0u, task_namespace->running_tasks.size());
201 DCHECK_EQ(0u, task_namespace->completed_tasks.size());
202
203 namespaces_.erase(worker_pool);
204 }
205
206 void TaskGraphRunner::WaitForTasksToFinishRunning(
207 const WorkerPool* worker_pool) {
208 base::AutoLock lock(lock_);
209
210 DCHECK(namespaces_.find(worker_pool) != namespaces_.end());
211 TaskNamespace* task_namespace = namespaces_[worker_pool].get();
212
213 while (true) {
214 if (has_finished_running_tasks(task_namespace)) {
215 // There may be other namespaces that have finished running
216 // tasks, so wake up another origin thread.
217 has_namespaces_with_finished_running_tasks_cv_.Signal();
218 return;
219 }
220 has_namespaces_with_finished_running_tasks_cv_.Wait();
221 }
222 }
223
224 void TaskGraphRunner::SetTaskGraph(const WorkerPool* worker_pool,
225 TaskGraph* graph) {
211 // It is OK to call SetTaskGraph() after shutdown if |graph| is empty. 226 // It is OK to call SetTaskGraph() after shutdown if |graph| is empty.
212 DCHECK(graph->empty() || !shutdown_); 227 DCHECK(graph->empty() || !shutdown_);
reveman 2013/12/30 08:57:06 This DCHECK is no longer correct. Please make it "
sohanjg 2013/12/30 09:18:55 Done.
213 228
214 GraphNodeMap new_pending_tasks; 229 TaskGraph new_pending_tasks;
215 GraphNodeMap new_running_tasks; 230 TaskGraph new_running_tasks;
216 TaskQueue new_ready_to_run_tasks; 231 TaskQueue new_ready_to_run_tasks;
232 TaskNamespaceQueue new_ready_to_run_namespaces;
217 233
218 new_pending_tasks.swap(*graph); 234 new_pending_tasks.swap(*graph);
219 235
220 { 236 {
221 base::AutoLock lock(lock_); 237 base::AutoLock lock(lock_);
222 238
239 DCHECK(namespaces_.find(worker_pool) != namespaces_.end());
240 TaskNamespace* task_namespace = namespaces_[worker_pool].get();
241
223 // First remove all completed tasks from |new_pending_tasks| and 242 // First remove all completed tasks from |new_pending_tasks| and
224 // adjust number of dependencies. 243 // adjust number of dependencies.
225 for (TaskVector::iterator it = completed_tasks_.begin(); 244 for (TaskVector::iterator it = task_namespace->completed_tasks.begin();
226 it != completed_tasks_.end(); ++it) { 245 it != task_namespace->completed_tasks.end(); ++it) {
227 internal::WorkerPoolTask* task = it->get(); 246 internal::WorkerPoolTask* task = it->get();
228 247
229 scoped_ptr<internal::GraphNode> node = new_pending_tasks.take_and_erase( 248 scoped_ptr<internal::GraphNode> node = new_pending_tasks.take_and_erase(
230 task); 249 task);
231 if (node) { 250 if (node) {
232 for (internal::GraphNode::Vector::const_iterator it = 251 for (internal::GraphNode::Vector::const_iterator it =
233 node->dependents().begin(); 252 node->dependents().begin();
234 it != node->dependents().end(); ++it) { 253 it != node->dependents().end(); ++it) {
235 internal::GraphNode* dependent_node = *it; 254 internal::GraphNode* dependent_node = *it;
236 dependent_node->remove_dependency(); 255 dependent_node->remove_dependency();
237 } 256 }
238 } 257 }
239 } 258 }
240 259
241 // Build new running task set. 260 // Build new running task set.
242 for (GraphNodeMap::iterator it = running_tasks_.begin(); 261 for (TaskGraph::iterator it = task_namespace->running_tasks.begin();
243 it != running_tasks_.end(); ++it) { 262 it != task_namespace->running_tasks.end(); ++it) {
244 internal::WorkerPoolTask* task = it->first; 263 internal::WorkerPoolTask* task = it->first;
245 // Transfer scheduled task value from |new_pending_tasks| to 264 // Transfer scheduled task value from |new_pending_tasks| to
246 // |new_running_tasks| if currently running. Value must be set to 265 // |new_running_tasks| if currently running. Value must be set to
247 // NULL if |new_pending_tasks| doesn't contain task. This does 266 // NULL if |new_pending_tasks| doesn't contain task. This does
248 // the right in both cases. 267 // the right in both cases.
249 new_running_tasks.set(task, new_pending_tasks.take_and_erase(task)); 268 new_running_tasks.set(task, new_pending_tasks.take_and_erase(task));
250 } 269 }
251 270
252 // Build new "ready to run" tasks queue. 271 // Build new "ready to run" tasks queue.
253 // TODO(reveman): Create this queue when building the task graph instead. 272 // TODO(reveman): Create this queue when building the task graph instead.
254 for (GraphNodeMap::iterator it = new_pending_tasks.begin(); 273 for (TaskGraph::iterator it = new_pending_tasks.begin();
255 it != new_pending_tasks.end(); ++it) { 274 it != new_pending_tasks.end(); ++it) {
256 internal::WorkerPoolTask* task = it->first; 275 internal::WorkerPoolTask* task = it->first;
257 DCHECK(task); 276 DCHECK(task);
258 internal::GraphNode* node = it->second; 277 internal::GraphNode* node = it->second;
259 278
260 // Completed tasks should not exist in |new_pending_tasks|. 279 // Completed tasks should not exist in |new_pending_tasks|.
261 DCHECK(!task->HasFinishedRunning()); 280 DCHECK(!task->HasFinishedRunning());
262 281
263 // Call DidSchedule() to indicate that this task has been scheduled. 282 // Call DidSchedule() to indicate that this task has been scheduled.
264 // Note: This is only for debugging purposes. 283 // Note: This is only for debugging purposes.
265 task->DidSchedule(); 284 task->DidSchedule();
266 285
267 if (!node->num_dependencies()) 286 if (!node->num_dependencies())
268 new_ready_to_run_tasks.push(node); 287 new_ready_to_run_tasks.push(node);
269 288
270 // Erase the task from old pending tasks. 289 // Erase the task from old pending tasks.
271 pending_tasks_.erase(task); 290 task_namespace->pending_tasks.erase(task);
272 } 291 }
273 292
274 completed_tasks_.reserve(completed_tasks_.size() + pending_tasks_.size()); 293 task_namespace->completed_tasks.reserve(
294 task_namespace->completed_tasks.size() +
295 task_namespace->pending_tasks.size());
275 296
276 // The items left in |pending_tasks_| need to be canceled. 297 // The items left in |pending_tasks| need to be canceled.
277 for (GraphNodeMap::const_iterator it = pending_tasks_.begin(); 298 for (TaskGraph::const_iterator it = task_namespace->pending_tasks.begin();
278 it != pending_tasks_.end(); 299 it != task_namespace->pending_tasks.end(); ++it) {
279 ++it) { 300 task_namespace->completed_tasks.push_back(it->first);
280 completed_tasks_.push_back(it->first);
281 } 301 }
282 302
283 // Swap task sets. 303 // Swap task sets.
284 // Note: old tasks are intentionally destroyed after releasing |lock_|. 304 // Note: old tasks are intentionally destroyed after releasing |lock_|.
285 pending_tasks_.swap(new_pending_tasks); 305 task_namespace->pending_tasks.swap(new_pending_tasks);
286 running_tasks_.swap(new_running_tasks); 306 task_namespace->running_tasks.swap(new_running_tasks);
287 std::swap(ready_to_run_tasks_, new_ready_to_run_tasks); 307 std::swap(task_namespace->ready_to_run_tasks, new_ready_to_run_tasks);
288 308
289 // If |ready_to_run_tasks_| is empty, it means we either have 309 // If |ready_to_run_tasks| is empty, it means we either have
290 // running tasks, or we have no pending tasks. 310 // running tasks, or we have no pending tasks.
291 DCHECK(!ready_to_run_tasks_.empty() || 311 DCHECK(!task_namespace->ready_to_run_tasks.empty() ||
292 (pending_tasks_.empty() || !running_tasks_.empty())); 312 (task_namespace->pending_tasks.empty() ||
313 !task_namespace->running_tasks.empty()));
314
315 // Build new "ready to run" task namespaces queue.
316 for (TaskNamespaceMap::iterator it = namespaces_.begin();
317 it != namespaces_.end(); ++it) {
318 if (!it->second->ready_to_run_tasks.empty())
319 new_ready_to_run_namespaces.push(it->second.get());
320 }
321 std::swap(ready_to_run_namespaces_, new_ready_to_run_namespaces);
293 322
294 // If there is more work available, wake up worker thread. 323 // If there is more work available, wake up worker thread.
295 if (!ready_to_run_tasks_.empty()) 324 if (!ready_to_run_namespaces_.empty())
296 has_ready_to_run_tasks_cv_.Signal(); 325 has_ready_to_run_tasks_cv_.Signal();
297 } 326 }
298 } 327 }
299 328
300 void WorkerPool::Inner::CollectCompletedTasks(TaskVector* completed_tasks) { 329 void TaskGraphRunner::CollectCompletedTasks(
330 const WorkerPool* worker_pool, TaskVector* completed_tasks) {
301 base::AutoLock lock(lock_); 331 base::AutoLock lock(lock_);
302 332
303 DCHECK_EQ(0u, completed_tasks->size()); 333 DCHECK_EQ(0u, completed_tasks->size());
304 completed_tasks->swap(completed_tasks_); 334 DCHECK(namespaces_.find(worker_pool) != namespaces_.end());
335 completed_tasks->swap(namespaces_[worker_pool]->completed_tasks);
305 } 336 }
306 337
307 void WorkerPool::Inner::Run() { 338 void TaskGraphRunner::Run() {
308 base::AutoLock lock(lock_); 339 base::AutoLock lock(lock_);
309 340
310 // Get a unique thread index. 341 // Get a unique thread index.
311 int thread_index = next_thread_index_++; 342 int thread_index = next_thread_index_++;
312 343
313 while (true) { 344 while (true) {
314 if (ready_to_run_tasks_.empty()) { 345 if (ready_to_run_namespaces_.empty()) {
315 // Exit when shutdown is set and no more tasks are pending. 346 // Exit when shutdown is set and no more tasks are pending.
316 if (shutdown_ && pending_tasks_.empty()) 347 if (shutdown_)
317 break; 348 break;
318 349
319 // Wait for more tasks. 350 // Wait for more tasks.
320 has_ready_to_run_tasks_cv_.Wait(); 351 has_ready_to_run_tasks_cv_.Wait();
321 continue; 352 continue;
322 } 353 }
323 354
324 // Take top priority task from |ready_to_run_tasks_|. 355 // Take top priority TaskNamespace from |ready_to_run_namespaces_|.
356 TaskNamespace* task_namespace = ready_to_run_namespaces_.top();
357 ready_to_run_namespaces_.pop();
358 DCHECK(!task_namespace->ready_to_run_tasks.empty());
359
360 // Take top priority task from |ready_to_run_tasks|.
325 scoped_refptr<internal::WorkerPoolTask> task( 361 scoped_refptr<internal::WorkerPoolTask> task(
326 ready_to_run_tasks_.top()->task()); 362 task_namespace->ready_to_run_tasks.top()->task());
327 ready_to_run_tasks_.pop(); 363 task_namespace->ready_to_run_tasks.pop();
328 364
329 // Move task from |pending_tasks_| to |running_tasks_|. 365 // Add task namespace back to |ready_to_run_namespaces_| if not
330 DCHECK(pending_tasks_.contains(task.get())); 366 // empty after taking top priority task.
331 DCHECK(!running_tasks_.contains(task.get())); 367 if (!task_namespace->ready_to_run_tasks.empty())
332 running_tasks_.set(task.get(), pending_tasks_.take_and_erase(task.get())); 368 ready_to_run_namespaces_.push(task_namespace);
369
370 // Move task from |pending_tasks| to |running_tasks|.
371 DCHECK(task_namespace->pending_tasks.contains(task.get()));
372 DCHECK(!task_namespace->running_tasks.contains(task.get()));
373 task_namespace->running_tasks.set(
374 task.get(),
375 task_namespace->pending_tasks.take_and_erase(task.get()));
333 376
334 // There may be more work available, so wake up another worker thread. 377 // There may be more work available, so wake up another worker thread.
335 has_ready_to_run_tasks_cv_.Signal(); 378 has_ready_to_run_tasks_cv_.Signal();
336 379
337 // Call WillRun() before releasing |lock_| and running task. 380 // Call WillRun() before releasing |lock_| and running task.
338 task->WillRun(); 381 task->WillRun();
339 382
340 { 383 {
341 base::AutoUnlock unlock(lock_); 384 base::AutoUnlock unlock(lock_);
342 385
343 task->RunOnWorkerThread(thread_index); 386 task->RunOnWorkerThread(thread_index);
344 } 387 }
345 388
346 // This will mark task as finished running. 389 // This will mark task as finished running.
347 task->DidRun(); 390 task->DidRun();
348 391
349 // Now iterate over all dependents to remove dependency and check 392 // Now iterate over all dependents to remove dependency and check
350 // if they are ready to run. 393 // if they are ready to run.
351 scoped_ptr<internal::GraphNode> node = running_tasks_.take_and_erase( 394 scoped_ptr<internal::GraphNode> node =
352 task.get()); 395 task_namespace->running_tasks.take_and_erase(task.get());
353 if (node) { 396 if (node) {
354 for (internal::GraphNode::Vector::const_iterator it = 397 for (internal::GraphNode::Vector::const_iterator it =
355 node->dependents().begin(); 398 node->dependents().begin();
356 it != node->dependents().end(); ++it) { 399 it != node->dependents().end(); ++it) {
357 internal::GraphNode* dependent_node = *it; 400 internal::GraphNode* dependent_node = *it;
358 401
359 dependent_node->remove_dependency(); 402 dependent_node->remove_dependency();
360 // Task is ready if it has no dependencies. Add it to 403 // Task is ready if it has no dependencies. Add it to
361 // |ready_to_run_tasks_|. 404 // |ready_to_run_tasks|.
362 if (!dependent_node->num_dependencies()) 405 if (!dependent_node->num_dependencies()) {
363 ready_to_run_tasks_.push(dependent_node); 406 bool was_empty = task_namespace->ready_to_run_tasks.empty();
407 task_namespace->ready_to_run_tasks.push(dependent_node);
408 // Task namespace is ready if it has at least one ready
409 // to run task. Add it to |ready_to_run_namespaces_| if
410 // it just become ready.
411 if (was_empty)
412 ready_to_run_namespaces_.push(task_namespace);
413 }
364 } 414 }
365 } 415 }
366 416
367 // Finally add task to |completed_tasks_|. 417 // Finally add task to |completed_tasks|.
368 completed_tasks_.push_back(task); 418 task_namespace->completed_tasks.push_back(task);
419
420 // If namespace has finished running all tasks, wake up origin thread.
421 if (has_finished_running_tasks(task_namespace))
422 has_namespaces_with_finished_running_tasks_cv_.Signal();
369 } 423 }
370 424
371 // We noticed we should exit. Wake up the next worker so it knows it should 425 // We noticed we should exit. Wake up the next worker so it knows it should
372 // exit as well (because the Shutdown() code only signals once). 426 // exit as well (because the Shutdown() code only signals once).
373 has_ready_to_run_tasks_cv_.Signal(); 427 has_ready_to_run_tasks_cv_.Signal();
374 } 428 }
375 429
376 WorkerPool::WorkerPool(size_t num_threads, 430 class CC_EXPORT CompositorRasterTaskGraphRunner
377 const std::string& thread_name_prefix) 431 : public TaskGraphRunner {
378 : in_dispatch_completion_callbacks_(false), 432 public:
379 inner_(make_scoped_ptr(new Inner(num_threads, thread_name_prefix))) { 433 CompositorRasterTaskGraphRunner() : TaskGraphRunner(
434 switches::GetNumRasterThreads(), "CompositorRaster") {
435 }
436 };
437
438 base::LazyInstance<CompositorRasterTaskGraphRunner>
439 g_task_graph_runner = LAZY_INSTANCE_INITIALIZER;
440
441 } // namespace
442
443 namespace internal {
444
445 WorkerPoolTask::WorkerPoolTask()
446 : did_schedule_(false),
447 did_run_(false),
448 did_complete_(false) {
449 }
450
451 WorkerPoolTask::~WorkerPoolTask() {
452 DCHECK_EQ(did_schedule_, did_complete_);
453 DCHECK(!did_run_ || did_schedule_);
454 DCHECK(!did_run_ || did_complete_);
455 }
456
457 void WorkerPoolTask::DidSchedule() {
458 DCHECK(!did_complete_);
459 did_schedule_ = true;
460 }
461
462 void WorkerPoolTask::WillRun() {
463 DCHECK(did_schedule_);
464 DCHECK(!did_complete_);
465 DCHECK(!did_run_);
466 }
467
468 void WorkerPoolTask::DidRun() {
469 did_run_ = true;
470 }
471
472 void WorkerPoolTask::WillComplete() {
473 DCHECK(!did_complete_);
474 }
475
476 void WorkerPoolTask::DidComplete() {
477 DCHECK(did_schedule_);
478 DCHECK(!did_complete_);
479 did_complete_ = true;
480 }
481
482 bool WorkerPoolTask::HasFinishedRunning() const {
483 return did_run_;
484 }
485
486 bool WorkerPoolTask::HasCompleted() const {
487 return did_complete_;
488 }
489
490 GraphNode::GraphNode(internal::WorkerPoolTask* task, unsigned priority)
491 : task_(task),
492 priority_(priority),
493 num_dependencies_(0) {
494 }
495
496 GraphNode::~GraphNode() {
497 }
498
499 } // namespace internal
500
501 WorkerPool::WorkerPool() : in_dispatch_completion_callbacks_(false) {
502 g_task_graph_runner.Pointer()->Register(this);
380 } 503 }
381 504
382 WorkerPool::~WorkerPool() { 505 WorkerPool::~WorkerPool() {
506 g_task_graph_runner.Pointer()->Unregister(this);
383 } 507 }
384 508
385 void WorkerPool::Shutdown() { 509 void WorkerPool::Shutdown() {
386 TRACE_EVENT0("cc", "WorkerPool::Shutdown"); 510 TRACE_EVENT0("cc", "WorkerPool::Shutdown");
387 511
388 DCHECK(!in_dispatch_completion_callbacks_); 512 DCHECK(!in_dispatch_completion_callbacks_);
389 513
390 inner_->Shutdown(); 514 g_task_graph_runner.Pointer()->WaitForTasksToFinishRunning(this);
391 } 515 }
392 516
393 void WorkerPool::CheckForCompletedTasks() { 517 void WorkerPool::CheckForCompletedTasks() {
394 TRACE_EVENT0("cc", "WorkerPool::CheckForCompletedTasks"); 518 TRACE_EVENT0("cc", "WorkerPool::CheckForCompletedTasks");
395 519
396 DCHECK(!in_dispatch_completion_callbacks_); 520 DCHECK(!in_dispatch_completion_callbacks_);
397 521
398 TaskVector completed_tasks; 522 TaskVector completed_tasks;
399 inner_->CollectCompletedTasks(&completed_tasks); 523 g_task_graph_runner.Pointer()->CollectCompletedTasks(this, &completed_tasks);
400 ProcessCompletedTasks(completed_tasks); 524 ProcessCompletedTasks(completed_tasks);
401 } 525 }
402 526
403 void WorkerPool::ProcessCompletedTasks( 527 void WorkerPool::ProcessCompletedTasks(
404 const TaskVector& completed_tasks) { 528 const TaskVector& completed_tasks) {
405 TRACE_EVENT1("cc", "WorkerPool::ProcessCompletedTasks", 529 TRACE_EVENT1("cc", "WorkerPool::ProcessCompletedTasks",
406 "completed_task_count", completed_tasks.size()); 530 "completed_task_count", completed_tasks.size());
407 531
408 // Worker pool instance is not reentrant while processing completed tasks. 532 // Worker pool instance is not reentrant while processing completed tasks.
409 in_dispatch_completion_callbacks_ = true; 533 in_dispatch_completion_callbacks_ = true;
(...skipping 10 matching lines...) Expand all
420 544
421 in_dispatch_completion_callbacks_ = false; 545 in_dispatch_completion_callbacks_ = false;
422 } 546 }
423 547
424 void WorkerPool::SetTaskGraph(TaskGraph* graph) { 548 void WorkerPool::SetTaskGraph(TaskGraph* graph) {
425 TRACE_EVENT1("cc", "WorkerPool::SetTaskGraph", 549 TRACE_EVENT1("cc", "WorkerPool::SetTaskGraph",
426 "num_tasks", graph->size()); 550 "num_tasks", graph->size());
427 551
428 DCHECK(!in_dispatch_completion_callbacks_); 552 DCHECK(!in_dispatch_completion_callbacks_);
429 553
430 inner_->SetTaskGraph(graph); 554 g_task_graph_runner.Pointer()->SetTaskGraph(this, graph);
431 } 555 }
432 556
433 } // namespace cc 557 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698