| 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 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 // A pool of threads used to run raster work. | 23 // A pool of threads used to run raster work. |
| 24 // Work can be scheduled on the threads using different interfaces. | 24 // Work can be scheduled on the threads using different interfaces. |
| 25 // The pool itself implements TaskRunner interface and tasks posted via that | 25 // The pool itself implements TaskRunner interface and tasks posted via that |
| 26 // interface might run in parallel. | 26 // interface might run in parallel. |
| 27 // CreateSequencedTaskRunner creates a sequenced task runner that might run in | 27 // CreateSequencedTaskRunner creates a sequenced task runner that might run in |
| 28 // parallel with other instances of sequenced task runners. | 28 // parallel with other instances of sequenced task runners. |
| 29 // 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 |
| 30 // of tasks with their dependencies. | 30 // of tasks with their dependencies. |
| 31 class CONTENT_EXPORT RasterWorkerPool : public base::TaskRunner, | 31 class CONTENT_EXPORT RasterWorkerPool : public base::TaskRunner, |
| 32 public cc::TaskGraphRunner { | 32 public cc::TaskGraphRunner, |
| 33 public cc::TaskGraphWorkQueueClient { |
| 33 public: | 34 public: |
| 34 RasterWorkerPool(); | 35 RasterWorkerPool(); |
| 35 | 36 |
| 36 // Overridden from base::TaskRunner: | 37 // Overridden from base::TaskRunner: |
| 37 bool PostDelayedTask(const tracked_objects::Location& from_here, | 38 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 38 const base::Closure& task, | 39 const base::Closure& task, |
| 39 base::TimeDelta delay) override; | 40 base::TimeDelta delay) override; |
| 40 bool RunsTasksOnCurrentThread() const override; | 41 bool RunsTasksOnCurrentThread() const override; |
| 41 | 42 |
| 42 // Overridden from cc::TaskGraphRunner: | 43 // Overridden from cc::TaskGraphRunner: |
| 43 cc::NamespaceToken GetNamespaceToken() override; | 44 cc::NamespaceToken GetNamespaceToken() override; |
| 44 void ScheduleTasks(cc::NamespaceToken token, cc::TaskGraph* graph) override; | 45 void ScheduleTasks(cc::NamespaceToken token, cc::TaskGraph* graph) override; |
| 45 void WaitForTasksToFinishRunning(cc::NamespaceToken token) override; | 46 void WaitForTasksToFinishRunning(cc::NamespaceToken token) override; |
| 46 void CollectCompletedTasks(cc::NamespaceToken token, | 47 void CollectCompletedTasks(cc::NamespaceToken token, |
| 47 cc::Task::Vector* completed_tasks) override; | 48 cc::Task::Vector* completed_tasks) override; |
| 48 | 49 |
| 50 // TODO(prashant.n): Move this to separate class to control workers |
| 51 // efficiently. |
| 52 // Overridden from cc::TaskGraphWorkQueueClient: |
| 53 void AdjustWorkerPriorityForTask(cc::Task* task, |
| 54 uint16_t old_category, |
| 55 uint16_t new_category) override; |
| 56 |
| 49 // Runs a task from one of the provided categories. Categories listed first | 57 // Runs a task from one of the provided categories. Categories listed first |
| 50 // have higher priority. | 58 // have higher priority. |
| 51 void Run(const std::vector<cc::TaskCategory>& categories, | 59 void Run(cc::TaskWorker* worker, |
| 60 const std::vector<cc::TaskCategory>& categories, |
| 52 base::ConditionVariable* has_ready_to_run_tasks_cv); | 61 base::ConditionVariable* has_ready_to_run_tasks_cv); |
| 53 | 62 |
| 54 void FlushForTesting(); | 63 void FlushForTesting(); |
| 55 | 64 |
| 56 // Spawn |num_threads| number of threads and start running work on the | 65 // Spawn |num_threads| number of threads and start running work on the |
| 57 // worker threads. | 66 // worker threads. |
| 58 void Start(int num_threads); | 67 void Start(int num_threads); |
| 59 | 68 |
| 60 // Finish running all the posted tasks (and nested task posted by those tasks) | 69 // Finish running all the posted tasks (and nested task posted by those tasks) |
| 61 // of all the associated task runners. | 70 // of all the associated task runners. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 DISALLOW_COPY_AND_ASSIGN(ClosureTask); | 103 DISALLOW_COPY_AND_ASSIGN(ClosureTask); |
| 95 }; | 104 }; |
| 96 | 105 |
| 97 void ScheduleTasksWithLockAcquired(cc::NamespaceToken token, | 106 void ScheduleTasksWithLockAcquired(cc::NamespaceToken token, |
| 98 cc::TaskGraph* graph); | 107 cc::TaskGraph* graph); |
| 99 void CollectCompletedTasksWithLockAcquired(cc::NamespaceToken token, | 108 void CollectCompletedTasksWithLockAcquired(cc::NamespaceToken token, |
| 100 cc::Task::Vector* completed_tasks); | 109 cc::Task::Vector* completed_tasks); |
| 101 | 110 |
| 102 // Runs a task from one of the provided categories. Categories listed first | 111 // Runs a task from one of the provided categories. Categories listed first |
| 103 // have higher priority. Returns false if there were no tasks to run. | 112 // have higher priority. Returns false if there were no tasks to run. |
| 104 bool RunTaskWithLockAcquired(const std::vector<cc::TaskCategory>& categories); | 113 bool RunTaskWithLockAcquired(cc::TaskWorker* worker, |
| 114 const std::vector<cc::TaskCategory>& categories); |
| 105 | 115 |
| 106 // Run next task for the given category. Caller must acquire |lock_| prior to | 116 // Run next task for the given category. Caller must acquire |lock_| prior to |
| 107 // calling this function and make sure at least one task is ready to run. | 117 // calling this function and make sure at least one task is ready to run. |
| 108 void RunTaskInCategoryWithLockAcquired(cc::TaskCategory category); | 118 void RunTaskInCategoryWithLockAcquired(cc::TaskWorker* worker, |
| 119 cc::TaskCategory category); |
| 109 | 120 |
| 110 // Helper function which signals worker threads if tasks are ready to run. | 121 // Helper function which signals worker threads if tasks are ready to run. |
| 111 void SignalHasReadyToRunTasksWithLockAcquired(); | 122 void SignalHasReadyToRunTasksWithLockAcquired(); |
| 112 | 123 |
| 113 // Determines if we should run a new task for the given category. This factors | 124 // Determines if we should run a new task for the given category. This factors |
| 114 // in whether a task is available and whether the count of running tasks is | 125 // in whether a task is available and whether the count of running tasks is |
| 115 // low enough to start a new one. | 126 // low enough to start a new one. |
| 116 bool ShouldRunTaskForCategoryWithLockAcquired(cc::TaskCategory category); | 127 bool ShouldRunTaskForCategoryWithLockAcquired(cc::TaskCategory category); |
| 117 | 128 |
| 118 // The actual threads where work is done. | 129 // The actual threads where work is done. |
| 119 std::vector<scoped_ptr<base::SimpleThread>> threads_; | 130 std::vector<scoped_ptr<base::DynamicPriorityThread>> threads_; |
| 120 | 131 |
| 121 // Lock to exclusively access all the following members that are used to | 132 // Lock to exclusively access all the following members that are used to |
| 122 // implement the TaskRunner and TaskGraphRunner interfaces. | 133 // implement the TaskRunner and TaskGraphRunner interfaces. |
| 123 base::Lock lock_; | 134 base::Lock lock_; |
| 124 // Stores the tasks to be run, sorted by priority. | 135 // Stores the tasks to be run, sorted by priority. |
| 125 cc::TaskGraphWorkQueue work_queue_; | 136 cc::TaskGraphWorkQueue work_queue_; |
| 126 // Namespace used to schedule tasks in the task graph runner. | 137 // Namespace used to schedule tasks in the task graph runner. |
| 127 cc::NamespaceToken namespace_token_; | 138 cc::NamespaceToken namespace_token_; |
| 128 // List of tasks currently queued up for execution. | 139 // List of tasks currently queued up for execution. |
| 129 cc::Task::Vector tasks_; | 140 cc::Task::Vector tasks_; |
| 130 // Graph object used for scheduling tasks. | 141 // Graph object used for scheduling tasks. |
| 131 cc::TaskGraph graph_; | 142 cc::TaskGraph graph_; |
| 132 // Cached vector to avoid allocation when getting the list of complete | 143 // Cached vector to avoid allocation when getting the list of complete |
| 133 // tasks. | 144 // tasks. |
| 134 cc::Task::Vector completed_tasks_; | 145 cc::Task::Vector completed_tasks_; |
| 135 // Condition variables for foreground and background tasks. | 146 // Condition variables for foreground and background tasks. |
| 136 base::ConditionVariable has_ready_to_run_foreground_tasks_cv_; | 147 base::ConditionVariable has_ready_to_run_foreground_tasks_cv_; |
| 137 base::ConditionVariable has_ready_to_run_background_tasks_cv_; | 148 base::ConditionVariable has_ready_to_run_background_tasks_cv_; |
| 138 // Condition variable that is waited on by origin threads until a namespace | 149 // Condition variable that is waited on by origin threads until a namespace |
| 139 // has finished running all associated tasks. | 150 // has finished running all associated tasks. |
| 140 base::ConditionVariable has_namespaces_with_finished_running_tasks_cv_; | 151 base::ConditionVariable has_namespaces_with_finished_running_tasks_cv_; |
| 141 // Set during shutdown. Tells Run() to return when no more tasks are pending. | 152 // Set during shutdown. Tells Run() to return when no more tasks are pending. |
| 142 bool shutdown_; | 153 bool shutdown_; |
| 143 }; | 154 }; |
| 144 | 155 |
| 145 } // namespace content | 156 } // namespace content |
| 146 | 157 |
| 147 #endif // CONTENT_RENDERER_RASTER_WORKER_POOL_H_ | 158 #endif // CONTENT_RENDERER_RASTER_WORKER_POOL_H_ |
| OLD | NEW |