OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ | 5 #ifndef CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ |
6 #define CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ | 6 #define CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ |
7 | 7 |
8 #include <limits> | 8 #include <limits> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/test/simple_test_tick_clock.h" |
15 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
16 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
17 #include "cc/test/test_now_source.h" | |
18 | 18 |
19 namespace cc { | 19 namespace cc { |
20 | 20 |
21 // Subclass of TestPendingTask which has a unique ID for every task, supports | 21 // Subclass of TestPendingTask which has a unique ID for every task, supports |
22 // being used inside a std::set and has debug tracing support. | 22 // being used inside a std::set and has debug tracing support. |
23 class TestOrderablePendingTask : public base::TestPendingTask { | 23 class TestOrderablePendingTask : public base::TestPendingTask { |
24 public: | 24 public: |
25 TestOrderablePendingTask(); | 25 TestOrderablePendingTask(); |
26 TestOrderablePendingTask(const tracked_objects::Location& location, | 26 TestOrderablePendingTask(const tracked_objects::Location& location, |
27 const base::Closure& task, | 27 const base::Closure& task, |
(...skipping 13 matching lines...) Expand all Loading... |
41 private: | 41 private: |
42 static size_t task_id_counter; | 42 static size_t task_id_counter; |
43 const size_t task_id_; | 43 const size_t task_id_; |
44 }; | 44 }; |
45 | 45 |
46 // This runs pending tasks based on task's post_time + delay. | 46 // This runs pending tasks based on task's post_time + delay. |
47 // We should not execute a delayed task sooner than some of the queued tasks | 47 // We should not execute a delayed task sooner than some of the queued tasks |
48 // which don't have a delay even though it is queued early. | 48 // which don't have a delay even though it is queued early. |
49 class OrderedSimpleTaskRunner : public base::SingleThreadTaskRunner { | 49 class OrderedSimpleTaskRunner : public base::SingleThreadTaskRunner { |
50 public: | 50 public: |
51 OrderedSimpleTaskRunner(); | 51 OrderedSimpleTaskRunner(base::SimpleTestTickClock* now_src, bool advance_now); |
52 OrderedSimpleTaskRunner(scoped_refptr<TestNowSource> now_src, | |
53 bool advance_now); | |
54 | 52 |
55 // base::TestSimpleTaskRunner implementation: | 53 // base::TestSimpleTaskRunner implementation: |
56 bool PostDelayedTask(const tracked_objects::Location& from_here, | 54 bool PostDelayedTask(const tracked_objects::Location& from_here, |
57 const base::Closure& task, | 55 const base::Closure& task, |
58 base::TimeDelta delay) override; | 56 base::TimeDelta delay) override; |
59 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 57 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
60 const base::Closure& task, | 58 const base::Closure& task, |
61 base::TimeDelta delay) override; | 59 base::TimeDelta delay) override; |
62 | 60 |
63 bool RunsTasksOnCurrentThread() const override; | 61 bool RunsTasksOnCurrentThread() const override; |
64 | 62 |
| 63 static base::TimeTicks AbsoluteMaxNow(); |
| 64 |
65 // Set a maximum number of tasks to run at once. Useful as a timeout to | 65 // Set a maximum number of tasks to run at once. Useful as a timeout to |
66 // prevent infinite task loops. | 66 // prevent infinite task loops. |
67 static const size_t kAbsoluteMaxTasks; | 67 static const size_t kAbsoluteMaxTasks; |
68 void SetRunTaskLimit(size_t max_tasks) { max_tasks_ = max_tasks; } | 68 void SetRunTaskLimit(size_t max_tasks) { max_tasks_ = max_tasks; } |
69 void ClearRunTaskLimit() { max_tasks_ = kAbsoluteMaxTasks; } | 69 void ClearRunTaskLimit() { max_tasks_ = kAbsoluteMaxTasks; } |
70 | 70 |
71 // Allow task runner to advance now when running tasks. | 71 // Allow task runner to advance now when running tasks. |
72 void SetAutoAdvanceNowToPendingTasks(bool advance_now) { | 72 void SetAutoAdvanceNowToPendingTasks(bool advance_now) { |
73 advance_now_ = advance_now; | 73 advance_now_ = advance_now; |
74 } | 74 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 bool TaskExistedInitiallyCallback( | 131 bool TaskExistedInitiallyCallback( |
132 const std::set<TestOrderablePendingTask>& existing_tasks); | 132 const std::set<TestOrderablePendingTask>& existing_tasks); |
133 bool NowBeforeCallback(base::TimeTicks stop_at); | 133 bool NowBeforeCallback(base::TimeTicks stop_at); |
134 bool AdvanceNowCallback(); | 134 bool AdvanceNowCallback(); |
135 | 135 |
136 ~OrderedSimpleTaskRunner() override; | 136 ~OrderedSimpleTaskRunner() override; |
137 | 137 |
138 base::ThreadChecker thread_checker_; | 138 base::ThreadChecker thread_checker_; |
139 | 139 |
140 bool advance_now_; | 140 bool advance_now_; |
141 scoped_refptr<TestNowSource> now_src_; | 141 // Not owned. |
| 142 base::SimpleTestTickClock* now_src_; |
142 | 143 |
143 size_t max_tasks_; | 144 size_t max_tasks_; |
144 | 145 |
145 bool inside_run_tasks_until_; | 146 bool inside_run_tasks_until_; |
146 std::set<TestOrderablePendingTask> pending_tasks_; | 147 std::set<TestOrderablePendingTask> pending_tasks_; |
147 | 148 |
148 private: | 149 private: |
149 DISALLOW_COPY_AND_ASSIGN(OrderedSimpleTaskRunner); | 150 DISALLOW_COPY_AND_ASSIGN(OrderedSimpleTaskRunner); |
150 }; | 151 }; |
151 | 152 |
152 } // namespace cc | 153 } // namespace cc |
153 | 154 |
154 #endif // CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ | 155 #endif // CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ |
OLD | NEW |