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