OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/renderer/scheduler/renderer_scheduler_selector.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/pending_task.h" | |
9 | |
10 namespace content { | |
11 | |
12 RendererSchedulerSelector::RendererSchedulerSelector() | |
13 : normal_queue_starvation_count_(0) { | |
14 } | |
15 | |
16 RendererSchedulerSelector::~RendererSchedulerSelector() { | |
17 } | |
18 | |
19 void RendererSchedulerSelector::RegisterWorkQueues( | |
20 const std::vector<const base::TaskQueue*>& work_queues) { | |
21 main_thread_checker_.CalledOnValidThread(); | |
22 work_queues_ = work_queues; | |
23 // By default, all work queues are set to normal priority. | |
24 for (size_t i = 0; i < work_queues.size(); i++) { | |
25 queue_priorities_[kNormalPriority].insert(i); | |
26 } | |
27 queue_priorities_[kControlPriority].clear(); | |
28 queue_priorities_[kHighPriority].clear(); | |
29 queue_priorities_[kBestEffortPriority].clear(); | |
30 } | |
31 | |
32 void RendererSchedulerSelector::SetQueuePriority(size_t queue_index, | |
33 QueuePriority priority) { | |
34 main_thread_checker_.CalledOnValidThread(); | |
35 DCHECK_LT(queue_index, work_queues_.size()); | |
36 DCHECK_LT(priority, kQueuePriorityCount); | |
37 DisableQueue(queue_index); | |
38 queue_priorities_[priority].insert(queue_index); | |
39 } | |
40 | |
41 void RendererSchedulerSelector::EnableQueue(size_t queue_index, | |
42 QueuePriority priority) { | |
43 SetQueuePriority(queue_index, priority); | |
44 } | |
45 | |
46 void RendererSchedulerSelector::DisableQueue(size_t queue_index) { | |
47 main_thread_checker_.CalledOnValidThread(); | |
48 DCHECK_LT(queue_index, work_queues_.size()); | |
49 static const QueuePriority kPriorities[] = { | |
50 kControlPriority, kHighPriority, kNormalPriority, kBestEffortPriority}; | |
51 for (QueuePriority priority : kPriorities) { | |
52 queue_priorities_[priority].erase(queue_index); | |
53 } | |
54 } | |
55 | |
56 bool RendererSchedulerSelector::IsOlder(const base::TaskQueue* queueA, | |
57 const base::TaskQueue* queueB) const { | |
58 return queueB->front() < queueA->front(); | |
59 } | |
60 bool RendererSchedulerSelector::ChooseOldestWithPriority( | |
alexclarke
2014/10/24 15:56:30
nit add a blank line between the two methods
rmcilroy
2014/10/27 16:25:01
Done.
| |
61 QueuePriority priority, | |
62 size_t* out_queue_index) const { | |
63 bool found_non_empty_queue = false; | |
64 size_t chosen_queue = 0; | |
65 for (int queue_index : queue_priorities_[priority]) { | |
66 if (work_queues_[queue_index]->empty()) { | |
67 continue; | |
68 } | |
69 // Note: the comparison between |chosen_queue| and |queue_index| is correct | |
70 // due to the fact that the PendingTask operator inverts comparison for | |
71 // priority queue. | |
72 if (!found_non_empty_queue || | |
73 IsOlder(work_queues_[queue_index], work_queues_[chosen_queue])) { | |
74 found_non_empty_queue = true; | |
75 chosen_queue = queue_index; | |
76 } | |
77 } | |
78 | |
79 if (found_non_empty_queue) { | |
80 *out_queue_index = chosen_queue; | |
81 } | |
82 return found_non_empty_queue; | |
83 } | |
84 | |
85 bool RendererSchedulerSelector::SelectWorkQueueToService( | |
86 size_t* out_queue_index) { | |
87 main_thread_checker_.CalledOnValidThread(); | |
88 DCHECK(work_queues_.size()); | |
89 // Always service the control queue if it has any work. | |
90 if (ChooseOldestWithPriority(kControlPriority, out_queue_index)) { | |
91 return true; | |
92 } | |
93 // Select from the normal priority queue if we are starving it. | |
94 if (normal_queue_starvation_count_ >= kMaxStarvationTasks && | |
95 ChooseOldestWithPriority(kNormalPriority, out_queue_index)) { | |
96 normal_queue_starvation_count_ = 0; | |
97 return true; | |
98 } | |
99 // Otherwise choose in priority order. | |
100 static const QueuePriority kPriorities[] = { | |
101 kHighPriority, kNormalPriority, kBestEffortPriority}; | |
102 for (QueuePriority priority : kPriorities) { | |
103 if (ChooseOldestWithPriority(priority, out_queue_index)) { | |
104 if (priority == kHighPriority) { | |
105 normal_queue_starvation_count_++; | |
106 } else { | |
107 normal_queue_starvation_count_ = 0; | |
108 } | |
109 return true; | |
110 } | |
111 } | |
112 return false; | |
113 } | |
114 | |
115 } // namespace content | |
OLD | NEW |