| 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 #include "cc/raster/single_thread_task_graph_runner.h" | 5 #include "cc/raster/single_thread_task_graph_runner.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/threading/simple_thread.h" | 9 #include "base/threading/simple_thread.h" |
| 10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 { | 105 { |
| 106 base::AutoLock lock(lock_); | 106 base::AutoLock lock(lock_); |
| 107 work_queue_.CollectCompletedTasks(token, completed_tasks); | 107 work_queue_.CollectCompletedTasks(token, completed_tasks); |
| 108 } | 108 } |
| 109 } | 109 } |
| 110 | 110 |
| 111 void SingleThreadTaskGraphRunner::Run() { | 111 void SingleThreadTaskGraphRunner::Run() { |
| 112 base::AutoLock lock(lock_); | 112 base::AutoLock lock(lock_); |
| 113 | 113 |
| 114 while (true) { | 114 while (true) { |
| 115 if (!work_queue_.HasReadyToRunTasks()) { | 115 if (!RunTaskWithLockAcquired()) { |
| 116 // Exit when shutdown is set and no more tasks are pending. | 116 // Exit when shutdown is set and no more tasks are pending. |
| 117 if (shutdown_) | 117 if (shutdown_) |
| 118 break; | 118 break; |
| 119 | 119 |
| 120 // Wait for more tasks. | 120 // Wait for more tasks. |
| 121 has_ready_to_run_tasks_cv_.Wait(); | 121 has_ready_to_run_tasks_cv_.Wait(); |
| 122 continue; | 122 continue; |
| 123 } | 123 } |
| 124 | |
| 125 RunTaskWithLockAcquired(); | |
| 126 } | 124 } |
| 127 } | 125 } |
| 128 | 126 |
| 129 void SingleThreadTaskGraphRunner::RunTaskWithLockAcquired() { | 127 bool SingleThreadTaskGraphRunner::RunTaskWithLockAcquired() { |
| 130 TRACE_EVENT0("toplevel", | 128 TRACE_EVENT0("toplevel", |
| 131 "SingleThreadTaskGraphRunner::RunTaskWithLockAcquired"); | 129 "SingleThreadTaskGraphRunner::RunTaskWithLockAcquired"); |
| 132 | 130 |
| 133 lock_.AssertAcquired(); | 131 lock_.AssertAcquired(); |
| 134 | 132 |
| 135 auto prioritized_task = work_queue_.GetNextTaskToRun(); | 133 // Find the first category with any tasks to run. This task graph runner |
| 134 // treats categories as an additional priority. |
| 135 const auto& ready_to_run_namespaces = work_queue_.ready_to_run_namespaces(); |
| 136 auto found = std::find_if( |
| 137 ready_to_run_namespaces.cbegin(), ready_to_run_namespaces.cend(), |
| 138 [](const std::pair<uint16_t, TaskGraphWorkQueue::TaskNamespace::Vector>& |
| 139 pair) { return !pair.second.empty(); }); |
| 140 |
| 141 if (found == ready_to_run_namespaces.cend()) { |
| 142 return false; |
| 143 } |
| 144 |
| 145 const uint16_t category = found->first; |
| 146 auto prioritized_task = work_queue_.GetNextTaskToRun(category); |
| 136 Task* task = prioritized_task.task; | 147 Task* task = prioritized_task.task; |
| 137 | 148 |
| 138 // Call WillRun() before releasing |lock_| and running task. | 149 // Call WillRun() before releasing |lock_| and running task. |
| 139 task->WillRun(); | 150 task->WillRun(); |
| 140 | 151 |
| 141 { | 152 { |
| 142 base::AutoUnlock unlock(lock_); | 153 base::AutoUnlock unlock(lock_); |
| 143 task->RunOnWorkerThread(); | 154 task->RunOnWorkerThread(); |
| 144 } | 155 } |
| 145 | 156 |
| 146 // This will mark task as finished running. | 157 // This will mark task as finished running. |
| 147 task->DidRun(); | 158 task->DidRun(); |
| 148 | 159 |
| 149 work_queue_.CompleteTask(prioritized_task); | 160 work_queue_.CompleteTask(prioritized_task); |
| 150 | 161 |
| 151 // If namespace has finished running all tasks, wake up origin thread. | 162 // If namespace has finished running all tasks, wake up origin thread. |
| 152 if (work_queue_.HasFinishedRunningTasksInNamespace( | 163 if (work_queue_.HasFinishedRunningTasksInNamespace( |
| 153 prioritized_task.task_namespace)) | 164 prioritized_task.task_namespace)) |
| 154 has_namespaces_with_finished_running_tasks_cv_.Signal(); | 165 has_namespaces_with_finished_running_tasks_cv_.Signal(); |
| 166 |
| 167 return true; |
| 155 } | 168 } |
| 156 | 169 |
| 157 } // namespace cc | 170 } // namespace cc |
| OLD | NEW |