| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef CONTENT_RENDERER_RASTER_WORKER_POOL_H_ | 5 #ifndef CONTENT_RENDERER_RASTER_WORKER_POOL_H_ |
| 6 #define CONTENT_RENDERER_RASTER_WORKER_POOL_H_ | 6 #define CONTENT_RENDERER_RASTER_WORKER_POOL_H_ |
| 7 | 7 |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/containers/hash_tables.h" | 9 #include "base/containers/hash_tables.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
| 12 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
| 13 #include "base/synchronization/condition_variable.h" | 13 #include "base/synchronization/condition_variable.h" |
| 14 #include "base/task_runner.h" | 14 #include "base/task_runner.h" |
| 15 #include "base/threading/simple_thread.h" | 15 #include "base/threading/simple_thread.h" |
| 16 #include "cc/raster/task_category.h" |
| 16 #include "cc/raster/task_graph_runner.h" | 17 #include "cc/raster/task_graph_runner.h" |
| 17 #include "cc/raster/task_graph_work_queue.h" | 18 #include "cc/raster/task_graph_work_queue.h" |
| 18 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
| 19 | 20 |
| 20 namespace content { | 21 namespace content { |
| 21 | 22 |
| 22 // A pool of threads used to run raster work. | 23 // A pool of threads used to run raster work. |
| 23 // Work can be scheduled on the threads using different interfaces. | 24 // Work can be scheduled on the threads using different interfaces. |
| 24 // The pool itself implements TaskRunner interface and tasks posted via that | 25 // The pool itself implements TaskRunner interface and tasks posted via that |
| 25 // interface might run in parallel. | 26 // interface might run in parallel. |
| 26 // CreateSequencedTaskRunner creates a sequenced task runner that might run in | 27 // CreateSequencedTaskRunner creates a sequenced task runner that might run in |
| 27 // parallel with other instances of sequenced task runners. | 28 // parallel with other instances of sequenced task runners. |
| 28 // It's also possible to get the underlying TaskGraphRunner to schedule a graph | 29 // It's also possible to get the underlying TaskGraphRunner to schedule a graph |
| 29 // of tasks with their dependencies. | 30 // of tasks with their dependencies. |
| 30 class CONTENT_EXPORT RasterWorkerPool | 31 class CONTENT_EXPORT RasterWorkerPool : public base::TaskRunner, |
| 31 : public base::TaskRunner, | 32 public cc::TaskGraphRunner { |
| 32 public cc::TaskGraphRunner, | |
| 33 public base::DelegateSimpleThread::Delegate { | |
| 34 public: | 33 public: |
| 35 RasterWorkerPool(); | 34 RasterWorkerPool(); |
| 36 | 35 |
| 37 // Overridden from base::TaskRunner: | 36 // Overridden from base::TaskRunner: |
| 38 bool PostDelayedTask(const tracked_objects::Location& from_here, | 37 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 39 const base::Closure& task, | 38 const base::Closure& task, |
| 40 base::TimeDelta delay) override; | 39 base::TimeDelta delay) override; |
| 41 bool RunsTasksOnCurrentThread() const override; | 40 bool RunsTasksOnCurrentThread() const override; |
| 42 | 41 |
| 43 // Overridden from cc::TaskGraphRunner: | 42 // Overridden from cc::TaskGraphRunner: |
| 44 cc::NamespaceToken GetNamespaceToken() override; | 43 cc::NamespaceToken GetNamespaceToken() override; |
| 45 void ScheduleTasks(cc::NamespaceToken token, cc::TaskGraph* graph) override; | 44 void ScheduleTasks(cc::NamespaceToken token, cc::TaskGraph* graph) override; |
| 46 void WaitForTasksToFinishRunning(cc::NamespaceToken token) override; | 45 void WaitForTasksToFinishRunning(cc::NamespaceToken token) override; |
| 47 void CollectCompletedTasks(cc::NamespaceToken token, | 46 void CollectCompletedTasks(cc::NamespaceToken token, |
| 48 cc::Task::Vector* completed_tasks) override; | 47 cc::Task::Vector* completed_tasks) override; |
| 49 | 48 |
| 50 // Overridden from base::DelegateSimpleThread::Delegate: | 49 // Runs a task from one of the provided categories. Categories listed first |
| 51 void Run() override; | 50 // have higher priority. |
| 51 void Run(const std::vector<cc::TaskCategory>& categories); |
| 52 | 52 |
| 53 void FlushForTesting(); | 53 void FlushForTesting(); |
| 54 | 54 |
| 55 // Spawn |num_threads| number of threads and start running work on the | 55 // Spawn |num_threads| number of threads and start running work on the |
| 56 // worker threads. | 56 // worker threads. |
| 57 void Start(int num_threads, | 57 void Start(int num_threads, |
| 58 const base::SimpleThread::Options& thread_options); | 58 const base::SimpleThread::Options& thread_options); |
| 59 | 59 |
| 60 // Finish running all the posted tasks (and nested task posted by those tasks) | 60 // Finish running all the posted tasks (and nested task posted by those tasks) |
| 61 // of all the associated task runners. | 61 // of all the associated task runners. |
| 62 // Once all the tasks are executed the method blocks until the threads are | 62 // Once all the tasks are executed the method blocks until the threads are |
| 63 // terminated. | 63 // terminated. |
| 64 void Shutdown(); | 64 void Shutdown(); |
| 65 | 65 |
| 66 cc::TaskGraphRunner* GetTaskGraphRunner() { return this; } | 66 cc::TaskGraphRunner* GetTaskGraphRunner() { return this; } |
| 67 | 67 |
| 68 // Create a new sequenced task graph runner. | 68 // Create a new sequenced task graph runner. |
| 69 scoped_refptr<base::SequencedTaskRunner> CreateSequencedTaskRunner(); | 69 scoped_refptr<base::SequencedTaskRunner> CreateSequencedTaskRunner(); |
| 70 | 70 |
| 71 protected: | 71 protected: |
| 72 ~RasterWorkerPool() override; | 72 ~RasterWorkerPool() override; |
| 73 | 73 |
| 74 private: | 74 private: |
| 75 class RasterWorkerPoolSequencedTaskRunner; | 75 class RasterWorkerPoolSequencedTaskRunner; |
| 76 friend class RasterWorkerPoolSequencedTaskRunner; | 76 friend class RasterWorkerPoolSequencedTaskRunner; |
| 77 | 77 |
| 78 // Run next task. Caller must acquire |lock_| prior to calling this function. | 78 // Runs a task from one of the provided categories. Categories listed first |
| 79 // Returns true if there was a task available to run. | 79 // have higher priority. Returns false if there were no tasks to run. |
| 80 bool RunTaskWithLockAcquired(); | 80 bool RunTaskWithLockAcquired(const std::vector<cc::TaskCategory>& categories); |
| 81 |
| 82 // Run next task for the given category. Caller must acquire |lock_| prior to |
| 83 // calling this function and make sure at least one task is ready to run. |
| 84 void RunTaskInCategoryWithLockAcquired(cc::TaskCategory category); |
| 81 | 85 |
| 82 // Simple Task for the TaskGraphRunner that wraps a closure. | 86 // Simple Task for the TaskGraphRunner that wraps a closure. |
| 83 // This class is used to schedule TaskRunner tasks on the | 87 // This class is used to schedule TaskRunner tasks on the |
| 84 // |task_graph_runner_|. | 88 // |task_graph_runner_|. |
| 85 class ClosureTask : public cc::Task { | 89 class ClosureTask : public cc::Task { |
| 86 public: | 90 public: |
| 87 explicit ClosureTask(const base::Closure& closure); | 91 explicit ClosureTask(const base::Closure& closure); |
| 88 | 92 |
| 89 // Overridden from cc::Task: | 93 // Overridden from cc::Task: |
| 90 void RunOnWorkerThread() override; | 94 void RunOnWorkerThread() override; |
| 91 | 95 |
| 92 protected: | 96 protected: |
| 93 ~ClosureTask() override; | 97 ~ClosureTask() override; |
| 94 | 98 |
| 95 private: | 99 private: |
| 96 base::Closure closure_; | 100 base::Closure closure_; |
| 97 | 101 |
| 98 DISALLOW_COPY_AND_ASSIGN(ClosureTask); | 102 DISALLOW_COPY_AND_ASSIGN(ClosureTask); |
| 99 }; | 103 }; |
| 100 | 104 |
| 101 void ScheduleTasksWithLockAcquired(cc::NamespaceToken token, | 105 void ScheduleTasksWithLockAcquired(cc::NamespaceToken token, |
| 102 cc::TaskGraph* graph); | 106 cc::TaskGraph* graph); |
| 103 void CollectCompletedTasksWithLockAcquired(cc::NamespaceToken token, | 107 void CollectCompletedTasksWithLockAcquired(cc::NamespaceToken token, |
| 104 cc::Task::Vector* completed_tasks); | 108 cc::Task::Vector* completed_tasks); |
| 105 | 109 |
| 106 // The actual threads where work is done. | 110 // The actual threads where work is done. |
| 107 ScopedVector<base::DelegateSimpleThread> threads_; | 111 std::vector<scoped_ptr<base::SimpleThread>> threads_; |
| 108 | 112 |
| 109 // Lock to exclusively access all the following members that are used to | 113 // Lock to exclusively access all the following members that are used to |
| 110 // implement the TaskRunner and TaskGraphRunner interfaces. | 114 // implement the TaskRunner and TaskGraphRunner interfaces. |
| 111 base::Lock lock_; | 115 base::Lock lock_; |
| 112 // Stores the tasks to be run, sorted by priority. | 116 // Stores the tasks to be run, sorted by priority. |
| 113 cc::TaskGraphWorkQueue work_queue_; | 117 cc::TaskGraphWorkQueue work_queue_; |
| 114 // Namespace used to schedule tasks in the task graph runner. | 118 // Namespace used to schedule tasks in the task graph runner. |
| 115 cc::NamespaceToken namespace_token_; | 119 cc::NamespaceToken namespace_token_; |
| 116 // List of tasks currently queued up for execution. | 120 // List of tasks currently queued up for execution. |
| 117 cc::Task::Vector tasks_; | 121 cc::Task::Vector tasks_; |
| 118 // Graph object used for scheduling tasks. | 122 // Graph object used for scheduling tasks. |
| 119 cc::TaskGraph graph_; | 123 cc::TaskGraph graph_; |
| 120 // Cached vector to avoid allocation when getting the list of complete | 124 // Cached vector to avoid allocation when getting the list of complete |
| 121 // tasks. | 125 // tasks. |
| 122 cc::Task::Vector completed_tasks_; | 126 cc::Task::Vector completed_tasks_; |
| 123 // Condition variable that is waited on by Run() until new tasks are ready to | 127 // Condition variable that is waited on by Run() until new tasks are ready to |
| 124 // run or shutdown starts. | 128 // run or shutdown starts. |
| 125 base::ConditionVariable has_ready_to_run_tasks_cv_; | 129 base::ConditionVariable has_ready_to_run_tasks_cv_; |
| 126 // Condition variable that is waited on by origin threads until a namespace | 130 // Condition variable that is waited on by origin threads until a namespace |
| 127 // has finished running all associated tasks. | 131 // has finished running all associated tasks. |
| 128 base::ConditionVariable has_namespaces_with_finished_running_tasks_cv_; | 132 base::ConditionVariable has_namespaces_with_finished_running_tasks_cv_; |
| 129 // Set during shutdown. Tells Run() to return when no more tasks are pending. | 133 // Set during shutdown. Tells Run() to return when no more tasks are pending. |
| 130 bool shutdown_; | 134 bool shutdown_; |
| 131 }; | 135 }; |
| 132 | 136 |
| 133 } // namespace content | 137 } // namespace content |
| 134 | 138 |
| 135 #endif // CONTENT_RENDERER_RASTER_WORKER_POOL_H_ | 139 #endif // CONTENT_RENDERER_RASTER_WORKER_POOL_H_ |
| OLD | NEW |