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 THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ | 5 #ifndef THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ |
6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ | 6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 base::TimeDelta delay) override; | 142 base::TimeDelta delay) override; |
143 TaskHandle PostCancellableDelayedTask( | 143 TaskHandle PostCancellableDelayedTask( |
144 const tracked_objects::Location& from_here, | 144 const tracked_objects::Location& from_here, |
145 const base::Closure& task, | 145 const base::Closure& task, |
146 base::TimeDelta delay) override; | 146 base::TimeDelta delay) override; |
147 bool CancelTask(const TaskHandle& handle) override; | 147 bool CancelTask(const TaskHandle& handle) override; |
148 void SetQueueEnabled(bool enabled) override; | 148 void SetQueueEnabled(bool enabled) override; |
149 bool IsQueueEnabled() const override; | 149 bool IsQueueEnabled() const override; |
150 bool IsEmpty() const override; | 150 bool IsEmpty() const override; |
151 bool HasPendingImmediateWork() const override; | 151 bool HasPendingImmediateWork() const override; |
152 bool NeedsPumping() const override; | |
153 void SetQueuePriority(QueuePriority priority) override; | 152 void SetQueuePriority(QueuePriority priority) override; |
154 QueuePriority GetQueuePriority() const override; | 153 QueuePriority GetQueuePriority() const override; |
155 void PumpQueue(LazyNow* lazy_now, bool may_post_dowork) override; | |
156 void SetPumpPolicy(PumpPolicy pump_policy) override; | |
157 PumpPolicy GetPumpPolicy() const override; | |
158 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override; | 154 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override; |
159 void RemoveTaskObserver( | 155 void RemoveTaskObserver( |
160 base::MessageLoop::TaskObserver* task_observer) override; | 156 base::MessageLoop::TaskObserver* task_observer) override; |
161 void SetTimeDomain(TimeDomain* time_domain) override; | 157 void SetTimeDomain(TimeDomain* time_domain) override; |
162 TimeDomain* GetTimeDomain() const override; | 158 TimeDomain* GetTimeDomain() const override; |
163 void SetBlameContext(base::trace_event::BlameContext* blame_context) override; | 159 void SetBlameContext(base::trace_event::BlameContext* blame_context) override; |
| 160 void InsertFence() override; |
| 161 void RemoveFence() override; |
| 162 bool BlockedByFence() const override; |
164 | 163 |
165 bool IsTaskPending(const TaskHandle& handle) const; | 164 bool IsTaskPending(const TaskHandle& handle) const; |
166 | 165 |
167 void UpdateImmediateWorkQueue(bool should_trigger_wakeup, | 166 void UpdateImmediateWorkQueue(); |
168 const Task* previous_task); | 167 void UpdateDelayedWorkQueue(LazyNow* lazy_now); |
169 void UpdateDelayedWorkQueue(LazyNow* lazy_now, | |
170 bool should_trigger_wakeup, | |
171 const Task* previous_task); | |
172 | |
173 WakeupPolicy wakeup_policy() const { | |
174 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
175 return wakeup_policy_; | |
176 } | |
177 | 168 |
178 const char* GetName() const override; | 169 const char* GetName() const override; |
179 | 170 |
180 void AsValueInto(base::trace_event::TracedValue* state) const; | 171 void AsValueInto(base::trace_event::TracedValue* state) const; |
181 | 172 |
182 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_; } | 173 bool GetQuiescenceMonitored() const { return should_monitor_quiescence_; } |
183 bool GetShouldNotifyObservers() const { return should_notify_observers_; } | 174 bool GetShouldNotifyObservers() const { return should_notify_observers_; } |
184 | 175 |
185 void NotifyWillProcessTask(const base::PendingTask& pending_task); | 176 void NotifyWillProcessTask(const base::PendingTask& pending_task); |
186 void NotifyDidProcessTask(const base::PendingTask& pending_task); | 177 void NotifyDidProcessTask(const base::PendingTask& pending_task); |
187 | 178 |
188 // Can be called on any thread. | 179 EnqueueOrder GetNewestImmediateTaskEnqueueOrder() const; |
189 static const char* PumpPolicyToString(TaskQueue::PumpPolicy pump_policy); | 180 EnqueueOrder GetNewestReadyDelayedTaskEnqueueOrder(); |
190 | |
191 // Can be called on any thread. | |
192 static const char* WakeupPolicyToString( | |
193 TaskQueue::WakeupPolicy wakeup_policy); | |
194 | 181 |
195 // Can be called on any thread. | 182 // Can be called on any thread. |
196 static const char* PriorityToString(TaskQueue::QueuePriority priority); | 183 static const char* PriorityToString(TaskQueue::QueuePriority priority); |
197 | 184 |
198 WorkQueue* delayed_work_queue() { | 185 WorkQueue* delayed_work_queue() { |
199 return main_thread_only().delayed_work_queue.get(); | 186 return main_thread_only().delayed_work_queue.get(); |
200 } | 187 } |
201 | 188 |
202 const WorkQueue* delayed_work_queue() const { | 189 const WorkQueue* delayed_work_queue() const { |
203 return main_thread_only().delayed_work_queue.get(); | 190 return main_thread_only().delayed_work_queue.get(); |
204 } | 191 } |
205 | 192 |
206 WorkQueue* immediate_work_queue() { | 193 WorkQueue* immediate_work_queue() { |
207 return main_thread_only().immediate_work_queue.get(); | 194 return main_thread_only().immediate_work_queue.get(); |
208 } | 195 } |
209 | 196 |
210 const WorkQueue* immediate_work_queue() const { | 197 const WorkQueue* immediate_work_queue() const { |
211 return main_thread_only().immediate_work_queue.get(); | 198 return main_thread_only().immediate_work_queue.get(); |
212 } | 199 } |
213 | 200 |
214 bool should_report_when_execution_blocked() const { | 201 bool should_report_when_execution_blocked() const { |
215 return should_report_when_execution_blocked_; | 202 return should_report_when_execution_blocked_; |
216 } | 203 } |
217 | 204 |
218 private: | 205 private: |
219 friend class WorkQueue; | 206 friend class WorkQueue; |
| 207 friend class WorkQueueTest; |
220 | 208 |
221 // Note both DelayedRunTimeQueue and ComparatorQueue are sets for fast task | 209 // Note both DelayedRunTimeQueue and ComparatorQueue are sets for fast task |
222 // cancellation. Typically queue sizes are well under 200 so the overhead of | 210 // cancellation. Typically queue sizes are well under 200 so the overhead of |
223 // std::set vs std::priority_queue and std::queue is lost in the noise of | 211 // std::set vs std::priority_queue and std::queue is lost in the noise of |
224 // everything else. | 212 // everything else. |
225 using DelayedRunTimeQueue = std::set<Task, Task::DelayedRunTimeComparator>; | 213 using DelayedRunTimeQueue = std::set<Task, Task::DelayedRunTimeComparator>; |
226 using ComparatorQueue = std::set<Task, Task::ComparatorFn>; | 214 using ComparatorQueue = std::set<Task, Task::ComparatorFn>; |
227 | 215 |
228 enum class TaskType { | 216 enum class TaskType { |
229 NORMAL, | 217 NORMAL, |
230 NON_NESTABLE, | 218 NON_NESTABLE, |
231 }; | 219 }; |
232 | 220 |
233 struct AnyThread { | 221 struct AnyThread { |
234 AnyThread(TaskQueueManager* task_queue_manager, | 222 AnyThread(TaskQueueManager* task_queue_manager, |
235 PumpPolicy pump_policy, | |
236 TimeDomain* time_domain); | 223 TimeDomain* time_domain); |
237 ~AnyThread(); | 224 ~AnyThread(); |
238 | 225 |
239 // TaskQueueManager, PumpPolicy and TimeDomain are maintained in two copies: | 226 // TaskQueueManager and TimeDomain are maintained in two copies: |
240 // inside AnyThread and inside MainThreadOnly. They can be changed only from | 227 // inside AnyThread and inside MainThreadOnly. They can be changed only from |
241 // main thread, so it should be locked before accessing from other threads. | 228 // main thread, so it should be locked before accessing from other threads. |
242 TaskQueueManager* task_queue_manager; | 229 TaskQueueManager* task_queue_manager; |
243 PumpPolicy pump_policy; | |
244 TimeDomain* time_domain; | 230 TimeDomain* time_domain; |
245 | 231 |
246 ComparatorQueue immediate_incoming_queue; | 232 ComparatorQueue immediate_incoming_queue; |
247 }; | 233 }; |
248 | 234 |
249 struct MainThreadOnly { | 235 struct MainThreadOnly { |
250 MainThreadOnly(TaskQueueManager* task_queue_manager, | 236 MainThreadOnly(TaskQueueManager* task_queue_manager, |
251 PumpPolicy pump_policy, | |
252 TaskQueueImpl* task_queue, | 237 TaskQueueImpl* task_queue, |
253 TimeDomain* time_domain); | 238 TimeDomain* time_domain); |
254 ~MainThreadOnly(); | 239 ~MainThreadOnly(); |
255 | 240 |
256 // Another copy of TaskQueueManager, PumpPolicy and TimeDomain for lock-free | 241 // Another copy of TaskQueueManager and TimeDomain for lock-free access from |
257 // access from the main thread. See description inside struct AnyThread for | 242 // the main thread. See description inside struct AnyThread for details. |
258 // details. | |
259 TaskQueueManager* task_queue_manager; | 243 TaskQueueManager* task_queue_manager; |
260 PumpPolicy pump_policy; | |
261 TimeDomain* time_domain; | 244 TimeDomain* time_domain; |
262 | 245 |
263 std::unique_ptr<WorkQueue> delayed_work_queue; | 246 std::unique_ptr<WorkQueue> delayed_work_queue; |
264 std::unique_ptr<WorkQueue> immediate_work_queue; | 247 std::unique_ptr<WorkQueue> immediate_work_queue; |
265 DelayedRunTimeQueue delayed_incoming_queue; | 248 DelayedRunTimeQueue delayed_incoming_queue; |
266 base::ObserverList<base::MessageLoop::TaskObserver> task_observers; | 249 base::ObserverList<base::MessageLoop::TaskObserver> task_observers; |
267 size_t set_index; | 250 size_t set_index; |
268 bool is_enabled; | 251 bool is_enabled; |
269 base::trace_event::BlameContext* blame_context; // Not owned. | 252 base::trace_event::BlameContext* blame_context; // Not owned. |
270 }; | 253 }; |
(...skipping 18 matching lines...) Expand all Loading... |
289 void PushOntoDelayedIncomingQueueLocked(Task pending_task); | 272 void PushOntoDelayedIncomingQueueLocked(Task pending_task); |
290 | 273 |
291 void ScheduleDelayedWorkTask(Task pending_task); | 274 void ScheduleDelayedWorkTask(Task pending_task); |
292 | 275 |
293 // Enqueues any delayed tasks which should be run now on the | 276 // Enqueues any delayed tasks which should be run now on the |
294 // |delayed_work_queue|. Must be called from the main thread. | 277 // |delayed_work_queue|. Must be called from the main thread. |
295 void MoveReadyDelayedTasksToDelayedWorkQueue(LazyNow* lazy_now); | 278 void MoveReadyDelayedTasksToDelayedWorkQueue(LazyNow* lazy_now); |
296 | 279 |
297 void MoveReadyImmediateTasksToImmediateWorkQueueLocked(); | 280 void MoveReadyImmediateTasksToImmediateWorkQueueLocked(); |
298 | 281 |
299 // Note this does nothing if its not called from the main thread. | |
300 void PumpQueueLocked(LazyNow* lazy_now, bool may_post_dowork); | |
301 | |
302 // Returns true if |task| is older than the oldest incoming immediate task. | |
303 // NOTE |any_thread_lock_| must be locked. | |
304 bool TaskIsOlderThanQueuedImmediateTasksLocked(const Task* task); | |
305 | |
306 // Returns true if |task| is older than the oldest delayed task. Must be | |
307 // called from the main thread. | |
308 bool TaskIsOlderThanQueuedDelayedTasks(const Task* task); | |
309 | |
310 // NOTE |any_thread_lock_| must be locked. | |
311 bool ShouldAutoPumpImmediateQueueLocked(bool should_trigger_wakeup, | |
312 const Task* previous_task); | |
313 | |
314 // Must be called from the main thread. | |
315 bool ShouldAutoPumpDelayedQueue(bool should_trigger_wakeup, | |
316 const Task* previous_task); | |
317 | |
318 // Push the task onto the |immediate_incoming_queue| and for auto pumped | 282 // Push the task onto the |immediate_incoming_queue| and for auto pumped |
319 // queues it calls MaybePostDoWorkOnMainRunner if the Incoming queue was | 283 // queues it calls MaybePostDoWorkOnMainRunner if the Incoming queue was |
320 // empty. | 284 // empty. |
321 void PushOntoImmediateIncomingQueueLocked(Task pending_task); | 285 void PushOntoImmediateIncomingQueueLocked(Task pending_task); |
322 | 286 |
323 void TraceQueueSize(bool is_locked) const; | 287 void TraceQueueSize(bool is_locked) const; |
324 static void QueueAsValueInto(const ComparatorQueue& queue, | 288 static void QueueAsValueInto(const ComparatorQueue& queue, |
325 base::trace_event::TracedValue* state); | 289 base::trace_event::TracedValue* state); |
326 static void QueueAsValueInto(const DelayedRunTimeQueue& queue, | 290 static void QueueAsValueInto(const DelayedRunTimeQueue& queue, |
327 base::trace_event::TracedValue* state); | 291 base::trace_event::TracedValue* state); |
(...skipping 21 matching lines...) Expand all Loading... |
349 MainThreadOnly main_thread_only_; | 313 MainThreadOnly main_thread_only_; |
350 MainThreadOnly& main_thread_only() { | 314 MainThreadOnly& main_thread_only() { |
351 DCHECK(main_thread_checker_.CalledOnValidThread()); | 315 DCHECK(main_thread_checker_.CalledOnValidThread()); |
352 return main_thread_only_; | 316 return main_thread_only_; |
353 } | 317 } |
354 const MainThreadOnly& main_thread_only() const { | 318 const MainThreadOnly& main_thread_only() const { |
355 DCHECK(main_thread_checker_.CalledOnValidThread()); | 319 DCHECK(main_thread_checker_.CalledOnValidThread()); |
356 return main_thread_only_; | 320 return main_thread_only_; |
357 } | 321 } |
358 | 322 |
359 const WakeupPolicy wakeup_policy_; | |
360 const bool should_monitor_quiescence_; | 323 const bool should_monitor_quiescence_; |
361 const bool should_notify_observers_; | 324 const bool should_notify_observers_; |
362 const bool should_report_when_execution_blocked_; | 325 const bool should_report_when_execution_blocked_; |
363 | 326 |
364 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); | 327 DISALLOW_COPY_AND_ASSIGN(TaskQueueImpl); |
365 }; | 328 }; |
366 | 329 |
367 } // namespace internal | 330 } // namespace internal |
368 } // namespace scheduler | 331 } // namespace scheduler |
369 } // namespace blink | 332 } // namespace blink |
370 | 333 |
371 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ | 334 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TASK_QUEUE_IMPL_H_ |
OLD | NEW |