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 #ifndef NET_BASE_PRIORITIZED_DISPATCHER_H_ | 5 #ifndef NET_BASE_PRIORITIZED_DISPATCHER_H_ |
6 #define NET_BASE_PRIORITIZED_DISPATCHER_H_ | 6 #define NET_BASE_PRIORITIZED_DISPATCHER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "net/base/net_export.h" | 10 #include "net/base/net_export.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 size_t num_running_jobs() const { return num_running_jobs_; } | 71 size_t num_running_jobs() const { return num_running_jobs_; } |
72 size_t num_queued_jobs() const { return queue_.size(); } | 72 size_t num_queued_jobs() const { return queue_.size(); } |
73 size_t num_priorities() const { return max_running_jobs_.size(); } | 73 size_t num_priorities() const { return max_running_jobs_.size(); } |
74 | 74 |
75 // Adds |job| with |priority| to the dispatcher. If limits permit, |job| is | 75 // Adds |job| with |priority| to the dispatcher. If limits permit, |job| is |
76 // started immediately. Returns handle to the job or null-handle if the job is | 76 // started immediately. Returns handle to the job or null-handle if the job is |
77 // started. The dispatcher does not own |job|, but |job| must live as long as | 77 // started. The dispatcher does not own |job|, but |job| must live as long as |
78 // it is queued in the dispatcher. | 78 // it is queued in the dispatcher. |
79 Handle Add(Job* job, Priority priority); | 79 Handle Add(Job* job, Priority priority); |
80 | 80 |
| 81 // Just like Add, except that it adds Job at the font of queue of jobs with |
| 82 // priorities of |priority|. |
| 83 Handle AddAtHead(Job* job, Priority priority); |
| 84 |
81 // Removes the job with |handle| from the queue. Invalidates |handle|. | 85 // Removes the job with |handle| from the queue. Invalidates |handle|. |
82 // Note: a Handle is valid iff the job is in the queue, i.e. has not Started. | 86 // Note: a Handle is valid iff the job is in the queue, i.e. has not Started. |
83 void Cancel(const Handle& handle); | 87 void Cancel(const Handle& handle); |
84 | 88 |
85 // Cancels and returns the oldest-lowest-priority Job invalidating any | 89 // Cancels and returns the oldest-lowest-priority Job invalidating any |
86 // handles to it. Returns NULL if the queue is empty. | 90 // handles to it. Returns NULL if the queue is empty. |
87 Job* EvictOldestLowest(); | 91 Job* EvictOldestLowest(); |
88 | 92 |
89 // Moves the queued job with |handle| to the end of all values with priority | 93 // Moves the queued job with |handle| to the end of all values with priority |
90 // |priority| and returns the updated handle, or null-handle if it starts the | 94 // |priority| and returns the updated handle, or null-handle if it starts the |
91 // job. Invalidates |handle|. No-op if priority did not change. | 95 // job. Invalidates |handle|. No-op if priority did not change. |
92 Handle ChangePriority(const Handle& handle, Priority priority); | 96 Handle ChangePriority(const Handle& handle, Priority priority); |
93 | 97 |
94 // Notifies the dispatcher that a running job has finished. Could start a job. | 98 // Notifies the dispatcher that a running job has finished. Could start a job. |
95 void OnJobFinished(); | 99 void OnJobFinished(); |
96 | 100 |
| 101 // Retrieves the Limits that |this| is currently using. This may not exactly |
| 102 // match the Limits this was created with. In particular, the number of slots |
| 103 // reserved for the lowest priority will always be 0, even if it was non-zero |
| 104 // in the Limits passed to the constructor or to SetLimits. |
| 105 Limits GetLimits() const; |
| 106 |
| 107 // Updates |max_running_jobs_| to match |limits|. Starts jobs if new limit |
| 108 // allows. Does not stop jobs if the new limits are lower than the old ones. |
| 109 void SetLimits(const Limits& limits); |
| 110 |
| 111 // Set the limits to zero for all priorities, allowing no new jobs to start. |
| 112 void SetLimitsToZero(); |
| 113 |
97 private: | 114 private: |
98 // Attempts to dispatch the job with |handle| at priority |priority| (might be | 115 // Attempts to dispatch the job with |handle| at priority |priority| (might be |
99 // different than |handle.priority()|. Returns true if successful. If so | 116 // different than |handle.priority()|. Returns true if successful. If so |
100 // the |handle| becomes invalid. | 117 // the |handle| becomes invalid. |
101 bool MaybeDispatchJob(const Handle& handle, Priority priority); | 118 bool MaybeDispatchJob(const Handle& handle, Priority priority); |
102 | 119 |
| 120 // Attempts to dispatch the next highest priority job in the queue. Returns |
| 121 // true if successful, and all handles to that job become invalid. |
| 122 bool MaybeDispatchNextJob(); |
| 123 |
103 // Queue for jobs that need to wait for a spare slot. | 124 // Queue for jobs that need to wait for a spare slot. |
104 PriorityQueue<Job*> queue_; | 125 PriorityQueue<Job*> queue_; |
105 // Maximum total number of running jobs allowed after a job at a particular | 126 // Maximum total number of running jobs allowed after a job at a particular |
106 // priority is started. If a greater or equal number of jobs are running, then | 127 // priority is started. If a greater or equal number of jobs are running, then |
107 // another job cannot be started. | 128 // another job cannot be started. |
108 std::vector<size_t> max_running_jobs_; | 129 std::vector<size_t> max_running_jobs_; |
109 // Total number of running jobs. | 130 // Total number of running jobs. |
110 size_t num_running_jobs_; | 131 size_t num_running_jobs_; |
111 | 132 |
112 DISALLOW_COPY_AND_ASSIGN(PrioritizedDispatcher); | 133 DISALLOW_COPY_AND_ASSIGN(PrioritizedDispatcher); |
113 }; | 134 }; |
114 | 135 |
115 } // namespace net | 136 } // namespace net |
116 | 137 |
117 #endif // NET_BASE_PRIORITIZED_DISPATCHER_H_ | 138 #endif // NET_BASE_PRIORITIZED_DISPATCHER_H_ |
OLD | NEW |