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

Side by Side Diff: base/test/scoped_task_scheduler.cc

Issue 2726523002: Pass Callback to TaskRunner by value and consume it on invocation (1) (Closed)
Patch Set: s/base::ResetAndReturn/std::move/ 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/test/scoped_task_scheduler.h" 5 #include "base/test/scoped_task_scheduler.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/callback.h"
12 #include "base/logging.h" 13 #include "base/logging.h"
13 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
14 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
15 #include "base/message_loop/message_loop.h" 16 #include "base/message_loop/message_loop.h"
16 #include "base/metrics/histogram_base.h" 17 #include "base/metrics/histogram_base.h"
17 #include "base/run_loop.h" 18 #include "base/run_loop.h"
18 #include "base/sequence_token.h" 19 #include "base/sequence_token.h"
19 #include "base/sequenced_task_runner.h" 20 #include "base/sequenced_task_runner.h"
20 #include "base/single_thread_task_runner.h" 21 #include "base/single_thread_task_runner.h"
21 #include "base/task_runner.h" 22 #include "base/task_runner.h"
(...skipping 13 matching lines...) Expand all
35 public: 36 public:
36 // |external_message_loop| is an externally provided MessageLoop on which to 37 // |external_message_loop| is an externally provided MessageLoop on which to
37 // run tasks. A MessageLoop will be created by TestTaskScheduler if 38 // run tasks. A MessageLoop will be created by TestTaskScheduler if
38 // |external_message_loop| is nullptr. 39 // |external_message_loop| is nullptr.
39 explicit TestTaskScheduler(MessageLoop* external_message_loop); 40 explicit TestTaskScheduler(MessageLoop* external_message_loop);
40 ~TestTaskScheduler() override; 41 ~TestTaskScheduler() override;
41 42
42 // TaskScheduler: 43 // TaskScheduler:
43 void PostDelayedTaskWithTraits(const tracked_objects::Location& from_here, 44 void PostDelayedTaskWithTraits(const tracked_objects::Location& from_here,
44 const TaskTraits& traits, 45 const TaskTraits& traits,
45 const Closure& task, 46 Closure task,
46 TimeDelta delay) override; 47 TimeDelta delay) override;
47 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( 48 scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits(
48 const TaskTraits& traits) override; 49 const TaskTraits& traits) override;
49 scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits( 50 scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits(
50 const TaskTraits& traits) override; 51 const TaskTraits& traits) override;
51 scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits( 52 scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunnerWithTraits(
52 const TaskTraits& traits) override; 53 const TaskTraits& traits) override;
53 std::vector<const HistogramBase*> GetHistograms() const override; 54 std::vector<const HistogramBase*> GetHistograms() const override;
54 int GetMaxConcurrentTasksWithTraitsDeprecated( 55 int GetMaxConcurrentTasksWithTraitsDeprecated(
55 const TaskTraits& traits) const override; 56 const TaskTraits& traits) const override;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 }; 104 };
104 105
105 class TestTaskSchedulerTaskRunner : public SingleThreadTaskRunner { 106 class TestTaskSchedulerTaskRunner : public SingleThreadTaskRunner {
106 public: 107 public:
107 TestTaskSchedulerTaskRunner(TestTaskScheduler* task_scheduler, 108 TestTaskSchedulerTaskRunner(TestTaskScheduler* task_scheduler,
108 ExecutionMode execution_mode, 109 ExecutionMode execution_mode,
109 TaskTraits traits); 110 TaskTraits traits);
110 111
111 // SingleThreadTaskRunner: 112 // SingleThreadTaskRunner:
112 bool PostDelayedTask(const tracked_objects::Location& from_here, 113 bool PostDelayedTask(const tracked_objects::Location& from_here,
113 const Closure& closure, 114 Closure closure,
114 TimeDelta delay) override; 115 TimeDelta delay) override;
115 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, 116 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
116 const Closure& closure, 117 Closure closure,
117 TimeDelta delay) override; 118 TimeDelta delay) override;
118 bool RunsTasksOnCurrentThread() const override; 119 bool RunsTasksOnCurrentThread() const override;
119 120
120 private: 121 private:
121 ~TestTaskSchedulerTaskRunner() override; 122 ~TestTaskSchedulerTaskRunner() override;
122 123
123 TestTaskScheduler* const task_scheduler_; 124 TestTaskScheduler* const task_scheduler_;
124 const ExecutionMode execution_mode_; 125 const ExecutionMode execution_mode_;
125 const SequenceToken sequence_token_; 126 const SequenceToken sequence_token_;
126 const TaskTraits traits_; 127 const TaskTraits traits_;
127 128
128 DISALLOW_COPY_AND_ASSIGN(TestTaskSchedulerTaskRunner); 129 DISALLOW_COPY_AND_ASSIGN(TestTaskSchedulerTaskRunner);
129 }; 130 };
130 131
131 TestTaskScheduler::TestTaskScheduler(MessageLoop* external_message_loop) 132 TestTaskScheduler::TestTaskScheduler(MessageLoop* external_message_loop)
132 : message_loop_owned_(external_message_loop ? nullptr 133 : message_loop_owned_(external_message_loop ? nullptr
133 : MakeUnique<MessageLoop>()), 134 : MakeUnique<MessageLoop>()),
134 message_loop_(message_loop_owned_ ? message_loop_owned_.get() 135 message_loop_(message_loop_owned_ ? message_loop_owned_.get()
135 : external_message_loop) {} 136 : external_message_loop) {}
136 137
137 TestTaskScheduler::~TestTaskScheduler() { 138 TestTaskScheduler::~TestTaskScheduler() {
138 // Shutdown if it hasn't already been done explicitly. 139 // Shutdown if it hasn't already been done explicitly.
139 if (!task_tracker_.HasShutdownStarted()) 140 if (!task_tracker_.HasShutdownStarted())
140 Shutdown(); 141 Shutdown();
141 } 142 }
142 143
143 void TestTaskScheduler::PostDelayedTaskWithTraits( 144 void TestTaskScheduler::PostDelayedTaskWithTraits(
144 const tracked_objects::Location& from_here, 145 const tracked_objects::Location& from_here,
145 const TaskTraits& traits, 146 const TaskTraits& traits,
146 const Closure& task, 147 Closure task,
147 TimeDelta delay) { 148 TimeDelta delay) {
148 CreateTaskRunnerWithTraits(traits)->PostDelayedTask(from_here, task, delay); 149 CreateTaskRunnerWithTraits(traits)->PostDelayedTask(from_here,
150 std::move(task), delay);
149 } 151 }
150 152
151 scoped_refptr<TaskRunner> TestTaskScheduler::CreateTaskRunnerWithTraits( 153 scoped_refptr<TaskRunner> TestTaskScheduler::CreateTaskRunnerWithTraits(
152 const TaskTraits& traits) { 154 const TaskTraits& traits) {
153 return make_scoped_refptr( 155 return make_scoped_refptr(
154 new TestTaskSchedulerTaskRunner(this, ExecutionMode::PARALLEL, traits)); 156 new TestTaskSchedulerTaskRunner(this, ExecutionMode::PARALLEL, traits));
155 } 157 }
156 158
157 scoped_refptr<SequencedTaskRunner> 159 scoped_refptr<SequencedTaskRunner>
158 TestTaskScheduler::CreateSequencedTaskRunnerWithTraits( 160 TestTaskScheduler::CreateSequencedTaskRunnerWithTraits(
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 TaskTraits traits) 241 TaskTraits traits)
240 : task_scheduler_(task_scheduler), 242 : task_scheduler_(task_scheduler),
241 execution_mode_(execution_mode), 243 execution_mode_(execution_mode),
242 sequence_token_(execution_mode == ExecutionMode::PARALLEL 244 sequence_token_(execution_mode == ExecutionMode::PARALLEL
243 ? SequenceToken() 245 ? SequenceToken()
244 : SequenceToken::Create()), 246 : SequenceToken::Create()),
245 traits_(traits) {} 247 traits_(traits) {}
246 248
247 bool TestTaskSchedulerTaskRunner::PostDelayedTask( 249 bool TestTaskSchedulerTaskRunner::PostDelayedTask(
248 const tracked_objects::Location& from_here, 250 const tracked_objects::Location& from_here,
249 const Closure& closure, 251 Closure closure,
250 TimeDelta delay) { 252 TimeDelta delay) {
251 auto task = MakeUnique<internal::Task>(from_here, closure, traits_, delay); 253 auto task =
254 MakeUnique<internal::Task>(from_here, std::move(closure), traits_, delay);
252 if (execution_mode_ == ExecutionMode::SEQUENCED) 255 if (execution_mode_ == ExecutionMode::SEQUENCED)
253 task->sequenced_task_runner_ref = make_scoped_refptr(this); 256 task->sequenced_task_runner_ref = make_scoped_refptr(this);
254 else if (execution_mode_ == ExecutionMode::SINGLE_THREADED) 257 else if (execution_mode_ == ExecutionMode::SINGLE_THREADED)
255 task->single_thread_task_runner_ref = make_scoped_refptr(this); 258 task->single_thread_task_runner_ref = make_scoped_refptr(this);
256 return task_scheduler_->PostTask(std::move(task), sequence_token_); 259 return task_scheduler_->PostTask(std::move(task), sequence_token_);
257 } 260 }
258 261
259 bool TestTaskSchedulerTaskRunner::PostNonNestableDelayedTask( 262 bool TestTaskSchedulerTaskRunner::PostNonNestableDelayedTask(
260 const tracked_objects::Location& from_here, 263 const tracked_objects::Location& from_here,
261 const Closure& closure, 264 Closure closure,
262 TimeDelta delay) { 265 TimeDelta delay) {
263 // Tasks are never nested within the task scheduler. 266 // Tasks are never nested within the task scheduler.
264 return PostDelayedTask(from_here, closure, delay); 267 return PostDelayedTask(from_here, std::move(closure), delay);
265 } 268 }
266 269
267 bool TestTaskSchedulerTaskRunner::RunsTasksOnCurrentThread() const { 270 bool TestTaskSchedulerTaskRunner::RunsTasksOnCurrentThread() const {
268 if (execution_mode_ == ExecutionMode::PARALLEL) 271 if (execution_mode_ == ExecutionMode::PARALLEL)
269 return task_scheduler_->RunsTasksOnCurrentThread(); 272 return task_scheduler_->RunsTasksOnCurrentThread();
270 return sequence_token_ == SequenceToken::GetForCurrentThread(); 273 return sequence_token_ == SequenceToken::GetForCurrentThread();
271 } 274 }
272 275
273 TestTaskSchedulerTaskRunner::~TestTaskSchedulerTaskRunner() = default; 276 TestTaskSchedulerTaskRunner::~TestTaskSchedulerTaskRunner() = default;
274 277
(...skipping 12 matching lines...) Expand all
287 DCHECK_EQ(task_scheduler_, TaskScheduler::GetInstance()); 290 DCHECK_EQ(task_scheduler_, TaskScheduler::GetInstance());
288 291
289 // Per contract, call JoinForTesting() before deleting the TaskScheduler. 292 // Per contract, call JoinForTesting() before deleting the TaskScheduler.
290 TaskScheduler::GetInstance()->JoinForTesting(); 293 TaskScheduler::GetInstance()->JoinForTesting();
291 294
292 TaskScheduler::SetInstance(nullptr); 295 TaskScheduler::SetInstance(nullptr);
293 } 296 }
294 297
295 } // namespace test 298 } // namespace test
296 } // namespace base 299 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698