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