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 THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ | 5 #ifndef THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ |
6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ | 6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 10 matching lines...) Expand all Loading... | |
21 namespace blink { | 21 namespace blink { |
22 namespace scheduler { | 22 namespace scheduler { |
23 class LazyNow; | 23 class LazyNow; |
24 class TimeDomain; | 24 class TimeDomain; |
25 class TaskQueueManager; | 25 class TaskQueueManager; |
26 | 26 |
27 namespace internal { | 27 namespace internal { |
28 class WorkQueue; | 28 class WorkQueue; |
29 class WorkQueueSets; | 29 class WorkQueueSets; |
30 | 30 |
31 // TaskQueueImpl has four main queues: | |
32 // | |
33 // Immediate (non-delayed) tasks: | |
34 // immediate_incoming_queue - PostTask enqueues tasks here | |
35 // immediate_work_queue | |
36 // | |
37 // Delayed tasks | |
38 // delayed_incoming_queue - PostDelayedTask enqueues tasks here | |
39 // delayed_work_queue | |
40 // | |
41 // The immediate_incoming_queue can be accessed from any thread, the other | |
42 // queues are main-thread only. To reduce the overhead of locking, | |
43 // immediate_work_queue is swapped with immediate_incoming_queue when it becomes | |
Sami
2016/08/18 15:53:43
nit: it => the former
alex clarke (OOO till 29th)
2016/08/18 16:51:41
Done.
| |
44 // empty. | |
45 // | |
46 // Delayed tasks are initially posted to delayed_incoming_queue and a wakeup | |
47 // is scheduled with the TimeDomain. When the delay has elapsed, the TimeDomain | |
48 // calls UpdateDelayedWorkQueue and ready delayed tasks are moved into the | |
49 // delayed_work_queue. Note the EnqueueOrder (used for ordering) for a delayed | |
50 // task is not set until it's moved into the delayed_work_queue. | |
51 // | |
52 // TaskQueueImpl uses the WorkQueueSets and the TaskQueueSelector to implement | |
53 // prioritization. Task selection is done by the TaskQueueSelector and when a | |
54 // queue is selected, it round-robbins between the immediate_work_queue and | |
Sami
2016/08/18 15:53:43
typo: round-robins
alex clarke (OOO till 29th)
2016/08/18 16:51:41
Done.
| |
55 // delayed_work_queue. The reason for this is we want to make sure delayed | |
56 // tasks (normally the most common type) don't starve out immediate work. | |
31 class BLINK_PLATFORM_EXPORT TaskQueueImpl final : public TaskQueue { | 57 class BLINK_PLATFORM_EXPORT TaskQueueImpl final : public TaskQueue { |
32 public: | 58 public: |
33 TaskQueueImpl(TaskQueueManager* task_queue_manager, | 59 TaskQueueImpl(TaskQueueManager* task_queue_manager, |
34 TimeDomain* time_domain, | 60 TimeDomain* time_domain, |
35 const Spec& spec, | 61 const Spec& spec, |
36 const char* disabled_by_default_tracing_category, | 62 const char* disabled_by_default_tracing_category, |
37 const char* disabled_by_default_verbose_tracing_category); | 63 const char* disabled_by_default_verbose_tracing_category); |
38 | 64 |
39 class BLINK_PLATFORM_EXPORT Task : public base::PendingTask { | 65 class BLINK_PLATFORM_EXPORT Task : public base::PendingTask { |
40 public: | 66 public: |
(...skipping 23 matching lines...) Expand all Loading... | |
64 DCHECK(!enqueue_order_set_); | 90 DCHECK(!enqueue_order_set_); |
65 enqueue_order_set_ = true; | 91 enqueue_order_set_ = true; |
66 #endif | 92 #endif |
67 enqueue_order_ = enqueue_order; | 93 enqueue_order_ = enqueue_order; |
68 } | 94 } |
69 | 95 |
70 #ifndef NDEBUG | 96 #ifndef NDEBUG |
71 bool enqueue_order_set() const { return enqueue_order_set_; } | 97 bool enqueue_order_set() const { return enqueue_order_set_; } |
72 #endif | 98 #endif |
73 | 99 |
100 using ComparatorFn = bool (*)(const Task&, const Task&); | |
101 | |
102 // Tasks are ordered by |delayed_run_time| smallest first then and by | |
103 // |sequence_num| in case of a tie. | |
104 class DelayedRunTimeComparator { | |
105 public: | |
106 bool operator()(const Task& a, const Task& b) const; | |
107 }; | |
108 | |
109 // Tasks are ordered by |enqueue_order_| smallest first. | |
110 static bool EnqueueOrderComparatorFn(const TaskQueueImpl::Task& a, | |
111 const TaskQueueImpl::Task& b); | |
112 | |
113 // Tasks are ordered by |delayed_run_time| smallest first then and by | |
114 // |sequence_num| in case of a tie. | |
115 static bool DelayedRunTimeComparatorFn(const TaskQueueImpl::Task& a, | |
116 const TaskQueueImpl::Task& b); | |
117 | |
74 private: | 118 private: |
75 #ifndef NDEBUG | 119 #ifndef NDEBUG |
76 bool enqueue_order_set_; | 120 bool enqueue_order_set_; |
77 #endif | 121 #endif |
78 // Similar to sequence number, but the |enqueue_order| is set by | 122 // Similar to sequence number, but ultimately the |enqueue_order_| is what |
79 // EnqueueTasksLocked and is not initially defined for delayed tasks until | 123 // the scheduler uses for task ordering. For immediate tasks |enqueue_order| |
80 // they are enqueued on the |immediate_incoming_queue_|. | 124 // is set when posted, but for delayed tasks it's defined until they are |
Sami
2016/08/18 15:53:43
it's defined until => it's not defined until?
alex clarke (OOO till 29th)
2016/08/18 16:51:41
Done.
| |
125 // enqueued on the |immediate_incoming_queue_|. This is because otherwise | |
126 // delayed tasks could run before an immediate task posted after the delayed | |
127 // task. | |
81 EnqueueOrder enqueue_order_; | 128 EnqueueOrder enqueue_order_; |
82 }; | 129 }; |
83 | 130 |
84 // TaskQueue implementation. | 131 // TaskQueue implementation. |
85 void UnregisterTaskQueue() override; | 132 void UnregisterTaskQueue() override; |
86 bool RunsTasksOnCurrentThread() const override; | 133 bool RunsTasksOnCurrentThread() const override; |
87 bool PostDelayedTask(const tracked_objects::Location& from_here, | 134 bool PostDelayedTask(const tracked_objects::Location& from_here, |
88 const base::Closure& task, | 135 const base::Closure& task, |
89 base::TimeDelta delay) override; | 136 base::TimeDelta delay) override; |
90 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 137 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
91 const base::Closure& task, | 138 const base::Closure& task, |
92 base::TimeDelta delay) override; | 139 base::TimeDelta delay) override; |
93 | 140 TaskHandle PostCancellableDelayedTask( |
141 const tracked_objects::Location& from_here, | |
142 const base::Closure& task, | |
143 base::TimeDelta delay) override; | |
144 bool CancelTask(const TaskHandle& handle) override; | |
94 void SetQueueEnabled(bool enabled) override; | 145 void SetQueueEnabled(bool enabled) override; |
95 bool IsQueueEnabled() const override; | 146 bool IsQueueEnabled() const override; |
96 bool IsEmpty() const override; | 147 bool IsEmpty() const override; |
97 bool HasPendingImmediateWork() const override; | 148 bool HasPendingImmediateWork() const override; |
98 bool NeedsPumping() const override; | 149 bool NeedsPumping() const override; |
99 void SetQueuePriority(QueuePriority priority) override; | 150 void SetQueuePriority(QueuePriority priority) override; |
100 QueuePriority GetQueuePriority() const override; | 151 QueuePriority GetQueuePriority() const override; |
101 void PumpQueue(LazyNow* lazy_now, bool may_post_dowork) override; | 152 void PumpQueue(LazyNow* lazy_now, bool may_post_dowork) override; |
102 void SetPumpPolicy(PumpPolicy pump_policy) override; | 153 void SetPumpPolicy(PumpPolicy pump_policy) override; |
103 PumpPolicy GetPumpPolicy() const override; | 154 PumpPolicy GetPumpPolicy() const override; |
104 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override; | 155 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override; |
105 void RemoveTaskObserver( | 156 void RemoveTaskObserver( |
106 base::MessageLoop::TaskObserver* task_observer) override; | 157 base::MessageLoop::TaskObserver* task_observer) override; |
107 void SetTimeDomain(TimeDomain* time_domain) override; | 158 void SetTimeDomain(TimeDomain* time_domain) override; |
108 TimeDomain* GetTimeDomain() const override; | 159 TimeDomain* GetTimeDomain() const override; |
109 void SetBlameContext(base::trace_event::BlameContext* blame_context) override; | 160 void SetBlameContext(base::trace_event::BlameContext* blame_context) override; |
110 | 161 |
162 bool IsTaskPending(const TaskHandle& handle) const; | |
163 | |
111 void UpdateImmediateWorkQueue(bool should_trigger_wakeup, | 164 void UpdateImmediateWorkQueue(bool should_trigger_wakeup, |
112 const Task* previous_task); | 165 const Task* previous_task); |
113 void UpdateDelayedWorkQueue(LazyNow* lazy_now, | 166 void UpdateDelayedWorkQueue(LazyNow* lazy_now, |
114 bool should_trigger_wakeup, | 167 bool should_trigger_wakeup, |
115 const Task* previous_task); | 168 const Task* previous_task); |
116 | 169 |
117 WakeupPolicy wakeup_policy() const { | 170 WakeupPolicy wakeup_policy() const { |
118 DCHECK(main_thread_checker_.CalledOnValidThread()); | 171 DCHECK(main_thread_checker_.CalledOnValidThread()); |
119 return wakeup_policy_; | 172 return wakeup_policy_; |
120 } | 173 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 return main_thread_only().immediate_work_queue.get(); | 208 return main_thread_only().immediate_work_queue.get(); |
156 } | 209 } |
157 | 210 |
158 bool should_report_when_execution_blocked() const { | 211 bool should_report_when_execution_blocked() const { |
159 return should_report_when_execution_blocked_; | 212 return should_report_when_execution_blocked_; |
160 } | 213 } |
161 | 214 |
162 private: | 215 private: |
163 friend class WorkQueue; | 216 friend class WorkQueue; |
164 | 217 |
218 // Note both DelayedRunTimeQueue and ComparatorQueue are sets for fast task | |
219 // cancellation. Typically queue sizes are well under 200 so the overhead of | |
220 // std::set vs std::priority_queue and std::queue is lost in the noise of | |
221 // everything else. | |
222 using DelayedRunTimeQueue = std::set<Task, Task::DelayedRunTimeComparator>; | |
223 using ComparatorQueue = std::set<Task, Task::ComparatorFn>; | |
224 | |
165 enum class TaskType { | 225 enum class TaskType { |
166 NORMAL, | 226 NORMAL, |
167 NON_NESTABLE, | 227 NON_NESTABLE, |
168 }; | 228 }; |
169 | 229 |
170 struct AnyThread { | 230 struct AnyThread { |
171 AnyThread(TaskQueueManager* task_queue_manager, | 231 AnyThread(TaskQueueManager* task_queue_manager, |
172 PumpPolicy pump_policy, | 232 PumpPolicy pump_policy, |
173 TimeDomain* time_domain); | 233 TimeDomain* time_domain); |
174 ~AnyThread(); | 234 ~AnyThread(); |
175 | 235 |
176 // TaskQueueManager, PumpPolicy and TimeDomain are maintained in two copies: | 236 // TaskQueueManager, PumpPolicy and TimeDomain are maintained in two copies: |
177 // inside AnyThread and inside MainThreadOnly. They can be changed only from | 237 // inside AnyThread and inside MainThreadOnly. They can be changed only from |
178 // main thread, so it should be locked before accessing from other threads. | 238 // main thread, so it should be locked before accessing from other threads. |
179 TaskQueueManager* task_queue_manager; | 239 TaskQueueManager* task_queue_manager; |
180 PumpPolicy pump_policy; | 240 PumpPolicy pump_policy; |
181 TimeDomain* time_domain; | 241 TimeDomain* time_domain; |
182 | 242 |
183 std::queue<Task> immediate_incoming_queue; | 243 ComparatorQueue immediate_incoming_queue; |
184 }; | 244 }; |
185 | 245 |
186 struct MainThreadOnly { | 246 struct MainThreadOnly { |
187 MainThreadOnly(TaskQueueManager* task_queue_manager, | 247 MainThreadOnly(TaskQueueManager* task_queue_manager, |
188 PumpPolicy pump_policy, | 248 PumpPolicy pump_policy, |
189 TaskQueueImpl* task_queue, | 249 TaskQueueImpl* task_queue, |
190 TimeDomain* time_domain); | 250 TimeDomain* time_domain); |
191 ~MainThreadOnly(); | 251 ~MainThreadOnly(); |
192 | 252 |
193 // Another copy of TaskQueueManager, PumpPolicy and TimeDomain for lock-free | 253 // Another copy of TaskQueueManager, PumpPolicy and TimeDomain for lock-free |
194 // access from the main thread. See description inside struct AnyThread for | 254 // access from the main thread. See description inside struct AnyThread for |
195 // details. | 255 // details. |
196 TaskQueueManager* task_queue_manager; | 256 TaskQueueManager* task_queue_manager; |
197 PumpPolicy pump_policy; | 257 PumpPolicy pump_policy; |
198 TimeDomain* time_domain; | 258 TimeDomain* time_domain; |
199 | 259 |
200 std::unique_ptr<WorkQueue> delayed_work_queue; | 260 std::unique_ptr<WorkQueue> delayed_work_queue; |
201 std::unique_ptr<WorkQueue> immediate_work_queue; | 261 std::unique_ptr<WorkQueue> immediate_work_queue; |
202 std::priority_queue<Task> delayed_incoming_queue; | 262 DelayedRunTimeQueue delayed_incoming_queue; |
203 base::ObserverList<base::MessageLoop::TaskObserver> task_observers; | 263 base::ObserverList<base::MessageLoop::TaskObserver> task_observers; |
204 size_t set_index; | 264 size_t set_index; |
205 bool is_enabled; | 265 bool is_enabled; |
206 base::trace_event::BlameContext* blame_context; // Not owned. | 266 base::trace_event::BlameContext* blame_context; // Not owned. |
207 }; | 267 }; |
208 | 268 |
209 ~TaskQueueImpl() override; | 269 ~TaskQueueImpl() override; |
210 | 270 |
211 bool PostImmediateTaskImpl(const tracked_objects::Location& from_here, | 271 bool PostImmediateTaskImpl(const tracked_objects::Location& from_here, |
212 const base::Closure& task, | 272 const base::Closure& task, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
251 // Must be called from the main thread. | 311 // Must be called from the main thread. |
252 bool ShouldAutoPumpDelayedQueue(bool should_trigger_wakeup, | 312 bool ShouldAutoPumpDelayedQueue(bool should_trigger_wakeup, |
253 const Task* previous_task); | 313 const Task* previous_task); |
254 | 314 |
255 // Push the task onto the |immediate_incoming_queue| and for auto pumped | 315 // Push the task onto the |immediate_incoming_queue| and for auto pumped |
256 // queues it calls MaybePostDoWorkOnMainRunner if the Incoming queue was | 316 // queues it calls MaybePostDoWorkOnMainRunner if the Incoming queue was |
257 // empty. | 317 // empty. |
258 void PushOntoImmediateIncomingQueueLocked(Task pending_task); | 318 void PushOntoImmediateIncomingQueueLocked(Task pending_task); |
259 | 319 |
260 void TraceQueueSize(bool is_locked) const; | 320 void TraceQueueSize(bool is_locked) const; |
261 static void QueueAsValueInto(const std::queue<Task>& queue, | 321 static void QueueAsValueInto(const ComparatorQueue& queue, |
262 base::trace_event::TracedValue* state); | 322 base::trace_event::TracedValue* state); |
263 static void QueueAsValueInto(const std::priority_queue<Task>& queue, | 323 static void QueueAsValueInto(const DelayedRunTimeQueue& queue, |
264 base::trace_event::TracedValue* state); | 324 base::trace_event::TracedValue* state); |
265 static void TaskAsValueInto(const Task& task, | 325 static void TaskAsValueInto(const Task& task, |
266 base::trace_event::TracedValue* state); | 326 base::trace_event::TracedValue* state); |
267 | 327 |
268 const base::PlatformThreadId thread_id_; | 328 const base::PlatformThreadId thread_id_; |
269 | 329 |
270 mutable base::Lock any_thread_lock_; | 330 mutable base::Lock any_thread_lock_; |
271 AnyThread any_thread_; | 331 AnyThread any_thread_; |
272 struct AnyThread& any_thread() { | 332 struct AnyThread& any_thread() { |
273 any_thread_lock_.AssertAcquired(); | 333 any_thread_lock_.AssertAcquired(); |
(...skipping 25 matching lines...) Expand all Loading... | |
299 const bool should_report_when_execution_blocked_; | 359 const bool should_report_when_execution_blocked_; |
300 | 360 |
301 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); | 361 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); |
302 }; | 362 }; |
303 | 363 |
304 } // namespace internal | 364 } // namespace internal |
305 } // namespace scheduler | 365 } // namespace scheduler |
306 } // namespace blink | 366 } // namespace blink |
307 | 367 |
308 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ | 368 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ |
OLD | NEW |