| Index: cc/resources/task_graph_runner.cc
|
| diff --git a/cc/resources/worker_pool.cc b/cc/resources/task_graph_runner.cc
|
| similarity index 51%
|
| rename from cc/resources/worker_pool.cc
|
| rename to cc/resources/task_graph_runner.cc
|
| index aa1a948ec0a2638e5d37a13e184eb4e80b549def..01faeefa68ff077f21d3059290374f979c906ab5 100644
|
| --- a/cc/resources/worker_pool.cc
|
| +++ b/cc/resources/task_graph_runner.cc
|
| @@ -2,135 +2,63 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "cc/resources/worker_pool.h"
|
| +#include "cc/resources/task_graph_runner.h"
|
|
|
| #include <algorithm>
|
|
|
| -#include "base/bind.h"
|
| #include "base/containers/hash_tables.h"
|
| #include "base/debug/trace_event.h"
|
| -#include "base/lazy_instance.h"
|
| -#include "base/memory/linked_ptr.h"
|
| #include "base/strings/stringprintf.h"
|
| -#include "base/synchronization/condition_variable.h"
|
| -#include "base/threading/simple_thread.h"
|
| #include "base/threading/thread_restrictions.h"
|
| -#include "cc/base/scoped_ptr_deque.h"
|
|
|
| namespace cc {
|
| +namespace internal {
|
|
|
| -namespace {
|
| -
|
| -// TaskGraphRunners can process task graphs from multiple
|
| -// workerpool instances. All members are guarded by |lock_|.
|
| -class TaskGraphRunner : public base::DelegateSimpleThread::Delegate {
|
| - public:
|
| - typedef WorkerPool::TaskGraph TaskGraph;
|
| - typedef WorkerPool::TaskVector TaskVector;
|
| -
|
| - TaskGraphRunner(size_t num_threads, const std::string& thread_name_prefix);
|
| - virtual ~TaskGraphRunner();
|
| -
|
| - void Register(const WorkerPool* worker_pool);
|
| - void Unregister(const WorkerPool* worker_pool);
|
| - // Schedule running of tasks in |graph|. Tasks previously scheduled but
|
| - // no longer needed will be canceled unless already running. Canceled
|
| - // tasks are moved to |completed_tasks| without being run. The result
|
| - // is that once scheduled, a task is guaranteed to end up in the
|
| - // |completed_tasks| queue even if it later get canceled by another
|
| - // call to SetTaskGraph().
|
| - void SetTaskGraph(const WorkerPool* worker_pool, TaskGraph* graph);
|
| -
|
| - // Wait for all scheduled tasks to finish running.
|
| - void WaitForTasksToFinishRunning(const WorkerPool* worker_pool);
|
| -
|
| - // Collect all completed tasks in |completed_tasks|.
|
| - void CollectCompletedTasks(const WorkerPool* worker_pool,
|
| - TaskVector* completed_tasks);
|
| -
|
| - private:
|
| - static bool CompareTaskPriority(const internal::GraphNode* a,
|
| - const internal::GraphNode* b) {
|
| - // In this system, numerically lower priority is run first.
|
| - if (a->priority() != b->priority())
|
| - return a->priority() > b->priority();
|
| -
|
| - // Run task with most dependents first when priority is the same.
|
| - return a->dependents().size() < b->dependents().size();
|
| - }
|
| -
|
| - struct TaskNamespace {
|
| - // This set contains all pending tasks.
|
| - TaskGraph pending_tasks;
|
| - // This set contains all currently running tasks.
|
| - TaskGraph running_tasks;
|
| - // Completed tasks not yet collected by origin thread.
|
| - TaskVector completed_tasks;
|
| - // Ordered set of tasks that are ready to run.
|
| - internal::GraphNode::Vector ready_to_run_tasks;
|
| - };
|
| -
|
| - static bool CompareTaskNamespacePriority(const TaskNamespace* a,
|
| - const TaskNamespace* b) {
|
| - DCHECK(!a->ready_to_run_tasks.empty());
|
| - DCHECK(!b->ready_to_run_tasks.empty());
|
| -
|
| - // Compare based on task priority of the ready_to_run_tasks heap
|
| - // .front() will hold the max element of the heap,
|
| - // except after pop_heap, when max element is moved to .back().
|
| - return CompareTaskPriority(a->ready_to_run_tasks.front(),
|
| - b->ready_to_run_tasks.front());
|
| - }
|
| -
|
| - typedef std::map<const WorkerPool*, linked_ptr<TaskNamespace> >
|
| - TaskNamespaceMap;
|
| -
|
| - // Overridden from base::DelegateSimpleThread:
|
| - virtual void Run() OVERRIDE;
|
| -
|
| - inline bool has_finished_running_tasks(TaskNamespace* task_namespace) {
|
| - return (task_namespace->pending_tasks.empty() &&
|
| - task_namespace->running_tasks.empty());
|
| - }
|
| +Task::Task()
|
| + : did_schedule_(false),
|
| + did_run_(false) {
|
| +}
|
|
|
| - // This lock protects all members of this class except
|
| - // |worker_pool_on_origin_thread_|. Do not read or modify anything
|
| - // without holding this lock. Do not block while holding this lock.
|
| - mutable base::Lock lock_;
|
| +Task::~Task() {
|
| + DCHECK(!did_run_ || did_schedule_);
|
| +}
|
|
|
| - // Condition variable that is waited on by worker threads until new
|
| - // tasks are ready to run or shutdown starts.
|
| - base::ConditionVariable has_ready_to_run_tasks_cv_;
|
| +void Task::DidSchedule() {
|
| + did_schedule_ = true;
|
| +}
|
|
|
| - // Condition variable that is waited on by origin threads until a
|
| - // namespace has finished running all associated tasks.
|
| - base::ConditionVariable has_namespaces_with_finished_running_tasks_cv_;
|
| +void Task::WillRun() {
|
| + DCHECK(did_schedule_);
|
| + DCHECK(!did_run_);
|
| +}
|
|
|
| - // Provides each running thread loop with a unique index. First thread
|
| - // loop index is 0.
|
| - unsigned next_thread_index_;
|
| +void Task::DidRun() {
|
| + did_run_ = true;
|
| +}
|
|
|
| - // Set during shutdown. Tells workers to exit when no more tasks
|
| - // are pending.
|
| - bool shutdown_;
|
| +bool Task::HasFinishedRunning() const {
|
| + return did_run_;
|
| +}
|
|
|
| - // This set contains all registered namespaces.
|
| - TaskNamespaceMap namespaces_;
|
| +GraphNode::GraphNode(Task* task, unsigned priority)
|
| + : task_(task),
|
| + priority_(priority),
|
| + num_dependencies_(0) {
|
| +}
|
|
|
| - // Ordered set of task namespaces that have ready to run tasks.
|
| - std::vector<TaskNamespace*> ready_to_run_namespaces_;
|
| +GraphNode::~GraphNode() {}
|
|
|
| - ScopedPtrDeque<base::DelegateSimpleThread> workers_;
|
| +TaskGraphRunner::TaskNamespace::TaskNamespace() {}
|
|
|
| - DISALLOW_COPY_AND_ASSIGN(TaskGraphRunner);
|
| -};
|
| +TaskGraphRunner::TaskNamespace::~TaskNamespace() {}
|
|
|
| TaskGraphRunner::TaskGraphRunner(
|
| size_t num_threads, const std::string& thread_name_prefix)
|
| : lock_(),
|
| has_ready_to_run_tasks_cv_(&lock_),
|
| has_namespaces_with_finished_running_tasks_cv_(&lock_),
|
| - next_thread_index_(0),
|
| + next_namespace_id_(1),
|
| + next_thread_index_(0u),
|
| shutdown_(false) {
|
| base::AutoLock lock(lock_);
|
|
|
| @@ -167,41 +95,30 @@ TaskGraphRunner::~TaskGraphRunner() {
|
|
|
| while (workers_.size()) {
|
| scoped_ptr<base::DelegateSimpleThread> worker = workers_.take_front();
|
| - // http://crbug.com/240453 - Join() is considered IO and will block this
|
| - // thread. See also http://crbug.com/239423 for further ideas.
|
| + // Join() is considered IO and will block this thread.
|
| base::ThreadRestrictions::ScopedAllowIO allow_io;
|
| worker->Join();
|
| }
|
| }
|
|
|
| -void TaskGraphRunner::Register(const WorkerPool* worker_pool) {
|
| - base::AutoLock lock(lock_);
|
| -
|
| - DCHECK(namespaces_.find(worker_pool) == namespaces_.end());
|
| - linked_ptr<TaskNamespace> task_set = make_linked_ptr(new TaskNamespace());
|
| - namespaces_[worker_pool] = task_set;
|
| -}
|
| -
|
| -void TaskGraphRunner::Unregister(const WorkerPool* worker_pool) {
|
| +NamespaceToken TaskGraphRunner::GetNamespaceToken() {
|
| base::AutoLock lock(lock_);
|
|
|
| - DCHECK(namespaces_.find(worker_pool) != namespaces_.end());
|
| - DCHECK_EQ(0u, namespaces_[worker_pool]->pending_tasks.size());
|
| - DCHECK_EQ(0u, namespaces_[worker_pool]->ready_to_run_tasks.size());
|
| - DCHECK_EQ(0u, namespaces_[worker_pool]->running_tasks.size());
|
| - DCHECK_EQ(0u, namespaces_[worker_pool]->completed_tasks.size());
|
| -
|
| - namespaces_.erase(worker_pool);
|
| + NamespaceToken token(next_namespace_id_++);
|
| + DCHECK(namespaces_.find(token.id_) == namespaces_.end());
|
| + return token;
|
| }
|
|
|
| -void TaskGraphRunner::WaitForTasksToFinishRunning(
|
| - const WorkerPool* worker_pool) {
|
| +void TaskGraphRunner::WaitForTasksToFinishRunning(NamespaceToken token) {
|
| base::AutoLock lock(lock_);
|
|
|
| - DCHECK(namespaces_.find(worker_pool) != namespaces_.end());
|
| - TaskNamespace* task_namespace = namespaces_[worker_pool].get();
|
| + DCHECK(token.IsValid());
|
| + TaskNamespaceMap::iterator it = namespaces_.find(token.id_);
|
| + if (it == namespaces_.end())
|
| + return;
|
|
|
| - while (!has_finished_running_tasks(task_namespace))
|
| + TaskNamespace* task_namespace = it->second;
|
| + while (!HasFinishedRunningTasksInNamespace(task_namespace))
|
| has_namespaces_with_finished_running_tasks_cv_.Wait();
|
|
|
| // There may be other namespaces that have finished running
|
| @@ -209,8 +126,9 @@ void TaskGraphRunner::WaitForTasksToFinishRunning(
|
| has_namespaces_with_finished_running_tasks_cv_.Signal();
|
| }
|
|
|
| -void TaskGraphRunner::SetTaskGraph(const WorkerPool* worker_pool,
|
| - TaskGraph* graph) {
|
| +void TaskGraphRunner::SetTaskGraph(NamespaceToken token, TaskGraph* graph) {
|
| + DCHECK(token.IsValid());
|
| +
|
| TaskGraph new_pending_tasks;
|
| TaskGraph new_running_tasks;
|
|
|
| @@ -220,22 +138,25 @@ void TaskGraphRunner::SetTaskGraph(const WorkerPool* worker_pool,
|
| base::AutoLock lock(lock_);
|
|
|
| DCHECK(!shutdown_);
|
| - DCHECK(namespaces_.find(worker_pool) != namespaces_.end());
|
| - TaskNamespace* task_namespace = namespaces_[worker_pool].get();
|
| +
|
| + scoped_ptr<TaskNamespace> task_namespace = namespaces_.take_and_erase(
|
| + token.id_);
|
| +
|
| + // Create task namespace if it doesn't exist.
|
| + if (!task_namespace)
|
| + task_namespace.reset(new TaskNamespace);
|
|
|
| // First remove all completed tasks from |new_pending_tasks| and
|
| // adjust number of dependencies.
|
| - for (TaskVector::iterator it = task_namespace->completed_tasks.begin();
|
| + for (Task::Vector::iterator it = task_namespace->completed_tasks.begin();
|
| it != task_namespace->completed_tasks.end(); ++it) {
|
| - internal::WorkerPoolTask* task = it->get();
|
| + Task* task = it->get();
|
|
|
| - scoped_ptr<internal::GraphNode> node = new_pending_tasks.take_and_erase(
|
| - task);
|
| + scoped_ptr<GraphNode> node = new_pending_tasks.take_and_erase(task);
|
| if (node) {
|
| - for (internal::GraphNode::Vector::const_iterator it =
|
| - node->dependents().begin();
|
| + for (GraphNode::Vector::const_iterator it = node->dependents().begin();
|
| it != node->dependents().end(); ++it) {
|
| - internal::GraphNode* dependent_node = *it;
|
| + GraphNode* dependent_node = *it;
|
| dependent_node->remove_dependency();
|
| }
|
| }
|
| @@ -244,7 +165,7 @@ void TaskGraphRunner::SetTaskGraph(const WorkerPool* worker_pool,
|
| // Build new running task set.
|
| for (TaskGraph::iterator it = task_namespace->running_tasks.begin();
|
| it != task_namespace->running_tasks.end(); ++it) {
|
| - internal::WorkerPoolTask* task = it->first;
|
| + Task* task = it->first;
|
| // Transfer scheduled task value from |new_pending_tasks| to
|
| // |new_running_tasks| if currently running. Value must be set to
|
| // NULL if |new_pending_tasks| doesn't contain task. This does
|
| @@ -256,9 +177,9 @@ void TaskGraphRunner::SetTaskGraph(const WorkerPool* worker_pool,
|
| task_namespace->ready_to_run_tasks.clear();
|
| for (TaskGraph::iterator it = new_pending_tasks.begin();
|
| it != new_pending_tasks.end(); ++it) {
|
| - internal::WorkerPoolTask* task = it->first;
|
| + Task* task = it->first;
|
| DCHECK(task);
|
| - internal::GraphNode* node = it->second;
|
| + GraphNode* node = it->second;
|
|
|
| // Completed tasks should not exist in |new_pending_tasks|.
|
| DCHECK(!task->HasFinishedRunning());
|
| @@ -301,12 +222,19 @@ void TaskGraphRunner::SetTaskGraph(const WorkerPool* worker_pool,
|
| (task_namespace->pending_tasks.empty() ||
|
| !task_namespace->running_tasks.empty()));
|
|
|
| + // Add task namespace if not empty.
|
| + if (!task_namespace->pending_tasks.empty() ||
|
| + !task_namespace->running_tasks.empty() ||
|
| + !task_namespace->completed_tasks.empty()) {
|
| + namespaces_.set(token.id_, task_namespace.Pass());
|
| + }
|
| +
|
| // Build new "ready to run" task namespaces queue.
|
| ready_to_run_namespaces_.clear();
|
| for (TaskNamespaceMap::iterator it = namespaces_.begin();
|
| it != namespaces_.end(); ++it) {
|
| if (!it->second->ready_to_run_tasks.empty())
|
| - ready_to_run_namespaces_.push_back(it->second.get());
|
| + ready_to_run_namespaces_.push_back(it->second);
|
| }
|
|
|
| // Rearrange the task namespaces in |ready_to_run_namespaces_|
|
| @@ -322,12 +250,27 @@ void TaskGraphRunner::SetTaskGraph(const WorkerPool* worker_pool,
|
| }
|
|
|
| void TaskGraphRunner::CollectCompletedTasks(
|
| - const WorkerPool* worker_pool, TaskVector* completed_tasks) {
|
| + NamespaceToken token, Task::Vector* completed_tasks) {
|
| base::AutoLock lock(lock_);
|
|
|
| + DCHECK(token.IsValid());
|
| + TaskNamespaceMap::iterator it = namespaces_.find(token.id_);
|
| + if (it == namespaces_.end())
|
| + return;
|
| +
|
| + TaskNamespace* task_namespace = it->second;
|
| +
|
| DCHECK_EQ(0u, completed_tasks->size());
|
| - DCHECK(namespaces_.find(worker_pool) != namespaces_.end());
|
| - completed_tasks->swap(namespaces_[worker_pool]->completed_tasks);
|
| + completed_tasks->swap(task_namespace->completed_tasks);
|
| + if (!HasFinishedRunningTasksInNamespace(task_namespace))
|
| + return;
|
| +
|
| + // Remove namespace if finished running tasks.
|
| + DCHECK_EQ(0u, task_namespace->pending_tasks.size());
|
| + DCHECK_EQ(0u, task_namespace->running_tasks.size());
|
| + DCHECK_EQ(0u, task_namespace->completed_tasks.size());
|
| + DCHECK_EQ(0u, task_namespace->ready_to_run_tasks.size());
|
| + namespaces_.erase(it);
|
| }
|
|
|
| void TaskGraphRunner::Run() {
|
| @@ -359,7 +302,7 @@ void TaskGraphRunner::Run() {
|
| std::pop_heap(task_namespace->ready_to_run_tasks.begin(),
|
| task_namespace->ready_to_run_tasks.end(),
|
| CompareTaskPriority);
|
| - scoped_refptr<internal::WorkerPoolTask> task(
|
| + scoped_refptr<Task> task(
|
| task_namespace->ready_to_run_tasks.back()->task());
|
| task_namespace->ready_to_run_tasks.pop_back();
|
|
|
| @@ -396,15 +339,14 @@ void TaskGraphRunner::Run() {
|
|
|
| // Now iterate over all dependents to remove dependency and check
|
| // if they are ready to run.
|
| - scoped_ptr<internal::GraphNode> node =
|
| + scoped_ptr<GraphNode> node =
|
| task_namespace->running_tasks.take_and_erase(task.get());
|
| if (node) {
|
| bool ready_to_run_namespaces_has_heap_properties = true;
|
|
|
| - for (internal::GraphNode::Vector::const_iterator it =
|
| - node->dependents().begin();
|
| + for (GraphNode::Vector::const_iterator it = node->dependents().begin();
|
| it != node->dependents().end(); ++it) {
|
| - internal::GraphNode* dependent_node = *it;
|
| + GraphNode* dependent_node = *it;
|
|
|
| dependent_node->remove_dependency();
|
| // Task is ready if it has no dependencies. Add it to
|
| @@ -442,7 +384,7 @@ void TaskGraphRunner::Run() {
|
| task_namespace->completed_tasks.push_back(task);
|
|
|
| // If namespace has finished running all tasks, wake up origin thread.
|
| - if (has_finished_running_tasks(task_namespace))
|
| + if (HasFinishedRunningTasksInNamespace(task_namespace))
|
| has_namespaces_with_finished_running_tasks_cv_.Signal();
|
| }
|
|
|
| @@ -451,151 +393,5 @@ void TaskGraphRunner::Run() {
|
| has_ready_to_run_tasks_cv_.Signal();
|
| }
|
|
|
| -class CompositorRasterTaskGraphRunner
|
| - : public TaskGraphRunner {
|
| - public:
|
| - CompositorRasterTaskGraphRunner() : TaskGraphRunner(
|
| - WorkerPool::GetNumRasterThreads(), "CompositorRaster") {
|
| - }
|
| -};
|
| -
|
| -base::LazyInstance<CompositorRasterTaskGraphRunner>
|
| - g_task_graph_runner = LAZY_INSTANCE_INITIALIZER;
|
| -
|
| -const int kDefaultNumRasterThreads = 1;
|
| -
|
| -int g_num_raster_threads = 0;
|
| -
|
| -} // namespace
|
| -
|
| -namespace internal {
|
| -
|
| -WorkerPoolTask::WorkerPoolTask()
|
| - : did_schedule_(false),
|
| - did_run_(false),
|
| - did_complete_(false) {
|
| -}
|
| -
|
| -WorkerPoolTask::~WorkerPoolTask() {
|
| - DCHECK_EQ(did_schedule_, did_complete_);
|
| - DCHECK(!did_run_ || did_schedule_);
|
| - DCHECK(!did_run_ || did_complete_);
|
| -}
|
| -
|
| -void WorkerPoolTask::DidSchedule() {
|
| - DCHECK(!did_complete_);
|
| - did_schedule_ = true;
|
| -}
|
| -
|
| -void WorkerPoolTask::WillRun() {
|
| - DCHECK(did_schedule_);
|
| - DCHECK(!did_complete_);
|
| - DCHECK(!did_run_);
|
| -}
|
| -
|
| -void WorkerPoolTask::DidRun() {
|
| - did_run_ = true;
|
| -}
|
| -
|
| -void WorkerPoolTask::WillComplete() {
|
| - DCHECK(!did_complete_);
|
| -}
|
| -
|
| -void WorkerPoolTask::DidComplete() {
|
| - DCHECK(did_schedule_);
|
| - DCHECK(!did_complete_);
|
| - did_complete_ = true;
|
| -}
|
| -
|
| -bool WorkerPoolTask::HasFinishedRunning() const {
|
| - return did_run_;
|
| -}
|
| -
|
| -bool WorkerPoolTask::HasCompleted() const {
|
| - return did_complete_;
|
| -}
|
| -
|
| -GraphNode::GraphNode(internal::WorkerPoolTask* task, unsigned priority)
|
| - : task_(task),
|
| - priority_(priority),
|
| - num_dependencies_(0) {
|
| -}
|
| -
|
| -GraphNode::~GraphNode() {
|
| -}
|
| -
|
| } // namespace internal
|
| -
|
| -// static
|
| -void WorkerPool::SetNumRasterThreads(int num_threads) {
|
| - DCHECK_LT(0, num_threads);
|
| - DCHECK_EQ(0, g_num_raster_threads);
|
| -
|
| - g_num_raster_threads = num_threads;
|
| -}
|
| -
|
| -// static
|
| -int WorkerPool::GetNumRasterThreads() {
|
| - if (!g_num_raster_threads)
|
| - g_num_raster_threads = kDefaultNumRasterThreads;
|
| -
|
| - return g_num_raster_threads;
|
| -}
|
| -
|
| -WorkerPool::WorkerPool() : in_dispatch_completion_callbacks_(false) {
|
| - g_task_graph_runner.Pointer()->Register(this);
|
| -}
|
| -
|
| -WorkerPool::~WorkerPool() {
|
| - g_task_graph_runner.Pointer()->Unregister(this);
|
| -}
|
| -
|
| -void WorkerPool::Shutdown() {
|
| - TRACE_EVENT0("cc", "WorkerPool::Shutdown");
|
| -
|
| - DCHECK(!in_dispatch_completion_callbacks_);
|
| -
|
| - g_task_graph_runner.Pointer()->WaitForTasksToFinishRunning(this);
|
| -}
|
| -
|
| -void WorkerPool::SetTaskGraph(TaskGraph* graph) {
|
| - TRACE_EVENT1("cc", "WorkerPool::SetTaskGraph",
|
| - "num_tasks", graph->size());
|
| -
|
| - DCHECK(!in_dispatch_completion_callbacks_);
|
| -
|
| - g_task_graph_runner.Pointer()->SetTaskGraph(this, graph);
|
| -}
|
| -
|
| -void WorkerPool::CheckForCompletedWorkerTasks() {
|
| - TRACE_EVENT0("cc", "WorkerPool::CheckForCompletedWorkerTasks");
|
| -
|
| - DCHECK(!in_dispatch_completion_callbacks_);
|
| -
|
| - TaskVector completed_tasks;
|
| - g_task_graph_runner.Pointer()->CollectCompletedTasks(this, &completed_tasks);
|
| - ProcessCompletedTasks(completed_tasks);
|
| -}
|
| -
|
| -void WorkerPool::ProcessCompletedTasks(
|
| - const TaskVector& completed_tasks) {
|
| - TRACE_EVENT1("cc", "WorkerPool::ProcessCompletedTasks",
|
| - "completed_task_count", completed_tasks.size());
|
| -
|
| - // Worker pool instance is not reentrant while processing completed tasks.
|
| - in_dispatch_completion_callbacks_ = true;
|
| -
|
| - for (TaskVector::const_iterator it = completed_tasks.begin();
|
| - it != completed_tasks.end();
|
| - ++it) {
|
| - internal::WorkerPoolTask* task = it->get();
|
| -
|
| - task->WillComplete();
|
| - task->CompleteOnOriginThread();
|
| - task->DidComplete();
|
| - }
|
| -
|
| - in_dispatch_completion_callbacks_ = false;
|
| -}
|
| -
|
| } // namespace cc
|
|
|