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_TASK_QUEUE_IMPL_H_ | 5 #ifndef CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_ |
| 6 #define CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_ | 6 #define CONTENT_RENDERER_SCHEDULER_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/child/lazy_now.h" | 14 #include "components/scheduler/child/lazy_now.h" |
| 15 #include "components/scheduler/child/task_queue.h" | 15 #include "components/scheduler/child/task_queue.h" |
| 16 #include "components/scheduler/scheduler_export.h" | 16 #include "components/scheduler/scheduler_export.h" |
| 17 | 17 |
| 18 namespace scheduler { | 18 namespace scheduler { |
| 19 class TaskQueueManager; | 19 class TaskQueueManager; |
| 20 | 20 |
| 21 namespace internal { | 21 namespace internal { |
| 22 | 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 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 struct Task : public base::PendingTask { | |
| 31 Task(const tracked_objects::Location& posted_from, | |
| 32 const base::Closure& task, | |
| 33 int sequence_number, | |
| 34 bool nestable); | |
| 35 | |
| 36 // Similar to sequence number, but the |enqueue_order| is set by | |
| 37 // EnqueueTasksLocked and is not initially defined for delayed tasks until | |
| 38 // they are enqueued on the |incoming_queue_|. | |
| 39 int enqueue_order; | |
| 40 }; | |
| 41 | |
| 30 // TaskQueue implementation. | 42 // TaskQueue implementation. |
| 31 bool RunsTasksOnCurrentThread() const override; | 43 bool RunsTasksOnCurrentThread() const override; |
| 32 bool PostDelayedTask(const tracked_objects::Location& from_here, | 44 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 33 const base::Closure& task, | 45 const base::Closure& task, |
| 34 base::TimeDelta delay) override; | 46 base::TimeDelta delay) override; |
| 35 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 47 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
| 36 const base::Closure& task, | 48 const base::Closure& task, |
| 37 base::TimeDelta delay) override; | 49 base::TimeDelta delay) override; |
| 38 bool PostDelayedTaskAt(const tracked_objects::Location& from_here, | 50 bool PostDelayedTaskAt(const tracked_objects::Location& from_here, |
| 39 const base::Closure& task, | 51 const base::Closure& task, |
| 40 base::TimeTicks desired_run_time) override; | 52 base::TimeTicks desired_run_time) override; |
| 41 | 53 |
| 42 bool IsQueueEnabled() const override; | 54 bool IsQueueEnabled() const override; |
| 43 QueueState GetQueueState() const override; | 55 QueueState GetQueueState() const override; |
| 44 void SetQueuePriority(QueuePriority priority) override; | 56 void SetQueuePriority(QueuePriority priority) override; |
| 45 void PumpQueue() override; | 57 void PumpQueue() override; |
| 46 void SetPumpPolicy(PumpPolicy pump_policy) override; | 58 void SetPumpPolicy(PumpPolicy pump_policy) override; |
| 47 | 59 |
| 48 bool NextPendingDelayedTaskRunTime( | 60 bool NextPendingDelayedTaskRunTime( |
| 49 base::TimeTicks* next_pending_delayed_task); | 61 base::TimeTicks* next_pending_delayed_task); |
| 50 | 62 |
| 51 void UpdateWorkQueue(LazyNow* lazy_now, | 63 void UpdateWorkQueue(LazyNow* lazy_now, |
| 52 bool should_trigger_wakeup, | 64 bool should_trigger_wakeup, |
| 53 const base::PendingTask* previous_task); | 65 const base::PendingTask* previous_task); |
| 54 base::PendingTask TakeTaskFromWorkQueue(); | 66 Task TakeTaskFromWorkQueue(); |
| 55 | 67 |
| 56 void WillDeleteTaskQueueManager(); | 68 void WillDeleteTaskQueueManager(); |
| 57 | 69 |
| 58 base::TaskQueue& work_queue() { return work_queue_; } | 70 std::queue<Task>& work_queue() { return work_queue_; } |
| 59 | 71 |
| 60 WakeupPolicy wakeup_policy() const { | 72 WakeupPolicy wakeup_policy() const { |
| 61 DCHECK(main_thread_checker_.CalledOnValidThread()); | 73 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 62 return wakeup_policy_; | 74 return wakeup_policy_; |
| 63 } | 75 } |
| 64 | 76 |
| 65 const char* GetName() const override; | 77 const char* GetName() const override; |
| 66 | 78 |
| 67 void AsValueInto(base::trace_event::TracedValue* state) const; | 79 void AsValueInto(base::trace_event::TracedValue* state) const; |
| 68 | 80 |
| 69 size_t get_task_queue_set_index() const { return set_index_; } | 81 size_t get_task_queue_set_index() const { return set_index_; } |
| 70 | 82 |
| 71 void set_task_queue_set_index(size_t set_index) { set_index_ = set_index; } | 83 void set_task_queue_set_index(size_t set_index) { set_index_ = set_index; } |
| 72 | 84 |
| 73 // If the work queue isn't empty, |age| gets set to the sequence number of the | 85 // If the work queue isn't empty, |enqueue_order| gets set to the sequence |
|
Sami
2015/08/05 14:51:15
nit: s/sequence number/enqueue order/ right?
alex clarke (OOO till 29th)
2015/08/05 15:33:05
Done.
| |
| 74 // front task and the dunctio returns true. Otherwise the function returns | 86 // number of the front task and the function returns true. Otherwise the |
| 75 // false. | 87 // function returns false. |
| 76 bool GetWorkQueueFrontTaskAge(int* age) const; | 88 bool GetWorkQueueFrontTaskEnqueueOrder(int* enqueue_order) const; |
| 77 | 89 |
| 78 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_; } | 90 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_; } |
| 79 bool GetShouldNotifyObservers() const { return should_notify_observers_; } | 91 bool GetShouldNotifyObservers() const { return should_notify_observers_; } |
| 80 | 92 |
| 81 // Test support functions. These should not be used in production code. | 93 // Test support functions. These should not be used in production code. |
| 82 void PushTaskOntoWorkQueueForTest(const base::PendingTask& task); | 94 void PushTaskOntoWorkQueueForTest(const Task& task); |
| 83 void PopTaskFromWorkQueueForTest(); | 95 void PopTaskFromWorkQueueForTest(); |
| 84 size_t WorkQueueSizeForTest() const { return work_queue_.size(); } | 96 size_t WorkQueueSizeForTest() const { return work_queue_.size(); } |
| 85 | 97 |
| 86 // Can be called on any thread. | 98 // Can be called on any thread. |
| 87 static const char* PumpPolicyToString(TaskQueue::PumpPolicy pump_policy); | 99 static const char* PumpPolicyToString(TaskQueue::PumpPolicy pump_policy); |
| 88 | 100 |
| 89 // Can be called on any thread. | 101 // Can be called on any thread. |
| 90 static const char* WakeupPolicyToString( | 102 static const char* WakeupPolicyToString( |
| 91 TaskQueue::WakeupPolicy wakeup_policy); | 103 TaskQueue::WakeupPolicy wakeup_policy); |
| 92 | 104 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 123 | 135 |
| 124 // Posts MoveReadyDelayedTasksToIncomingQueue if there isn't already a task | 136 // Posts MoveReadyDelayedTasksToIncomingQueue if there isn't already a task |
| 125 // posted on the underlying runloop for the next task's scheduled run time. | 137 // posted on the underlying runloop for the next task's scheduled run time. |
| 126 void ScheduleDelayedWorkLocked(LazyNow* lazy_now); | 138 void ScheduleDelayedWorkLocked(LazyNow* lazy_now); |
| 127 | 139 |
| 128 void PumpQueueLocked(); | 140 void PumpQueueLocked(); |
| 129 bool TaskIsOlderThanQueuedTasks(const base::PendingTask* task); | 141 bool TaskIsOlderThanQueuedTasks(const base::PendingTask* task); |
| 130 bool ShouldAutoPumpQueueLocked(bool should_trigger_wakeup, | 142 bool ShouldAutoPumpQueueLocked(bool should_trigger_wakeup, |
| 131 const base::PendingTask* previous_task); | 143 const base::PendingTask* previous_task); |
| 132 | 144 |
| 133 // Push the task onto the |incoming_queue_| and allocate a sequence number | 145 enum class EnqueueOrderPolicy { SET_ENQUEUE_ORDER, DONT_SET_ENQUEUE_ORDER }; |
| 134 // for it. | 146 |
| 135 void EnqueueTaskLocked(const base::PendingTask& pending_task); | 147 // Push the task onto the |incoming_queue_| and maybe allocate an |
| 148 // enqueue_order for it based on |enqueue_order_policy|. | |
| 149 void EnqueueTaskLocked(const Task& pending_task, | |
| 150 EnqueueOrderPolicy enqueue_order_policy); | |
| 136 | 151 |
| 137 void TraceQueueSize(bool is_locked) const; | 152 void TraceQueueSize(bool is_locked) const; |
| 138 static void QueueAsValueInto(const base::TaskQueue& queue, | 153 static void QueueAsValueInto(const std::queue<Task>& queue, |
| 139 base::trace_event::TracedValue* state); | 154 base::trace_event::TracedValue* state); |
| 140 static void QueueAsValueInto(const base::DelayedTaskQueue& queue, | 155 static void QueueAsValueInto(const std::priority_queue<Task>& queue, |
| 141 base::trace_event::TracedValue* state); | 156 base::trace_event::TracedValue* state); |
| 142 static void TaskAsValueInto(const base::PendingTask& task, | 157 static void TaskAsValueInto(const Task& task, |
| 143 base::trace_event::TracedValue* state); | 158 base::trace_event::TracedValue* state); |
| 144 | 159 |
| 145 // This lock protects all members in the contigious block below. | 160 // This lock protects all members in the contigious block below. |
| 146 // TODO(alexclarke): Group all the members protected by the lock into a struct | 161 // TODO(alexclarke): Group all the members protected by the lock into a struct |
| 147 mutable base::Lock lock_; | 162 mutable base::Lock lock_; |
| 148 base::PlatformThreadId thread_id_; | 163 base::PlatformThreadId thread_id_; |
| 149 TaskQueueManager* task_queue_manager_; | 164 TaskQueueManager* task_queue_manager_; |
| 150 base::TaskQueue incoming_queue_; | 165 std::queue<Task> incoming_queue_; |
| 151 PumpPolicy pump_policy_; | 166 PumpPolicy pump_policy_; |
| 152 // Queue-local task sequence number for maintaining the order of delayed | 167 std::priority_queue<Task> delayed_task_queue_; |
| 153 // tasks which are posted for the exact same time. Note that this will be | |
| 154 // replaced by the global sequence number when the delay has elapsed. | |
| 155 int delayed_task_sequence_number_; | |
| 156 base::DelayedTaskQueue delayed_task_queue_; | |
| 157 std::set<base::TimeTicks> in_flight_kick_delayed_tasks_; | 168 std::set<base::TimeTicks> in_flight_kick_delayed_tasks_; |
| 158 | 169 |
| 159 const char* name_; | 170 const char* name_; |
| 160 const char* disabled_by_default_tracing_category_; | 171 const char* disabled_by_default_tracing_category_; |
| 161 const char* disabled_by_default_verbose_tracing_category_; | 172 const char* disabled_by_default_verbose_tracing_category_; |
| 162 | 173 |
| 163 base::ThreadChecker main_thread_checker_; | 174 base::ThreadChecker main_thread_checker_; |
| 164 base::TaskQueue work_queue_; | 175 std::queue<Task> work_queue_; |
| 165 WakeupPolicy wakeup_policy_; | 176 WakeupPolicy wakeup_policy_; |
| 166 size_t set_index_; | 177 size_t set_index_; |
| 167 bool should_monitor_quiescence_; | 178 bool should_monitor_quiescence_; |
| 168 bool should_notify_observers_; | 179 bool should_notify_observers_; |
| 169 | 180 |
| 170 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); | 181 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); |
| 171 }; | 182 }; |
| 172 | 183 |
| 173 } // namespace internal | 184 } // namespace internal |
| 174 } // namespace scheduler | 185 } // namespace scheduler |
| 175 | 186 |
| 176 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_ | 187 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_ |
| OLD | NEW |