OLD | NEW |
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 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 // Posts |task| to this TaskScheduler with |sequence_token|. Returns true on | 71 // Posts |task| to this TaskScheduler with |sequence_token|. Returns true on |
72 // success. | 72 // success. |
73 bool PostTask(std::unique_ptr<internal::Task> task, | 73 bool PostTask(std::unique_ptr<internal::Task> task, |
74 const SequenceToken& sequence_token); | 74 const SequenceToken& sequence_token); |
75 | 75 |
76 // Runs |task| with |sequence_token| using this TaskScheduler's TaskTracker. | 76 // Runs |task| with |sequence_token| using this TaskScheduler's TaskTracker. |
77 void RunTask(std::unique_ptr<internal::Task> task, | 77 void RunTask(std::unique_ptr<internal::Task> task, |
78 const SequenceToken& sequence_token); | 78 const SequenceToken& sequence_token); |
79 | 79 |
80 // Returns true if this TaskScheduler runs its tasks on the current thread. | 80 // Returns true if this TaskScheduler runs its tasks on the current thread. |
81 bool RunsTasksOnCurrentThread() const; | 81 bool RunsTasksInCurrentSequence() const; |
82 | 82 |
83 private: | 83 private: |
84 // Returns the TaskRunner to which this TaskScheduler forwards tasks. It may | 84 // Returns the TaskRunner to which this TaskScheduler forwards tasks. It may |
85 // be |message_loop_->task_runner()| or a reference to it saved on entry to | 85 // be |message_loop_->task_runner()| or a reference to it saved on entry to |
86 // RunTask(). | 86 // RunTask(). |
87 scoped_refptr<SingleThreadTaskRunner> MessageLoopTaskRunner() const { | 87 scoped_refptr<SingleThreadTaskRunner> MessageLoopTaskRunner() const { |
88 if (saved_task_runner_) | 88 if (saved_task_runner_) |
89 return saved_task_runner_; | 89 return saved_task_runner_; |
90 DCHECK(message_loop_->task_runner()); | 90 DCHECK(message_loop_->task_runner()); |
91 return message_loop_->task_runner(); | 91 return message_loop_->task_runner(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 ExecutionMode execution_mode, | 132 ExecutionMode execution_mode, |
133 TaskTraits traits); | 133 TaskTraits traits); |
134 | 134 |
135 // SingleThreadTaskRunner: | 135 // SingleThreadTaskRunner: |
136 bool PostDelayedTask(const tracked_objects::Location& from_here, | 136 bool PostDelayedTask(const tracked_objects::Location& from_here, |
137 OnceClosure closure, | 137 OnceClosure closure, |
138 TimeDelta delay) override; | 138 TimeDelta delay) override; |
139 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 139 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
140 OnceClosure closure, | 140 OnceClosure closure, |
141 TimeDelta delay) override; | 141 TimeDelta delay) override; |
142 bool RunsTasksOnCurrentThread() const override; | 142 bool RunsTasksInCurrentSequence() const override; |
143 | 143 |
144 private: | 144 private: |
145 ~TestTaskSchedulerTaskRunner() override; | 145 ~TestTaskSchedulerTaskRunner() override; |
146 | 146 |
147 TestTaskScheduler* const task_scheduler_; | 147 TestTaskScheduler* const task_scheduler_; |
148 const ExecutionMode execution_mode_; | 148 const ExecutionMode execution_mode_; |
149 const SequenceToken sequence_token_; | 149 const SequenceToken sequence_token_; |
150 const TaskTraits traits_; | 150 const TaskTraits traits_; |
151 | 151 |
152 DISALLOW_COPY_AND_ASSIGN(TestTaskSchedulerTaskRunner); | 152 DISALLOW_COPY_AND_ASSIGN(TestTaskSchedulerTaskRunner); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 : SequenceToken::Create()); | 257 : SequenceToken::Create()); |
258 | 258 |
259 // Make sure that any task runner that was registered was also cleaned up. | 259 // Make sure that any task runner that was registered was also cleaned up. |
260 DCHECK(!MessageLoop::current()->task_runner()); | 260 DCHECK(!MessageLoop::current()->task_runner()); |
261 | 261 |
262 // Restore the MessageLoop TaskRunner. | 262 // Restore the MessageLoop TaskRunner. |
263 MessageLoop::current()->SetTaskRunner(saved_task_runner_); | 263 MessageLoop::current()->SetTaskRunner(saved_task_runner_); |
264 saved_task_runner_ = nullptr; | 264 saved_task_runner_ = nullptr; |
265 } | 265 } |
266 | 266 |
267 bool TestTaskScheduler::RunsTasksOnCurrentThread() const { | 267 bool TestTaskScheduler::RunsTasksInCurrentSequence() const { |
268 return MessageLoopTaskRunner()->RunsTasksOnCurrentThread(); | 268 return MessageLoopTaskRunner()->RunsTasksInCurrentSequence(); |
269 } | 269 } |
270 | 270 |
271 TestTaskSchedulerTaskRunner::TestTaskSchedulerTaskRunner( | 271 TestTaskSchedulerTaskRunner::TestTaskSchedulerTaskRunner( |
272 TestTaskScheduler* task_scheduler, | 272 TestTaskScheduler* task_scheduler, |
273 ExecutionMode execution_mode, | 273 ExecutionMode execution_mode, |
274 TaskTraits traits) | 274 TaskTraits traits) |
275 : task_scheduler_(task_scheduler), | 275 : task_scheduler_(task_scheduler), |
276 execution_mode_(execution_mode), | 276 execution_mode_(execution_mode), |
277 sequence_token_(execution_mode == ExecutionMode::PARALLEL | 277 sequence_token_(execution_mode == ExecutionMode::PARALLEL |
278 ? SequenceToken() | 278 ? SequenceToken() |
(...skipping 14 matching lines...) Expand all Loading... |
293 } | 293 } |
294 | 294 |
295 bool TestTaskSchedulerTaskRunner::PostNonNestableDelayedTask( | 295 bool TestTaskSchedulerTaskRunner::PostNonNestableDelayedTask( |
296 const tracked_objects::Location& from_here, | 296 const tracked_objects::Location& from_here, |
297 OnceClosure closure, | 297 OnceClosure closure, |
298 TimeDelta delay) { | 298 TimeDelta delay) { |
299 // Tasks are never nested within the task scheduler. | 299 // Tasks are never nested within the task scheduler. |
300 return PostDelayedTask(from_here, std::move(closure), delay); | 300 return PostDelayedTask(from_here, std::move(closure), delay); |
301 } | 301 } |
302 | 302 |
303 bool TestTaskSchedulerTaskRunner::RunsTasksOnCurrentThread() const { | 303 bool TestTaskSchedulerTaskRunner::RunsTasksInCurrentSequence() const { |
304 if (execution_mode_ == ExecutionMode::PARALLEL) | 304 if (execution_mode_ == ExecutionMode::PARALLEL) |
305 return task_scheduler_->RunsTasksOnCurrentThread(); | 305 return task_scheduler_->RunsTasksInCurrentSequence(); |
306 return sequence_token_ == SequenceToken::GetForCurrentThread(); | 306 return sequence_token_ == SequenceToken::GetForCurrentThread(); |
307 } | 307 } |
308 | 308 |
309 TestTaskSchedulerTaskRunner::~TestTaskSchedulerTaskRunner() = default; | 309 TestTaskSchedulerTaskRunner::~TestTaskSchedulerTaskRunner() = default; |
310 | 310 |
311 } // namespace | 311 } // namespace |
312 | 312 |
313 ScopedTaskScheduler::ScopedTaskScheduler() : ScopedTaskScheduler(nullptr) {} | 313 ScopedTaskScheduler::ScopedTaskScheduler() : ScopedTaskScheduler(nullptr) {} |
314 | 314 |
315 ScopedTaskScheduler::ScopedTaskScheduler(MessageLoop* message_loop) { | 315 ScopedTaskScheduler::ScopedTaskScheduler(MessageLoop* message_loop) { |
316 DCHECK(!TaskScheduler::GetInstance()); | 316 DCHECK(!TaskScheduler::GetInstance()); |
317 TaskScheduler::SetInstance(MakeUnique<TestTaskScheduler>(message_loop)); | 317 TaskScheduler::SetInstance(MakeUnique<TestTaskScheduler>(message_loop)); |
318 task_scheduler_ = TaskScheduler::GetInstance(); | 318 task_scheduler_ = TaskScheduler::GetInstance(); |
319 } | 319 } |
320 | 320 |
321 ScopedTaskScheduler::~ScopedTaskScheduler() { | 321 ScopedTaskScheduler::~ScopedTaskScheduler() { |
322 DCHECK(thread_checker_.CalledOnValidThread()); | 322 DCHECK(thread_checker_.CalledOnValidThread()); |
323 DCHECK_EQ(task_scheduler_, TaskScheduler::GetInstance()); | 323 DCHECK_EQ(task_scheduler_, TaskScheduler::GetInstance()); |
324 | 324 |
325 // Per contract, call JoinForTesting() before deleting the TaskScheduler. | 325 // Per contract, call JoinForTesting() before deleting the TaskScheduler. |
326 TaskScheduler::GetInstance()->JoinForTesting(); | 326 TaskScheduler::GetInstance()->JoinForTesting(); |
327 | 327 |
328 TaskScheduler::SetInstance(nullptr); | 328 TaskScheduler::SetInstance(nullptr); |
329 } | 329 } |
330 | 330 |
331 } // namespace test | 331 } // namespace test |
332 } // namespace base | 332 } // namespace base |
OLD | NEW |