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 } | |
14 | |
15 RendererSchedulerSelector::~RendererSchedulerSelector() { | |
16 } | |
17 | |
18 void RendererSchedulerSelector::RegisterWorkQueues( | |
19 const std::vector<const base::TaskQueue*>& work_queues) { | |
20 main_thread_checker_.CalledOnValidThread(); | |
21 DCHECK(work_queues.size() < queue_priorities_[kNormalPriority] * CHAR_BIT); | |
Sami
2014/10/22 13:26:06
Still missing the sizeof here? Also, DCHECK_LT.
rmcilroy
2014/10/23 14:54:49
Sorry missed your comment here last time - done.
| |
22 work_queues_ = work_queues; | |
23 // By default, all work queues are set to normal priority. | |
24 queue_priorities_[kControlPriority] = 0; | |
25 queue_priorities_[kHighPriority] = 0; | |
26 queue_priorities_[kNormalPriority] = (1 << work_queues.size()) - 1; | |
27 queue_priorities_[kBestEffortPriority] = 0; | |
28 } | |
29 | |
30 RendererSchedulerSelector::QueuePriority | |
31 RendererSchedulerSelector::NextPriority(QueuePriority priority) { | |
32 DCHECK(priority < kQueuePriorityCount); | |
Sami
2014/10/22 13:26:06
DCHECK_LT
rmcilroy
2014/10/23 14:54:49
Done.
| |
33 return static_cast<QueuePriority>(static_cast<int>(priority) + 1); | |
34 } | |
35 | |
36 size_t RendererSchedulerSelector::PrioritiesMaskFromQueueIndex( | |
37 size_t queue_index) const { | |
38 return 1 << queue_index; | |
39 } | |
40 | |
41 void RendererSchedulerSelector::SetQueuePriority(size_t queue_index, | |
42 QueuePriority priority) { | |
43 main_thread_checker_.CalledOnValidThread(); | |
44 DCHECK(queue_index < work_queues_.size()); | |
Sami
2014/10/22 13:26:06
DCHECK_LT again (2x).
rmcilroy
2014/10/23 14:54:50
Done.
| |
45 DCHECK(priority < kQueuePriorityCount); | |
46 DisableQueue(queue_index); | |
47 queue_priorities_[priority] |= PrioritiesMaskFromQueueIndex(queue_index); | |
48 } | |
49 | |
50 void RendererSchedulerSelector::EnableQueue(size_t queue_index, | |
51 QueuePriority priority) { | |
52 SetQueuePriority(queue_index, priority); | |
53 } | |
54 | |
55 void RendererSchedulerSelector::DisableQueue(size_t queue_index) { | |
56 main_thread_checker_.CalledOnValidThread(); | |
57 DCHECK(queue_index < work_queues_.size()); | |
Sami
2014/10/22 13:26:06
DCHECK_LT
rmcilroy
2014/10/23 14:54:49
Done.
| |
58 for (QueuePriority priority = kControlPriority; | |
59 priority < kQueuePriorityCount; | |
60 priority = NextPriority(priority)) { | |
61 queue_priorities_[priority] &= ~(PrioritiesMaskFromQueueIndex(queue_index)); | |
Sami
2014/10/22 13:26:06
nit: No parens needed around the function call.
rmcilroy
2014/10/23 14:54:49
Done.
| |
62 } | |
63 } | |
64 | |
65 bool RendererSchedulerSelector::QueueEnabledWithPriority( | |
66 size_t queue_index, | |
67 QueuePriority priority) const { | |
68 DCHECK(queue_index < work_queues_.size()); | |
69 DCHECK(priority < kQueuePriorityCount); | |
70 return queue_priorities_[priority] & | |
71 PrioritiesMaskFromQueueIndex(queue_index); | |
72 } | |
73 | |
74 bool RendererSchedulerSelector::ChooseOldestWithPriority( | |
75 QueuePriority priority, | |
76 size_t* out_queue_index) const { | |
77 bool found_non_empty_queue = false; | |
78 size_t chosen_queue = 0; | |
79 for (size_t i = 0; i < work_queues_.size(); i++) { | |
80 if (work_queues_[i]->empty() || | |
81 !QueueEnabledWithPriority(i, priority)) { | |
82 continue; | |
83 } | |
84 if (!found_non_empty_queue || | |
85 work_queues_[i]->front() < work_queues_[chosen_queue]->front()) { | |
86 found_non_empty_queue = true; | |
87 chosen_queue = i; | |
88 } | |
89 } | |
90 | |
91 if (found_non_empty_queue) { | |
92 *out_queue_index = chosen_queue; | |
93 } | |
94 return found_non_empty_queue; | |
95 } | |
96 | |
97 bool RendererSchedulerSelector::SelectWorkQueueToService( | |
98 size_t* out_queue_index) { | |
99 main_thread_checker_.CalledOnValidThread(); | |
100 DCHECK(work_queues_.size()); | |
101 // Always service the control queue if it has any work. | |
102 if (ChooseOldestWithPriority(kControlPriority, out_queue_index)) { | |
103 return true; | |
104 } | |
105 // Select from the normal priority queue if we are starving it. | |
106 if (normal_queue_starvation_count_ >= kMaxStarvationTasks && | |
107 ChooseOldestWithPriority(kNormalPriority, out_queue_index)) { | |
108 normal_queue_starvation_count_ = 0; | |
109 return true; | |
110 } | |
111 // Otherwise choose in priority order. | |
112 for (QueuePriority priority = kHighPriority; | |
113 priority < kQueuePriorityCount; | |
114 priority = NextPriority(priority)) { | |
115 if (ChooseOldestWithPriority(priority, out_queue_index)) { | |
116 if (priority == kHighPriority) { | |
117 normal_queue_starvation_count_++; | |
118 } else { | |
119 normal_queue_starvation_count_ = 0; | |
120 } | |
121 return true; | |
122 } | |
123 } | |
124 return false; | |
125 } | |
126 | |
127 } // namespace content | |
OLD | NEW |