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 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 base::TimeDelta delay) override; | 142 base::TimeDelta delay) override; |
143 TaskHandle PostCancellableDelayedTask( | 143 TaskHandle PostCancellableDelayedTask( |
144 const tracked_objects::Location& from_here, | 144 const tracked_objects::Location& from_here, |
145 const base::Closure& task, | 145 const base::Closure& task, |
146 base::TimeDelta delay) override; | 146 base::TimeDelta delay) override; |
147 bool CancelTask(const TaskHandle& handle) override; | 147 bool CancelTask(const TaskHandle& handle) override; |
148 void SetQueueEnabled(bool enabled) override; | 148 void SetQueueEnabled(bool enabled) override; |
149 bool IsQueueEnabled() const override; | 149 bool IsQueueEnabled() const override; |
150 bool IsEmpty() const override; | 150 bool IsEmpty() const override; |
151 bool HasPendingImmediateWork() const override; | 151 bool HasPendingImmediateWork() const override; |
152 bool NeedsPumping() const override; | |
153 void SetQueuePriority(QueuePriority priority) override; | 152 void SetQueuePriority(QueuePriority priority) override; |
154 QueuePriority GetQueuePriority() const override; | 153 QueuePriority GetQueuePriority() const override; |
155 void PumpQueue(LazyNow* lazy_now, bool may_post_dowork) override; | |
156 void SetPumpPolicy(PumpPolicy pump_policy) override; | |
157 PumpPolicy GetPumpPolicy() const override; | |
158 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override; | 154 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override; |
159 void RemoveTaskObserver( | 155 void RemoveTaskObserver( |
160 base::MessageLoop::TaskObserver* task_observer) override; | 156 base::MessageLoop::TaskObserver* task_observer) override; |
161 void SetTimeDomain(TimeDomain* time_domain) override; | 157 void SetTimeDomain(TimeDomain* time_domain) override; |
162 TimeDomain* GetTimeDomain() const override; | 158 TimeDomain* GetTimeDomain() const override; |
163 void SetBlameContext(base::trace_event::BlameContext* blame_context) override; | 159 void SetBlameContext(base::trace_event::BlameContext* blame_context) override; |
| 160 void InsertFence() override; |
| 161 void RemoveFence() override; |
| 162 bool BlockedByFence() const override; |
164 | 163 |
165 bool IsTaskPending(const TaskHandle& handle) const; | 164 bool IsTaskPending(const TaskHandle& handle) const; |
166 | 165 |
167 void UpdateImmediateWorkQueue(bool should_trigger_wakeup, | 166 void UpdateImmediateWorkQueue(); |
168 const Task* previous_task); | 167 void UpdateDelayedWorkQueue(LazyNow* lazy_now); |
169 void UpdateDelayedWorkQueue(LazyNow* lazy_now, | |
170 bool should_trigger_wakeup, | |
171 const Task* previous_task); | |
172 | |
173 WakeupPolicy wakeup_policy() const { | |
174 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
175 return wakeup_policy_; | |
176 } | |
177 | 168 |
178 const char* GetName() const override; | 169 const char* GetName() const override; |
179 | 170 |
180 void AsValueInto(base::trace_event::TracedValue* state) const; | 171 void AsValueInto(base::trace_event::TracedValue* state) const; |
181 | 172 |
182 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_; } | 173 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_; } |
183 bool GetShouldNotifyObservers() const { return should_notify_observers_; } | 174 bool GetShouldNotifyObservers() const { return should_notify_observers_; } |
184 | 175 |
185 void NotifyWillProcessTask(const base::PendingTask& pending_task); | 176 void NotifyWillProcessTask(const base::PendingTask& pending_task); |
186 void NotifyDidProcessTask(const base::PendingTask& pending_task); | 177 void NotifyDidProcessTask(const base::PendingTask& pending_task); |
187 | 178 |
188 // Can be called on any thread. | 179 // Can be called on any thread. |
189 static const char* PumpPolicyToString(TaskQueue::PumpPolicy pump_policy); | |
190 | |
191 // Can be called on any thread. | |
192 static const char* WakeupPolicyToString( | |
193 TaskQueue::WakeupPolicy wakeup_policy); | |
194 | |
195 // Can be called on any thread. | |
196 static const char* PriorityToString(TaskQueue::QueuePriority priority); | 180 static const char* PriorityToString(TaskQueue::QueuePriority priority); |
197 | 181 |
198 WorkQueue* delayed_work_queue() { | 182 WorkQueue* delayed_work_queue() { |
199 return main_thread_only().delayed_work_queue.get(); | 183 return main_thread_only().delayed_work_queue.get(); |
200 } | 184 } |
201 | 185 |
202 const WorkQueue* delayed_work_queue() const { | 186 const WorkQueue* delayed_work_queue() const { |
203 return main_thread_only().delayed_work_queue.get(); | 187 return main_thread_only().delayed_work_queue.get(); |
204 } | 188 } |
205 | 189 |
206 WorkQueue* immediate_work_queue() { | 190 WorkQueue* immediate_work_queue() { |
207 return main_thread_only().immediate_work_queue.get(); | 191 return main_thread_only().immediate_work_queue.get(); |
208 } | 192 } |
209 | 193 |
210 const WorkQueue* immediate_work_queue() const { | 194 const WorkQueue* immediate_work_queue() const { |
211 return main_thread_only().immediate_work_queue.get(); | 195 return main_thread_only().immediate_work_queue.get(); |
212 } | 196 } |
213 | 197 |
214 bool should_report_when_execution_blocked() const { | 198 bool should_report_when_execution_blocked() const { |
215 return should_report_when_execution_blocked_; | 199 return should_report_when_execution_blocked_; |
216 } | 200 } |
217 | 201 |
218 private: | 202 private: |
219 friend class WorkQueue; | 203 friend class WorkQueue; |
| 204 friend class WorkQueueTest; |
220 | 205 |
221 // Note both DelayedRunTimeQueue and ComparatorQueue are sets for fast task | 206 // Note both DelayedRunTimeQueue and ComparatorQueue are sets for fast task |
222 // cancellation. Typically queue sizes are well under 200 so the overhead of | 207 // cancellation. Typically queue sizes are well under 200 so the overhead of |
223 // std::set vs std::priority_queue and std::queue is lost in the noise of | 208 // std::set vs std::priority_queue and std::queue is lost in the noise of |
224 // everything else. | 209 // everything else. |
225 using DelayedRunTimeQueue = std::set<Task, Task::DelayedRunTimeComparator>; | 210 using DelayedRunTimeQueue = std::set<Task, Task::DelayedRunTimeComparator>; |
226 using ComparatorQueue = std::set<Task, Task::ComparatorFn>; | 211 using ComparatorQueue = std::set<Task, Task::ComparatorFn>; |
227 | 212 |
228 enum class TaskType { | 213 enum class TaskType { |
229 NORMAL, | 214 NORMAL, |
230 NON_NESTABLE, | 215 NON_NESTABLE, |
231 }; | 216 }; |
232 | 217 |
233 struct AnyThread { | 218 struct AnyThread { |
234 AnyThread(TaskQueueManager* task_queue_manager, | 219 AnyThread(TaskQueueManager* task_queue_manager, |
235 PumpPolicy pump_policy, | |
236 TimeDomain* time_domain); | 220 TimeDomain* time_domain); |
237 ~AnyThread(); | 221 ~AnyThread(); |
238 | 222 |
239 // TaskQueueManager, PumpPolicy and TimeDomain are maintained in two copies: | 223 // TaskQueueManager and TimeDomain are maintained in two copies: |
240 // inside AnyThread and inside MainThreadOnly. They can be changed only from | 224 // inside AnyThread and inside MainThreadOnly. They can be changed only from |
241 // main thread, so it should be locked before accessing from other threads. | 225 // main thread, so it should be locked before accessing from other threads. |
242 TaskQueueManager* task_queue_manager; | 226 TaskQueueManager* task_queue_manager; |
243 PumpPolicy pump_policy; | |
244 TimeDomain* time_domain; | 227 TimeDomain* time_domain; |
245 | 228 |
246 ComparatorQueue immediate_incoming_queue; | 229 ComparatorQueue immediate_incoming_queue; |
247 }; | 230 }; |
248 | 231 |
249 struct MainThreadOnly { | 232 struct MainThreadOnly { |
250 MainThreadOnly(TaskQueueManager* task_queue_manager, | 233 MainThreadOnly(TaskQueueManager* task_queue_manager, |
251 PumpPolicy pump_policy, | |
252 TaskQueueImpl* task_queue, | 234 TaskQueueImpl* task_queue, |
253 TimeDomain* time_domain); | 235 TimeDomain* time_domain); |
254 ~MainThreadOnly(); | 236 ~MainThreadOnly(); |
255 | 237 |
256 // Another copy of TaskQueueManager, PumpPolicy and TimeDomain for lock-free | 238 // Another copy of TaskQueueManager and TimeDomain for lock-free access from |
257 // access from the main thread. See description inside struct AnyThread for | 239 // the main thread. See description inside struct AnyThread for details. |
258 // details. | |
259 TaskQueueManager* task_queue_manager; | 240 TaskQueueManager* task_queue_manager; |
260 PumpPolicy pump_policy; | |
261 TimeDomain* time_domain; | 241 TimeDomain* time_domain; |
262 | 242 |
263 std::unique_ptr<WorkQueue> delayed_work_queue; | 243 std::unique_ptr<WorkQueue> delayed_work_queue; |
264 std::unique_ptr<WorkQueue> immediate_work_queue; | 244 std::unique_ptr<WorkQueue> immediate_work_queue; |
265 DelayedRunTimeQueue delayed_incoming_queue; | 245 DelayedRunTimeQueue delayed_incoming_queue; |
266 base::ObserverList<base::MessageLoop::TaskObserver> task_observers; | 246 base::ObserverList<base::MessageLoop::TaskObserver> task_observers; |
267 size_t set_index; | 247 size_t set_index; |
268 bool is_enabled; | 248 bool is_enabled; |
269 base::trace_event::BlameContext* blame_context; // Not owned. | 249 base::trace_event::BlameContext* blame_context; // Not owned. |
| 250 EnqueueOrder current_fence; |
270 }; | 251 }; |
271 | 252 |
272 ~TaskQueueImpl() override; | 253 ~TaskQueueImpl() override; |
273 | 254 |
274 bool PostImmediateTaskImpl(const tracked_objects::Location& from_here, | 255 bool PostImmediateTaskImpl(const tracked_objects::Location& from_here, |
275 const base::Closure& task, | 256 const base::Closure& task, |
276 TaskType task_type); | 257 TaskType task_type); |
277 bool PostDelayedTaskImpl(const tracked_objects::Location& from_here, | 258 bool PostDelayedTaskImpl(const tracked_objects::Location& from_here, |
278 const base::Closure& task, | 259 const base::Closure& task, |
279 base::TimeDelta delay, | 260 base::TimeDelta delay, |
280 TaskType task_type); | 261 TaskType task_type); |
281 | 262 |
282 // Push the task onto the |delayed_incoming_queue|. Lock-free main thread | 263 // Push the task onto the |delayed_incoming_queue|. Lock-free main thread |
283 // only fast path. | 264 // only fast path. |
284 void PushOntoDelayedIncomingQueueFromMainThread(Task pending_task, | 265 void PushOntoDelayedIncomingQueueFromMainThread(Task pending_task, |
285 base::TimeTicks now); | 266 base::TimeTicks now); |
286 | 267 |
287 // Push the task onto the |delayed_incoming_queue|. Slow path from other | 268 // Push the task onto the |delayed_incoming_queue|. Slow path from other |
288 // threads. | 269 // threads. |
289 void PushOntoDelayedIncomingQueueLocked(Task pending_task); | 270 void PushOntoDelayedIncomingQueueLocked(Task pending_task); |
290 | 271 |
291 void ScheduleDelayedWorkTask(Task pending_task); | 272 void ScheduleDelayedWorkTask(Task pending_task); |
292 | 273 |
293 // Enqueues any delayed tasks which should be run now on the | 274 // Enqueues any delayed tasks which should be run now on the |
294 // |delayed_work_queue|. Must be called from the main thread. | 275 // |delayed_work_queue|. Must be called from the main thread. |
295 void MoveReadyDelayedTasksToDelayedWorkQueue(LazyNow* lazy_now); | 276 void MoveReadyDelayedTasksToDelayedWorkQueue(LazyNow* lazy_now); |
296 | 277 |
297 void MoveReadyImmediateTasksToImmediateWorkQueueLocked(); | 278 void MoveReadyImmediateTasksToImmediateWorkQueueLocked(); |
298 | 279 |
299 // Note this does nothing if its not called from the main thread. | |
300 void PumpQueueLocked(LazyNow* lazy_now, bool may_post_dowork); | |
301 | |
302 // Returns true if |task| is older than the oldest incoming immediate task. | |
303 // NOTE |any_thread_lock_| must be locked. | |
304 bool TaskIsOlderThanQueuedImmediateTasksLocked(const Task* task); | |
305 | |
306 // Returns true if |task| is older than the oldest delayed task. Must be | |
307 // called from the main thread. | |
308 bool TaskIsOlderThanQueuedDelayedTasks(const Task* task); | |
309 | |
310 // NOTE |any_thread_lock_| must be locked. | |
311 bool ShouldAutoPumpImmediateQueueLocked(bool should_trigger_wakeup, | |
312 const Task* previous_task); | |
313 | |
314 // Must be called from the main thread. | |
315 bool ShouldAutoPumpDelayedQueue(bool should_trigger_wakeup, | |
316 const Task* previous_task); | |
317 | |
318 // Push the task onto the |immediate_incoming_queue| and for auto pumped | 280 // Push the task onto the |immediate_incoming_queue| and for auto pumped |
319 // queues it calls MaybePostDoWorkOnMainRunner if the Incoming queue was | 281 // queues it calls MaybePostDoWorkOnMainRunner if the Incoming queue was |
320 // empty. | 282 // empty. |
321 void PushOntoImmediateIncomingQueueLocked(Task pending_task); | 283 void PushOntoImmediateIncomingQueueLocked(Task pending_task); |
322 | 284 |
| 285 // As BlockedByFence but safe to be called while locked. |
| 286 bool BlockedByFenceLocked() const; |
| 287 |
323 void TraceQueueSize(bool is_locked) const; | 288 void TraceQueueSize(bool is_locked) const; |
324 static void QueueAsValueInto(const ComparatorQueue& queue, | 289 static void QueueAsValueInto(const ComparatorQueue& queue, |
325 base::trace_event::TracedValue* state); | 290 base::trace_event::TracedValue* state); |
326 static void QueueAsValueInto(const DelayedRunTimeQueue& queue, | 291 static void QueueAsValueInto(const DelayedRunTimeQueue& queue, |
327 base::trace_event::TracedValue* state); | 292 base::trace_event::TracedValue* state); |
328 static void TaskAsValueInto(const Task& task, | 293 static void TaskAsValueInto(const Task& task, |
329 base::trace_event::TracedValue* state); | 294 base::trace_event::TracedValue* state); |
330 | 295 |
331 const base::PlatformThreadId thread_id_; | 296 const base::PlatformThreadId thread_id_; |
332 | 297 |
(...skipping 16 matching lines...) Expand all Loading... |
349 MainThreadOnly main_thread_only_; | 314 MainThreadOnly main_thread_only_; |
350 MainThreadOnly& main_thread_only() { | 315 MainThreadOnly& main_thread_only() { |
351 DCHECK(main_thread_checker_.CalledOnValidThread()); | 316 DCHECK(main_thread_checker_.CalledOnValidThread()); |
352 return main_thread_only_; | 317 return main_thread_only_; |
353 } | 318 } |
354 const MainThreadOnly& main_thread_only() const { | 319 const MainThreadOnly& main_thread_only() const { |
355 DCHECK(main_thread_checker_.CalledOnValidThread()); | 320 DCHECK(main_thread_checker_.CalledOnValidThread()); |
356 return main_thread_only_; | 321 return main_thread_only_; |
357 } | 322 } |
358 | 323 |
359 const WakeupPolicy wakeup_policy_; | |
360 const bool should_monitor_quiescence_; | 324 const bool should_monitor_quiescence_; |
361 const bool should_notify_observers_; | 325 const bool should_notify_observers_; |
362 const bool should_report_when_execution_blocked_; | 326 const bool should_report_when_execution_blocked_; |
363 | 327 |
364 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); | 328 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); |
365 }; | 329 }; |
366 | 330 |
367 } // namespace internal | 331 } // namespace internal |
368 } // namespace scheduler | 332 } // namespace scheduler |
369 } // namespace blink | 333 } // namespace blink |
370 | 334 |
371 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ | 335 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ |
OLD | NEW |