Chromium Code Reviews| 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 28 matching lines...) Expand all Loading... | |
| 39 base::TimeDelta delay) override; | 39 base::TimeDelta delay) override; |
| 40 bool RunsTasksOnCurrentThread() const override; | 40 bool RunsTasksOnCurrentThread() const override; |
| 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 the provided category. |
| 50 // have higher priority. | 50 void Run(cc::TaskCategory category); |
| 51 void Run(const std::vector<cc::TaskCategory>& categories); | |
| 52 | 51 |
| 53 void FlushForTesting(); | 52 void FlushForTesting(); |
| 54 | 53 |
| 55 // Spawn |num_threads| number of threads and start running work on the | 54 // Spawn |num_threads| number of threads and start running work on the |
| 56 // worker threads. | 55 // worker threads. |
| 57 void Start(int num_threads, | 56 void Start(int num_threads, |
| 58 const base::SimpleThread::Options& thread_options); | 57 const base::SimpleThread::Options& thread_options); |
| 59 | 58 |
| 60 // Finish running all the posted tasks (and nested task posted by those tasks) | 59 // Finish running all the posted tasks (and nested task posted by those tasks) |
| 61 // of all the associated task runners. | 60 // of all the associated task runners. |
| 62 // Once all the tasks are executed the method blocks until the threads are | 61 // Once all the tasks are executed the method blocks until the threads are |
| 63 // terminated. | 62 // terminated. |
| 64 void Shutdown(); | 63 void Shutdown(); |
| 65 | 64 |
| 66 cc::TaskGraphRunner* GetTaskGraphRunner() { return this; } | 65 cc::TaskGraphRunner* GetTaskGraphRunner() { return this; } |
| 67 | 66 |
| 68 // Create a new sequenced task graph runner. | 67 // Create a new sequenced task graph runner. |
| 69 scoped_refptr<base::SequencedTaskRunner> CreateSequencedTaskRunner(); | 68 scoped_refptr<base::SequencedTaskRunner> CreateSequencedTaskRunner(); |
| 70 | 69 |
| 71 protected: | 70 protected: |
| 72 ~RasterWorkerPool() override; | 71 ~RasterWorkerPool() override; |
| 73 | 72 |
| 74 private: | 73 private: |
| 75 class RasterWorkerPoolSequencedTaskRunner; | 74 class RasterWorkerPoolSequencedTaskRunner; |
| 76 friend class RasterWorkerPoolSequencedTaskRunner; | 75 friend class RasterWorkerPoolSequencedTaskRunner; |
| 77 | 76 |
| 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 | |
| 86 // Simple Task for the TaskGraphRunner that wraps a closure. | 77 // Simple Task for the TaskGraphRunner that wraps a closure. |
| 87 // This class is used to schedule TaskRunner tasks on the | 78 // This class is used to schedule TaskRunner tasks on the |
| 88 // |task_graph_runner_|. | 79 // |task_graph_runner_|. |
| 89 class ClosureTask : public cc::Task { | 80 class ClosureTask : public cc::Task { |
| 90 public: | 81 public: |
| 91 explicit ClosureTask(const base::Closure& closure); | 82 explicit ClosureTask(const base::Closure& closure); |
| 92 | 83 |
| 93 // Overridden from cc::Task: | 84 // Overridden from cc::Task: |
| 94 void RunOnWorkerThread() override; | 85 void RunOnWorkerThread() override; |
| 95 | 86 |
| 96 protected: | 87 protected: |
| 97 ~ClosureTask() override; | 88 ~ClosureTask() override; |
| 98 | 89 |
| 99 private: | 90 private: |
| 100 base::Closure closure_; | 91 base::Closure closure_; |
| 101 | 92 |
| 102 DISALLOW_COPY_AND_ASSIGN(ClosureTask); | 93 DISALLOW_COPY_AND_ASSIGN(ClosureTask); |
| 103 }; | 94 }; |
| 104 | 95 |
| 96 // Helper class used to increment the running task count while a task is | |
| 97 // running. Automatically decrements when this class goes out of scope. | |
| 98 class ScopedTaskCount { | |
| 99 public: | |
| 100 ScopedTaskCount(int* running_task_count) | |
| 101 : running_task_count_(running_task_count) { | |
| 102 (*running_task_count_)++; | |
| 103 } | |
| 104 ~ScopedTaskCount() { | |
| 105 (*running_task_count_)--; | |
| 106 DCHECK_GE(*running_task_count_, 0); | |
| 107 } | |
| 108 | |
| 109 private: | |
| 110 int* const running_task_count_; | |
| 111 }; | |
| 112 | |
| 113 // Run next task for the given category. Caller must acquire |lock_| prior to | |
| 114 // calling this function and make sure at least one task is ready to run. | |
| 115 void RunTaskWithLockAcquired(cc::TaskCategory category); | |
| 116 | |
| 117 // Helper function which signals CVs for each category where tasks are ready | |
| 118 // to be run. | |
| 119 void SignalTaskReadyConditionVariables(); | |
| 120 | |
| 121 // Helper which returns a ScopedTaskCount for the given category. This | |
| 122 // increments the count immediately, and decrements it when the returned | |
| 123 // ScopedTaskCount is deleted. | |
| 124 ScopedTaskCount ScopedTaskCountForCategory(cc::TaskCategory category); | |
| 125 | |
| 126 // Determines if we should run a new task for the given category. This factors | |
| 127 // in whether a task is available and whether the count of running tasks is | |
| 128 // low enough to start a new one. | |
| 129 bool ShouldRunTaskForCategory(cc::TaskCategory category); | |
| 130 | |
| 131 // Helper which returns the condition variable being used to signal work | |
| 132 // available for the given category. | |
| 133 base::ConditionVariable& ConditionVariableForCategory( | |
| 134 cc::TaskCategory category); | |
| 135 | |
| 105 void ScheduleTasksWithLockAcquired(cc::NamespaceToken token, | 136 void ScheduleTasksWithLockAcquired(cc::NamespaceToken token, |
| 106 cc::TaskGraph* graph); | 137 cc::TaskGraph* graph); |
| 107 void CollectCompletedTasksWithLockAcquired(cc::NamespaceToken token, | 138 void CollectCompletedTasksWithLockAcquired(cc::NamespaceToken token, |
| 108 cc::Task::Vector* completed_tasks); | 139 cc::Task::Vector* completed_tasks); |
| 109 | 140 |
| 110 // The actual threads where work is done. | 141 // The actual threads where work is done. |
| 111 std::vector<scoped_ptr<base::SimpleThread>> threads_; | 142 std::vector<scoped_ptr<base::SimpleThread>> threads_; |
| 112 | 143 |
| 113 // Lock to exclusively access all the following members that are used to | 144 // Lock to exclusively access all the following members that are used to |
| 114 // implement the TaskRunner and TaskGraphRunner interfaces. | 145 // implement the TaskRunner and TaskGraphRunner interfaces. |
| 115 base::Lock lock_; | 146 base::Lock lock_; |
| 116 // Stores the tasks to be run, sorted by priority. | 147 // Stores the tasks to be run, sorted by priority. |
| 117 cc::TaskGraphWorkQueue work_queue_; | 148 cc::TaskGraphWorkQueue work_queue_; |
| 118 // Namespace used to schedule tasks in the task graph runner. | 149 // Namespace used to schedule tasks in the task graph runner. |
| 119 cc::NamespaceToken namespace_token_; | 150 cc::NamespaceToken namespace_token_; |
| 120 // List of tasks currently queued up for execution. | 151 // List of tasks currently queued up for execution. |
| 121 cc::Task::Vector tasks_; | 152 cc::Task::Vector tasks_; |
| 122 // Graph object used for scheduling tasks. | 153 // Graph object used for scheduling tasks. |
| 123 cc::TaskGraph graph_; | 154 cc::TaskGraph graph_; |
| 124 // Cached vector to avoid allocation when getting the list of complete | 155 // Cached vector to avoid allocation when getting the list of complete |
| 125 // tasks. | 156 // tasks. |
| 126 cc::Task::Vector completed_tasks_; | 157 cc::Task::Vector completed_tasks_; |
| 127 // Condition variable that is waited on by Run() until new tasks are ready to | 158 // Condition variable for each cc::TaskCategory. Waited on by Run() until new |
| 128 // run or shutdown starts. | 159 // tasks are ready to run or shutdown starts. |
| 129 base::ConditionVariable has_ready_to_run_tasks_cv_; | 160 base::ConditionVariable nonconcurrent_foreground_has_ready_to_run_tasks_cv_; |
| 161 base::ConditionVariable foreground_has_ready_to_run_tasks_cv_; | |
| 162 base::ConditionVariable background_has_ready_to_run_tasks_cv_; | |
| 163 // Counters which track the number of running tasks for each category. | |
|
reveman
2016/02/05 17:15:02
Can we use the existing run count in work_queue in
| |
| 164 int nonconcurrent_foreground_running_task_count_; | |
| 165 int foreground_running_task_count_; | |
| 166 int background_running_task_count_; | |
| 167 // The target number of threads that should be active at any given time. | |
| 168 int target_running_task_count_; | |
|
reveman
2016/02/05 17:15:02
I find this a bit awkward. Now that we're using th
| |
| 130 // Condition variable that is waited on by origin threads until a namespace | 169 // Condition variable that is waited on by origin threads until a namespace |
| 131 // has finished running all associated tasks. | 170 // has finished running all associated tasks. |
| 132 base::ConditionVariable has_namespaces_with_finished_running_tasks_cv_; | 171 base::ConditionVariable has_namespaces_with_finished_running_tasks_cv_; |
| 133 // Set during shutdown. Tells Run() to return when no more tasks are pending. | 172 // Set during shutdown. Tells Run() to return when no more tasks are pending. |
| 134 bool shutdown_; | 173 bool shutdown_; |
| 135 }; | 174 }; |
| 136 | 175 |
| 137 } // namespace content | 176 } // namespace content |
| 138 | 177 |
| 139 #endif // CONTENT_RENDERER_RASTER_WORKER_POOL_H_ | 178 #endif // CONTENT_RENDERER_RASTER_WORKER_POOL_H_ |
| OLD | NEW |