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 class SCHEDULER_EXPORT Task : public base::PendingTask { |
| 31 public: |
| 32 Task(); |
| 33 Task(const tracked_objects::Location& posted_from, |
| 34 const base::Closure& task, |
| 35 int sequence_number, |
| 36 bool nestable); |
| 37 |
| 38 int enqueue_order() const { |
| 39 #ifndef NDEBUG |
| 40 DCHECK(enqueue_order_set_); |
| 41 #endif |
| 42 return enqueue_order_; |
| 43 } |
| 44 |
| 45 void set_enqueue_order(int enqueue_order) { |
| 46 #ifndef NDEBUG |
| 47 DCHECK(!enqueue_order_set_); |
| 48 enqueue_order_set_ = true; |
| 49 #endif |
| 50 enqueue_order_ = enqueue_order; |
| 51 } |
| 52 |
| 53 private: |
| 54 #ifndef NDEBUG |
| 55 bool enqueue_order_set_; |
| 56 #endif |
| 57 // Similar to sequence number, but the |enqueue_order| is set by |
| 58 // EnqueueTasksLocked and is not initially defined for delayed tasks until |
| 59 // they are enqueued on the |incoming_queue_|. |
| 60 int enqueue_order_; |
| 61 }; |
| 62 |
30 // TaskQueue implementation. | 63 // TaskQueue implementation. |
31 bool RunsTasksOnCurrentThread() const override; | 64 bool RunsTasksOnCurrentThread() const override; |
32 bool PostDelayedTask(const tracked_objects::Location& from_here, | 65 bool PostDelayedTask(const tracked_objects::Location& from_here, |
33 const base::Closure& task, | 66 const base::Closure& task, |
34 base::TimeDelta delay) override; | 67 base::TimeDelta delay) override; |
35 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 68 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
36 const base::Closure& task, | 69 const base::Closure& task, |
37 base::TimeDelta delay) override; | 70 base::TimeDelta delay) override; |
38 bool PostDelayedTaskAt(const tracked_objects::Location& from_here, | 71 bool PostDelayedTaskAt(const tracked_objects::Location& from_here, |
39 const base::Closure& task, | 72 const base::Closure& task, |
40 base::TimeTicks desired_run_time) override; | 73 base::TimeTicks desired_run_time) override; |
41 | 74 |
42 bool IsQueueEnabled() const override; | 75 bool IsQueueEnabled() const override; |
43 QueueState GetQueueState() const override; | 76 QueueState GetQueueState() const override; |
44 void SetQueuePriority(QueuePriority priority) override; | 77 void SetQueuePriority(QueuePriority priority) override; |
45 void PumpQueue() override; | 78 void PumpQueue() override; |
46 void SetPumpPolicy(PumpPolicy pump_policy) override; | 79 void SetPumpPolicy(PumpPolicy pump_policy) override; |
47 | 80 |
48 bool NextPendingDelayedTaskRunTime( | 81 bool NextPendingDelayedTaskRunTime( |
49 base::TimeTicks* next_pending_delayed_task); | 82 base::TimeTicks* next_pending_delayed_task); |
50 | 83 |
51 void UpdateWorkQueue(LazyNow* lazy_now, | 84 void UpdateWorkQueue(LazyNow* lazy_now, |
52 bool should_trigger_wakeup, | 85 bool should_trigger_wakeup, |
53 const base::PendingTask* previous_task); | 86 const Task* previous_task); |
54 base::PendingTask TakeTaskFromWorkQueue(); | 87 Task TakeTaskFromWorkQueue(); |
55 | 88 |
56 void WillDeleteTaskQueueManager(); | 89 void WillDeleteTaskQueueManager(); |
57 | 90 |
58 base::TaskQueue& work_queue() { return work_queue_; } | 91 std::queue<Task>& work_queue() { return work_queue_; } |
59 | 92 |
60 WakeupPolicy wakeup_policy() const { | 93 WakeupPolicy wakeup_policy() const { |
61 DCHECK(main_thread_checker_.CalledOnValidThread()); | 94 DCHECK(main_thread_checker_.CalledOnValidThread()); |
62 return wakeup_policy_; | 95 return wakeup_policy_; |
63 } | 96 } |
64 | 97 |
65 const char* GetName() const override; | 98 const char* GetName() const override; |
66 | 99 |
67 void AsValueInto(base::trace_event::TracedValue* state) const; | 100 void AsValueInto(base::trace_event::TracedValue* state) const; |
68 | 101 |
69 size_t get_task_queue_set_index() const { return set_index_; } | 102 size_t get_task_queue_set_index() const { return set_index_; } |
70 | 103 |
71 void set_task_queue_set_index(size_t set_index) { set_index_ = set_index; } | 104 void set_task_queue_set_index(size_t set_index) { set_index_ = set_index; } |
72 | 105 |
73 // If the work queue isn't empty, |age| gets set to the sequence number of the | 106 // If the work queue isn't empty, |enqueue_order| gets set to the enqueue |
74 // front task and the dunctio returns true. Otherwise the function returns | 107 // order of the front task and the function returns true. Otherwise the |
75 // false. | 108 // function returns false. |
76 bool GetWorkQueueFrontTaskAge(int* age) const; | 109 bool GetWorkQueueFrontTaskEnqueueOrder(int* enqueue_order) const; |
77 | 110 |
78 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_; } | 111 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_; } |
79 bool GetShouldNotifyObservers() const { return should_notify_observers_; } | 112 bool GetShouldNotifyObservers() const { return should_notify_observers_; } |
80 | 113 |
81 // Test support functions. These should not be used in production code. | 114 // Test support functions. These should not be used in production code. |
82 void PushTaskOntoWorkQueueForTest(const base::PendingTask& task); | 115 void PushTaskOntoWorkQueueForTest(const Task& task); |
83 void PopTaskFromWorkQueueForTest(); | 116 void PopTaskFromWorkQueueForTest(); |
84 size_t WorkQueueSizeForTest() const { return work_queue_.size(); } | 117 size_t WorkQueueSizeForTest() const { return work_queue_.size(); } |
85 | 118 |
86 // Can be called on any thread. | 119 // Can be called on any thread. |
87 static const char* PumpPolicyToString(TaskQueue::PumpPolicy pump_policy); | 120 static const char* PumpPolicyToString(TaskQueue::PumpPolicy pump_policy); |
88 | 121 |
89 // Can be called on any thread. | 122 // Can be called on any thread. |
90 static const char* WakeupPolicyToString( | 123 static const char* WakeupPolicyToString( |
91 TaskQueue::WakeupPolicy wakeup_policy); | 124 TaskQueue::WakeupPolicy wakeup_policy); |
92 | 125 |
(...skipping 26 matching lines...) Expand all Loading... |
119 // Enqueues any delayed tasks which should be run now on the incoming_queue_ | 152 // Enqueues any delayed tasks which should be run now on the incoming_queue_ |
120 // and calls ScheduleDelayedWorkLocked to ensure future tasks are scheduled. | 153 // and calls ScheduleDelayedWorkLocked to ensure future tasks are scheduled. |
121 // Must be called with |lock_| locked. | 154 // Must be called with |lock_| locked. |
122 void MoveReadyDelayedTasksToIncomingQueueLocked(LazyNow* lazy_now); | 155 void MoveReadyDelayedTasksToIncomingQueueLocked(LazyNow* lazy_now); |
123 | 156 |
124 // Posts MoveReadyDelayedTasksToIncomingQueue if there isn't already a task | 157 // Posts MoveReadyDelayedTasksToIncomingQueue if there isn't already a task |
125 // posted on the underlying runloop for the next task's scheduled run time. | 158 // posted on the underlying runloop for the next task's scheduled run time. |
126 void ScheduleDelayedWorkLocked(LazyNow* lazy_now); | 159 void ScheduleDelayedWorkLocked(LazyNow* lazy_now); |
127 | 160 |
128 void PumpQueueLocked(); | 161 void PumpQueueLocked(); |
129 bool TaskIsOlderThanQueuedTasks(const base::PendingTask* task); | 162 bool TaskIsOlderThanQueuedTasks(const Task* task); |
130 bool ShouldAutoPumpQueueLocked(bool should_trigger_wakeup, | 163 bool ShouldAutoPumpQueueLocked(bool should_trigger_wakeup, |
131 const base::PendingTask* previous_task); | 164 const Task* previous_task); |
132 | 165 |
133 // Push the task onto the |incoming_queue_| and allocate a sequence number | 166 enum class EnqueueOrderPolicy { SET_ENQUEUE_ORDER, DONT_SET_ENQUEUE_ORDER }; |
134 // for it. | 167 |
135 void EnqueueTaskLocked(const base::PendingTask& pending_task); | 168 // Push the task onto the |incoming_queue_| and maybe allocate an |
| 169 // enqueue_order for it based on |enqueue_order_policy|. |
| 170 void EnqueueTaskLocked(const Task& pending_task, |
| 171 EnqueueOrderPolicy enqueue_order_policy); |
136 | 172 |
137 void TraceQueueSize(bool is_locked) const; | 173 void TraceQueueSize(bool is_locked) const; |
138 static void QueueAsValueInto(const base::TaskQueue& queue, | 174 static void QueueAsValueInto(const std::queue<Task>& queue, |
139 base::trace_event::TracedValue* state); | 175 base::trace_event::TracedValue* state); |
140 static void QueueAsValueInto(const base::DelayedTaskQueue& queue, | 176 static void QueueAsValueInto(const std::priority_queue<Task>& queue, |
141 base::trace_event::TracedValue* state); | 177 base::trace_event::TracedValue* state); |
142 static void TaskAsValueInto(const base::PendingTask& task, | 178 static void TaskAsValueInto(const Task& task, |
143 base::trace_event::TracedValue* state); | 179 base::trace_event::TracedValue* state); |
144 | 180 |
145 // This lock protects all members in the contigious block below. | 181 // This lock protects all members in the contigious block below. |
146 // TODO(alexclarke): Group all the members protected by the lock into a struct | 182 // TODO(alexclarke): Group all the members protected by the lock into a struct |
147 mutable base::Lock lock_; | 183 mutable base::Lock lock_; |
148 base::PlatformThreadId thread_id_; | 184 base::PlatformThreadId thread_id_; |
149 TaskQueueManager* task_queue_manager_; | 185 TaskQueueManager* task_queue_manager_; |
150 base::TaskQueue incoming_queue_; | 186 std::queue<Task> incoming_queue_; |
151 PumpPolicy pump_policy_; | 187 PumpPolicy pump_policy_; |
152 // Queue-local task sequence number for maintaining the order of delayed | 188 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_; | 189 std::set<base::TimeTicks> in_flight_kick_delayed_tasks_; |
158 | 190 |
159 const char* name_; | 191 const char* name_; |
160 const char* disabled_by_default_tracing_category_; | 192 const char* disabled_by_default_tracing_category_; |
161 const char* disabled_by_default_verbose_tracing_category_; | 193 const char* disabled_by_default_verbose_tracing_category_; |
162 | 194 |
163 base::ThreadChecker main_thread_checker_; | 195 base::ThreadChecker main_thread_checker_; |
164 base::TaskQueue work_queue_; | 196 std::queue<Task> work_queue_; |
165 WakeupPolicy wakeup_policy_; | 197 WakeupPolicy wakeup_policy_; |
166 size_t set_index_; | 198 size_t set_index_; |
167 bool should_monitor_quiescence_; | 199 bool should_monitor_quiescence_; |
168 bool should_notify_observers_; | 200 bool should_notify_observers_; |
169 | 201 |
170 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); | 202 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); |
171 }; | 203 }; |
172 | 204 |
173 } // namespace internal | 205 } // namespace internal |
174 } // namespace scheduler | 206 } // namespace scheduler |
175 | 207 |
176 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_ | 208 #endif // CONTENT_RENDERER_SCHEDULER_TASK_QUEUE_IMPL_H_ |
OLD | NEW |