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 21 matching lines...) Expand all Loading... |
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 Handle handle = queue_.FirstMax(); |
82 if (handle.is_null()) { | 84 if (handle.is_null()) { |
83 DCHECK_EQ(0u, queue_.size()); | 85 DCHECK_EQ(0u, queue_.size()); |
84 return; | 86 return; |
85 } | 87 } |
86 MaybeDispatchJob(handle, handle.priority()); | 88 MaybeDispatchJob(handle, handle.priority()); |
87 } | 89 } |
88 | 90 |
| 91 void PrioritizedDispatcher::Disable() { |
| 92 // Set limits to allow no new jobs to start. |
| 93 SetLimits(Limits(queue_.num_priorities(), 0)); |
| 94 } |
| 95 |
89 bool PrioritizedDispatcher::MaybeDispatchJob(const Handle& handle, | 96 bool PrioritizedDispatcher::MaybeDispatchJob(const Handle& handle, |
90 Priority job_priority) { | 97 Priority job_priority) { |
91 DCHECK_LT(job_priority, num_priorities()); | 98 DCHECK_LT(job_priority, num_priorities()); |
92 if (num_running_jobs_ >= max_running_jobs_[job_priority]) | 99 if (num_running_jobs_ >= max_running_jobs_[job_priority]) |
93 return false; | 100 return false; |
94 Job* job = handle.value(); | 101 Job* job = handle.value(); |
95 queue_.Erase(handle); | 102 queue_.Erase(handle); |
96 ++num_running_jobs_; | 103 ++num_running_jobs_; |
97 job->Start(); | 104 job->Start(); |
98 return true; | 105 return true; |
99 } | 106 } |
100 | 107 |
| 108 void PrioritizedDispatcher::SetLimits(const Limits& limits) { |
| 109 DCHECK_EQ(queue_.num_priorities(), limits.reserved_slots.size()); |
| 110 size_t total = 0; |
| 111 for (size_t i = 0; i < limits.reserved_slots.size(); ++i) { |
| 112 total += limits.reserved_slots[i]; |
| 113 max_running_jobs_[i] = total; |
| 114 } |
| 115 // Unreserved slots are available for all priorities. |
| 116 DCHECK_LE(total, limits.total_jobs) << "sum(reserved_slots) <= total_jobs"; |
| 117 size_t spare = limits.total_jobs - total; |
| 118 for (size_t i = limits.reserved_slots.size(); i > 0; --i) { |
| 119 max_running_jobs_[i - 1] += spare; |
| 120 } |
| 121 } |
| 122 |
101 } // namespace net | 123 } // namespace net |
OLD | NEW |