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 | |
85 // Removes the job with |handle| from the queue. Invalidates |handle|. | 81 // Removes the job with |handle| from the queue. Invalidates |handle|. |
86 // Note: a Handle is valid iff the job is in the queue, i.e. has not Started. | 82 // Note: a Handle is valid iff the job is in the queue, i.e. has not Started. |
87 void Cancel(const Handle& handle); | 83 void Cancel(const Handle& handle); |
88 | 84 |
89 // Cancels and returns the oldest-lowest-priority Job invalidating any | 85 // Cancels and returns the oldest-lowest-priority Job invalidating any |
90 // handles to it. Returns NULL if the queue is empty. | 86 // handles to it. Returns NULL if the queue is empty. |
91 Job* EvictOldestLowest(); | 87 Job* EvictOldestLowest(); |
92 | 88 |
93 // Moves the queued job with |handle| to the end of all values with priority | 89 // Moves the queued job with |handle| to the end of all values with priority |
94 // |priority| and returns the updated handle, or null-handle if it starts the | 90 // |priority| and returns the updated handle, or null-handle if it starts the |
95 // job. Invalidates |handle|. No-op if priority did not change. | 91 // job. Invalidates |handle|. No-op if priority did not change. |
96 Handle ChangePriority(const Handle& handle, Priority priority); | 92 Handle ChangePriority(const Handle& handle, Priority priority); |
97 | 93 |
98 // Notifies the dispatcher that a running job has finished. Could start a job. | 94 // Notifies the dispatcher that a running job has finished. Could start a job. |
99 void OnJobFinished(); | 95 void OnJobFinished(); |
100 | 96 |
101 // Tells the dispatcher not to start new jobs when old jobs complete or are | |
102 // cancelled. Can't be undone. Can be used to avoid starting new jobs during | |
103 // cleanup. | |
104 void Disable(); | |
105 | |
106 private: | 97 private: |
107 // Attempts to dispatch the job with |handle| at priority |priority| (might be | 98 // Attempts to dispatch the job with |handle| at priority |priority| (might be |
108 // different than |handle.priority()|. Returns true if successful. If so | 99 // different than |handle.priority()|. Returns true if successful. If so |
109 // the |handle| becomes invalid. | 100 // the |handle| becomes invalid. |
110 bool MaybeDispatchJob(const Handle& handle, Priority priority); | 101 bool MaybeDispatchJob(const Handle& handle, Priority priority); |
111 | 102 |
112 // Updates |max_running_jobs_| to match |limits|. Does not start any new | |
113 // new jobs, even if the new limits would allow queued jobs to start. | |
114 void SetLimits(const Limits& limits); | |
115 | |
116 // Queue for jobs that need to wait for a spare slot. | 103 // Queue for jobs that need to wait for a spare slot. |
117 PriorityQueue<Job*> queue_; | 104 PriorityQueue<Job*> queue_; |
118 // Maximum total number of running jobs allowed after a job at a particular | 105 // Maximum total number of running jobs allowed after a job at a particular |
119 // priority is started. If a greater or equal number of jobs are running, then | 106 // priority is started. If a greater or equal number of jobs are running, then |
120 // another job cannot be started. | 107 // another job cannot be started. |
121 std::vector<size_t> max_running_jobs_; | 108 std::vector<size_t> max_running_jobs_; |
122 // Total number of running jobs. | 109 // Total number of running jobs. |
123 size_t num_running_jobs_; | 110 size_t num_running_jobs_; |
124 | 111 |
125 DISALLOW_COPY_AND_ASSIGN(PrioritizedDispatcher); | 112 DISALLOW_COPY_AND_ASSIGN(PrioritizedDispatcher); |
126 }; | 113 }; |
127 | 114 |
128 } // namespace net | 115 } // namespace net |
129 | 116 |
130 #endif // NET_BASE_PRIORITIZED_DISPATCHER_H_ | 117 #endif // NET_BASE_PRIORITIZED_DISPATCHER_H_ |
OLD | NEW |