| 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" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 | 41 |
| 42 // Overridden from cc::TaskGraphRunner: | 42 // Overridden from cc::TaskGraphRunner: |
| 43 cc::NamespaceToken GetNamespaceToken() override; | 43 cc::NamespaceToken GetNamespaceToken() override; |
| 44 void ScheduleTasks(cc::NamespaceToken token, cc::TaskGraph* graph) override; | 44 void ScheduleTasks(cc::NamespaceToken token, cc::TaskGraph* graph) override; |
| 45 void WaitForTasksToFinishRunning(cc::NamespaceToken token) override; | 45 void WaitForTasksToFinishRunning(cc::NamespaceToken token) override; |
| 46 void CollectCompletedTasks(cc::NamespaceToken token, | 46 void CollectCompletedTasks(cc::NamespaceToken token, |
| 47 cc::Task::Vector* completed_tasks) override; | 47 cc::Task::Vector* completed_tasks) override; |
| 48 | 48 |
| 49 // Runs a task from one of the provided categories. Categories listed first | 49 // Runs a task from one of the provided categories. Categories listed first |
| 50 // have higher priority. | 50 // have higher priority. |
| 51 void Run(const std::vector<cc::TaskCategory>& categories, | 51 void Run(const std::vector<cc::TaskCategory>& categories); |
| 52 base::ConditionVariable* has_ready_to_run_tasks_cv); | |
| 53 | 52 |
| 54 void FlushForTesting(); | 53 void FlushForTesting(); |
| 55 | 54 |
| 56 // 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 |
| 57 // worker threads. | 56 // worker threads. |
| 58 void Start(int num_threads, | 57 void Start(int num_threads, |
| 59 const base::SimpleThread::Options& thread_options); | 58 const base::SimpleThread::Options& thread_options); |
| 60 | 59 |
| 61 // 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) |
| 62 // of all the associated task runners. | 61 // of all the associated task runners. |
| 63 // 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 |
| 64 // terminated. | 63 // terminated. |
| 65 void Shutdown(); | 64 void Shutdown(); |
| 66 | 65 |
| 67 cc::TaskGraphRunner* GetTaskGraphRunner() { return this; } | 66 cc::TaskGraphRunner* GetTaskGraphRunner() { return this; } |
| 68 | 67 |
| 69 // Create a new sequenced task graph runner. | 68 // Create a new sequenced task graph runner. |
| 70 scoped_refptr<base::SequencedTaskRunner> CreateSequencedTaskRunner(); | 69 scoped_refptr<base::SequencedTaskRunner> CreateSequencedTaskRunner(); |
| 71 | 70 |
| 72 protected: | 71 protected: |
| 73 ~RasterWorkerPool() override; | 72 ~RasterWorkerPool() override; |
| 74 | 73 |
| 75 private: | 74 private: |
| 76 class RasterWorkerPoolSequencedTaskRunner; | 75 class RasterWorkerPoolSequencedTaskRunner; |
| 77 friend class RasterWorkerPoolSequencedTaskRunner; | 76 friend class RasterWorkerPoolSequencedTaskRunner; |
| 78 | 77 |
| 78 // Runs a task from one of the provided categories. Categories listed first |
| 79 // have higher priority. Returns false if there were no tasks to run. |
| 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); |
| 85 |
| 79 // Simple Task for the TaskGraphRunner that wraps a closure. | 86 // Simple Task for the TaskGraphRunner that wraps a closure. |
| 80 // This class is used to schedule TaskRunner tasks on the | 87 // This class is used to schedule TaskRunner tasks on the |
| 81 // |task_graph_runner_|. | 88 // |task_graph_runner_|. |
| 82 class ClosureTask : public cc::Task { | 89 class ClosureTask : public cc::Task { |
| 83 public: | 90 public: |
| 84 explicit ClosureTask(const base::Closure& closure); | 91 explicit ClosureTask(const base::Closure& closure); |
| 85 | 92 |
| 86 // Overridden from cc::Task: | 93 // Overridden from cc::Task: |
| 87 void RunOnWorkerThread() override; | 94 void RunOnWorkerThread() override; |
| 88 | 95 |
| 89 protected: | 96 protected: |
| 90 ~ClosureTask() override; | 97 ~ClosureTask() override; |
| 91 | 98 |
| 92 private: | 99 private: |
| 93 base::Closure closure_; | 100 base::Closure closure_; |
| 94 | 101 |
| 95 DISALLOW_COPY_AND_ASSIGN(ClosureTask); | 102 DISALLOW_COPY_AND_ASSIGN(ClosureTask); |
| 96 }; | 103 }; |
| 97 | 104 |
| 98 void ScheduleTasksWithLockAcquired(cc::NamespaceToken token, | 105 void ScheduleTasksWithLockAcquired(cc::NamespaceToken token, |
| 99 cc::TaskGraph* graph); | 106 cc::TaskGraph* graph); |
| 100 void CollectCompletedTasksWithLockAcquired(cc::NamespaceToken token, | 107 void CollectCompletedTasksWithLockAcquired(cc::NamespaceToken token, |
| 101 cc::Task::Vector* completed_tasks); | 108 cc::Task::Vector* completed_tasks); |
| 102 | 109 |
| 103 // Runs a task from one of the provided categories. Categories listed first | |
| 104 // have higher priority. Returns false if there were no tasks to run. | |
| 105 bool RunTaskWithLockAcquired(const std::vector<cc::TaskCategory>& categories); | |
| 106 | |
| 107 // Run next task for the given category. Caller must acquire |lock_| prior to | |
| 108 // calling this function and make sure at least one task is ready to run. | |
| 109 void RunTaskInCategoryWithLockAcquired(cc::TaskCategory category); | |
| 110 | |
| 111 // Helper function which signals worker threads if tasks are ready to run. | |
| 112 void SignalHasReadyToRunTasksWithLockAcquired(); | |
| 113 | |
| 114 // Determines if we should run a new task for the given category. This factors | |
| 115 // in whether a task is available and whether the count of running tasks is | |
| 116 // low enough to start a new one. | |
| 117 bool ShouldRunTaskForCategoryWithLockAcquired(cc::TaskCategory category); | |
| 118 | |
| 119 // The actual threads where work is done. | 110 // The actual threads where work is done. |
| 120 std::vector<scoped_ptr<base::SimpleThread>> threads_; | 111 std::vector<scoped_ptr<base::SimpleThread>> threads_; |
| 121 | 112 |
| 122 // 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 |
| 123 // implement the TaskRunner and TaskGraphRunner interfaces. | 114 // implement the TaskRunner and TaskGraphRunner interfaces. |
| 124 base::Lock lock_; | 115 base::Lock lock_; |
| 125 // Stores the tasks to be run, sorted by priority. | 116 // Stores the tasks to be run, sorted by priority. |
| 126 cc::TaskGraphWorkQueue work_queue_; | 117 cc::TaskGraphWorkQueue work_queue_; |
| 127 // Namespace used to schedule tasks in the task graph runner. | 118 // Namespace used to schedule tasks in the task graph runner. |
| 128 cc::NamespaceToken namespace_token_; | 119 cc::NamespaceToken namespace_token_; |
| 129 // List of tasks currently queued up for execution. | 120 // List of tasks currently queued up for execution. |
| 130 cc::Task::Vector tasks_; | 121 cc::Task::Vector tasks_; |
| 131 // Graph object used for scheduling tasks. | 122 // Graph object used for scheduling tasks. |
| 132 cc::TaskGraph graph_; | 123 cc::TaskGraph graph_; |
| 133 // Cached vector to avoid allocation when getting the list of complete | 124 // Cached vector to avoid allocation when getting the list of complete |
| 134 // tasks. | 125 // tasks. |
| 135 cc::Task::Vector completed_tasks_; | 126 cc::Task::Vector completed_tasks_; |
| 136 // Condition variables for foreground and background tasks. | 127 // Condition variable that is waited on by Run() until new tasks are ready to |
| 137 base::ConditionVariable has_ready_to_run_foreground_tasks_cv_; | 128 // run or shutdown starts. |
| 138 base::ConditionVariable has_ready_to_run_background_tasks_cv_; | 129 base::ConditionVariable has_ready_to_run_tasks_cv_; |
| 139 // 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 |
| 140 // has finished running all associated tasks. | 131 // has finished running all associated tasks. |
| 141 base::ConditionVariable has_namespaces_with_finished_running_tasks_cv_; | 132 base::ConditionVariable has_namespaces_with_finished_running_tasks_cv_; |
| 142 // 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. |
| 143 bool shutdown_; | 134 bool shutdown_; |
| 144 }; | 135 }; |
| 145 | 136 |
| 146 } // namespace content | 137 } // namespace content |
| 147 | 138 |
| 148 #endif // CONTENT_RENDERER_RASTER_WORKER_POOL_H_ | 139 #endif // CONTENT_RENDERER_RASTER_WORKER_POOL_H_ |
| OLD | NEW |