OLD | NEW |
---|---|
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; | |
reveman
2013/12/22 15:49:25
nit: please add a blank line between types and cto
sohanjg
2013/12/26 13:38:47
Done.
| |
33 TaskGraphRunner(size_t num_threads, const std::string& thread_name_prefix); | |
34 virtual ~TaskGraphRunner(); | |
28 | 35 |
29 WorkerPoolTask::~WorkerPoolTask() { | 36 void Register(const WorkerPool* worker_pool); |
30 DCHECK_EQ(did_schedule_, did_complete_); | 37 void Unregister(const WorkerPool* worker_pool); |
31 DCHECK(!did_run_ || did_schedule_); | |
32 DCHECK(!did_run_ || did_complete_); | |
33 } | |
34 | |
35 void WorkerPoolTask::DidSchedule() { | |
36 DCHECK(!did_complete_); | |
37 did_schedule_ = true; | |
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(); | 38 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. | |
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(), | |
reveman
2013/12/22 15:49:25
nit: should be indented 2 spaces, not 4
sohanjg
2013/12/26 13:38:47
Done.
| |
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. | |
92 typedef std::priority_queue<TaskNamespace*, | |
93 std::vector<TaskNamespace*>, | |
reveman
2013/12/22 15:49:25
nit: "std::vector<TaskName.." vertically aligned w
sohanjg
2013/12/26 13:38:47
Done.
| |
94 TaskNamespacePriorityComparator> TaskNamespaceQueue; | |
reveman
2013/12/22 15:49:25
nit: "TaskNamespacePri.." vertically aligned with
sohanjg
2013/12/26 13:38:47
Done.
| |
95 | |
114 // Overridden from base::DelegateSimpleThread: | 96 // Overridden from base::DelegateSimpleThread: |
115 virtual void Run() OVERRIDE; | 97 virtual void Run() OVERRIDE; |
116 | 98 |
117 // This lock protects all members of this class except | 99 // This lock protects all members of this class except |
118 // |worker_pool_on_origin_thread_|. Do not read or modify anything | 100 // |worker_pool_on_origin_thread_|. Do not read or modify anything |
119 // without holding this lock. Do not block while holding this lock. | 101 // without holding this lock. Do not block while holding this lock. |
120 mutable base::Lock lock_; | 102 mutable base::Lock lock_; |
121 | 103 |
122 // Condition variable that is waited on by worker threads until new | 104 // Condition variable that is waited on by worker threads until new |
123 // tasks are ready to run or shutdown starts. | 105 // tasks are ready to run or shutdown starts. |
124 base::ConditionVariable has_ready_to_run_tasks_cv_; | 106 base::ConditionVariable has_ready_to_run_tasks_cv_; |
125 | 107 |
126 // Provides each running thread loop with a unique index. First thread | 108 // Provides each running thread loop with a unique index. First thread |
127 // loop index is 0. | 109 // loop index is 0. |
128 unsigned next_thread_index_; | 110 unsigned next_thread_index_; |
129 | 111 |
130 // Set during shutdown. Tells workers to exit when no more tasks | 112 // Set during shutdown. Tells workers to exit when no more tasks |
131 // are pending. | 113 // are pending. |
132 bool shutdown_; | 114 bool shutdown_; |
133 | 115 |
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_; | 116 ScopedPtrDeque<base::DelegateSimpleThread> workers_; |
150 | 117 |
151 DISALLOW_COPY_AND_ASSIGN(Inner); | 118 TaskNamespaceMap namespaces_; |
119 | |
120 TaskNamespaceQueue ready_to_run_namespaces_; | |
121 | |
122 DISALLOW_COPY_AND_ASSIGN(TaskGraphRunner); | |
152 }; | 123 }; |
153 | 124 |
154 WorkerPool::Inner::Inner( | 125 TaskGraphRunner::TaskGraphRunner( |
155 size_t num_threads, const std::string& thread_name_prefix) | 126 size_t num_threads, const std::string& thread_name_prefix) |
156 : lock_(), | 127 : lock_(), |
157 has_ready_to_run_tasks_cv_(&lock_), | 128 has_ready_to_run_tasks_cv_(&lock_), |
158 next_thread_index_(0), | 129 next_thread_index_(0), |
159 shutdown_(false) { | 130 shutdown_(false) { |
160 base::AutoLock lock(lock_); | 131 base::AutoLock lock(lock_); |
161 | 132 |
162 while (workers_.size() < num_threads) { | 133 while (workers_.size() < num_threads) { |
163 scoped_ptr<base::DelegateSimpleThread> worker = make_scoped_ptr( | 134 scoped_ptr<base::DelegateSimpleThread> worker = make_scoped_ptr( |
164 new base::DelegateSimpleThread( | 135 new base::DelegateSimpleThread( |
165 this, | 136 this, |
166 thread_name_prefix + | 137 thread_name_prefix + |
167 base::StringPrintf( | 138 base::StringPrintf( |
168 "Worker%u", | 139 "Worker%u", |
169 static_cast<unsigned>(workers_.size() + 1)).c_str())); | 140 static_cast<unsigned>(workers_.size() + 1)).c_str())); |
170 worker->Start(); | 141 worker->Start(); |
171 #if defined(OS_ANDROID) || defined(OS_LINUX) | 142 #if defined(OS_ANDROID) || defined(OS_LINUX) |
172 worker->SetThreadPriority(base::kThreadPriority_Background); | 143 worker->SetThreadPriority(base::kThreadPriority_Background); |
173 #endif | 144 #endif |
174 workers_.push_back(worker.Pass()); | 145 workers_.push_back(worker.Pass()); |
175 } | 146 } |
176 } | 147 } |
177 | 148 |
178 WorkerPool::Inner::~Inner() { | 149 TaskGraphRunner::~TaskGraphRunner() { |
179 base::AutoLock lock(lock_); | 150 base::AutoLock lock(lock_); |
180 | 151 |
181 DCHECK(shutdown_); | 152 DCHECK(shutdown_); |
182 | 153 DCHECK_EQ(0u, ready_to_run_namespaces_.size()); |
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 } | 154 } |
188 | 155 |
189 void WorkerPool::Inner::Shutdown() { | 156 void TaskGraphRunner::Register(const WorkerPool* worker_pool) { |
157 base::AutoLock lock(lock_); | |
158 | |
159 DCHECK(namespaces_.find(worker_pool) == namespaces_.end()); | |
160 linked_ptr<TaskNamespace> task_set = make_linked_ptr(new TaskNamespace()); | |
161 namespaces_[worker_pool] = task_set; | |
162 } | |
163 | |
164 void TaskGraphRunner::Unregister(const WorkerPool* worker_pool) { | |
165 base::AutoLock lock(lock_); | |
166 | |
167 DCHECK(namespaces_.find(worker_pool) != namespaces_.end()); | |
168 namespaces_.erase(worker_pool); | |
169 } | |
170 | |
171 void TaskGraphRunner::Shutdown() { | |
190 { | 172 { |
191 base::AutoLock lock(lock_); | 173 base::AutoLock lock(lock_); |
192 | 174 |
193 DCHECK(!shutdown_); | 175 DCHECK(!shutdown_); |
194 shutdown_ = true; | 176 shutdown_ = true; |
195 | |
reveman
2013/12/22 15:49:25
nit: no need to remove this line
sohanjg
2013/12/26 13:38:47
Done.
| |
196 // Wake up a worker so it knows it should exit. This will cause all workers | 177 // 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. | 178 // to exit as each will wake up another worker before exiting. |
198 has_ready_to_run_tasks_cv_.Signal(); | 179 has_ready_to_run_tasks_cv_.Signal(); |
199 } | 180 } |
200 | 181 |
201 while (workers_.size()) { | 182 while (workers_.size()) { |
202 scoped_ptr<base::DelegateSimpleThread> worker = workers_.take_front(); | 183 scoped_ptr<base::DelegateSimpleThread> worker = workers_.take_front(); |
203 // http://crbug.com/240453 - Join() is considered IO and will block this | 184 // http://crbug.com/240453 - Join() is considered IO and will block this |
204 // thread. See also http://crbug.com/239423 for further ideas. | 185 // thread. See also http://crbug.com/239423 for further ideas. |
205 base::ThreadRestrictions::ScopedAllowIO allow_io; | 186 base::ThreadRestrictions::ScopedAllowIO allow_io; |
206 worker->Join(); | 187 worker->Join(); |
207 } | 188 } |
208 } | 189 } |
209 | 190 |
210 void WorkerPool::Inner::SetTaskGraph(TaskGraph* graph) { | 191 void TaskGraphRunner::SetTaskGraph(const WorkerPool* worker_pool, |
192 TaskGraph* graph) { | |
211 // It is OK to call SetTaskGraph() after shutdown if |graph| is empty. | 193 // It is OK to call SetTaskGraph() after shutdown if |graph| is empty. |
212 DCHECK(graph->empty() || !shutdown_); | 194 DCHECK(graph->empty() || !shutdown_); |
213 | 195 |
214 GraphNodeMap new_pending_tasks; | 196 TaskGraph new_pending_tasks; |
215 GraphNodeMap new_running_tasks; | 197 TaskGraph new_running_tasks; |
216 TaskQueue new_ready_to_run_tasks; | 198 TaskQueue new_ready_to_run_tasks; |
199 TaskNamespaceQueue new_ready_to_run_namespaces; | |
217 | 200 |
218 new_pending_tasks.swap(*graph); | 201 new_pending_tasks.swap(*graph); |
219 | 202 |
220 { | 203 { |
221 base::AutoLock lock(lock_); | 204 base::AutoLock lock(lock_); |
222 | 205 |
206 DCHECK(namespaces_.find(worker_pool) != namespaces_.end()); | |
207 TaskNamespace* task_namespace = namespaces_[worker_pool].get(); | |
208 | |
223 // First remove all completed tasks from |new_pending_tasks| and | 209 // First remove all completed tasks from |new_pending_tasks| and |
224 // adjust number of dependencies. | 210 // adjust number of dependencies. |
225 for (TaskVector::iterator it = completed_tasks_.begin(); | 211 for (TaskVector::iterator it = task_namespace->completed_tasks.begin(); |
226 it != completed_tasks_.end(); ++it) { | 212 it != task_namespace->completed_tasks.end(); ++it) { |
227 internal::WorkerPoolTask* task = it->get(); | 213 internal::WorkerPoolTask* task = it->get(); |
228 | |
reveman
2013/12/22 15:49:25
nit: no need to remove this line
sohanjg
2013/12/26 13:38:47
Done.
| |
229 scoped_ptr<internal::GraphNode> node = new_pending_tasks.take_and_erase( | 214 scoped_ptr<internal::GraphNode> node = new_pending_tasks.take_and_erase( |
230 task); | 215 task); |
231 if (node) { | 216 if (node) { |
232 for (internal::GraphNode::Vector::const_iterator it = | 217 for (internal::GraphNode::Vector::const_iterator it = |
233 node->dependents().begin(); | 218 node->dependents().begin(); |
234 it != node->dependents().end(); ++it) { | 219 it != node->dependents().end(); ++it) { |
235 internal::GraphNode* dependent_node = *it; | 220 internal::GraphNode* dependent_node = *it; |
236 dependent_node->remove_dependency(); | 221 dependent_node->remove_dependency(); |
237 } | 222 } |
238 } | 223 } |
239 } | 224 } |
240 | |
reveman
2013/12/22 15:49:25
nit: no need to remove this line
sohanjg
2013/12/26 13:38:47
Done.
| |
241 // Build new running task set. | 225 // Build new running task set. |
242 for (GraphNodeMap::iterator it = running_tasks_.begin(); | 226 for (TaskGraph::iterator it = task_namespace->running_tasks.begin(); |
243 it != running_tasks_.end(); ++it) { | 227 it != task_namespace->running_tasks.end(); ++it) { |
reveman
2013/12/22 15:49:25
nit: "it != task_name.." vertically aligned with "
sohanjg
2013/12/26 13:38:47
Done.
| |
244 internal::WorkerPoolTask* task = it->first; | 228 internal::WorkerPoolTask* task = it->first; |
245 // Transfer scheduled task value from |new_pending_tasks| to | 229 // Transfer scheduled task value from |new_pending_tasks| to |
246 // |new_running_tasks| if currently running. Value must be set to | 230 // |new_running_tasks| if currently running. Value must be set to |
247 // NULL if |new_pending_tasks| doesn't contain task. This does | 231 // NULL if |new_pending_tasks| doesn't contain task. This does |
248 // the right in both cases. | 232 // the right in both cases. |
249 new_running_tasks.set(task, new_pending_tasks.take_and_erase(task)); | 233 new_running_tasks.set(task, new_pending_tasks.take_and_erase(task)); |
250 } | 234 } |
251 | |
reveman
2013/12/22 15:49:25
nit: no need to remove this line
sohanjg
2013/12/26 13:38:47
Done.
| |
252 // Build new "ready to run" tasks queue. | 235 // Build new "ready to run" tasks queue. |
253 // TODO(reveman): Create this queue when building the task graph instead. | 236 // TODO(reveman): Create this queue when building the task graph instead. |
254 for (GraphNodeMap::iterator it = new_pending_tasks.begin(); | 237 for (TaskGraph::iterator it = new_pending_tasks.begin(); |
255 it != new_pending_tasks.end(); ++it) { | 238 it != new_pending_tasks.end(); ++it) { |
256 internal::WorkerPoolTask* task = it->first; | 239 internal::WorkerPoolTask* task = it->first; |
257 DCHECK(task); | 240 DCHECK(task); |
258 internal::GraphNode* node = it->second; | 241 internal::GraphNode* node = it->second; |
259 | 242 |
260 // Completed tasks should not exist in |new_pending_tasks|. | 243 // Completed tasks should not exist in |new_pending_tasks|. |
261 DCHECK(!task->HasFinishedRunning()); | 244 DCHECK(!task->HasFinishedRunning()); |
262 | 245 |
263 // Call DidSchedule() to indicate that this task has been scheduled. | 246 // Call DidSchedule() to indicate that this task has been scheduled. |
264 // Note: This is only for debugging purposes. | 247 // Note: This is only for debugging purposes. |
265 task->DidSchedule(); | 248 task->DidSchedule(); |
266 | 249 |
267 if (!node->num_dependencies()) | 250 if (!node->num_dependencies()) |
268 new_ready_to_run_tasks.push(node); | 251 new_ready_to_run_tasks.push(node); |
269 | 252 |
270 // Erase the task from old pending tasks. | 253 // Erase the task from old pending tasks. |
271 pending_tasks_.erase(task); | 254 task_namespace->pending_tasks.erase(task); |
272 } | 255 } |
273 | 256 |
274 completed_tasks_.reserve(completed_tasks_.size() + pending_tasks_.size()); | 257 task_namespace->completed_tasks.reserve( |
258 task_namespace->completed_tasks.size() + | |
259 task_namespace->pending_tasks.size()); | |
275 | 260 |
276 // The items left in |pending_tasks_| need to be canceled. | 261 // The items left in |pending_tasks| need to be canceled. |
277 for (GraphNodeMap::const_iterator it = pending_tasks_.begin(); | 262 for (TaskGraph::const_iterator it = task_namespace->pending_tasks.begin(); |
278 it != pending_tasks_.end(); | 263 it != task_namespace->pending_tasks.end(); ++it) { |
reveman
2013/12/22 15:49:25
nit: "it != task_name.." vertically aligned with "
sohanjg
2013/12/26 13:38:47
Done.
| |
279 ++it) { | 264 task_namespace->completed_tasks.push_back(it->first); |
280 completed_tasks_.push_back(it->first); | |
281 } | 265 } |
282 | 266 |
283 // Swap task sets. | 267 // Swap task sets. |
284 // Note: old tasks are intentionally destroyed after releasing |lock_|. | 268 // Note: old tasks are intentionally destroyed after releasing |lock_|. |
285 pending_tasks_.swap(new_pending_tasks); | 269 task_namespace->pending_tasks.swap(new_pending_tasks); |
286 running_tasks_.swap(new_running_tasks); | 270 task_namespace->running_tasks.swap(new_running_tasks); |
287 std::swap(ready_to_run_tasks_, new_ready_to_run_tasks); | 271 std::swap(task_namespace->ready_to_run_tasks, new_ready_to_run_tasks); |
288 | 272 |
289 // If |ready_to_run_tasks_| is empty, it means we either have | 273 // If |ready_to_run_tasks| is empty, it means we either have |
290 // running tasks, or we have no pending tasks. | 274 // running tasks, or we have no pending tasks. |
291 DCHECK(!ready_to_run_tasks_.empty() || | 275 DCHECK(!task_namespace->ready_to_run_tasks.empty() || |
292 (pending_tasks_.empty() || !running_tasks_.empty())); | 276 (task_namespace->pending_tasks.empty() || |
277 !task_namespace->running_tasks.empty())); | |
278 | |
279 // Re-create the ready_to_run_namespaces_ | |
280 for (TaskNamespaceMap::iterator it =namespaces_.begin(); | |
reveman
2013/12/22 15:49:25
nit: space between "=" and "namespaces_.b.."
sohanjg
2013/12/26 13:38:47
Done.
| |
281 it != namespaces_.end(); ++it) { | |
reveman
2013/12/22 15:49:25
nit: "it !=.." vertically aligned with "TaskNamesp
sohanjg
2013/12/26 13:38:47
Done.
| |
282 if (!it->second->ready_to_run_tasks.empty()) | |
283 new_ready_to_run_namespaces.push(it->second.get()); | |
284 } | |
285 std::swap(ready_to_run_namespaces_, new_ready_to_run_namespaces); | |
293 | 286 |
294 // If there is more work available, wake up worker thread. | 287 // If there is more work available, wake up worker thread. |
295 if (!ready_to_run_tasks_.empty()) | 288 if (!ready_to_run_namespaces_.empty()) |
296 has_ready_to_run_tasks_cv_.Signal(); | 289 has_ready_to_run_tasks_cv_.Signal(); |
297 } | 290 } |
298 } | 291 } |
299 | 292 |
300 void WorkerPool::Inner::CollectCompletedTasks(TaskVector* completed_tasks) { | 293 void TaskGraphRunner::CollectCompletedTasks( |
294 const WorkerPool* worker_pool, TaskVector* completed_tasks) { | |
reveman
2013/12/22 15:49:25
nit: this line should be indented 4 spaces, not 2
sohanjg
2013/12/26 13:38:47
Done.
| |
301 base::AutoLock lock(lock_); | 295 base::AutoLock lock(lock_); |
302 | 296 |
303 DCHECK_EQ(0u, completed_tasks->size()); | 297 DCHECK_EQ(0u, completed_tasks->size()); |
304 completed_tasks->swap(completed_tasks_); | 298 DCHECK(namespaces_.find(worker_pool) != namespaces_.end()); |
299 completed_tasks->swap(namespaces_[worker_pool]->completed_tasks); | |
305 } | 300 } |
306 | 301 |
307 void WorkerPool::Inner::Run() { | 302 void TaskGraphRunner::Run() { |
308 base::AutoLock lock(lock_); | 303 base::AutoLock lock(lock_); |
309 | 304 |
310 // Get a unique thread index. | 305 // Get a unique thread index. |
311 int thread_index = next_thread_index_++; | 306 int thread_index = next_thread_index_++; |
312 | 307 |
313 while (true) { | 308 while (true) { |
314 if (ready_to_run_tasks_.empty()) { | 309 if (ready_to_run_namespaces_.empty()) { |
315 // Exit when shutdown is set and no more tasks are pending. | 310 // Exit when shutdown is set and no more tasks are pending. |
316 if (shutdown_ && pending_tasks_.empty()) | 311 if (shutdown_) |
317 break; | 312 break; |
318 | |
reveman
2013/12/22 15:49:25
nit: no need to remove this line
sohanjg
2013/12/26 13:38:47
Done.
| |
319 // Wait for more tasks. | 313 // Wait for more tasks. |
320 has_ready_to_run_tasks_cv_.Wait(); | 314 has_ready_to_run_tasks_cv_.Wait(); |
321 continue; | 315 continue; |
322 } | 316 } |
323 | 317 |
324 // Take top priority task from |ready_to_run_tasks_|. | 318 // Take top priority TaskNamespace from |ready_to_run_namespaces_|. |
319 TaskNamespace* task_namespace = ready_to_run_namespaces_.top(); | |
320 ready_to_run_namespaces_.pop(); | |
321 DCHECK(!task_namespace->ready_to_run_tasks.empty()); | |
322 | |
323 // Take top priority task from |ready_to_run_tasks|. | |
325 scoped_refptr<internal::WorkerPoolTask> task( | 324 scoped_refptr<internal::WorkerPoolTask> task( |
326 ready_to_run_tasks_.top()->task()); | 325 task_namespace->ready_to_run_tasks.top()->task()); |
327 ready_to_run_tasks_.pop(); | 326 task_namespace->ready_to_run_tasks.pop(); |
328 | 327 |
329 // Move task from |pending_tasks_| to |running_tasks_|. | 328 if (!task_namespace->ready_to_run_tasks.empty()) |
reveman
2013/12/22 15:49:25
Please add something like this comment here:
// A
sohanjg
2013/12/26 13:38:47
Done.
| |
330 DCHECK(pending_tasks_.contains(task.get())); | 329 ready_to_run_namespaces_.push(task_namespace); |
331 DCHECK(!running_tasks_.contains(task.get())); | 330 |
332 running_tasks_.set(task.get(), pending_tasks_.take_and_erase(task.get())); | 331 // Move task from |pending_tasks| to |running_tasks|. |
332 DCHECK(task_namespace->pending_tasks.contains(task.get())); | |
333 DCHECK(!task_namespace->running_tasks.contains(task.get())); | |
334 | |
reveman
2013/12/22 15:49:25
Please don't add this blank line. The line below s
sohanjg
2013/12/26 13:38:47
Done.
| |
335 task_namespace->running_tasks.set( | |
336 task.get(), | |
337 task_namespace->pending_tasks.take_and_erase(task.get())); | |
333 | 338 |
334 // There may be more work available, so wake up another worker thread. | 339 // There may be more work available, so wake up another worker thread. |
335 has_ready_to_run_tasks_cv_.Signal(); | 340 has_ready_to_run_tasks_cv_.Signal(); |
336 | 341 |
337 // Call WillRun() before releasing |lock_| and running task. | 342 // Call WillRun() before releasing |lock_| and running task. |
338 task->WillRun(); | 343 task->WillRun(); |
339 | 344 |
340 { | 345 { |
341 base::AutoUnlock unlock(lock_); | 346 base::AutoUnlock unlock(lock_); |
342 | |
reveman
2013/12/22 15:49:25
nit: no need to remove this line
sohanjg
2013/12/26 13:38:47
Done.
| |
343 task->RunOnWorkerThread(thread_index); | 347 task->RunOnWorkerThread(thread_index); |
344 } | 348 } |
345 | 349 |
346 // This will mark task as finished running. | 350 // This will mark task as finished running. |
347 task->DidRun(); | 351 task->DidRun(); |
348 | 352 |
349 // Now iterate over all dependents to remove dependency and check | 353 // Now iterate over all dependents to remove dependency and check |
350 // if they are ready to run. | 354 // if they are ready to run. |
351 scoped_ptr<internal::GraphNode> node = running_tasks_.take_and_erase( | 355 scoped_ptr<internal::GraphNode> node = |
352 task.get()); | 356 task_namespace->running_tasks.take_and_erase(task.get()); |
353 if (node) { | 357 if (node) { |
354 for (internal::GraphNode::Vector::const_iterator it = | 358 for (internal::GraphNode::Vector::const_iterator it = |
355 node->dependents().begin(); | 359 node->dependents().begin(); |
356 it != node->dependents().end(); ++it) { | 360 it != node->dependents().end(); ++it) { |
357 internal::GraphNode* dependent_node = *it; | 361 internal::GraphNode* dependent_node = *it; |
358 | 362 |
359 dependent_node->remove_dependency(); | 363 dependent_node->remove_dependency(); |
360 // Task is ready if it has no dependencies. Add it to | 364 // Task is ready if it has no dependencies. Add it to |
361 // |ready_to_run_tasks_|. | 365 // |ready_to_run_tasks|. |
362 if (!dependent_node->num_dependencies()) | 366 if (!dependent_node->num_dependencies()) { |
363 ready_to_run_tasks_.push(dependent_node); | 367 bool was_empty = task_namespace->ready_to_run_tasks.empty(); |
368 task_namespace->ready_to_run_tasks.push(dependent_node); | |
reveman
2013/12/22 15:49:25
Please add something like this comment here:
// T
sohanjg
2013/12/26 13:38:47
Done.
| |
369 if (was_empty) | |
370 ready_to_run_namespaces_.push(task_namespace); | |
371 } | |
364 } | 372 } |
365 } | 373 } |
366 | 374 // Finally add task to |completed_tasks|. |
reveman
2013/12/22 15:49:25
nit: please keep the blank line between "}" and "/
sohanjg
2013/12/26 13:38:47
Done.
| |
367 // Finally add task to |completed_tasks_|. | 375 task_namespace->completed_tasks.push_back(task); |
368 completed_tasks_.push_back(task); | |
369 } | 376 } |
370 | 377 |
371 // We noticed we should exit. Wake up the next worker so it knows it should | 378 // 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). | 379 // exit as well (because the Shutdown() code only signals once). |
373 has_ready_to_run_tasks_cv_.Signal(); | 380 has_ready_to_run_tasks_cv_.Signal(); |
374 } | 381 } |
375 | 382 |
383 class CC_EXPORT CompositorRasterTaskGraphRunner | |
384 : public TaskGraphRunner { | |
385 public: | |
386 CompositorRasterTaskGraphRunner() : TaskGraphRunner( | |
reveman
2013/12/22 15:49:25
nit: indent 1 space relative to "public:"
sohanjg
2013/12/26 13:38:47
Done.
| |
387 switches::GetNumRasterThreads(), "CompositorRaster") {} | |
reveman
2013/12/22 15:49:25
nit: indent 4 spaces relative to "CompositorRas.."
sohanjg
2013/12/26 13:38:47
Done.
| |
388 }; | |
389 | |
390 base::LazyInstance<CompositorRasterTaskGraphRunner> | |
391 g_task_graph_runner = LAZY_INSTANCE_INITIALIZER; | |
392 | |
393 } // namespace | |
394 | |
395 namespace internal { | |
396 | |
397 WorkerPoolTask::WorkerPoolTask() | |
398 : did_schedule_(false), | |
399 did_run_(false), | |
400 did_complete_(false) { | |
401 } | |
402 | |
403 WorkerPoolTask::~WorkerPoolTask() { | |
404 DCHECK_EQ(did_schedule_, did_complete_); | |
405 DCHECK(!did_run_ || did_schedule_); | |
406 DCHECK(!did_run_ || did_complete_); | |
407 } | |
408 | |
409 void WorkerPoolTask::DidSchedule() { | |
410 DCHECK(!did_complete_); | |
411 did_schedule_ = true; | |
412 } | |
413 | |
414 void WorkerPoolTask::WillRun() { | |
415 DCHECK(did_schedule_); | |
416 DCHECK(!did_complete_); | |
417 DCHECK(!did_run_); | |
418 } | |
419 | |
420 void WorkerPoolTask::DidRun() { | |
421 did_run_ = true; | |
422 } | |
423 | |
424 void WorkerPoolTask::WillComplete() { | |
425 DCHECK(!did_complete_); | |
426 } | |
427 | |
428 void WorkerPoolTask::DidComplete() { | |
429 DCHECK(did_schedule_); | |
430 DCHECK(!did_complete_); | |
431 did_complete_ = true; | |
432 } | |
433 | |
434 bool WorkerPoolTask::HasFinishedRunning() const { | |
435 return did_run_; | |
436 } | |
437 | |
438 bool WorkerPoolTask::HasCompleted() const { | |
439 return did_complete_; | |
440 } | |
441 | |
442 GraphNode::GraphNode(internal::WorkerPoolTask* task, unsigned priority) | |
443 : task_(task), | |
444 priority_(priority), | |
445 num_dependencies_(0) { | |
446 } | |
447 | |
448 GraphNode::~GraphNode() { | |
449 } | |
450 | |
451 } // namespace internal | |
452 | |
453 | |
reveman
2013/12/22 15:49:25
nit: only one blank line between "} // namesp.."
sohanjg
2013/12/26 13:38:47
Done.
| |
376 WorkerPool::WorkerPool(size_t num_threads, | 454 WorkerPool::WorkerPool(size_t num_threads, |
377 const std::string& thread_name_prefix) | 455 const std::string& thread_name_prefix) |
378 : in_dispatch_completion_callbacks_(false), | 456 : in_dispatch_completion_callbacks_(false) { |
379 inner_(make_scoped_ptr(new Inner(num_threads, thread_name_prefix))) { | 457 g_task_graph_runner.Pointer()->Register(this); |
380 } | 458 } |
381 | 459 |
382 WorkerPool::~WorkerPool() { | 460 WorkerPool::~WorkerPool() { |
461 g_task_graph_runner.Pointer()->Unregister(this); | |
383 } | 462 } |
384 | 463 |
385 void WorkerPool::Shutdown() { | 464 void WorkerPool::Shutdown() { |
386 TRACE_EVENT0("cc", "WorkerPool::Shutdown"); | 465 TRACE_EVENT0("cc", "WorkerPool::Shutdown"); |
387 | 466 |
388 DCHECK(!in_dispatch_completion_callbacks_); | 467 DCHECK(!in_dispatch_completion_callbacks_); |
389 | 468 g_task_graph_runner.Pointer()->Shutdown(); |
reveman
2013/12/22 15:49:25
nit: no need to remove this blank line
sohanjg
2013/12/26 13:38:47
Done.
| |
390 inner_->Shutdown(); | |
391 } | 469 } |
392 | 470 |
393 void WorkerPool::CheckForCompletedTasks() { | 471 void WorkerPool::CheckForCompletedTasks() { |
394 TRACE_EVENT0("cc", "WorkerPool::CheckForCompletedTasks"); | 472 TRACE_EVENT0("cc", "WorkerPool::CheckForCompletedTasks"); |
395 | 473 |
396 DCHECK(!in_dispatch_completion_callbacks_); | 474 DCHECK(!in_dispatch_completion_callbacks_); |
397 | 475 |
398 TaskVector completed_tasks; | 476 TaskVector completed_tasks; |
399 inner_->CollectCompletedTasks(&completed_tasks); | 477 g_task_graph_runner.Pointer()->CollectCompletedTasks(this, &completed_tasks); |
400 ProcessCompletedTasks(completed_tasks); | 478 ProcessCompletedTasks(completed_tasks); |
401 } | 479 } |
402 | 480 |
403 void WorkerPool::ProcessCompletedTasks( | 481 void WorkerPool::ProcessCompletedTasks( |
404 const TaskVector& completed_tasks) { | 482 const TaskVector& completed_tasks) { |
405 TRACE_EVENT1("cc", "WorkerPool::ProcessCompletedTasks", | 483 TRACE_EVENT1("cc", "WorkerPool::ProcessCompletedTasks", |
406 "completed_task_count", completed_tasks.size()); | 484 "completed_task_count", completed_tasks.size()); |
407 | 485 |
408 // Worker pool instance is not reentrant while processing completed tasks. | 486 // Worker pool instance is not reentrant while processing completed tasks. |
409 in_dispatch_completion_callbacks_ = true; | 487 in_dispatch_completion_callbacks_ = true; |
410 | 488 |
411 for (TaskVector::const_iterator it = completed_tasks.begin(); | 489 for (TaskVector::const_iterator it = completed_tasks.begin(); |
412 it != completed_tasks.end(); | 490 it != completed_tasks.end(); |
413 ++it) { | 491 ++it) { |
414 internal::WorkerPoolTask* task = it->get(); | 492 internal::WorkerPoolTask* task = it->get(); |
415 | 493 |
416 task->WillComplete(); | 494 task->WillComplete(); |
417 task->CompleteOnOriginThread(); | 495 task->CompleteOnOriginThread(); |
418 task->DidComplete(); | 496 task->DidComplete(); |
419 } | 497 } |
420 | 498 |
421 in_dispatch_completion_callbacks_ = false; | 499 in_dispatch_completion_callbacks_ = false; |
422 } | 500 } |
423 | 501 |
424 void WorkerPool::SetTaskGraph(TaskGraph* graph) { | 502 void WorkerPool::SetTaskGraph(TaskGraph* graph) { |
425 TRACE_EVENT1("cc", "WorkerPool::SetTaskGraph", | 503 TRACE_EVENT1("cc", "WorkerPool::SetTaskGraph", |
426 "num_tasks", graph->size()); | 504 "num_tasks", graph->size()); |
427 | 505 |
428 DCHECK(!in_dispatch_completion_callbacks_); | 506 DCHECK(!in_dispatch_completion_callbacks_); |
429 | 507 g_task_graph_runner.Pointer()->SetTaskGraph(this, graph); |
reveman
2013/12/22 15:49:25
nit: no need to remove this blank line
sohanjg
2013/12/26 13:38:47
Done.
| |
430 inner_->SetTaskGraph(graph); | |
431 } | 508 } |
432 | 509 |
433 } // namespace cc | 510 } // namespace cc |
OLD | NEW |