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 SetLimits(limits); | 21 size_t total = 0; |
| 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 } |
22 } | 32 } |
23 | 33 |
24 PrioritizedDispatcher::~PrioritizedDispatcher() {} | 34 PrioritizedDispatcher::~PrioritizedDispatcher() {} |
25 | 35 |
26 PrioritizedDispatcher::Handle PrioritizedDispatcher::Add( | 36 PrioritizedDispatcher::Handle PrioritizedDispatcher::Add( |
27 Job* job, Priority priority) { | 37 Job* job, Priority priority) { |
28 DCHECK(job); | 38 DCHECK(job); |
29 DCHECK_LT(priority, num_priorities()); | 39 DCHECK_LT(priority, num_priorities()); |
30 if (num_running_jobs_ < max_running_jobs_[priority]) { | 40 if (num_running_jobs_ < max_running_jobs_[priority]) { |
31 ++num_running_jobs_; | 41 ++num_running_jobs_; |
32 job->Start(); | 42 job->Start(); |
33 return Handle(); | 43 return Handle(); |
34 } | 44 } |
35 return queue_.Insert(job, priority); | 45 return queue_.Insert(job, priority); |
36 } | 46 } |
37 | 47 |
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 | |
50 void PrioritizedDispatcher::Cancel(const Handle& handle) { | 48 void PrioritizedDispatcher::Cancel(const Handle& handle) { |
51 queue_.Erase(handle); | 49 queue_.Erase(handle); |
52 } | 50 } |
53 | 51 |
54 PrioritizedDispatcher::Job* PrioritizedDispatcher::EvictOldestLowest() { | 52 PrioritizedDispatcher::Job* PrioritizedDispatcher::EvictOldestLowest() { |
55 Handle handle = queue_.FirstMin(); | 53 Handle handle = queue_.FirstMin(); |
56 if (handle.is_null()) | 54 if (handle.is_null()) |
57 return NULL; | 55 return NULL; |
58 Job* job = handle.value(); | 56 Job* job = handle.value(); |
59 Cancel(handle); | 57 Cancel(handle); |
(...skipping 21 matching lines...) Expand all Loading... |
81 DCHECK_GT(num_running_jobs_, 0u); | 79 DCHECK_GT(num_running_jobs_, 0u); |
82 --num_running_jobs_; | 80 --num_running_jobs_; |
83 Handle handle = queue_.FirstMax(); | 81 Handle handle = queue_.FirstMax(); |
84 if (handle.is_null()) { | 82 if (handle.is_null()) { |
85 DCHECK_EQ(0u, queue_.size()); | 83 DCHECK_EQ(0u, queue_.size()); |
86 return; | 84 return; |
87 } | 85 } |
88 MaybeDispatchJob(handle, handle.priority()); | 86 MaybeDispatchJob(handle, handle.priority()); |
89 } | 87 } |
90 | 88 |
91 void PrioritizedDispatcher::Disable() { | |
92 // Set limits to allow no new jobs to start. | |
93 SetLimits(Limits(queue_.num_priorities(), 0)); | |
94 } | |
95 | |
96 bool PrioritizedDispatcher::MaybeDispatchJob(const Handle& handle, | 89 bool PrioritizedDispatcher::MaybeDispatchJob(const Handle& handle, |
97 Priority job_priority) { | 90 Priority job_priority) { |
98 DCHECK_LT(job_priority, num_priorities()); | 91 DCHECK_LT(job_priority, num_priorities()); |
99 if (num_running_jobs_ >= max_running_jobs_[job_priority]) | 92 if (num_running_jobs_ >= max_running_jobs_[job_priority]) |
100 return false; | 93 return false; |
101 Job* job = handle.value(); | 94 Job* job = handle.value(); |
102 queue_.Erase(handle); | 95 queue_.Erase(handle); |
103 ++num_running_jobs_; | 96 ++num_running_jobs_; |
104 job->Start(); | 97 job->Start(); |
105 return true; | 98 return true; |
106 } | 99 } |
107 | 100 |
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 | |
123 } // namespace net | 101 } // namespace net |
OLD | NEW |