| 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; |
| 164 | 160 |
| 165 bool IsTaskPending(const TaskHandle& handle) const; | 161 bool IsTaskPending(const TaskHandle& handle) const; |
| 166 | 162 |
| 167 void UpdateImmediateWorkQueue(bool should_trigger_wakeup, | 163 void UpdateImmediateWorkQueue(); |
| 168 const Task* previous_task); | 164 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 | 165 |
| 178 const char* GetName() const override; | 166 const char* GetName() const override; |
| 179 | 167 |
| 180 void AsValueInto(base::trace_event::TracedValue* state) const; | 168 void AsValueInto(base::trace_event::TracedValue* state) const; |
| 181 | 169 |
| 182 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_; } | 170 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_; } |
| 183 bool GetShouldNotifyObservers() const { return should_notify_observers_; } | 171 bool GetShouldNotifyObservers() const { return should_notify_observers_; } |
| 184 | 172 |
| 185 void NotifyWillProcessTask(const base::PendingTask& pending_task); | 173 void NotifyWillProcessTask(const base::PendingTask& pending_task); |
| 186 void NotifyDidProcessTask(const base::PendingTask& pending_task); | 174 void NotifyDidProcessTask(const base::PendingTask& pending_task); |
| 187 | 175 |
| 188 // Can be called on any thread. | 176 // 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); | 177 static const char* PriorityToString(TaskQueue::QueuePriority priority); |
| 197 | 178 |
| 198 WorkQueue* delayed_work_queue() { | 179 WorkQueue* delayed_work_queue() { |
| 199 return main_thread_only().delayed_work_queue.get(); | 180 return main_thread_only().delayed_work_queue.get(); |
| 200 } | 181 } |
| 201 | 182 |
| 202 const WorkQueue* delayed_work_queue() const { | 183 const WorkQueue* delayed_work_queue() const { |
| 203 return main_thread_only().delayed_work_queue.get(); | 184 return main_thread_only().delayed_work_queue.get(); |
| 204 } | 185 } |
| 205 | 186 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 225 using DelayedRunTimeQueue = std::set<Task, Task::DelayedRunTimeComparator>; | 206 using DelayedRunTimeQueue = std::set<Task, Task::DelayedRunTimeComparator>; |
| 226 using ComparatorQueue = std::set<Task, Task::ComparatorFn>; | 207 using ComparatorQueue = std::set<Task, Task::ComparatorFn>; |
| 227 | 208 |
| 228 enum class TaskType { | 209 enum class TaskType { |
| 229 NORMAL, | 210 NORMAL, |
| 230 NON_NESTABLE, | 211 NON_NESTABLE, |
| 231 }; | 212 }; |
| 232 | 213 |
| 233 struct AnyThread { | 214 struct AnyThread { |
| 234 AnyThread(TaskQueueManager* task_queue_manager, | 215 AnyThread(TaskQueueManager* task_queue_manager, |
| 235 PumpPolicy pump_policy, | |
| 236 TimeDomain* time_domain); | 216 TimeDomain* time_domain); |
| 237 ~AnyThread(); | 217 ~AnyThread(); |
| 238 | 218 |
| 239 // TaskQueueManager, PumpPolicy and TimeDomain are maintained in two copies: | 219 // TaskQueueManager and TimeDomain are maintained in two copies: |
| 240 // inside AnyThread and inside MainThreadOnly. They can be changed only from | 220 // inside AnyThread and inside MainThreadOnly. They can be changed only from |
| 241 // main thread, so it should be locked before accessing from other threads. | 221 // main thread, so it should be locked before accessing from other threads. |
| 242 TaskQueueManager* task_queue_manager; | 222 TaskQueueManager* task_queue_manager; |
| 243 PumpPolicy pump_policy; | |
| 244 TimeDomain* time_domain; | 223 TimeDomain* time_domain; |
| 245 | 224 |
| 246 ComparatorQueue immediate_incoming_queue; | 225 ComparatorQueue immediate_incoming_queue; |
| 247 }; | 226 }; |
| 248 | 227 |
| 249 struct MainThreadOnly { | 228 struct MainThreadOnly { |
| 250 MainThreadOnly(TaskQueueManager* task_queue_manager, | 229 MainThreadOnly(TaskQueueManager* task_queue_manager, |
| 251 PumpPolicy pump_policy, | |
| 252 TaskQueueImpl* task_queue, | 230 TaskQueueImpl* task_queue, |
| 253 TimeDomain* time_domain); | 231 TimeDomain* time_domain); |
| 254 ~MainThreadOnly(); | 232 ~MainThreadOnly(); |
| 255 | 233 |
| 256 // Another copy of TaskQueueManager, PumpPolicy and TimeDomain for lock-free | 234 // Another copy of TaskQueueManager and TimeDomain for lock-free access from |
| 257 // access from the main thread. See description inside struct AnyThread for | 235 // the main thread. See description inside struct AnyThread for details. |
| 258 // details. | |
| 259 TaskQueueManager* task_queue_manager; | 236 TaskQueueManager* task_queue_manager; |
| 260 PumpPolicy pump_policy; | |
| 261 TimeDomain* time_domain; | 237 TimeDomain* time_domain; |
| 262 | 238 |
| 263 std::unique_ptr<WorkQueue> delayed_work_queue; | 239 std::unique_ptr<WorkQueue> delayed_work_queue; |
| 264 std::unique_ptr<WorkQueue> immediate_work_queue; | 240 std::unique_ptr<WorkQueue> immediate_work_queue; |
| 265 DelayedRunTimeQueue delayed_incoming_queue; | 241 DelayedRunTimeQueue delayed_incoming_queue; |
| 266 base::ObserverList<base::MessageLoop::TaskObserver> task_observers; | 242 base::ObserverList<base::MessageLoop::TaskObserver> task_observers; |
| 267 size_t set_index; | 243 size_t set_index; |
| 268 bool is_enabled; | 244 bool is_enabled; |
| 269 base::trace_event::BlameContext* blame_context; // Not owned. | 245 base::trace_event::BlameContext* blame_context; // Not owned. |
| 270 }; | 246 }; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 289 void PushOntoDelayedIncomingQueueLocked(Task pending_task); | 265 void PushOntoDelayedIncomingQueueLocked(Task pending_task); |
| 290 | 266 |
| 291 void ScheduleDelayedWorkTask(Task pending_task); | 267 void ScheduleDelayedWorkTask(Task pending_task); |
| 292 | 268 |
| 293 // Enqueues any delayed tasks which should be run now on the | 269 // Enqueues any delayed tasks which should be run now on the |
| 294 // |delayed_work_queue|. Must be called from the main thread. | 270 // |delayed_work_queue|. Must be called from the main thread. |
| 295 void MoveReadyDelayedTasksToDelayedWorkQueue(LazyNow* lazy_now); | 271 void MoveReadyDelayedTasksToDelayedWorkQueue(LazyNow* lazy_now); |
| 296 | 272 |
| 297 void MoveReadyImmediateTasksToImmediateWorkQueueLocked(); | 273 void MoveReadyImmediateTasksToImmediateWorkQueueLocked(); |
| 298 | 274 |
| 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 | 275 // Push the task onto the |immediate_incoming_queue| and for auto pumped |
| 319 // queues it calls MaybePostDoWorkOnMainRunner if the Incoming queue was | 276 // queues it calls MaybePostDoWorkOnMainRunner if the Incoming queue was |
| 320 // empty. | 277 // empty. |
| 321 void PushOntoImmediateIncomingQueueLocked(Task pending_task); | 278 void PushOntoImmediateIncomingQueueLocked(Task pending_task); |
| 322 | 279 |
| 323 void TraceQueueSize(bool is_locked) const; | 280 void TraceQueueSize(bool is_locked) const; |
| 324 static void QueueAsValueInto(const ComparatorQueue& queue, | 281 static void QueueAsValueInto(const ComparatorQueue& queue, |
| 325 base::trace_event::TracedValue* state); | 282 base::trace_event::TracedValue* state); |
| 326 static void QueueAsValueInto(const DelayedRunTimeQueue& queue, | 283 static void QueueAsValueInto(const DelayedRunTimeQueue& queue, |
| 327 base::trace_event::TracedValue* state); | 284 base::trace_event::TracedValue* state); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 349 MainThreadOnly main_thread_only_; | 306 MainThreadOnly main_thread_only_; |
| 350 MainThreadOnly& main_thread_only() { | 307 MainThreadOnly& main_thread_only() { |
| 351 DCHECK(main_thread_checker_.CalledOnValidThread()); | 308 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 352 return main_thread_only_; | 309 return main_thread_only_; |
| 353 } | 310 } |
| 354 const MainThreadOnly& main_thread_only() const { | 311 const MainThreadOnly& main_thread_only() const { |
| 355 DCHECK(main_thread_checker_.CalledOnValidThread()); | 312 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 356 return main_thread_only_; | 313 return main_thread_only_; |
| 357 } | 314 } |
| 358 | 315 |
| 359 const WakeupPolicy wakeup_policy_; | |
| 360 const bool should_monitor_quiescence_; | 316 const bool should_monitor_quiescence_; |
| 361 const bool should_notify_observers_; | 317 const bool should_notify_observers_; |
| 362 const bool should_report_when_execution_blocked_; | 318 const bool should_report_when_execution_blocked_; |
| 363 | 319 |
| 364 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); | 320 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); |
| 365 }; | 321 }; |
| 366 | 322 |
| 367 } // namespace internal | 323 } // namespace internal |
| 368 } // namespace scheduler | 324 } // namespace scheduler |
| 369 } // namespace blink | 325 } // namespace blink |
| 370 | 326 |
| 371 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ | 327 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ |
| OLD | NEW |