Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: net/base/prioritized_dispatcher.cc

Issue 8965025: Refactoring of job dispatch in HostResolverImpl in preparation for DnsClient. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased to trunk. Created 8 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 // We rely on the priority enum values being sequential having starting at 0,
12 // and increasing for lower priorities.
13 COMPILE_ASSERT(HIGHEST == 0u &&
14 LOWEST > HIGHEST &&
15 IDLE > LOWEST &&
16 NUM_PRIORITIES > IDLE,
17 priority_indexes_incompatible);
18
19 PrioritizedDispatcher::PrioritizedDispatcher(const Limits& limits,
20 size_t max_queued)
21 : num_running_jobs_(0), max_queued_jobs_(max_queued) {
22 size_t total = 0;
23 for (size_t i = 0; i < NUM_PRIORITIES; ++i) {
24 total += limits.reserved_slots[NUM_PRIORITIES - i - 1];
25 max_running_jobs_[NUM_PRIORITIES - i - 1] = total;
26 }
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 < NUM_PRIORITIES; ++i) {
30 max_running_jobs_[i] += spare;
31 }
32 }
33
34 PrioritizedDispatcher::~PrioritizedDispatcher() {}
35
36 void PrioritizedDispatcher::SetMaxQueued(size_t max_queued) {
37 DCHECK_EQ(0u, num_queued_jobs());
38 max_queued_jobs_ = max_queued;
39 }
40
41 bool PrioritizedDispatcher::WouldEvict(RequestPriority priority) const {
42 if (num_queued_jobs() < max_queued_jobs_)
43 return false;
44 // Temporarily remove const to perform the check.
45 Handle evicted =
46 const_cast<PrioritizedDispatcher*>(this)->queue_.OldestLowest();
47 DCHECK(!evicted.is_null());
48 return evicted.priority() < priority;
49 }
50
51 PrioritizedDispatcher::Handle PrioritizedDispatcher::Add(
52 Job* job, RequestPriority priority) {
53 DCHECK(job);
54 if (num_running_jobs_ < max_running_jobs_[priority]) {
55 ++num_running_jobs_;
56 job->Start();
57 return Handle();
58 }
59 Handle handle = queue_.Insert(job, priority);
60 if (queue_.size() > max_queued_jobs_) {
61 // Evict oldest lowest-priority job.
62 Handle evicted = queue_.OldestLowest();
63 DCHECK(!evicted.is_null());
64 if (evicted.equals(handle))
65 handle = Handle();
66 Job* evicted_job = evicted.value();
67 queue_.Erase(evicted);
68 DCHECK(evicted_job);
69 evicted_job->OnEvicted();
70 }
71 return handle;
72 }
73
74 void PrioritizedDispatcher::Cancel(const Handle& handle) {
75 queue_.Erase(handle);
76 }
77
78 PrioritizedDispatcher::Handle PrioritizedDispatcher::Update(
79 const Handle& handle, RequestPriority priority) {
80 DCHECK(!handle.is_null());
81 DCHECK_GE(num_running_jobs_, max_running_jobs_[handle.priority()]) <<
82 "Job should not be in queue when limits permit it to start.";
83 DCHECK_LE(queue_.size(), max_queued_jobs_);
84
85 if (handle.priority() == priority)
86 return handle;
87
88 if (DispatchJob(handle, priority))
89 return Handle();
90 Job* job = handle.value();
91 queue_.Erase(handle);
92 return queue_.Insert(job, priority);
93 }
94
95 void PrioritizedDispatcher::OnJobFinished() {
96 DCHECK_GT(num_running_jobs_, 0u);
97 --num_running_jobs_;
98 Handle handle = queue_.First();
99 if (handle.is_null()) {
100 DCHECK_EQ(0u, queue_.size());
101 return;
102 }
103 DispatchJob(handle, handle.priority());
104 }
105
106 bool PrioritizedDispatcher::DispatchJob(const Handle& handle,
107 RequestPriority job_priority) {
108 if (num_running_jobs_ >= max_running_jobs_[job_priority])
109 return false;
110 Job* job = handle.value();
111 queue_.Erase(handle);
112 ++num_running_jobs_;
113 DCHECK(job);
114 job->Start();
115 return true;
116 }
117
118 } // namespace net
119
120
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698