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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 AutoLock auto_lock(lock_); | 34 AutoLock auto_lock(lock_); |
35 pending_tasks_.push_back(TestPendingTask(from_here, std::move(task), | 35 pending_tasks_.push_back(TestPendingTask(from_here, std::move(task), |
36 TimeTicks(), delay, | 36 TimeTicks(), delay, |
37 TestPendingTask::NON_NESTABLE)); | 37 TestPendingTask::NON_NESTABLE)); |
38 return true; | 38 return true; |
39 } | 39 } |
40 | 40 |
41 // TODO(gab): Use SequenceToken here to differentiate between tasks running in | 41 // TODO(gab): Use SequenceToken here to differentiate between tasks running in |
42 // the scope of this TestSimpleTaskRunner and other task runners sharing this | 42 // the scope of this TestSimpleTaskRunner and other task runners sharing this |
43 // thread. http://crbug.com/631186 | 43 // thread. http://crbug.com/631186 |
44 bool TestSimpleTaskRunner::RunsTasksOnCurrentThread() const { | 44 bool TestSimpleTaskRunner::RunsTasksInCurrentSequence() const { |
45 return thread_ref_ == PlatformThread::CurrentRef(); | 45 return thread_ref_ == PlatformThread::CurrentRef(); |
46 } | 46 } |
47 | 47 |
48 std::deque<TestPendingTask> TestSimpleTaskRunner::TakePendingTasks() { | 48 std::deque<TestPendingTask> TestSimpleTaskRunner::TakePendingTasks() { |
49 AutoLock auto_lock(lock_); | 49 AutoLock auto_lock(lock_); |
50 return std::move(pending_tasks_); | 50 return std::move(pending_tasks_); |
51 } | 51 } |
52 | 52 |
53 size_t TestSimpleTaskRunner::NumPendingTasks() const { | 53 size_t TestSimpleTaskRunner::NumPendingTasks() const { |
54 AutoLock auto_lock(lock_); | 54 AutoLock auto_lock(lock_); |
(...skipping 14 matching lines...) Expand all Loading... |
69 AutoLock auto_lock(lock_); | 69 AutoLock auto_lock(lock_); |
70 return pending_tasks_.back().GetTimeToRun() - base::TimeTicks(); | 70 return pending_tasks_.back().GetTimeToRun() - base::TimeTicks(); |
71 } | 71 } |
72 | 72 |
73 void TestSimpleTaskRunner::ClearPendingTasks() { | 73 void TestSimpleTaskRunner::ClearPendingTasks() { |
74 AutoLock auto_lock(lock_); | 74 AutoLock auto_lock(lock_); |
75 pending_tasks_.clear(); | 75 pending_tasks_.clear(); |
76 } | 76 } |
77 | 77 |
78 void TestSimpleTaskRunner::RunPendingTasks() { | 78 void TestSimpleTaskRunner::RunPendingTasks() { |
79 DCHECK(RunsTasksOnCurrentThread()); | 79 DCHECK(RunsTasksInCurrentSequence()); |
80 | 80 |
81 // Swap with a local variable to avoid re-entrancy problems. | 81 // Swap with a local variable to avoid re-entrancy problems. |
82 std::deque<TestPendingTask> tasks_to_run; | 82 std::deque<TestPendingTask> tasks_to_run; |
83 { | 83 { |
84 AutoLock auto_lock(lock_); | 84 AutoLock auto_lock(lock_); |
85 tasks_to_run.swap(pending_tasks_); | 85 tasks_to_run.swap(pending_tasks_); |
86 } | 86 } |
87 | 87 |
88 // Multiple test task runners can share the same thread for determinism in | 88 // Multiple test task runners can share the same thread for determinism in |
89 // unit tests. Make sure this TestSimpleTaskRunner's tasks run in its scope. | 89 // unit tests. Make sure this TestSimpleTaskRunner's tasks run in its scope. |
90 ScopedClosureRunner undo_override; | 90 ScopedClosureRunner undo_override; |
91 if (!ThreadTaskRunnerHandle::IsSet() || | 91 if (!ThreadTaskRunnerHandle::IsSet() || |
92 ThreadTaskRunnerHandle::Get() != this) { | 92 ThreadTaskRunnerHandle::Get() != this) { |
93 undo_override = ThreadTaskRunnerHandle::OverrideForTesting(this); | 93 undo_override = ThreadTaskRunnerHandle::OverrideForTesting(this); |
94 } | 94 } |
95 | 95 |
96 for (auto& task : tasks_to_run) | 96 for (auto& task : tasks_to_run) |
97 std::move(task.task).Run(); | 97 std::move(task.task).Run(); |
98 } | 98 } |
99 | 99 |
100 void TestSimpleTaskRunner::RunUntilIdle() { | 100 void TestSimpleTaskRunner::RunUntilIdle() { |
101 while (!pending_tasks_.empty()) { | 101 while (!pending_tasks_.empty()) { |
102 RunPendingTasks(); | 102 RunPendingTasks(); |
103 } | 103 } |
104 } | 104 } |
105 | 105 |
106 } // namespace base | 106 } // namespace base |
OLD | NEW |