| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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 "net/base/prioritized_dispatcher.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace net { |
| 10 |
| 11 PrioritizedDispatcher::Limits::Limits(Priority num_priorities, |
| 12 size_t total_jobs) |
| 13 : total_jobs(total_jobs), reserved_slots(num_priorities) {} |
| 14 |
| 15 PrioritizedDispatcher::Limits::~Limits() {} |
| 16 |
| 17 PrioritizedDispatcher::PrioritizedDispatcher(const Limits& limits) |
| 18 : queue_(limits.reserved_slots.size()), |
| 19 max_running_jobs_(limits.reserved_slots.size()), |
| 20 num_running_jobs_(0) { |
| 21 size_t total = 0; |
| 22 for (size_t i = limits.reserved_slots.size(); i > 0; --i) { |
| 23 total += limits.reserved_slots[i - 1]; |
| 24 max_running_jobs_[i - 1] = 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 = 0; i < max_running_jobs_.size(); ++i) { |
| 30 max_running_jobs_[i] += spare; |
| 31 } |
| 32 } |
| 33 |
| 34 PrioritizedDispatcher::~PrioritizedDispatcher() {} |
| 35 |
| 36 PrioritizedDispatcher::Handle PrioritizedDispatcher::Add( |
| 37 Job* job, Priority priority) { |
| 38 DCHECK(job); |
| 39 DCHECK_LT(priority, num_priorities()); |
| 40 if (num_running_jobs_ < max_running_jobs_[priority]) { |
| 41 ++num_running_jobs_; |
| 42 job->Start(); |
| 43 return Handle(); |
| 44 } |
| 45 return queue_.Insert(job, priority); |
| 46 } |
| 47 |
| 48 void PrioritizedDispatcher::Cancel(const Handle& handle) { |
| 49 queue_.Erase(handle); |
| 50 } |
| 51 |
| 52 PrioritizedDispatcher::Job* PrioritizedDispatcher::EvictOldestLowest() { |
| 53 Handle handle = queue_.FirstMax(); |
| 54 if (handle.is_null()) |
| 55 return NULL; |
| 56 Job* job = handle.value(); |
| 57 Cancel(handle); |
| 58 return job; |
| 59 } |
| 60 |
| 61 PrioritizedDispatcher::Handle PrioritizedDispatcher::ChangePriority( |
| 62 const Handle& handle, Priority priority) { |
| 63 DCHECK(!handle.is_null()); |
| 64 DCHECK_LT(priority, num_priorities()); |
| 65 DCHECK_GE(num_running_jobs_, max_running_jobs_[handle.priority()]) << |
| 66 "Job should not be in queue when limits permit it to start."; |
| 67 DCHECK_LE(queue_.size(), max_queued_jobs_); |
| 68 |
| 69 if (handle.priority() == priority) |
| 70 return handle; |
| 71 |
| 72 if (DispatchJob(handle, priority)) |
| 73 return Handle(); |
| 74 Job* job = handle.value(); |
| 75 queue_.Erase(handle); |
| 76 return queue_.Insert(job, priority); |
| 77 } |
| 78 |
| 79 void PrioritizedDispatcher::OnJobFinished() { |
| 80 DCHECK_GT(num_running_jobs_, 0u); |
| 81 --num_running_jobs_; |
| 82 Handle handle = queue_.FirstMin(); |
| 83 if (handle.is_null()) { |
| 84 DCHECK_EQ(0u, queue_.size()); |
| 85 return; |
| 86 } |
| 87 DispatchJob(handle, handle.priority()); |
| 88 } |
| 89 |
| 90 bool PrioritizedDispatcher::DispatchJob(const Handle& handle, |
| 91 Priority job_priority) { |
| 92 DCHECK_LT(job_priority, num_priorities()); |
| 93 if (num_running_jobs_ >= max_running_jobs_[job_priority]) |
| 94 return false; |
| 95 Job* job = handle.value(); |
| 96 queue_.Erase(handle); |
| 97 ++num_running_jobs_; |
| 98 job->Start(); |
| 99 return true; |
| 100 } |
| 101 |
| 102 } // namespace net |
| 103 |
| 104 |
| OLD | NEW |