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