| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/base/prioritized_dispatcher.h" | 5 #include "net/base/prioritized_dispatcher.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace net { | 9 namespace net { |
| 10 | 10 |
| 11 PrioritizedDispatcher::Limits::Limits(Priority num_priorities, | 11 PrioritizedDispatcher::Limits::Limits(Priority num_priorities, |
| 12 size_t total_jobs) | 12 size_t total_jobs) |
| 13 : total_jobs(total_jobs), reserved_slots(num_priorities) {} | 13 : total_jobs(total_jobs), reserved_slots(num_priorities) {} |
| 14 | 14 |
| 15 PrioritizedDispatcher::Limits::~Limits() {} | 15 PrioritizedDispatcher::Limits::~Limits() {} |
| 16 | 16 |
| 17 PrioritizedDispatcher::PrioritizedDispatcher(const Limits& limits) | 17 PrioritizedDispatcher::PrioritizedDispatcher(const Limits& limits) |
| 18 : queue_(limits.reserved_slots.size()), | 18 : queue_(limits.reserved_slots.size()), |
| 19 max_running_jobs_(limits.reserved_slots.size()), | 19 max_running_jobs_(limits.reserved_slots.size()), |
| 20 num_running_jobs_(0) { | 20 num_running_jobs_(0) { |
| 21 size_t total = 0; | 21 SetLimits(limits); |
| 22 for (size_t i = 0; i < limits.reserved_slots.size(); ++i) { | |
| 23 total += limits.reserved_slots[i]; | |
| 24 max_running_jobs_[i] = total; | |
| 25 } | |
| 26 // Unreserved slots are available for all priorities. | |
| 27 DCHECK_LE(total, limits.total_jobs) << "sum(reserved_slots) <= total_jobs"; | |
| 28 size_t spare = limits.total_jobs - total; | |
| 29 for (size_t i = limits.reserved_slots.size(); i > 0; --i) { | |
| 30 max_running_jobs_[i - 1] += spare; | |
| 31 } | |
| 32 } | 22 } |
| 33 | 23 |
| 34 PrioritizedDispatcher::~PrioritizedDispatcher() {} | 24 PrioritizedDispatcher::~PrioritizedDispatcher() {} |
| 35 | 25 |
| 36 PrioritizedDispatcher::Handle PrioritizedDispatcher::Add( | 26 PrioritizedDispatcher::Handle PrioritizedDispatcher::Add( |
| 37 Job* job, Priority priority) { | 27 Job* job, Priority priority) { |
| 38 DCHECK(job); | 28 DCHECK(job); |
| 39 DCHECK_LT(priority, num_priorities()); | 29 DCHECK_LT(priority, num_priorities()); |
| 40 if (num_running_jobs_ < max_running_jobs_[priority]) { | 30 if (num_running_jobs_ < max_running_jobs_[priority]) { |
| 41 ++num_running_jobs_; | 31 ++num_running_jobs_; |
| 42 job->Start(); | 32 job->Start(); |
| 43 return Handle(); | 33 return Handle(); |
| 44 } | 34 } |
| 45 return queue_.Insert(job, priority); | 35 return queue_.Insert(job, priority); |
| 46 } | 36 } |
| 47 | 37 |
| 38 PrioritizedDispatcher::Handle PrioritizedDispatcher::AddAtHead( |
| 39 Job* job, Priority priority) { |
| 40 DCHECK(job); |
| 41 DCHECK_LT(priority, num_priorities()); |
| 42 if (num_running_jobs_ < max_running_jobs_[priority]) { |
| 43 ++num_running_jobs_; |
| 44 job->Start(); |
| 45 return Handle(); |
| 46 } |
| 47 return queue_.InsertAtFront(job, priority); |
| 48 } |
| 49 |
| 48 void PrioritizedDispatcher::Cancel(const Handle& handle) { | 50 void PrioritizedDispatcher::Cancel(const Handle& handle) { |
| 49 queue_.Erase(handle); | 51 queue_.Erase(handle); |
| 50 } | 52 } |
| 51 | 53 |
| 52 PrioritizedDispatcher::Job* PrioritizedDispatcher::EvictOldestLowest() { | 54 PrioritizedDispatcher::Job* PrioritizedDispatcher::EvictOldestLowest() { |
| 53 Handle handle = queue_.FirstMin(); | 55 Handle handle = queue_.FirstMin(); |
| 54 if (handle.is_null()) | 56 if (handle.is_null()) |
| 55 return NULL; | 57 return NULL; |
| 56 Job* job = handle.value(); | 58 Job* job = handle.value(); |
| 57 Cancel(handle); | 59 Cancel(handle); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 71 if (MaybeDispatchJob(handle, priority)) | 73 if (MaybeDispatchJob(handle, priority)) |
| 72 return Handle(); | 74 return Handle(); |
| 73 Job* job = handle.value(); | 75 Job* job = handle.value(); |
| 74 queue_.Erase(handle); | 76 queue_.Erase(handle); |
| 75 return queue_.Insert(job, priority); | 77 return queue_.Insert(job, priority); |
| 76 } | 78 } |
| 77 | 79 |
| 78 void PrioritizedDispatcher::OnJobFinished() { | 80 void PrioritizedDispatcher::OnJobFinished() { |
| 79 DCHECK_GT(num_running_jobs_, 0u); | 81 DCHECK_GT(num_running_jobs_, 0u); |
| 80 --num_running_jobs_; | 82 --num_running_jobs_; |
| 81 Handle handle = queue_.FirstMax(); | 83 MaybeDispatchNextJob(); |
| 82 if (handle.is_null()) { | 84 } |
| 83 DCHECK_EQ(0u, queue_.size()); | 85 |
| 84 return; | 86 PrioritizedDispatcher::Limits PrioritizedDispatcher::GetLimits() const { |
| 87 size_t num_priorities = max_running_jobs_.size(); |
| 88 Limits limits(num_priorities, max_running_jobs_.back()); |
| 89 |
| 90 // Calculate the number of jobs reserved for each priority and higher. Leave |
| 91 // the number of jobs reserved for the lowest priority or higher as 0. |
| 92 for (size_t i = 1; i < num_priorities; ++i) { |
| 93 limits.reserved_slots[i] = max_running_jobs_[i] - max_running_jobs_[i - 1]; |
| 85 } | 94 } |
| 86 MaybeDispatchJob(handle, handle.priority()); | 95 |
| 96 return limits; |
| 97 } |
| 98 |
| 99 void PrioritizedDispatcher::SetLimits(const Limits& limits) { |
| 100 DCHECK_EQ(queue_.num_priorities(), limits.reserved_slots.size()); |
| 101 size_t total = 0; |
| 102 for (size_t i = 0; i < limits.reserved_slots.size(); ++i) { |
| 103 total += limits.reserved_slots[i]; |
| 104 max_running_jobs_[i] = total; |
| 105 } |
| 106 // Unreserved slots are available for all priorities. |
| 107 DCHECK_LE(total, limits.total_jobs) << "sum(reserved_slots) <= total_jobs"; |
| 108 size_t spare = limits.total_jobs - total; |
| 109 for (size_t i = limits.reserved_slots.size(); i > 0; --i) { |
| 110 max_running_jobs_[i - 1] += spare; |
| 111 } |
| 112 |
| 113 // Start pending jobs, if limits permit. |
| 114 while (true) { |
| 115 if (!MaybeDispatchNextJob()) |
| 116 break; |
| 117 } |
| 118 } |
| 119 |
| 120 void PrioritizedDispatcher::SetLimitsToZero() { |
| 121 SetLimits(Limits(queue_.num_priorities(), 0)); |
| 87 } | 122 } |
| 88 | 123 |
| 89 bool PrioritizedDispatcher::MaybeDispatchJob(const Handle& handle, | 124 bool PrioritizedDispatcher::MaybeDispatchJob(const Handle& handle, |
| 90 Priority job_priority) { | 125 Priority job_priority) { |
| 91 DCHECK_LT(job_priority, num_priorities()); | 126 DCHECK_LT(job_priority, num_priorities()); |
| 92 if (num_running_jobs_ >= max_running_jobs_[job_priority]) | 127 if (num_running_jobs_ >= max_running_jobs_[job_priority]) |
| 93 return false; | 128 return false; |
| 94 Job* job = handle.value(); | 129 Job* job = handle.value(); |
| 95 queue_.Erase(handle); | 130 queue_.Erase(handle); |
| 96 ++num_running_jobs_; | 131 ++num_running_jobs_; |
| 97 job->Start(); | 132 job->Start(); |
| 98 return true; | 133 return true; |
| 99 } | 134 } |
| 100 | 135 |
| 136 bool PrioritizedDispatcher::MaybeDispatchNextJob() { |
| 137 Handle handle = queue_.FirstMax(); |
| 138 if (handle.is_null()) { |
| 139 DCHECK_EQ(0u, queue_.size()); |
| 140 return false; |
| 141 } |
| 142 return MaybeDispatchJob(handle, handle.priority()); |
| 143 } |
| 144 |
| 101 } // namespace net | 145 } // namespace net |
| OLD | NEW |