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

Side by Side Diff: base/task_scheduler/scheduler_worker_pool_impl.cc

Issue 2726523002: Pass Callback to TaskRunner by value and consume it on invocation (1) (Closed)
Patch Set: rebase Created 3 years, 9 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "base/task_scheduler/scheduler_worker_pool_impl.h" 5 #include "base/task_scheduler/scheduler_worker_pool_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <utility> 10 #include <utility>
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 // long as |worker_pool| is alive. 52 // long as |worker_pool| is alive.
53 // TODO(robliao): Find a concrete way to manage |worker_pool|'s memory. 53 // TODO(robliao): Find a concrete way to manage |worker_pool|'s memory.
54 SchedulerParallelTaskRunner(const TaskTraits& traits, 54 SchedulerParallelTaskRunner(const TaskTraits& traits,
55 SchedulerWorkerPool* worker_pool) 55 SchedulerWorkerPool* worker_pool)
56 : traits_(traits), worker_pool_(worker_pool) { 56 : traits_(traits), worker_pool_(worker_pool) {
57 DCHECK(worker_pool_); 57 DCHECK(worker_pool_);
58 } 58 }
59 59
60 // TaskRunner: 60 // TaskRunner:
61 bool PostDelayedTask(const tracked_objects::Location& from_here, 61 bool PostDelayedTask(const tracked_objects::Location& from_here,
62 const Closure& closure, 62 Closure closure,
63 TimeDelta delay) override { 63 TimeDelta delay) override {
64 // Post the task as part of a one-off single-task Sequence. 64 // Post the task as part of a one-off single-task Sequence.
65 return worker_pool_->PostTaskWithSequence( 65 return worker_pool_->PostTaskWithSequence(
66 MakeUnique<Task>(from_here, closure, traits_, delay), 66 MakeUnique<Task>(from_here, std::move(closure), traits_, delay),
67 make_scoped_refptr(new Sequence), nullptr); 67 make_scoped_refptr(new Sequence), nullptr);
68 } 68 }
69 69
70 bool RunsTasksOnCurrentThread() const override { 70 bool RunsTasksOnCurrentThread() const override {
71 return tls_current_worker_pool.Get().Get() == worker_pool_; 71 return tls_current_worker_pool.Get().Get() == worker_pool_;
72 } 72 }
73 73
74 private: 74 private:
75 ~SchedulerParallelTaskRunner() override = default; 75 ~SchedulerParallelTaskRunner() override = default;
76 76
(...skipping 10 matching lines...) Expand all
87 // so long as |worker_pool| is alive. 87 // so long as |worker_pool| is alive.
88 // TODO(robliao): Find a concrete way to manage |worker_pool|'s memory. 88 // TODO(robliao): Find a concrete way to manage |worker_pool|'s memory.
89 SchedulerSequencedTaskRunner(const TaskTraits& traits, 89 SchedulerSequencedTaskRunner(const TaskTraits& traits,
90 SchedulerWorkerPool* worker_pool) 90 SchedulerWorkerPool* worker_pool)
91 : traits_(traits), worker_pool_(worker_pool) { 91 : traits_(traits), worker_pool_(worker_pool) {
92 DCHECK(worker_pool_); 92 DCHECK(worker_pool_);
93 } 93 }
94 94
95 // SequencedTaskRunner: 95 // SequencedTaskRunner:
96 bool PostDelayedTask(const tracked_objects::Location& from_here, 96 bool PostDelayedTask(const tracked_objects::Location& from_here,
97 const Closure& closure, 97 Closure closure,
98 TimeDelta delay) override { 98 TimeDelta delay) override {
99 std::unique_ptr<Task> task(new Task(from_here, closure, traits_, delay)); 99 std::unique_ptr<Task> task(
100 new Task(from_here, std::move(closure), traits_, delay));
100 task->sequenced_task_runner_ref = this; 101 task->sequenced_task_runner_ref = this;
101 102
102 // Post the task as part of |sequence_|. 103 // Post the task as part of |sequence_|.
103 return worker_pool_->PostTaskWithSequence(std::move(task), sequence_, 104 return worker_pool_->PostTaskWithSequence(std::move(task), sequence_,
104 nullptr); 105 nullptr);
105 } 106 }
106 107
107 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, 108 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
108 const Closure& closure, 109 Closure closure,
109 base::TimeDelta delay) override { 110 base::TimeDelta delay) override {
110 // Tasks are never nested within the task scheduler. 111 // Tasks are never nested within the task scheduler.
111 return PostDelayedTask(from_here, closure, delay); 112 return PostDelayedTask(from_here, std::move(closure), delay);
112 } 113 }
113 114
114 bool RunsTasksOnCurrentThread() const override { 115 bool RunsTasksOnCurrentThread() const override {
115 // TODO(fdoray): Rename TaskRunner::RunsTaskOnCurrentThread() to something 116 // TODO(fdoray): Rename TaskRunner::RunsTaskOnCurrentThread() to something
116 // that reflects this behavior more accurately. crbug.com/646905 117 // that reflects this behavior more accurately. crbug.com/646905
117 return sequence_->token() == SequenceToken::GetForCurrentThread(); 118 return sequence_->token() == SequenceToken::GetForCurrentThread();
118 } 119 }
119 120
120 private: 121 private:
121 ~SchedulerSequencedTaskRunner() override = default; 122 ~SchedulerSequencedTaskRunner() override = default;
(...skipping 27 matching lines...) Expand all
149 // Constructs a SchedulerSingleThreadTaskRunner which can be used to post 150 // Constructs a SchedulerSingleThreadTaskRunner which can be used to post
150 // tasks so long as |worker_pool| and |worker| are alive. 151 // tasks so long as |worker_pool| and |worker| are alive.
151 // TODO(robliao): Find a concrete way to manage the memory of |worker_pool| 152 // TODO(robliao): Find a concrete way to manage the memory of |worker_pool|
152 // and |worker|. 153 // and |worker|.
153 SchedulerSingleThreadTaskRunner(const TaskTraits& traits, 154 SchedulerSingleThreadTaskRunner(const TaskTraits& traits,
154 SchedulerWorkerPool* worker_pool, 155 SchedulerWorkerPool* worker_pool,
155 SchedulerWorker* worker); 156 SchedulerWorker* worker);
156 157
157 // SingleThreadTaskRunner: 158 // SingleThreadTaskRunner:
158 bool PostDelayedTask(const tracked_objects::Location& from_here, 159 bool PostDelayedTask(const tracked_objects::Location& from_here,
159 const Closure& closure, 160 Closure closure,
160 TimeDelta delay) override { 161 TimeDelta delay) override {
161 std::unique_ptr<Task> task(new Task(from_here, closure, traits_, delay)); 162 std::unique_ptr<Task> task(
163 new Task(from_here, std::move(closure), traits_, delay));
162 task->single_thread_task_runner_ref = this; 164 task->single_thread_task_runner_ref = this;
163 165
164 // Post the task to be executed by |worker_| as part of |sequence_|. 166 // Post the task to be executed by |worker_| as part of |sequence_|.
165 return worker_pool_->PostTaskWithSequence(std::move(task), sequence_, 167 return worker_pool_->PostTaskWithSequence(std::move(task), sequence_,
166 worker_); 168 worker_);
167 } 169 }
168 170
169 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, 171 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
170 const Closure& closure, 172 Closure closure,
171 base::TimeDelta delay) override { 173 base::TimeDelta delay) override {
172 // Tasks are never nested within the task scheduler. 174 // Tasks are never nested within the task scheduler.
173 return PostDelayedTask(from_here, closure, delay); 175 return PostDelayedTask(from_here, std::move(closure), delay);
174 } 176 }
175 177
176 bool RunsTasksOnCurrentThread() const override { 178 bool RunsTasksOnCurrentThread() const override {
177 // Even though this is a SingleThreadTaskRunner, test the actual sequence 179 // Even though this is a SingleThreadTaskRunner, test the actual sequence
178 // instead of the assigned worker so that another task randomly assigned 180 // instead of the assigned worker so that another task randomly assigned
179 // to the same worker doesn't return true by happenstance. 181 // to the same worker doesn't return true by happenstance.
180 return sequence_->token() == SequenceToken::GetForCurrentThread(); 182 return sequence_->token() == SequenceToken::GetForCurrentThread();
181 } 183 }
182 184
183 private: 185 private:
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after
759 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); 761 AutoSchedulerLock auto_lock(idle_workers_stack_lock_);
760 idle_workers_stack_.Remove(worker); 762 idle_workers_stack_.Remove(worker);
761 } 763 }
762 764
763 bool SchedulerWorkerPoolImpl::CanWorkerDetachForTesting() { 765 bool SchedulerWorkerPoolImpl::CanWorkerDetachForTesting() {
764 return !worker_detachment_disallowed_.IsSet(); 766 return !worker_detachment_disallowed_.IsSet();
765 } 767 }
766 768
767 } // namespace internal 769 } // namespace internal
768 } // namespace base 770 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698