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

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

Powered by Google App Engine
This is Rietveld 408576698