OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_ | 5 #ifndef CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_ |
6 #define CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_ | 6 #define CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/pending_task.h" | 10 #include "base/pending_task.h" |
11 #include "base/threading/thread_checker.h" | 11 #include "base/threading/thread_checker.h" |
12 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
13 #include "base/trace_event/trace_event_argument.h" | 13 #include "base/trace_event/trace_event_argument.h" |
14 #include "components/scheduler/child/lazy_now.h" | 14 #include "components/scheduler/child/lazy_now.h" |
15 #include "components/scheduler/child/task_queue.h" | 15 #include "components/scheduler/child/task_queue.h" |
16 #include "components/scheduler/scheduler_export.h" | 16 #include "components/scheduler/scheduler_export.h" |
17 | 17 |
18 namespace scheduler { | 18 namespace scheduler { |
19 class TaskQueueManager; | 19 class TaskQueueManager; |
20 | 20 |
21 namespace internal { | 21 namespace internal { |
22 | 22 |
23 class SCHEDULER_EXPORT TaskQueueImpl final : public TaskQueue { | 23 class SCHEDULER_EXPORT TaskQueueImpl final : public TaskQueue { |
24 public: | 24 public: |
25 TaskQueueImpl(TaskQueueManager* task_queue_manager, | 25 TaskQueueImpl(TaskQueueManager* task_queue_manager, |
26 const Spec& spec, | 26 const Spec& spec, |
27 const char* disabled_by_default_tracing_category, | 27 const char* disabled_by_default_tracing_category, |
28 const char* disabled_by_default_verbose_tracing_category); | 28 const char* disabled_by_default_verbose_tracing_category); |
29 | 29 |
30 class SCHEDULER_EXPORT Task : public base::PendingTask { | |
31 public: | |
32 Task(); | |
33 Task(const tracked_objects::Location& posted_from, | |
34 const base::Closure& task, | |
35 int sequence_number, | |
36 bool nestable); | |
37 | |
38 int enqueue_order() const { | |
39 #ifndef NDEBUG | |
40 DCHECK(enqueue_order_set_); | |
41 #endif | |
42 return enqueue_order_; | |
43 } | |
44 | |
45 void set_enqueue_order(int enqueue_order) { | |
46 #ifndef NDEBUG | |
47 DCHECK(!enqueue_order_set_); | |
48 enqueue_order_set_ = true; | |
49 #endif | |
50 enqueue_order_ = enqueue_order; | |
51 } | |
52 | |
53 private: | |
54 #ifndef NDEBUG | |
55 bool enqueue_order_set_; | |
56 #endif | |
57 // Similar to sequence number, but the |enqueue_order| is set by | |
58 // EnqueueTasksLocked and is not initially defined for delayed tasks until | |
59 // they are enqueued on the |incoming_queue_|. | |
60 int enqueue_order_; | |
61 }; | |
62 | |
63 // TaskQueue implementation. | 30 // TaskQueue implementation. |
64 bool RunsTasksOnCurrentThread() const override; | 31 bool RunsTasksOnCurrentThread() const override; |
65 bool PostDelayedTask(const tracked_objects::Location& from_here, | 32 bool PostDelayedTask(const tracked_objects::Location& from_here, |
66 const base::Closure& task, | 33 const base::Closure& task, |
67 base::TimeDelta delay) override; | 34 base::TimeDelta delay) override; |
68 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 35 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
69 const base::Closure& task, | 36 const base::Closure& task, |
70 base::TimeDelta delay) override; | 37 base::TimeDelta delay) override; |
71 bool PostDelayedTaskAt(const tracked_objects::Location& from_here, | 38 bool PostDelayedTaskAt(const tracked_objects::Location& from_here, |
72 const base::Closure& task, | 39 const base::Closure& task, |
73 base::TimeTicks desired_run_time) override; | 40 base::TimeTicks desired_run_time) override; |
74 | 41 |
75 bool IsQueueEnabled() const override; | 42 bool IsQueueEnabled() const override; |
76 QueueState GetQueueState() const override; | 43 QueueState GetQueueState() const override; |
77 void SetQueuePriority(QueuePriority priority) override; | 44 void SetQueuePriority(QueuePriority priority) override; |
78 void PumpQueue() override; | 45 void PumpQueue() override; |
79 void SetPumpPolicy(PumpPolicy pump_policy) override; | 46 void SetPumpPolicy(PumpPolicy pump_policy) override; |
80 | 47 |
81 bool NextPendingDelayedTaskRunTime( | 48 bool NextPendingDelayedTaskRunTime( |
82 base::TimeTicks* next_pending_delayed_task); | 49 base::TimeTicks* next_pending_delayed_task); |
83 | 50 |
84 void UpdateWorkQueue(LazyNow* lazy_now, | 51 void UpdateWorkQueue(LazyNow* lazy_now, |
85 bool should_trigger_wakeup, | 52 bool should_trigger_wakeup, |
86 const Task* previous_task); | 53 const base::PendingTask* previous_task); |
87 Task TakeTaskFromWorkQueue(); | 54 base::PendingTask TakeTaskFromWorkQueue(); |
88 | 55 |
89 void WillDeleteTaskQueueManager(); | 56 void WillDeleteTaskQueueManager(); |
90 | 57 |
91 std::queue<Task>& work_queue() { return work_queue_; } | 58 base::TaskQueue& work_queue() { return work_queue_; } |
92 | 59 |
93 WakeupPolicy wakeup_policy() const { | 60 WakeupPolicy wakeup_policy() const { |
94 DCHECK(main_thread_checker_.CalledOnValidThread()); | 61 DCHECK(main_thread_checker_.CalledOnValidThread()); |
95 return wakeup_policy_; | 62 return wakeup_policy_; |
96 } | 63 } |
97 | 64 |
98 const char* GetName() const override; | 65 const char* GetName() const override; |
99 | 66 |
100 void AsValueInto(base::trace_event::TracedValue* state) const; | 67 void AsValueInto(base::trace_event::TracedValue* state) const; |
101 | 68 |
102 size_t get_task_queue_set_index() const { return set_index_; } | 69 size_t get_task_queue_set_index() const { return set_index_; } |
103 | 70 |
104 void set_task_queue_set_index(size_t set_index) { set_index_ = set_index; } | 71 void set_task_queue_set_index(size_t set_index) { set_index_ = set_index; } |
105 | 72 |
106 // If the work queue isn't empty, |enqueue_order| gets set to the enqueue | 73 // If the work queue isn't empty, |age| gets set to the sequence number of the |
107 // order of the front task and the function returns true. Otherwise the | 74 // front task and the dunctio returns true. Otherwise the function returns |
108 // function returns false. | 75 // false. |
109 bool GetWorkQueueFrontTaskEnqueueOrder(int* enqueue_order) const; | 76 bool GetWorkQueueFrontTaskAge(int* age) const; |
110 | 77 |
111 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_; } | 78 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_; } |
112 bool GetShouldNotifyObservers() const { return should_notify_observers_; } | 79 bool GetShouldNotifyObservers() const { return should_notify_observers_; } |
113 | 80 |
114 // Test support functions. These should not be used in production code. | 81 // Test support functions. These should not be used in production code. |
115 void PushTaskOntoWorkQueueForTest(const Task& task); | 82 void PushTaskOntoWorkQueueForTest(const base::PendingTask& task); |
116 void PopTaskFromWorkQueueForTest(); | 83 void PopTaskFromWorkQueueForTest(); |
117 size_t WorkQueueSizeForTest() const { return work_queue_.size(); } | 84 size_t WorkQueueSizeForTest() const { return work_queue_.size(); } |
118 | 85 |
119 // Can be called on any thread. | 86 // Can be called on any thread. |
120 static const char* PumpPolicyToString(TaskQueue::PumpPolicy pump_policy); | 87 static const char* PumpPolicyToString(TaskQueue::PumpPolicy pump_policy); |
121 | 88 |
122 // Can be called on any thread. | 89 // Can be called on any thread. |
123 static const char* WakeupPolicyToString( | 90 static const char* WakeupPolicyToString( |
124 TaskQueue::WakeupPolicy wakeup_policy); | 91 TaskQueue::WakeupPolicy wakeup_policy); |
125 | 92 |
(...skipping 26 matching lines...) Expand all Loading... |
152 // Enqueues any delayed tasks which should be run now on the incoming_queue_ | 119 // Enqueues any delayed tasks which should be run now on the incoming_queue_ |
153 // and calls ScheduleDelayedWorkLocked to ensure future tasks are scheduled. | 120 // and calls ScheduleDelayedWorkLocked to ensure future tasks are scheduled. |
154 // Must be called with |lock_| locked. | 121 // Must be called with |lock_| locked. |
155 void MoveReadyDelayedTasksToIncomingQueueLocked(LazyNow* lazy_now); | 122 void MoveReadyDelayedTasksToIncomingQueueLocked(LazyNow* lazy_now); |
156 | 123 |
157 // Posts MoveReadyDelayedTasksToIncomingQueue if there isn't already a task | 124 // Posts MoveReadyDelayedTasksToIncomingQueue if there isn't already a task |
158 // posted on the underlying runloop for the next task's scheduled run time. | 125 // posted on the underlying runloop for the next task's scheduled run time. |
159 void ScheduleDelayedWorkLocked(LazyNow* lazy_now); | 126 void ScheduleDelayedWorkLocked(LazyNow* lazy_now); |
160 | 127 |
161 void PumpQueueLocked(); | 128 void PumpQueueLocked(); |
162 bool TaskIsOlderThanQueuedTasks(const Task* task); | 129 bool TaskIsOlderThanQueuedTasks(const base::PendingTask* task); |
163 bool ShouldAutoPumpQueueLocked(bool should_trigger_wakeup, | 130 bool ShouldAutoPumpQueueLocked(bool should_trigger_wakeup, |
164 const Task* previous_task); | 131 const base::PendingTask* previous_task); |
165 | 132 |
166 enum class EnqueueOrderPolicy { SET_ENQUEUE_ORDER, DONT_SET_ENQUEUE_ORDER }; | 133 // Push the task onto the |incoming_queue_| and allocate a sequence number |
167 | 134 // for it. |
168 // Push the task onto the |incoming_queue_| and maybe allocate an | 135 void EnqueueTaskLocked(const base::PendingTask& pending_task); |
169 // enqueue_order for it based on |enqueue_order_policy|. | |
170 void EnqueueTaskLocked(const Task& pending_task, | |
171 EnqueueOrderPolicy enqueue_order_policy); | |
172 | 136 |
173 void TraceQueueSize(bool is_locked) const; | 137 void TraceQueueSize(bool is_locked) const; |
174 static void QueueAsValueInto(const std::queue<Task>& queue, | 138 static void QueueAsValueInto(const base::TaskQueue& queue, |
175 base::trace_event::TracedValue* state); | 139 base::trace_event::TracedValue* state); |
176 static void QueueAsValueInto(const std::priority_queue<Task>& queue, | 140 static void QueueAsValueInto(const base::DelayedTaskQueue& queue, |
177 base::trace_event::TracedValue* state); | 141 base::trace_event::TracedValue* state); |
178 static void TaskAsValueInto(const Task& task, | 142 static void TaskAsValueInto(const base::PendingTask& task, |
179 base::trace_event::TracedValue* state); | 143 base::trace_event::TracedValue* state); |
180 | 144 |
181 // This lock protects all members in the contigious block below. | 145 // This lock protects all members in the contigious block below. |
182 // TODO(alexclarke): Group all the members protected by the lock into a struct | 146 // TODO(alexclarke): Group all the members protected by the lock into a struct |
183 mutable base::Lock lock_; | 147 mutable base::Lock lock_; |
184 base::PlatformThreadId thread_id_; | 148 base::PlatformThreadId thread_id_; |
185 TaskQueueManager* task_queue_manager_; | 149 TaskQueueManager* task_queue_manager_; |
186 std::queue<Task> incoming_queue_; | 150 base::TaskQueue incoming_queue_; |
187 PumpPolicy pump_policy_; | 151 PumpPolicy pump_policy_; |
188 std::priority_queue<Task> delayed_task_queue_; | 152 // Queue-local task sequence number for maintaining the order of delayed |
| 153 // tasks which are posted for the exact same time. Note that this will be |
| 154 // replaced by the global sequence number when the delay has elapsed. |
| 155 int delayed_task_sequence_number_; |
| 156 base::DelayedTaskQueue delayed_task_queue_; |
189 std::set<base::TimeTicks> in_flight_kick_delayed_tasks_; | 157 std::set<base::TimeTicks> in_flight_kick_delayed_tasks_; |
190 | 158 |
191 const char* name_; | 159 const char* name_; |
192 const char* disabled_by_default_tracing_category_; | 160 const char* disabled_by_default_tracing_category_; |
193 const char* disabled_by_default_verbose_tracing_category_; | 161 const char* disabled_by_default_verbose_tracing_category_; |
194 | 162 |
195 base::ThreadChecker main_thread_checker_; | 163 base::ThreadChecker main_thread_checker_; |
196 std::queue<Task> work_queue_; | 164 base::TaskQueue work_queue_; |
197 WakeupPolicy wakeup_policy_; | 165 WakeupPolicy wakeup_policy_; |
198 size_t set_index_; | 166 size_t set_index_; |
199 bool should_monitor_quiescence_; | 167 bool should_monitor_quiescence_; |
200 bool should_notify_observers_; | 168 bool should_notify_observers_; |
201 | 169 |
202 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); | 170 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); |
203 }; | 171 }; |
204 | 172 |
205 } // namespace internal | 173 } // namespace internal |
206 } // namespace scheduler | 174 } // namespace scheduler |
207 | 175 |
208 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_ | 176 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_ |
OLD | NEW |