Chromium Code Reviews| 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 namespace { | |
| 13 int kBitsInByte = 8; | |
| 14 } | |
|
picksi1
2014/10/20 11:42:04
Does this exist elsewhere as a constant already?
rmcilroy
2014/10/21 17:21:27
Changed to CHAR_BITS as suggested by Petr.
| |
| 15 | |
| 16 RendererSchedulerSelector::RendererSchedulerSelector() { } | |
| 17 | |
| 18 RendererSchedulerSelector::~RendererSchedulerSelector() { } | |
| 19 | |
| 20 void RendererSchedulerSelector::RegisterWorkQueues( | |
| 21 const std::vector<const base::TaskQueue*>& work_queues) { | |
| 22 main_thread_checker_.CalledOnValidThread(); | |
| 23 DCHECK(work_queues.size() < queue_priorities_[kNormalPriority] * kBitsInByte); | |
| 24 work_queues_ = work_queues; | |
| 25 // By default, all work queues are set to normal priority. | |
| 26 queue_priorities_[kControlPriority] = 0; | |
| 27 queue_priorities_[kHighPriority] = 0; | |
| 28 queue_priorities_[kNormalPriority] = (1 << work_queues.size()) - 1; | |
|
picksi1
2014/10/20 11:42:04
Is it worth doing setting the initial bits here to
rmcilroy
2014/10/21 17:21:27
I could do this but I'm not sure it's worth it. H
| |
| 29 queue_priorities_[kBestEffortPriority] = 0; | |
| 30 } | |
| 31 | |
| 32 RendererSchedulerSelector::QueuePriority RendererSchedulerSelector::NextPriorty( | |
| 33 QueuePriority priority) { | |
| 34 DCHECK(priority < kQueuePriorityCount); | |
| 35 return static_cast<QueuePriority>(static_cast<int>(priority) + 1); | |
| 36 } | |
| 37 | |
| 38 void RendererSchedulerSelector::SetQueuePriority( | |
| 39 size_t queue_id, QueuePriority set_priority) { | |
| 40 main_thread_checker_.CalledOnValidThread(); | |
| 41 DCHECK(queue_id < work_queues_.size()); | |
| 42 DCHECK(set_priority < kQueuePriorityCount); | |
| 43 int64_t queue_id_mask_ = 1 << queue_id; | |
| 44 for (QueuePriority priority = kControlPriority; | |
| 45 priority < kQueuePriorityCount; | |
| 46 priority = NextPriorty(priority)) { | |
| 47 if (priority == set_priority) { | |
| 48 queue_priorities_[priority] |= queue_id_mask_; | |
| 49 } else { | |
| 50 queue_priorities_[priority] &= ~queue_id_mask_; | |
| 51 } | |
| 52 } | |
|
picksi1
2014/10/20 11:42:04
Does each queue only exist in a single 'priority'
rmcilroy
2014/10/21 17:21:27
Good point, I went with the second option. Done.
| |
| 53 } | |
| 54 | |
| 55 void RendererSchedulerSelector::EnableQueue( | |
| 56 size_t queue_id, QueuePriority priority) { | |
| 57 SetQueuePriority(queue_id, priority); | |
| 58 } | |
| 59 | |
| 60 void RendererSchedulerSelector::DisableQueue(size_t queue_id) { | |
| 61 main_thread_checker_.CalledOnValidThread(); | |
| 62 DCHECK(queue_id < work_queues_.size()); | |
| 63 int64_t queue_id_mask_ = 1 << queue_id; | |
| 64 for (QueuePriority priority = kControlPriority; | |
| 65 priority < kQueuePriorityCount; | |
| 66 priority = NextPriorty(priority)) { | |
| 67 queue_priorities_[priority] &= ~queue_id_mask_; | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 bool RendererSchedulerSelector::QueueEnabledWithPriority( | |
| 72 size_t queue_id, QueuePriority priority) { | |
| 73 DCHECK(queue_id < work_queues_.size()); | |
| 74 DCHECK(priority < kQueuePriorityCount); | |
| 75 return queue_priorities_[priority] & (1 << queue_id); | |
| 76 } | |
|
picksi1
2014/10/20 11:42:04
There's a lot of (1<<queue_id)'s in the code. Shou
rmcilroy
2014/10/21 17:21:27
Done.
| |
| 77 | |
| 78 bool RendererSchedulerSelector::ChooseOldestWithPriority( | |
| 79 QueuePriority priority, size_t* out_queue_index) { | |
| 80 bool found_non_empty_queue = false; | |
| 81 size_t chosen_queue = 0; | |
| 82 for (size_t queue_id = 0; queue_id < work_queues_.size(); queue_id++) { | |
| 83 if (!QueueEnabledWithPriority(queue_id, priority) || | |
| 84 work_queues_[queue_id]->empty()) { | |
| 85 continue; | |
| 86 } | |
|
picksi1
2014/10/20 11:42:04
For clarity it might be worth breaking this 'if' s
rmcilroy
2014/10/21 17:21:27
I changed the order, but I'm not sure I agree that
| |
| 87 if (!found_non_empty_queue || | |
| 88 work_queues_[queue_id]->front() < work_queues_[chosen_queue]->front()) { | |
| 89 found_non_empty_queue = true; | |
| 90 chosen_queue = queue_id; | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 if (found_non_empty_queue) { | |
| 95 *out_queue_index = chosen_queue; | |
| 96 } | |
| 97 return found_non_empty_queue; | |
| 98 } | |
| 99 | |
| 100 bool RendererSchedulerSelector::SelectWorkQueueToService( | |
| 101 size_t* out_queue_index) { | |
| 102 main_thread_checker_.CalledOnValidThread(); | |
| 103 DCHECK(work_queues_.size()); | |
| 104 for (QueuePriority priority = kControlPriority; | |
| 105 priority < kQueuePriorityCount; | |
| 106 priority = NextPriorty(priority)) { | |
| 107 if (ChooseOldestWithPriority(priority, out_queue_index)) { | |
| 108 return true; | |
| 109 } | |
| 110 } | |
| 111 return false; | |
| 112 } | |
| 113 | |
| 114 | |
| 115 } // namespace content | |
| OLD | NEW |