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 "content/renderer/categorized_worker_pool.h" | 5 #include "content/renderer/categorized_worker_pool.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
| 11 #include "base/callback_helpers.h" |
11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
12 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
13 #include "base/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
14 #include "cc/base/math_util.h" | 15 #include "cc/base/math_util.h" |
15 #include "cc/raster/task_category.h" | 16 #include "cc/raster/task_category.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 namespace { | 19 namespace { |
19 | 20 |
20 // A thread which forwards to CategorizedWorkerPool::Run with the runnable | 21 // A thread which forwards to CategorizedWorkerPool::Run with the runnable |
(...skipping 25 matching lines...) Expand all Loading... |
46 class CategorizedWorkerPool::CategorizedWorkerPoolSequencedTaskRunner | 47 class CategorizedWorkerPool::CategorizedWorkerPoolSequencedTaskRunner |
47 : public base::SequencedTaskRunner { | 48 : public base::SequencedTaskRunner { |
48 public: | 49 public: |
49 explicit CategorizedWorkerPoolSequencedTaskRunner( | 50 explicit CategorizedWorkerPoolSequencedTaskRunner( |
50 cc::TaskGraphRunner* task_graph_runner) | 51 cc::TaskGraphRunner* task_graph_runner) |
51 : task_graph_runner_(task_graph_runner), | 52 : task_graph_runner_(task_graph_runner), |
52 namespace_token_(task_graph_runner->GenerateNamespaceToken()) {} | 53 namespace_token_(task_graph_runner->GenerateNamespaceToken()) {} |
53 | 54 |
54 // Overridden from base::TaskRunner: | 55 // Overridden from base::TaskRunner: |
55 bool PostDelayedTask(const tracked_objects::Location& from_here, | 56 bool PostDelayedTask(const tracked_objects::Location& from_here, |
56 const base::Closure& task, | 57 base::Closure task, |
57 base::TimeDelta delay) override { | 58 base::TimeDelta delay) override { |
58 return PostNonNestableDelayedTask(from_here, task, delay); | 59 return PostNonNestableDelayedTask(from_here, std::move(task), delay); |
59 } | 60 } |
60 bool RunsTasksOnCurrentThread() const override { return true; } | 61 bool RunsTasksOnCurrentThread() const override { return true; } |
61 | 62 |
62 // Overridden from base::SequencedTaskRunner: | 63 // Overridden from base::SequencedTaskRunner: |
63 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 64 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
64 const base::Closure& task, | 65 base::Closure task, |
65 base::TimeDelta delay) override { | 66 base::TimeDelta delay) override { |
66 base::AutoLock lock(lock_); | 67 base::AutoLock lock(lock_); |
67 | 68 |
68 // Remove completed tasks. | 69 // Remove completed tasks. |
69 DCHECK(completed_tasks_.empty()); | 70 DCHECK(completed_tasks_.empty()); |
70 task_graph_runner_->CollectCompletedTasks(namespace_token_, | 71 task_graph_runner_->CollectCompletedTasks(namespace_token_, |
71 &completed_tasks_); | 72 &completed_tasks_); |
72 | 73 |
73 tasks_.erase(tasks_.begin(), tasks_.begin() + completed_tasks_.size()); | 74 tasks_.erase(tasks_.begin(), tasks_.begin() + completed_tasks_.size()); |
74 | 75 |
75 tasks_.push_back(make_scoped_refptr(new ClosureTask(task))); | 76 tasks_.push_back(make_scoped_refptr(new ClosureTask(std::move(task)))); |
76 graph_.Reset(); | 77 graph_.Reset(); |
77 for (const auto& graph_task : tasks_) { | 78 for (const auto& graph_task : tasks_) { |
78 int dependencies = 0; | 79 int dependencies = 0; |
79 if (!graph_.nodes.empty()) | 80 if (!graph_.nodes.empty()) |
80 dependencies = 1; | 81 dependencies = 1; |
81 | 82 |
82 // Treat any tasks that are enqueued through the SequencedTaskRunner as | 83 // Treat any tasks that are enqueued through the SequencedTaskRunner as |
83 // FOREGROUND priority. We don't have enough information to know the | 84 // FOREGROUND priority. We don't have enough information to know the |
84 // actual priority of such tasks, so we run them as soon as possible. | 85 // actual priority of such tasks, so we run them as soon as possible. |
85 cc::TaskGraph::Node node(graph_task, cc::TASK_CATEGORY_FOREGROUND, | 86 cc::TaskGraph::Node node(graph_task, cc::TASK_CATEGORY_FOREGROUND, |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 } | 180 } |
180 while (!threads_.empty()) { | 181 while (!threads_.empty()) { |
181 threads_.back()->Join(); | 182 threads_.back()->Join(); |
182 threads_.pop_back(); | 183 threads_.pop_back(); |
183 } | 184 } |
184 } | 185 } |
185 | 186 |
186 // Overridden from base::TaskRunner: | 187 // Overridden from base::TaskRunner: |
187 bool CategorizedWorkerPool::PostDelayedTask( | 188 bool CategorizedWorkerPool::PostDelayedTask( |
188 const tracked_objects::Location& from_here, | 189 const tracked_objects::Location& from_here, |
189 const base::Closure& task, | 190 base::Closure task, |
190 base::TimeDelta delay) { | 191 base::TimeDelta delay) { |
191 base::AutoLock lock(lock_); | 192 base::AutoLock lock(lock_); |
192 | 193 |
193 // Remove completed tasks. | 194 // Remove completed tasks. |
194 DCHECK(completed_tasks_.empty()); | 195 DCHECK(completed_tasks_.empty()); |
195 CollectCompletedTasksWithLockAcquired(namespace_token_, &completed_tasks_); | 196 CollectCompletedTasksWithLockAcquired(namespace_token_, &completed_tasks_); |
196 | 197 |
197 cc::Task::Vector::iterator end = std::remove_if( | 198 cc::Task::Vector::iterator end = std::remove_if( |
198 tasks_.begin(), tasks_.end(), [this](const scoped_refptr<cc::Task>& e) { | 199 tasks_.begin(), tasks_.end(), [this](const scoped_refptr<cc::Task>& e) { |
199 return std::find(this->completed_tasks_.begin(), | 200 return std::find(this->completed_tasks_.begin(), |
200 this->completed_tasks_.end(), | 201 this->completed_tasks_.end(), |
201 e) != this->completed_tasks_.end(); | 202 e) != this->completed_tasks_.end(); |
202 }); | 203 }); |
203 tasks_.erase(end, tasks_.end()); | 204 tasks_.erase(end, tasks_.end()); |
204 | 205 |
205 tasks_.push_back(make_scoped_refptr(new ClosureTask(task))); | 206 tasks_.push_back(make_scoped_refptr(new ClosureTask(std::move(task)))); |
206 graph_.Reset(); | 207 graph_.Reset(); |
207 for (const auto& graph_task : tasks_) { | 208 for (const auto& graph_task : tasks_) { |
208 // Delayed tasks are assigned FOREGROUND category, ensuring that they run as | 209 // Delayed tasks are assigned FOREGROUND category, ensuring that they run as |
209 // soon as possible once their delay has expired. | 210 // soon as possible once their delay has expired. |
210 graph_.nodes.push_back( | 211 graph_.nodes.push_back( |
211 cc::TaskGraph::Node(graph_task.get(), cc::TASK_CATEGORY_FOREGROUND, | 212 cc::TaskGraph::Node(graph_task.get(), cc::TASK_CATEGORY_FOREGROUND, |
212 0u /* priority */, 0u /* dependencies */)); | 213 0u /* priority */, 0u /* dependencies */)); |
213 } | 214 } |
214 | 215 |
215 ScheduleTasksWithLockAcquired(namespace_token_, &graph_); | 216 ScheduleTasksWithLockAcquired(namespace_token_, &graph_); |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 ShouldRunTaskForCategoryWithLockAcquired( | 408 ShouldRunTaskForCategoryWithLockAcquired( |
408 cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND)) { | 409 cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND)) { |
409 has_ready_to_run_foreground_tasks_cv_.Signal(); | 410 has_ready_to_run_foreground_tasks_cv_.Signal(); |
410 } | 411 } |
411 | 412 |
412 if (ShouldRunTaskForCategoryWithLockAcquired(cc::TASK_CATEGORY_BACKGROUND)) { | 413 if (ShouldRunTaskForCategoryWithLockAcquired(cc::TASK_CATEGORY_BACKGROUND)) { |
413 has_ready_to_run_background_tasks_cv_.Signal(); | 414 has_ready_to_run_background_tasks_cv_.Signal(); |
414 } | 415 } |
415 } | 416 } |
416 | 417 |
417 CategorizedWorkerPool::ClosureTask::ClosureTask(const base::Closure& closure) | 418 CategorizedWorkerPool::ClosureTask::ClosureTask(base::Closure closure) |
418 : closure_(closure) {} | 419 : closure_(std::move(closure)) {} |
419 | 420 |
420 // Overridden from cc::Task: | 421 // Overridden from cc::Task: |
421 void CategorizedWorkerPool::ClosureTask::RunOnWorkerThread() { | 422 void CategorizedWorkerPool::ClosureTask::RunOnWorkerThread() { |
422 closure_.Run(); | 423 std::move(closure_).Run(); |
423 closure_.Reset(); | |
424 } | 424 } |
425 | 425 |
426 CategorizedWorkerPool::ClosureTask::~ClosureTask() {} | 426 CategorizedWorkerPool::ClosureTask::~ClosureTask() {} |
427 | 427 |
428 } // namespace content | 428 } // namespace content |
OLD | NEW |