| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/test_simple_task_runner.h" | 5 #include "base/test/test_simple_task_runner.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace base { | 9 namespace base { |
| 10 | 10 |
| 11 TestSimpleTaskRunner::TestSimpleTaskRunner() {} | 11 TestSimpleTaskRunner::TestSimpleTaskRunner() = default; |
| 12 | 12 |
| 13 TestSimpleTaskRunner::~TestSimpleTaskRunner() { | 13 TestSimpleTaskRunner::~TestSimpleTaskRunner() = default; |
| 14 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 15 } | |
| 16 | 14 |
| 17 bool TestSimpleTaskRunner::PostDelayedTask( | 15 bool TestSimpleTaskRunner::PostDelayedTask( |
| 18 const tracked_objects::Location& from_here, | 16 const tracked_objects::Location& from_here, |
| 19 const Closure& task, | 17 const Closure& task, |
| 20 TimeDelta delay) { | 18 TimeDelta delay) { |
| 21 DCHECK(thread_checker_.CalledOnValidThread()); | 19 AutoLock auto_lock(lock_); |
| 22 pending_tasks_.push_back( | 20 pending_tasks_.push_back( |
| 23 TestPendingTask(from_here, task, TimeTicks(), delay, | 21 TestPendingTask(from_here, task, TimeTicks(), delay, |
| 24 TestPendingTask::NESTABLE)); | 22 TestPendingTask::NESTABLE)); |
| 25 return true; | 23 return true; |
| 26 } | 24 } |
| 27 | 25 |
| 28 bool TestSimpleTaskRunner::PostNonNestableDelayedTask( | 26 bool TestSimpleTaskRunner::PostNonNestableDelayedTask( |
| 29 const tracked_objects::Location& from_here, | 27 const tracked_objects::Location& from_here, |
| 30 const Closure& task, | 28 const Closure& task, |
| 31 TimeDelta delay) { | 29 TimeDelta delay) { |
| 32 DCHECK(thread_checker_.CalledOnValidThread()); | 30 AutoLock auto_lock(lock_); |
| 33 pending_tasks_.push_back( | 31 pending_tasks_.push_back( |
| 34 TestPendingTask(from_here, task, TimeTicks(), delay, | 32 TestPendingTask(from_here, task, TimeTicks(), delay, |
| 35 TestPendingTask::NON_NESTABLE)); | 33 TestPendingTask::NON_NESTABLE)); |
| 36 return true; | 34 return true; |
| 37 } | 35 } |
| 38 | 36 |
| 39 bool TestSimpleTaskRunner::RunsTasksOnCurrentThread() const { | 37 bool TestSimpleTaskRunner::RunsTasksOnCurrentThread() const { |
| 40 DCHECK(thread_checker_.CalledOnValidThread()); | 38 return thread_ref_ == PlatformThread::CurrentRef(); |
| 41 return true; | |
| 42 } | 39 } |
| 43 | 40 |
| 44 const std::deque<TestPendingTask>& | 41 std::deque<TestPendingTask> TestSimpleTaskRunner::GetPendingTasks() const { |
| 45 TestSimpleTaskRunner::GetPendingTasks() const { | 42 AutoLock auto_lock(lock_); |
| 46 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 47 return pending_tasks_; | 43 return pending_tasks_; |
| 48 } | 44 } |
| 49 | 45 |
| 46 size_t TestSimpleTaskRunner::NumPendingTasks() const { |
| 47 AutoLock auto_lock(lock_); |
| 48 return pending_tasks_.size(); |
| 49 } |
| 50 |
| 50 bool TestSimpleTaskRunner::HasPendingTask() const { | 51 bool TestSimpleTaskRunner::HasPendingTask() const { |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); | 52 AutoLock auto_lock(lock_); |
| 52 return !pending_tasks_.empty(); | 53 return !pending_tasks_.empty(); |
| 53 } | 54 } |
| 54 | 55 |
| 55 base::TimeDelta TestSimpleTaskRunner::NextPendingTaskDelay() const { | 56 base::TimeDelta TestSimpleTaskRunner::NextPendingTaskDelay() const { |
| 56 DCHECK(thread_checker_.CalledOnValidThread()); | 57 AutoLock auto_lock(lock_); |
| 57 return pending_tasks_.front().GetTimeToRun() - base::TimeTicks(); | 58 return pending_tasks_.front().GetTimeToRun() - base::TimeTicks(); |
| 58 } | 59 } |
| 59 | 60 |
| 60 void TestSimpleTaskRunner::ClearPendingTasks() { | 61 void TestSimpleTaskRunner::ClearPendingTasks() { |
| 61 DCHECK(thread_checker_.CalledOnValidThread()); | 62 AutoLock auto_lock(lock_); |
| 62 pending_tasks_.clear(); | 63 pending_tasks_.clear(); |
| 63 } | 64 } |
| 64 | 65 |
| 65 void TestSimpleTaskRunner::RunPendingTasks() { | 66 void TestSimpleTaskRunner::RunPendingTasks() { |
| 66 DCHECK(thread_checker_.CalledOnValidThread()); | 67 DCHECK(RunsTasksOnCurrentThread()); |
| 68 |
| 67 // Swap with a local variable to avoid re-entrancy problems. | 69 // Swap with a local variable to avoid re-entrancy problems. |
| 68 std::deque<TestPendingTask> tasks_to_run; | 70 std::deque<TestPendingTask> tasks_to_run; |
| 69 tasks_to_run.swap(pending_tasks_); | 71 { |
| 70 for (std::deque<TestPendingTask>::iterator it = tasks_to_run.begin(); | 72 AutoLock auto_lock(lock_); |
| 71 it != tasks_to_run.end(); ++it) { | 73 tasks_to_run.swap(pending_tasks_); |
| 72 it->task.Run(); | |
| 73 } | 74 } |
| 75 |
| 76 for (const auto& task : tasks_to_run) |
| 77 task.task.Run(); |
| 74 } | 78 } |
| 75 | 79 |
| 76 void TestSimpleTaskRunner::RunUntilIdle() { | 80 void TestSimpleTaskRunner::RunUntilIdle() { |
| 77 while (!pending_tasks_.empty()) { | 81 while (!pending_tasks_.empty()) { |
| 78 RunPendingTasks(); | 82 RunPendingTasks(); |
| 79 } | 83 } |
| 80 } | 84 } |
| 81 | 85 |
| 82 } // namespace base | 86 } // namespace base |
| OLD | NEW |