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 24 matching lines...) Expand all Loading... | |
35 const base::Closure& task, | 35 const base::Closure& task, |
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 void GetNextPendingDelayedTask(base::TimeTicks* next_pending_delayed_task); | |
46 | |
45 bool UpdateWorkQueue(base::TimeTicks* next_pending_delayed_task, | 47 bool UpdateWorkQueue(base::TimeTicks* next_pending_delayed_task, |
46 const base::PendingTask* previous_task); | 48 const base::PendingTask* previous_task); |
47 base::PendingTask TakeTaskFromWorkQueue(); | 49 base::PendingTask TakeTaskFromWorkQueue(); |
48 | 50 |
49 void WillDeleteTaskQueueManager(); | 51 void WillDeleteTaskQueueManager(); |
50 | 52 |
51 base::TaskQueue& work_queue() { return work_queue_; } | 53 base::TaskQueue& work_queue() { return work_queue_; } |
52 | 54 |
53 void set_name(const char* name) { name_ = name; } | 55 void set_name(const char* name) { name_ = name; } |
54 | 56 |
(...skipping 14 matching lines...) Expand all Loading... | |
69 | 71 |
70 // Adds a task at the end of the incoming task queue and schedules a call to | 72 // 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 | 73 // TaskQueueManager::DoWork() if the incoming queue was empty and automatic |
72 // pumping is enabled. Can be called on an arbitrary thread. | 74 // pumping is enabled. Can be called on an arbitrary thread. |
73 void EnqueueTask(const base::PendingTask& pending_task); | 75 void EnqueueTask(const base::PendingTask& pending_task); |
74 | 76 |
75 void PumpQueueLocked(); | 77 void PumpQueueLocked(); |
76 bool TaskIsOlderThanQueuedTasks(const base::PendingTask* task); | 78 bool TaskIsOlderThanQueuedTasks(const base::PendingTask* task); |
77 bool ShouldAutoPumpQueueLocked(const base::PendingTask* previous_task); | 79 bool ShouldAutoPumpQueueLocked(const base::PendingTask* previous_task); |
78 void EnqueueTaskLocked(const base::PendingTask& pending_task); | 80 void EnqueueTaskLocked(const base::PendingTask& pending_task); |
81 void GetNextPendingDelayedTaskLocked( | |
82 base::TimeTicks* next_pending_delayed_task); | |
79 | 83 |
80 void TraceQueueSize(bool is_locked) const; | 84 void TraceQueueSize(bool is_locked) const; |
81 static const char* PumpPolicyToString( | 85 static const char* PumpPolicyToString( |
82 TaskQueueManager::PumpPolicy pump_policy); | 86 TaskQueueManager::PumpPolicy pump_policy); |
83 static void QueueAsValueInto(const base::TaskQueue& queue, | 87 static void QueueAsValueInto(const base::TaskQueue& queue, |
84 base::trace_event::TracedValue* state); | 88 base::trace_event::TracedValue* state); |
85 static void TaskAsValueInto(const base::PendingTask& task, | 89 static void TaskAsValueInto(const base::PendingTask& task, |
86 base::trace_event::TracedValue* state); | 90 base::trace_event::TracedValue* state); |
87 | 91 |
88 // This lock protects all members except the work queue. | 92 // This lock protects all members except the work queue. |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 if (pump_policy_ == TaskQueueManager::PumpPolicy::MANUAL) | 183 if (pump_policy_ == TaskQueueManager::PumpPolicy::MANUAL) |
180 return false; | 184 return false; |
181 if (pump_policy_ == TaskQueueManager::PumpPolicy::AFTER_WAKEUP && | 185 if (pump_policy_ == TaskQueueManager::PumpPolicy::AFTER_WAKEUP && |
182 TaskIsOlderThanQueuedTasks(previous_task)) | 186 TaskIsOlderThanQueuedTasks(previous_task)) |
183 return false; | 187 return false; |
184 if (incoming_queue_.empty()) | 188 if (incoming_queue_.empty()) |
185 return false; | 189 return false; |
186 return true; | 190 return true; |
187 } | 191 } |
188 | 192 |
193 void TaskQueue::GetNextPendingDelayedTask( | |
194 base::TimeTicks* next_pending_delayed_task) { | |
195 base::AutoLock lock(lock_); | |
196 GetNextPendingDelayedTaskLocked(next_pending_delayed_task); | |
197 } | |
198 | |
199 void TaskQueue::GetNextPendingDelayedTaskLocked( | |
picksi
2015/03/04 10:43:23
Can this deal better with the delayed_task_run_tim
rmcilroy
2015/03/04 14:23:12
Done.
| |
200 base::TimeTicks* next_pending_delayed_task) { | |
201 lock_.AssertAcquired(); | |
202 if (!delayed_task_run_times_.empty()) { | |
203 *next_pending_delayed_task = | |
204 std::min(*next_pending_delayed_task, delayed_task_run_times_.top()); | |
205 } | |
206 } | |
207 | |
189 bool TaskQueue::UpdateWorkQueue( | 208 bool TaskQueue::UpdateWorkQueue( |
190 base::TimeTicks* next_pending_delayed_task, | 209 base::TimeTicks* next_pending_delayed_task, |
191 const base::PendingTask* previous_task) { | 210 const base::PendingTask* previous_task) { |
192 if (!work_queue_.empty()) | 211 if (!work_queue_.empty()) |
193 return true; | 212 return true; |
194 | 213 |
195 { | 214 { |
196 base::AutoLock lock(lock_); | 215 base::AutoLock lock(lock_); |
197 if (!delayed_task_run_times_.empty()) { | 216 GetNextPendingDelayedTaskLocked(next_pending_delayed_task); |
198 *next_pending_delayed_task = | |
199 std::min(*next_pending_delayed_task, delayed_task_run_times_.top()); | |
200 } | |
201 if (!ShouldAutoPumpQueueLocked(previous_task)) | 217 if (!ShouldAutoPumpQueueLocked(previous_task)) |
202 return false; | 218 return false; |
203 work_queue_.Swap(&incoming_queue_); | 219 work_queue_.Swap(&incoming_queue_); |
204 TraceQueueSize(true); | 220 TraceQueueSize(true); |
205 return true; | 221 return true; |
206 } | 222 } |
207 } | 223 } |
208 | 224 |
209 base::PendingTask TaskQueue::TakeTaskFromWorkQueue() { | 225 base::PendingTask TaskQueue::TakeTaskFromWorkQueue() { |
210 base::PendingTask pending_task = work_queue_.front(); | 226 base::PendingTask pending_task = work_queue_.front(); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
381 scoped_refptr<base::SingleThreadTaskRunner> | 397 scoped_refptr<base::SingleThreadTaskRunner> |
382 TaskQueueManager::TaskRunnerForQueue(size_t queue_index) const { | 398 TaskQueueManager::TaskRunnerForQueue(size_t queue_index) const { |
383 return Queue(queue_index); | 399 return Queue(queue_index); |
384 } | 400 } |
385 | 401 |
386 bool TaskQueueManager::IsQueueEmpty(size_t queue_index) const { | 402 bool TaskQueueManager::IsQueueEmpty(size_t queue_index) const { |
387 internal::TaskQueue* queue = Queue(queue_index); | 403 internal::TaskQueue* queue = Queue(queue_index); |
388 return queue->IsQueueEmpty(); | 404 return queue->IsQueueEmpty(); |
389 } | 405 } |
390 | 406 |
407 base::TimeTicks TaskQueueManager::NextPendingDelayedTask() { | |
408 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
409 base::TimeTicks next_pending_delayed_task( | |
410 base::TimeTicks::FromInternalValue(kMaxTimeTicks)); | |
411 for (auto& queue : queues_) { | |
412 queue->GetNextPendingDelayedTask(&next_pending_delayed_task); | |
413 } | |
414 | |
415 if (next_pending_delayed_task == | |
picksi
2015/03/04 10:43:22
If I understand the code, this check for unchanged
rmcilroy
2015/03/04 14:23:12
Done (although we don't really need to worry about
picksi
2015/03/04 16:29:05
I suppose the heat death of the universe will be a
| |
416 base::TimeTicks::FromInternalValue(kMaxTimeTicks)) | |
417 return base::TimeTicks(); | |
418 | |
419 return next_pending_delayed_task; | |
420 } | |
421 | |
391 void TaskQueueManager::SetPumpPolicy(size_t queue_index, | 422 void TaskQueueManager::SetPumpPolicy(size_t queue_index, |
392 PumpPolicy pump_policy) { | 423 PumpPolicy pump_policy) { |
393 DCHECK(main_thread_checker_.CalledOnValidThread()); | 424 DCHECK(main_thread_checker_.CalledOnValidThread()); |
394 internal::TaskQueue* queue = Queue(queue_index); | 425 internal::TaskQueue* queue = Queue(queue_index); |
395 queue->SetPumpPolicy(pump_policy); | 426 queue->SetPumpPolicy(pump_policy); |
396 } | 427 } |
397 | 428 |
398 void TaskQueueManager::PumpQueue(size_t queue_index) { | 429 void TaskQueueManager::PumpQueue(size_t queue_index) { |
399 DCHECK(main_thread_checker_.CalledOnValidThread()); | 430 DCHECK(main_thread_checker_.CalledOnValidThread()); |
400 internal::TaskQueue* queue = Queue(queue_index); | 431 internal::TaskQueue* queue = Queue(queue_index); |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
575 state->EndArray(); | 606 state->EndArray(); |
576 state->BeginDictionary("selector"); | 607 state->BeginDictionary("selector"); |
577 selector_->AsValueInto(state.get()); | 608 selector_->AsValueInto(state.get()); |
578 state->EndDictionary(); | 609 state->EndDictionary(); |
579 if (should_run) | 610 if (should_run) |
580 state->SetInteger("selected_queue", selected_queue); | 611 state->SetInteger("selected_queue", selected_queue); |
581 return state; | 612 return state; |
582 } | 613 } |
583 | 614 |
584 } // namespace content | 615 } // namespace content |
OLD | NEW |