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 <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
11 #include <memory> | 11 #include <memory> |
12 #include <set> | 12 #include <set> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/test/simple_test_tick_clock.h" | 18 #include "base/test/simple_test_tick_clock.h" |
19 #include "base/test/test_simple_task_runner.h" | 19 #include "base/test/test_simple_task_runner.h" |
20 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
21 #include "base/trace_event/trace_event.h" | 21 #include "base/trace_event/trace_event.h" |
22 | 22 |
23 namespace cc { | 23 namespace cc { |
24 | 24 |
25 // Subclass of TestPendingTask which has a unique ID for every task, supports | 25 // Subclass of TestPendingTask which has a unique ID for every task, supports |
26 // being used inside a std::set and has debug tracing support. | 26 // being used inside a std::set and has debug tracing support. |
27 class TestOrderablePendingTask : public base::TestPendingTask { | 27 class TestOrderablePendingTask : public base::TestPendingTask { |
28 public: | 28 public: |
29 TestOrderablePendingTask(); | 29 TestOrderablePendingTask(); |
| 30 TestOrderablePendingTask(TestOrderablePendingTask&&); |
30 TestOrderablePendingTask(const tracked_objects::Location& location, | 31 TestOrderablePendingTask(const tracked_objects::Location& location, |
31 const base::Closure& task, | 32 base::OnceClosure task, |
32 base::TimeTicks post_time, | 33 base::TimeTicks post_time, |
33 base::TimeDelta delay, | 34 base::TimeDelta delay, |
34 TestNestability nestability); | 35 TestNestability nestability); |
35 ~TestOrderablePendingTask(); | 36 ~TestOrderablePendingTask(); |
36 | 37 |
37 // operators needed by std::set and comparison | 38 // operators needed by std::set and comparison |
38 bool operator==(const TestOrderablePendingTask& other) const; | 39 bool operator==(const TestOrderablePendingTask& other) const; |
39 bool operator<(const TestOrderablePendingTask& other) const; | 40 bool operator<(const TestOrderablePendingTask& other) const; |
40 | 41 |
41 // base::trace_event tracing functionality | 42 // base::trace_event tracing functionality |
42 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> AsValue() const; | 43 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> AsValue() const; |
43 void AsValueInto(base::trace_event::TracedValue* state) const; | 44 void AsValueInto(base::trace_event::TracedValue* state) const; |
44 | 45 |
| 46 size_t task_id() const { return task_id_; } |
| 47 |
45 private: | 48 private: |
46 static size_t task_id_counter; | 49 static size_t task_id_counter; |
47 const size_t task_id_; | 50 const size_t task_id_; |
48 }; | 51 }; |
49 | 52 |
50 // This runs pending tasks based on task's post_time + delay. | 53 // This runs pending tasks based on task's post_time + delay. |
51 // We should not execute a delayed task sooner than some of the queued tasks | 54 // We should not execute a delayed task sooner than some of the queued tasks |
52 // which don't have a delay even though it is queued early. | 55 // which don't have a delay even though it is queued early. |
53 class OrderedSimpleTaskRunner : public base::SingleThreadTaskRunner { | 56 class OrderedSimpleTaskRunner : public base::SingleThreadTaskRunner { |
54 public: | 57 public: |
55 OrderedSimpleTaskRunner(base::SimpleTestTickClock* now_src, bool advance_now); | 58 OrderedSimpleTaskRunner(base::SimpleTestTickClock* now_src, bool advance_now); |
56 | 59 |
57 // base::TestSimpleTaskRunner implementation: | 60 // base::TestSimpleTaskRunner implementation: |
58 bool PostDelayedTask(const tracked_objects::Location& from_here, | 61 bool PostDelayedTask(const tracked_objects::Location& from_here, |
59 const base::Closure& task, | 62 base::OnceClosure task, |
60 base::TimeDelta delay) override; | 63 base::TimeDelta delay) override; |
61 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 64 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
62 const base::Closure& task, | 65 base::OnceClosure task, |
63 base::TimeDelta delay) override; | 66 base::TimeDelta delay) override; |
64 | 67 |
65 bool RunsTasksOnCurrentThread() const override; | 68 bool RunsTasksOnCurrentThread() const override; |
66 | 69 |
67 static base::TimeTicks AbsoluteMaxNow(); | 70 static base::TimeTicks AbsoluteMaxNow(); |
68 | 71 |
69 // Set a maximum number of tasks to run at once. Useful as a timeout to | 72 // Set a maximum number of tasks to run at once. Useful as a timeout to |
70 // prevent infinite task loops. | 73 // prevent infinite task loops. |
71 static const size_t kAbsoluteMaxTasks; | 74 static const size_t kAbsoluteMaxTasks; |
72 void SetRunTaskLimit(size_t max_tasks) { max_tasks_ = max_tasks; } | 75 void SetRunTaskLimit(size_t max_tasks) { max_tasks_ = max_tasks; } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 base::Callback<bool(void)> TaskExistedInitially(); | 128 base::Callback<bool(void)> TaskExistedInitially(); |
126 | 129 |
127 // Stop running tasks when NextTaskTime() >= stop_at | 130 // Stop running tasks when NextTaskTime() >= stop_at |
128 base::Callback<bool(void)> NowBefore(base::TimeTicks stop_at); | 131 base::Callback<bool(void)> NowBefore(base::TimeTicks stop_at); |
129 | 132 |
130 // Advance Now() to the next task to run. | 133 // Advance Now() to the next task to run. |
131 base::Callback<bool(void)> AdvanceNow(); | 134 base::Callback<bool(void)> AdvanceNow(); |
132 | 135 |
133 protected: | 136 protected: |
134 static bool TaskRunCountBelowCallback(size_t max_tasks, size_t* task_run); | 137 static bool TaskRunCountBelowCallback(size_t max_tasks, size_t* task_run); |
135 bool TaskExistedInitiallyCallback( | 138 bool TaskExistedInitiallyCallback(const std::set<size_t>& existing_tasks); |
136 const std::set<TestOrderablePendingTask>& existing_tasks); | |
137 bool NowBeforeCallback(base::TimeTicks stop_at); | 139 bool NowBeforeCallback(base::TimeTicks stop_at); |
138 bool AdvanceNowCallback(); | 140 bool AdvanceNowCallback(); |
139 | 141 |
140 ~OrderedSimpleTaskRunner() override; | 142 ~OrderedSimpleTaskRunner() override; |
141 | 143 |
142 base::ThreadChecker thread_checker_; | 144 base::ThreadChecker thread_checker_; |
143 | 145 |
144 bool advance_now_; | 146 bool advance_now_; |
145 // Not owned. | 147 // Not owned. |
146 base::SimpleTestTickClock* now_src_; | 148 base::SimpleTestTickClock* now_src_; |
147 | 149 |
148 size_t max_tasks_; | 150 size_t max_tasks_; |
149 | 151 |
150 bool inside_run_tasks_until_; | 152 bool inside_run_tasks_until_; |
151 std::set<TestOrderablePendingTask> pending_tasks_; | 153 std::set<TestOrderablePendingTask> pending_tasks_; |
152 | 154 |
153 private: | 155 private: |
154 DISALLOW_COPY_AND_ASSIGN(OrderedSimpleTaskRunner); | 156 DISALLOW_COPY_AND_ASSIGN(OrderedSimpleTaskRunner); |
155 }; | 157 }; |
156 | 158 |
157 } // namespace cc | 159 } // namespace cc |
158 | 160 |
159 #endif // CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ | 161 #endif // CC_TEST_ORDERED_SIMPLE_TASK_RUNNER_H_ |
OLD | NEW |