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