| 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_BASE_TASK_QUEUE_IMPL_H_ | 5 #ifndef CONTENT_RENDERER_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ |
| 6 #define CONTENT_RENDERER_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ | 6 #define CONTENT_RENDERER_SCHEDULER_BASE_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/base/lazy_now.h" | |
| 15 #include "components/scheduler/base/task_queue.h" | 14 #include "components/scheduler/base/task_queue.h" |
| 16 #include "components/scheduler/scheduler_export.h" | 15 #include "components/scheduler/scheduler_export.h" |
| 17 | 16 |
| 18 namespace scheduler { | 17 namespace scheduler { |
| 18 class LazyNow; |
| 19 class TimeDomain; |
| 19 class TaskQueueManager; | 20 class TaskQueueManager; |
| 20 | 21 |
| 21 namespace internal { | 22 namespace internal { |
| 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 scoped_refptr<TimeDomain>& time_domain, |
| 26 const Spec& spec, | 27 const Spec& spec, |
| 27 const char* disabled_by_default_tracing_category, | 28 const char* disabled_by_default_tracing_category, |
| 28 const char* disabled_by_default_verbose_tracing_category); | 29 const char* disabled_by_default_verbose_tracing_category); |
| 29 | 30 |
| 30 class SCHEDULER_EXPORT Task : public base::PendingTask { | 31 class SCHEDULER_EXPORT Task : public base::PendingTask { |
| 31 public: | 32 public: |
| 32 Task(); | 33 Task(); |
| 33 Task(const tracked_objects::Location& posted_from, | 34 Task(const tracked_objects::Location& posted_from, |
| 34 const base::Closure& task, | 35 const base::Closure& task, |
| 35 int sequence_number, | 36 int sequence_number, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 base::TimeTicks desired_run_time) override; | 75 base::TimeTicks desired_run_time) override; |
| 75 | 76 |
| 76 bool IsQueueEnabled() const override; | 77 bool IsQueueEnabled() const override; |
| 77 QueueState GetQueueState() const override; | 78 QueueState GetQueueState() const override; |
| 78 void SetQueuePriority(QueuePriority priority) override; | 79 void SetQueuePriority(QueuePriority priority) override; |
| 79 void PumpQueue() override; | 80 void PumpQueue() override; |
| 80 void SetPumpPolicy(PumpPolicy pump_policy) override; | 81 void SetPumpPolicy(PumpPolicy pump_policy) override; |
| 81 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override; | 82 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override; |
| 82 void RemoveTaskObserver( | 83 void RemoveTaskObserver( |
| 83 base::MessageLoop::TaskObserver* task_observer) override; | 84 base::MessageLoop::TaskObserver* task_observer) override; |
| 84 | 85 void SetTimeDomain(const scoped_refptr<TimeDomain>& time_domain) override; |
| 85 bool NextPendingDelayedTaskRunTime( | |
| 86 base::TimeTicks* next_pending_delayed_task); | |
| 87 | 86 |
| 88 void UpdateWorkQueue(LazyNow* lazy_now, | 87 void UpdateWorkQueue(LazyNow* lazy_now, |
| 89 bool should_trigger_wakeup, | 88 bool should_trigger_wakeup, |
| 90 const Task* previous_task); | 89 const Task* previous_task); |
| 91 Task TakeTaskFromWorkQueue(); | 90 Task TakeTaskFromWorkQueue(); |
| 92 | 91 |
| 93 std::queue<Task>& work_queue() { return main_thread_only().work_queue; } | 92 std::queue<Task>& work_queue() { return main_thread_only().work_queue; } |
| 94 | 93 |
| 95 WakeupPolicy wakeup_policy() const { | 94 WakeupPolicy wakeup_policy() const { |
| 96 DCHECK(main_thread_checker_.CalledOnValidThread()); | 95 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 117 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_; } | 116 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_; } |
| 118 bool GetShouldNotifyObservers() const { | 117 bool GetShouldNotifyObservers() const { |
| 119 return should_notify_observers_; | 118 return should_notify_observers_; |
| 120 } | 119 } |
| 121 | 120 |
| 122 void NotifyWillProcessTask(const base::PendingTask& pending_task); | 121 void NotifyWillProcessTask(const base::PendingTask& pending_task); |
| 123 void NotifyDidProcessTask(const base::PendingTask& pending_task); | 122 void NotifyDidProcessTask(const base::PendingTask& pending_task); |
| 124 | 123 |
| 125 // Delayed task posted to the underlying run loop, which locks | 124 // Delayed task posted to the underlying run loop, which locks |
| 126 // |any_thread_lock_| and calls MoveReadyDelayedTasksToIncomingQueueLocked to | 125 // |any_thread_lock_| and calls MoveReadyDelayedTasksToIncomingQueueLocked to |
| 127 // process dealyed tasks | 126 // process dealyed tasks that need to be run now. Thread safe, but in |
| 128 // that need to be run now. Thread safe, but in practice it's always called | 127 // practice it's always called from the main thread. |
| 129 // from the main thread. | |
| 130 void MoveReadyDelayedTasksToIncomingQueue(LazyNow* lazy_now); | 128 void MoveReadyDelayedTasksToIncomingQueue(LazyNow* lazy_now); |
| 131 | 129 |
| 132 // Test support functions. These should not be used in production code. | 130 // Test support functions. These should not be used in production code. |
| 133 void PushTaskOntoWorkQueueForTest(const Task& task); | 131 void PushTaskOntoWorkQueueForTest(const Task& task); |
| 134 void PopTaskFromWorkQueueForTest(); | 132 void PopTaskFromWorkQueueForTest(); |
| 135 size_t WorkQueueSizeForTest() const { | 133 size_t WorkQueueSizeForTest() const { |
| 136 return main_thread_only().work_queue.size(); | 134 return main_thread_only().work_queue.size(); |
| 137 } | 135 } |
| 136 size_t IncomingQueueSizeForTest() const; |
| 138 | 137 |
| 139 // Can be called on any thread. | 138 // Can be called on any thread. |
| 140 static const char* PumpPolicyToString(TaskQueue::PumpPolicy pump_policy); | 139 static const char* PumpPolicyToString(TaskQueue::PumpPolicy pump_policy); |
| 141 | 140 |
| 142 // Can be called on any thread. | 141 // Can be called on any thread. |
| 143 static const char* WakeupPolicyToString( | 142 static const char* WakeupPolicyToString( |
| 144 TaskQueue::WakeupPolicy wakeup_policy); | 143 TaskQueue::WakeupPolicy wakeup_policy); |
| 145 | 144 |
| 146 // Can be called on any thread. | 145 // Can be called on any thread. |
| 147 static const char* PriorityToString(TaskQueue::QueuePriority priority); | 146 static const char* PriorityToString(TaskQueue::QueuePriority priority); |
| 148 | 147 |
| 149 private: | 148 private: |
| 150 enum class TaskType { | 149 enum class TaskType { |
| 151 NORMAL, | 150 NORMAL, |
| 152 NON_NESTABLE, | 151 NON_NESTABLE, |
| 153 }; | 152 }; |
| 154 | 153 |
| 155 struct AnyThread { | 154 struct AnyThread { |
| 156 AnyThread(TaskQueueManager* task_queue_manager, PumpPolicy pump_policy); | 155 AnyThread(TaskQueueManager* task_queue_manager, |
| 156 PumpPolicy pump_policy, |
| 157 const scoped_refptr<TimeDomain>& time_domain); |
| 157 ~AnyThread(); | 158 ~AnyThread(); |
| 158 | 159 |
| 159 // TaskQueueManager is maintained in two copies: inside AnyThread and inside | 160 // TaskQueueManager is maintained in two copies: inside AnyThread and inside |
| 160 // MainThreadOnly. It can be changed only from main thread, so it should be | 161 // MainThreadOnly. It can be changed only from main thread, so it should be |
| 161 // locked before accessing from other threads. | 162 // locked before accessing from other threads. |
| 162 TaskQueueManager* task_queue_manager; | 163 TaskQueueManager* task_queue_manager; |
| 163 | 164 |
| 164 std::queue<Task> incoming_queue; | 165 std::queue<Task> incoming_queue; |
| 165 PumpPolicy pump_policy; | 166 PumpPolicy pump_policy; |
| 166 std::priority_queue<Task> delayed_task_queue; | 167 std::priority_queue<Task> delayed_task_queue; |
| 168 scoped_refptr<TimeDomain> time_domain; |
| 167 }; | 169 }; |
| 168 | 170 |
| 169 struct MainThreadOnly { | 171 struct MainThreadOnly { |
| 170 explicit MainThreadOnly(TaskQueueManager* task_queue_manager); | 172 MainThreadOnly(TaskQueueManager* task_queue_manager); |
| 171 ~MainThreadOnly(); | 173 ~MainThreadOnly(); |
| 172 | 174 |
| 173 // Another copy of TaskQueueManager for lock-free access from the main | 175 // Another copy of TaskQueueManager for lock-free access from the main |
| 174 // thread. See description inside struct AnyThread for details. | 176 // thread. See description inside struct AnyThread for details. |
| 175 TaskQueueManager* task_queue_manager; | 177 TaskQueueManager* task_queue_manager; |
| 176 | 178 |
| 177 std::queue<Task> work_queue; | 179 std::queue<Task> work_queue; |
| 178 base::ObserverList<base::MessageLoop::TaskObserver> task_observers; | 180 base::ObserverList<base::MessageLoop::TaskObserver> task_observers; |
| 179 size_t set_index; | 181 size_t set_index; |
| 180 }; | 182 }; |
| 181 | 183 |
| 182 ~TaskQueueImpl() override; | 184 ~TaskQueueImpl() override; |
| 183 | 185 |
| 184 bool PostDelayedTaskImpl(const tracked_objects::Location& from_here, | 186 bool PostDelayedTaskImpl(const tracked_objects::Location& from_here, |
| 185 const base::Closure& task, | 187 const base::Closure& task, |
| 186 base::TimeDelta delay, | 188 base::TimeDelta delay, |
| 187 TaskType task_type); | 189 TaskType task_type); |
| 188 bool PostDelayedTaskLocked(LazyNow* lazy_now, | 190 bool PostDelayedTaskLocked(LazyNow* lazy_now, |
| 189 const tracked_objects::Location& from_here, | 191 const tracked_objects::Location& from_here, |
| 190 const base::Closure& task, | 192 const base::Closure& task, |
| 191 base::TimeTicks desired_run_time, | 193 base::TimeTicks desired_run_time, |
| 192 TaskType task_type); | 194 TaskType task_type); |
| 195 void ScheduleDelayedWorkTask(const scoped_refptr<TimeDomain> time_domain, |
| 196 base::TimeTicks desired_run_time); |
| 193 | 197 |
| 194 // Enqueues any delayed tasks which should be run now on the incoming_queue_ | 198 // Enqueues any delayed tasks which should be run now on the incoming_queue_. |
| 195 // and calls ScheduleDelayedWorkLocked to ensure future tasks are scheduled. | |
| 196 // Must be called with |any_thread_lock_| locked. | 199 // Must be called with |any_thread_lock_| locked. |
| 197 void MoveReadyDelayedTasksToIncomingQueueLocked(LazyNow* lazy_now); | 200 void MoveReadyDelayedTasksToIncomingQueueLocked(LazyNow* lazy_now); |
| 198 | 201 |
| 199 void PumpQueueLocked(); | 202 void PumpQueueLocked(); |
| 200 bool TaskIsOlderThanQueuedTasks(const Task* task); | 203 bool TaskIsOlderThanQueuedTasks(const Task* task); |
| 201 bool ShouldAutoPumpQueueLocked(bool should_trigger_wakeup, | 204 bool ShouldAutoPumpQueueLocked(bool should_trigger_wakeup, |
| 202 const Task* previous_task); | 205 const Task* previous_task); |
| 203 | 206 |
| 204 // Push the task onto the |incoming_queue_| and for auto pumped queues it | 207 // Push the task onto the |incoming_queue_| and for auto pumped queues it |
| 205 // calls MaybePostDoWorkOnMainRunner if the incomming queue was empty. | 208 // calls MaybePostDoWorkOnMainRunner if the incomming queue was empty. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 const bool should_monitor_quiescence_; | 253 const bool should_monitor_quiescence_; |
| 251 const bool should_notify_observers_; | 254 const bool should_notify_observers_; |
| 252 | 255 |
| 253 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); | 256 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); |
| 254 }; | 257 }; |
| 255 | 258 |
| 256 } // namespace internal | 259 } // namespace internal |
| 257 } // namespace scheduler | 260 } // namespace scheduler |
| 258 | 261 |
| 259 #endif // CONTENT_RENDERER_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ | 262 #endif // CONTENT_RENDERER_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ |
| OLD | NEW |