OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "content/renderer/scheduler/task_queue_manager.h" | 5 #include "content/renderer/scheduler/task_queue_manager.h" |
6 | 6 |
7 #include <queue> | 7 #include <queue> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
(...skipping 25 matching lines...) Expand all Loading... | |
36 base::TimeDelta delay) override { | 36 base::TimeDelta delay) override { |
37 return PostDelayedTaskImpl(from_here, task, delay, TaskType::NON_NESTABLE); | 37 return PostDelayedTaskImpl(from_here, task, delay, TaskType::NON_NESTABLE); |
38 } | 38 } |
39 | 39 |
40 bool IsQueueEmpty() const; | 40 bool IsQueueEmpty() const; |
41 | 41 |
42 void SetPumpPolicy(TaskQueueManager::PumpPolicy pump_policy); | 42 void SetPumpPolicy(TaskQueueManager::PumpPolicy pump_policy); |
43 void PumpQueue(); | 43 void PumpQueue(); |
44 | 44 |
45 bool UpdateWorkQueue(base::TimeTicks* next_pending_delayed_task, | 45 bool UpdateWorkQueue(base::TimeTicks* next_pending_delayed_task, |
46 TaskQueueManager::WorkQueueUpdateEventType event_type); | 46 const base::PendingTask* previous_task); |
47 base::PendingTask TakeTaskFromWorkQueue(); | 47 base::PendingTask TakeTaskFromWorkQueue(); |
48 | 48 |
49 void WillDeleteTaskQueueManager(); | 49 void WillDeleteTaskQueueManager(); |
50 | 50 |
51 base::TaskQueue& work_queue() { return work_queue_; } | 51 base::TaskQueue& work_queue() { return work_queue_; } |
52 | 52 |
53 void set_name(const char* name) { name_ = name; } | 53 void set_name(const char* name) { name_ = name; } |
54 | 54 |
55 void AsValueInto(base::trace_event::TracedValue* state) const; | 55 void AsValueInto(base::trace_event::TracedValue* state) const; |
56 | 56 |
57 private: | 57 private: |
58 enum class TaskType { | 58 enum class TaskType { |
59 NORMAL, | 59 NORMAL, |
60 NON_NESTABLE, | 60 NON_NESTABLE, |
61 }; | 61 }; |
62 | 62 |
63 ~TaskQueue() override; | 63 ~TaskQueue() override; |
64 | 64 |
65 bool PostDelayedTaskImpl(const tracked_objects::Location& from_here, | 65 bool PostDelayedTaskImpl(const tracked_objects::Location& from_here, |
66 const base::Closure& task, | 66 const base::Closure& task, |
67 base::TimeDelta delay, | 67 base::TimeDelta delay, |
68 TaskType task_type); | 68 TaskType task_type); |
69 | 69 |
70 // Adds a task at the end of the incoming task queue and schedules a call to | 70 // Adds a task at the end of the incoming task queue and schedules a call to |
71 // TaskQueueManager::DoWork() if the incoming queue was empty and automatic | 71 // TaskQueueManager::DoWork() if the incoming queue was empty and automatic |
72 // pumping is enabled. Can be called on an arbitrary thread. | 72 // pumping is enabled. Can be called on an arbitrary thread. |
73 void EnqueueTask(const base::PendingTask& pending_task); | 73 void EnqueueTask(const base::PendingTask& pending_task); |
74 | 74 |
75 void PumpQueueLocked(); | 75 void PumpQueueLocked(); |
76 bool ShouldAutoPumpQueueLocked( | 76 bool TaskIsOlderThanQueuedTasks(const base::PendingTask* task); |
77 TaskQueueManager::WorkQueueUpdateEventType event_type); | 77 bool ShouldAutoPumpQueueLocked(const base::PendingTask* previous_task); |
78 void EnqueueTaskLocked(const base::PendingTask& pending_task); | 78 void EnqueueTaskLocked(const base::PendingTask& pending_task); |
79 | 79 |
80 void TraceQueueSize(bool is_locked) const; | 80 void TraceQueueSize(bool is_locked) const; |
81 static const char* PumpPolicyToString( | 81 static const char* PumpPolicyToString( |
82 TaskQueueManager::PumpPolicy pump_policy); | 82 TaskQueueManager::PumpPolicy pump_policy); |
83 static void QueueAsValueInto(const base::TaskQueue& queue, | 83 static void QueueAsValueInto(const base::TaskQueue& queue, |
84 base::trace_event::TracedValue* state); | 84 base::trace_event::TracedValue* state); |
85 static void TaskAsValueInto(const base::PendingTask& task, | 85 static void TaskAsValueInto(const base::PendingTask& task, |
86 base::trace_event::TracedValue* state); | 86 base::trace_event::TracedValue* state); |
87 | 87 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 bool TaskQueue::IsQueueEmpty() const { | 146 bool TaskQueue::IsQueueEmpty() const { |
147 if (!work_queue_.empty()) | 147 if (!work_queue_.empty()) |
148 return false; | 148 return false; |
149 | 149 |
150 { | 150 { |
151 base::AutoLock lock(lock_); | 151 base::AutoLock lock(lock_); |
152 return incoming_queue_.empty(); | 152 return incoming_queue_.empty(); |
153 } | 153 } |
154 } | 154 } |
155 | 155 |
156 bool TaskQueue::TaskIsOlderThanQueuedTasks(const base::PendingTask* task) { | |
157 lock_.AssertAcquired(); | |
158 // Always return true if passed a NULL task. | |
159 if (task == NULL) | |
picksi
2015/03/02 14:30:06
Is this to protect against null-pointer deferencin
Sami
2015/03/02 14:48:46
Also, I think we're using nullptr now, although "(
rmcilroy
2015/03/02 15:34:51
Very true (the comment added more before I explici
rmcilroy
2015/03/02 15:34:52
Done.
| |
160 return true; | |
161 | |
162 // Return false if there are no task in the incoming queue. | |
163 if (incoming_queue_.empty()) | |
164 return false; | |
165 | |
166 base::PendingTask oldest_queued_task = incoming_queue_.front(); | |
167 | |
168 // Note: the comparison is correct due to the fact that the PendingTask | |
169 // operator inverts its comparison operation in order to work well in a heap | |
170 // based priority queue. | |
171 return oldest_queued_task < *task; | |
172 } | |
173 | |
156 bool TaskQueue::ShouldAutoPumpQueueLocked( | 174 bool TaskQueue::ShouldAutoPumpQueueLocked( |
157 TaskQueueManager::WorkQueueUpdateEventType event_type) { | 175 const base::PendingTask* previous_task) { |
158 lock_.AssertAcquired(); | 176 lock_.AssertAcquired(); |
159 if (pump_policy_ == TaskQueueManager::PumpPolicy::MANUAL) | 177 if (pump_policy_ == TaskQueueManager::PumpPolicy::MANUAL) |
160 return false; | 178 return false; |
161 if (pump_policy_ == TaskQueueManager::PumpPolicy::AFTER_WAKEUP && | 179 if (pump_policy_ == TaskQueueManager::PumpPolicy::AFTER_WAKEUP && |
162 event_type != TaskQueueManager::WorkQueueUpdateEventType::AFTER_WAKEUP) | 180 TaskIsOlderThanQueuedTasks(previous_task)) |
163 return false; | 181 return false; |
164 if (incoming_queue_.empty()) | 182 if (incoming_queue_.empty()) |
165 return false; | 183 return false; |
166 return true; | 184 return true; |
167 } | 185 } |
168 | 186 |
169 bool TaskQueue::UpdateWorkQueue( | 187 bool TaskQueue::UpdateWorkQueue( |
170 base::TimeTicks* next_pending_delayed_task, | 188 base::TimeTicks* next_pending_delayed_task, |
171 TaskQueueManager::WorkQueueUpdateEventType event_type) { | 189 const base::PendingTask* previous_task) { |
172 if (!work_queue_.empty()) | 190 if (!work_queue_.empty()) |
173 return true; | 191 return true; |
174 | 192 |
175 { | 193 { |
176 base::AutoLock lock(lock_); | 194 base::AutoLock lock(lock_); |
177 if (!delayed_task_run_times_.empty()) { | 195 if (!delayed_task_run_times_.empty()) { |
178 *next_pending_delayed_task = | 196 *next_pending_delayed_task = |
179 std::min(*next_pending_delayed_task, delayed_task_run_times_.top()); | 197 std::min(*next_pending_delayed_task, delayed_task_run_times_.top()); |
180 } | 198 } |
181 if (!ShouldAutoPumpQueueLocked(event_type)) | 199 if (!ShouldAutoPumpQueueLocked(previous_task)) |
182 return false; | 200 return false; |
183 work_queue_.Swap(&incoming_queue_); | 201 work_queue_.Swap(&incoming_queue_); |
184 TraceQueueSize(true); | 202 TraceQueueSize(true); |
185 return true; | 203 return true; |
186 } | 204 } |
187 } | 205 } |
188 | 206 |
189 base::PendingTask TaskQueue::TakeTaskFromWorkQueue() { | 207 base::PendingTask TaskQueue::TakeTaskFromWorkQueue() { |
190 base::PendingTask pending_task = work_queue_.front(); | 208 base::PendingTask pending_task = work_queue_.front(); |
191 work_queue_.pop(); | 209 work_queue_.pop(); |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
376 } | 394 } |
377 | 395 |
378 void TaskQueueManager::PumpQueue(size_t queue_index) { | 396 void TaskQueueManager::PumpQueue(size_t queue_index) { |
379 DCHECK(main_thread_checker_.CalledOnValidThread()); | 397 DCHECK(main_thread_checker_.CalledOnValidThread()); |
380 internal::TaskQueue* queue = Queue(queue_index); | 398 internal::TaskQueue* queue = Queue(queue_index); |
381 queue->PumpQueue(); | 399 queue->PumpQueue(); |
382 } | 400 } |
383 | 401 |
384 bool TaskQueueManager::UpdateWorkQueues( | 402 bool TaskQueueManager::UpdateWorkQueues( |
385 base::TimeTicks* next_pending_delayed_task, | 403 base::TimeTicks* next_pending_delayed_task, |
386 WorkQueueUpdateEventType event_type) { | 404 const base::PendingTask* previous_task) { |
387 // TODO(skyostil): This is not efficient when the number of queues grows very | 405 // TODO(skyostil): This is not efficient when the number of queues grows very |
388 // large due to the number of locks taken. Consider optimizing when we get | 406 // large due to the number of locks taken. Consider optimizing when we get |
389 // there. | 407 // there. |
390 DCHECK(main_thread_checker_.CalledOnValidThread()); | 408 DCHECK(main_thread_checker_.CalledOnValidThread()); |
391 bool has_work = false; | 409 bool has_work = false; |
392 for (auto& queue : queues_) { | 410 for (auto& queue : queues_) { |
393 has_work |= queue->UpdateWorkQueue(next_pending_delayed_task, event_type); | 411 has_work |= queue->UpdateWorkQueue(next_pending_delayed_task, |
412 previous_task); | |
394 if (!queue->work_queue().empty()) { | 413 if (!queue->work_queue().empty()) { |
395 // Currently we should not be getting tasks with delayed run times in any | 414 // Currently we should not be getting tasks with delayed run times in any |
396 // of the work queues. | 415 // of the work queues. |
397 DCHECK(queue->work_queue().front().delayed_run_time.is_null()); | 416 DCHECK(queue->work_queue().front().delayed_run_time.is_null()); |
398 } | 417 } |
399 } | 418 } |
400 return has_work; | 419 return has_work; |
401 } | 420 } |
402 | 421 |
403 void TaskQueueManager::MaybePostDoWorkOnMainRunner() { | 422 void TaskQueueManager::MaybePostDoWorkOnMainRunner() { |
(...skipping 14 matching lines...) Expand all Loading... | |
418 | 437 |
419 void TaskQueueManager::DoWork(bool posted_from_main_thread) { | 438 void TaskQueueManager::DoWork(bool posted_from_main_thread) { |
420 if (posted_from_main_thread) { | 439 if (posted_from_main_thread) { |
421 pending_dowork_count_--; | 440 pending_dowork_count_--; |
422 DCHECK_GE(pending_dowork_count_, 0); | 441 DCHECK_GE(pending_dowork_count_, 0); |
423 } | 442 } |
424 DCHECK(main_thread_checker_.CalledOnValidThread()); | 443 DCHECK(main_thread_checker_.CalledOnValidThread()); |
425 | 444 |
426 base::TimeTicks next_pending_delayed_task( | 445 base::TimeTicks next_pending_delayed_task( |
427 base::TimeTicks::FromInternalValue(kMaxTimeTicks)); | 446 base::TimeTicks::FromInternalValue(kMaxTimeTicks)); |
447 base::PendingTask previous_task((tracked_objects::Location()), | |
448 (base::Closure())); | |
428 | 449 |
429 if (!UpdateWorkQueues(&next_pending_delayed_task, | 450 if (!UpdateWorkQueues(&next_pending_delayed_task, NULL)) |
picksi
2015/03/02 14:30:06
Can you add a comment/const to make it clear what
Sami
2015/03/02 14:48:46
nullptr here too.
rmcilroy
2015/03/02 15:34:51
Done.
rmcilroy
2015/03/02 15:34:51
Done.
| |
430 WorkQueueUpdateEventType::BEFORE_WAKEUP)) | |
431 return; | 451 return; |
432 | 452 |
433 base::PendingTask previous_task((tracked_objects::Location()), | |
434 (base::Closure())); | |
435 for (int i = 0; i < work_batch_size_; i++) { | 453 for (int i = 0; i < work_batch_size_; i++) { |
436 // Interrupt the work batch if we should run the next delayed task. | 454 // Interrupt the work batch if we should run the next delayed task. |
437 if (i > 0 && next_pending_delayed_task.ToInternalValue() != kMaxTimeTicks && | 455 if (i > 0 && next_pending_delayed_task.ToInternalValue() != kMaxTimeTicks && |
438 Now() >= next_pending_delayed_task) | 456 Now() >= next_pending_delayed_task) |
439 return; | 457 return; |
440 | 458 |
441 size_t queue_index; | 459 size_t queue_index; |
442 if (!SelectWorkQueueToService(&queue_index)) | 460 if (!SelectWorkQueueToService(&queue_index)) |
443 return; | 461 return; |
444 // Note that this function won't post another call to DoWork if one is | 462 // Note that this function won't post another call to DoWork if one is |
445 // already pending, so it is safe to call it in a loop. | 463 // already pending, so it is safe to call it in a loop. |
446 MaybePostDoWorkOnMainRunner(); | 464 MaybePostDoWorkOnMainRunner(); |
447 ProcessTaskFromWorkQueue(queue_index, i > 0, &previous_task); | 465 ProcessTaskFromWorkQueue(queue_index, i > 0, &previous_task); |
448 | 466 |
449 if (!UpdateWorkQueues(&next_pending_delayed_task, | 467 if (!UpdateWorkQueues(&next_pending_delayed_task, &previous_task)) |
450 WorkQueueUpdateEventType::AFTER_WAKEUP)) | |
451 return; | 468 return; |
452 } | 469 } |
453 } | 470 } |
454 | 471 |
455 bool TaskQueueManager::SelectWorkQueueToService(size_t* out_queue_index) { | 472 bool TaskQueueManager::SelectWorkQueueToService(size_t* out_queue_index) { |
456 bool should_run = selector_->SelectWorkQueueToService(out_queue_index); | 473 bool should_run = selector_->SelectWorkQueueToService(out_queue_index); |
457 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( | 474 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( |
458 TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"), "TaskQueueManager", this, | 475 TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"), "TaskQueueManager", this, |
459 AsValueWithSelectorResult(should_run, *out_queue_index)); | 476 AsValueWithSelectorResult(should_run, *out_queue_index)); |
460 return should_run; | 477 return should_run; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
554 state->EndArray(); | 571 state->EndArray(); |
555 state->BeginDictionary("selector"); | 572 state->BeginDictionary("selector"); |
556 selector_->AsValueInto(state.get()); | 573 selector_->AsValueInto(state.get()); |
557 state->EndDictionary(); | 574 state->EndDictionary(); |
558 if (should_run) | 575 if (should_run) |
559 state->SetInteger("selected_queue", selected_queue); | 576 state->SetInteger("selected_queue", selected_queue); |
560 return state; | 577 return state; |
561 } | 578 } |
562 | 579 |
563 } // namespace content | 580 } // namespace content |
OLD | NEW |