Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(585)

Side by Side Diff: content/renderer/categorized_worker_pool.cc

Issue 2726523002: Pass Callback to TaskRunner by value and consume it on invocation (1) (Closed)
Patch Set: erase Closure* Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 class CategorizedWorkerPool::CategorizedWorkerPoolSequencedTaskRunner 46 class CategorizedWorkerPool::CategorizedWorkerPoolSequencedTaskRunner
47 : public base::SequencedTaskRunner { 47 : public base::SequencedTaskRunner {
48 public: 48 public:
49 explicit CategorizedWorkerPoolSequencedTaskRunner( 49 explicit CategorizedWorkerPoolSequencedTaskRunner(
50 cc::TaskGraphRunner* task_graph_runner) 50 cc::TaskGraphRunner* task_graph_runner)
51 : task_graph_runner_(task_graph_runner), 51 : task_graph_runner_(task_graph_runner),
52 namespace_token_(task_graph_runner->GenerateNamespaceToken()) {} 52 namespace_token_(task_graph_runner->GenerateNamespaceToken()) {}
53 53
54 // Overridden from base::TaskRunner: 54 // Overridden from base::TaskRunner:
55 bool PostDelayedTask(const tracked_objects::Location& from_here, 55 bool PostDelayedTask(const tracked_objects::Location& from_here,
56 const base::Closure& task, 56 base::Closure task,
57 base::TimeDelta delay) override { 57 base::TimeDelta delay) override {
58 return PostNonNestableDelayedTask(from_here, task, delay); 58 return PostNonNestableDelayedTask(from_here, std::move(task), delay);
59 } 59 }
60 bool RunsTasksOnCurrentThread() const override { return true; } 60 bool RunsTasksOnCurrentThread() const override { return true; }
61 61
62 // Overridden from base::SequencedTaskRunner: 62 // Overridden from base::SequencedTaskRunner:
63 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, 63 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
64 const base::Closure& task, 64 base::Closure task,
65 base::TimeDelta delay) override { 65 base::TimeDelta delay) override {
66 base::AutoLock lock(lock_); 66 base::AutoLock lock(lock_);
67 67
68 // Remove completed tasks. 68 // Remove completed tasks.
69 DCHECK(completed_tasks_.empty()); 69 DCHECK(completed_tasks_.empty());
70 task_graph_runner_->CollectCompletedTasks(namespace_token_, 70 task_graph_runner_->CollectCompletedTasks(namespace_token_,
71 &completed_tasks_); 71 &completed_tasks_);
72 72
73 tasks_.erase(tasks_.begin(), tasks_.begin() + completed_tasks_.size()); 73 tasks_.erase(tasks_.begin(), tasks_.begin() + completed_tasks_.size());
74 74
75 tasks_.push_back(make_scoped_refptr(new ClosureTask(task))); 75 tasks_.push_back(make_scoped_refptr(new ClosureTask(std::move(task))));
76 graph_.Reset(); 76 graph_.Reset();
77 for (const auto& graph_task : tasks_) { 77 for (const auto& graph_task : tasks_) {
78 int dependencies = 0; 78 int dependencies = 0;
79 if (!graph_.nodes.empty()) 79 if (!graph_.nodes.empty())
80 dependencies = 1; 80 dependencies = 1;
81 81
82 // Treat any tasks that are enqueued through the SequencedTaskRunner as 82 // Treat any tasks that are enqueued through the SequencedTaskRunner as
83 // FOREGROUND priority. We don't have enough information to know the 83 // 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. 84 // actual priority of such tasks, so we run them as soon as possible.
85 cc::TaskGraph::Node node(graph_task, cc::TASK_CATEGORY_FOREGROUND, 85 cc::TaskGraph::Node node(graph_task, cc::TASK_CATEGORY_FOREGROUND,
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 } 179 }
180 while (!threads_.empty()) { 180 while (!threads_.empty()) {
181 threads_.back()->Join(); 181 threads_.back()->Join();
182 threads_.pop_back(); 182 threads_.pop_back();
183 } 183 }
184 } 184 }
185 185
186 // Overridden from base::TaskRunner: 186 // Overridden from base::TaskRunner:
187 bool CategorizedWorkerPool::PostDelayedTask( 187 bool CategorizedWorkerPool::PostDelayedTask(
188 const tracked_objects::Location& from_here, 188 const tracked_objects::Location& from_here,
189 const base::Closure& task, 189 base::Closure task,
190 base::TimeDelta delay) { 190 base::TimeDelta delay) {
191 base::AutoLock lock(lock_); 191 base::AutoLock lock(lock_);
192 192
193 // Remove completed tasks. 193 // Remove completed tasks.
194 DCHECK(completed_tasks_.empty()); 194 DCHECK(completed_tasks_.empty());
195 CollectCompletedTasksWithLockAcquired(namespace_token_, &completed_tasks_); 195 CollectCompletedTasksWithLockAcquired(namespace_token_, &completed_tasks_);
196 196
197 cc::Task::Vector::iterator end = std::remove_if( 197 cc::Task::Vector::iterator end = std::remove_if(
198 tasks_.begin(), tasks_.end(), [this](const scoped_refptr<cc::Task>& e) { 198 tasks_.begin(), tasks_.end(), [this](const scoped_refptr<cc::Task>& e) {
199 return std::find(this->completed_tasks_.begin(), 199 return std::find(this->completed_tasks_.begin(),
200 this->completed_tasks_.end(), 200 this->completed_tasks_.end(),
201 e) != this->completed_tasks_.end(); 201 e) != this->completed_tasks_.end();
202 }); 202 });
203 tasks_.erase(end, tasks_.end()); 203 tasks_.erase(end, tasks_.end());
204 204
205 tasks_.push_back(make_scoped_refptr(new ClosureTask(task))); 205 tasks_.push_back(make_scoped_refptr(new ClosureTask(std::move(task))));
206 graph_.Reset(); 206 graph_.Reset();
207 for (const auto& graph_task : tasks_) { 207 for (const auto& graph_task : tasks_) {
208 // Delayed tasks are assigned FOREGROUND category, ensuring that they run as 208 // Delayed tasks are assigned FOREGROUND category, ensuring that they run as
209 // soon as possible once their delay has expired. 209 // soon as possible once their delay has expired.
210 graph_.nodes.push_back( 210 graph_.nodes.push_back(
211 cc::TaskGraph::Node(graph_task.get(), cc::TASK_CATEGORY_FOREGROUND, 211 cc::TaskGraph::Node(graph_task.get(), cc::TASK_CATEGORY_FOREGROUND,
212 0u /* priority */, 0u /* dependencies */)); 212 0u /* priority */, 0u /* dependencies */));
213 } 213 }
214 214
215 ScheduleTasksWithLockAcquired(namespace_token_, &graph_); 215 ScheduleTasksWithLockAcquired(namespace_token_, &graph_);
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 ShouldRunTaskForCategoryWithLockAcquired( 407 ShouldRunTaskForCategoryWithLockAcquired(
408 cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND)) { 408 cc::TASK_CATEGORY_NONCONCURRENT_FOREGROUND)) {
409 has_ready_to_run_foreground_tasks_cv_.Signal(); 409 has_ready_to_run_foreground_tasks_cv_.Signal();
410 } 410 }
411 411
412 if (ShouldRunTaskForCategoryWithLockAcquired(cc::TASK_CATEGORY_BACKGROUND)) { 412 if (ShouldRunTaskForCategoryWithLockAcquired(cc::TASK_CATEGORY_BACKGROUND)) {
413 has_ready_to_run_background_tasks_cv_.Signal(); 413 has_ready_to_run_background_tasks_cv_.Signal();
414 } 414 }
415 } 415 }
416 416
417 CategorizedWorkerPool::ClosureTask::ClosureTask(const base::Closure& closure) 417 CategorizedWorkerPool::ClosureTask::ClosureTask(base::Closure closure)
418 : closure_(closure) {} 418 : closure_(std::move(closure)) {}
419 419
420 // Overridden from cc::Task: 420 // Overridden from cc::Task:
421 void CategorizedWorkerPool::ClosureTask::RunOnWorkerThread() { 421 void CategorizedWorkerPool::ClosureTask::RunOnWorkerThread() {
422 closure_.Run(); 422 std::move(closure_).Run();
423 closure_.Reset();
424 } 423 }
425 424
426 CategorizedWorkerPool::ClosureTask::~ClosureTask() {} 425 CategorizedWorkerPool::ClosureTask::~ClosureTask() {}
427 426
428 } // namespace content 427 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/categorized_worker_pool.h ('k') | content/renderer/render_thread_impl_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698