Chromium Code Reviews| 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_PRIORITY_DISPATCH_H_ | 5 #ifndef NET_BASE_PRIORITIZED_DISPATCHER_H_ |
| 6 #define NET_BASE_PRIORITY_DISPATCH_H_ | 6 #define NET_BASE_PRIORITIZED_DISPATCHER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "net/base/net_export.h" | 11 #include "net/base/net_export.h" |
| 12 #include "net/base/priority_queue.h" | 12 #include "net/base/priority_queue.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 // A priority-based dispatcher of jobs. Dispatch order is by priority (highest | 16 // A priority-based dispatcher of jobs. Dispatch order is by priority (highest |
| 17 // first) and then FIFO. The dispatcher enforces limits on the number of running | 17 // first) and then FIFO. The dispatcher enforces limits on the number of running |
| 18 // jobs. It never revokes a job once started. The job must call OnJobFinished | 18 // jobs. It never revokes a job once started. The job must call OnJobFinished |
| 19 // once it finishes in order to dispatch further jobs. | 19 // once it finishes in order to dispatch further jobs. |
| 20 // | 20 // |
| 21 // All operations are O(p) time for p priority levels. The class is fully | 21 // This class is NOT thread-safe which is enforced by the underlying |
| 22 // reentrant: it is safe to execute any method (incl. destructor) from within | 22 // non-thread-safe PriorityQueue. All operations are O(p) time for p priority |
| 23 // Job callbacks. However, this class is NOT thread-safe, which is enforced | 23 // levels. It is safe to execute any method, including destructor, from within |
| 24 // by the underlying non-thread-safe PriorityQueue. | 24 // Job::Start. |
| 25 // | 25 // |
| 26 class NET_EXPORT_PRIVATE PrioritizedDispatcher { | 26 class NET_EXPORT_PRIVATE PrioritizedDispatcher { |
| 27 public: | 27 public: |
| 28 class Job; | 28 class Job; |
| 29 typedef PriorityQueue<Job*>::Priority Priority; | 29 typedef PriorityQueue<Job*>::Priority Priority; |
| 30 | 30 |
| 31 // Describes the limits for the number of jobs started by the dispatcher. | 31 // Describes the limits for the number of jobs started by the dispatcher. |
| 32 // For example, |total_jobs| = 30 and |reserved_slots| = { 5, 10, 5 } | 32 // For example, |total_jobs| = 30 and |reserved_slots| = { 0, 5, 10, 5 } |
| 33 // allow for at most 30 running jobs in total. If there are already 24 jobs | 33 // allow for at most 30 running jobs in total. If there are already 24 jobs |
|
mmenke
2012/06/01 17:45:34
optional nit: Suggest you add ", and no jobs at p
szym
2012/06/01 18:15:01
Those are reserved slots. Only 20 slots are reserv
mmenke
2012/06/01 18:30:05
Ah, right, I forgot about the 10.
| |
| 34 // running, then there can be 6 more jobs started of which at most 1 can be | 34 // running, then only 6 more jobs can start. No jobs at priority 1 or below |
| 35 // at priority 1 or 2, but the rest have to be at 2. | 35 // can start. After one more job starts, no jobs at priority 2 or below can |
| 36 // start, since the remaining 5 slots are reserved for priority 3 or above. | |
| 36 struct NET_EXPORT_PRIVATE Limits { | 37 struct NET_EXPORT_PRIVATE Limits { |
| 37 Limits(Priority num_priorities, size_t total_jobs); | 38 Limits(Priority num_priorities, size_t total_jobs); |
| 38 ~Limits(); | 39 ~Limits(); |
| 39 | 40 |
| 40 // Total allowed running jobs. | 41 // Total allowed running jobs. |
| 41 size_t total_jobs; | 42 size_t total_jobs; |
| 42 // Number of slots reserved for each priority and higher. | 43 // Number of slots reserved for each priority and higher. |
| 43 // Sum of |reserved_slots| must be no greater than |total_jobs|. | 44 // Sum of |reserved_slots| must be no greater than |total_jobs|. |
| 44 std::vector<size_t> reserved_slots; | 45 std::vector<size_t> reserved_slots; |
| 45 }; | 46 }; |
| 46 | 47 |
| 47 // An interface to the job dispatched by PrioritizedDispatcher. The dispatcher | 48 // An interface to the job dispatched by PrioritizedDispatcher. The dispatcher |
| 48 // does not own the Job but expects it to live as long as the Job is queued. | 49 // does not own the Job but expects it to live as long as the Job is queued. |
| 49 // Use Cancel to remove Job from queue before it is dispatched. The Job can be | 50 // Use Cancel to remove Job from queue before it is dispatched. The Job can be |
| 50 // deleted after it is dispatched or canceled, or the dispatcher is destroyed. | 51 // deleted after it is dispatched or canceled, or the dispatcher is destroyed. |
| 51 class Job { | 52 class Job { |
| 52 public: | 53 public: |
| 53 // Note: PriorityDispatch will never delete a Job. | 54 // Note: PrioritizedDispatcher will never delete a Job. |
| 54 virtual ~Job() {} | 55 virtual ~Job() {} |
| 55 // Called when the dispatcher starts the job. Must call OnJobFinished when | 56 // Called when the dispatcher starts the job. Once the job finishes, it must |
| 56 // done. | 57 // call OnJobFinished. |
| 57 virtual void Start() = 0; | 58 virtual void Start() = 0; |
| 58 }; | 59 }; |
| 59 | 60 |
| 60 // A handle to the enqueued job. The handle becomes invalid when the job is | 61 // A handle to the enqueued job. The handle becomes invalid when the job is |
| 61 // canceled, updated, or started. | 62 // canceled, updated, or started. |
| 62 typedef PriorityQueue<Job*>::Pointer Handle; | 63 typedef PriorityQueue<Job*>::Pointer Handle; |
| 63 | 64 |
| 64 // Creates a dispatcher enforcing |limits| on number of running jobs. | 65 // Creates a dispatcher enforcing |limits| on number of running jobs. |
| 65 PrioritizedDispatcher(const Limits& limits); | 66 explicit PrioritizedDispatcher(const Limits& limits); |
| 66 | 67 |
| 67 ~PrioritizedDispatcher(); | 68 ~PrioritizedDispatcher(); |
| 68 | 69 |
| 69 size_t num_running_jobs() const { return num_running_jobs_; } | 70 size_t num_running_jobs() const { return num_running_jobs_; } |
| 70 size_t num_queued_jobs() const { return queue_.size(); } | 71 size_t num_queued_jobs() const { return queue_.size(); } |
| 71 size_t num_priorities() const { return max_running_jobs_.size(); } | 72 size_t num_priorities() const { return max_running_jobs_.size(); } |
| 72 | 73 |
| 73 // Adds |job| with |priority| to the dispatcher. If limits permit, |job| is | 74 // Adds |job| with |priority| to the dispatcher. If limits permit, |job| is |
| 74 // started immediately. Returns handle to the job or null-handle if the job is | 75 // started immediately. Returns handle to the job or null-handle if the job is |
| 75 // started. | 76 // started. The dispatcher does not own |job|, but must be outlived by |job|. |
|
mmenke
2012/06/01 17:45:34
Shouldn't this be "but must outlive |job|"?
szym
2012/06/01 18:15:01
The Job must outlive the dispatcher so that it can
mmenke
2012/06/01 18:30:05
But you say Jobs must call OnJobFinished after bei
mmenke
2012/06/01 18:32:49
Err...Or evicted.
Ok...Maybe "|jobs| that are sti
| |
| 76 Handle Add(Job* job, Priority priority); | 77 Handle Add(Job* job, Priority priority); |
| 77 | 78 |
| 78 // Removes the job with |handle| from the queue. Invalidates |handle|. | 79 // Removes the job with |handle| from the queue. Invalidates |handle|. |
| 79 // Note: a Handle is valid iff the job is in the queue, i.e. has not Started. | 80 // Note: a Handle is valid iff the job is in the queue, i.e. has not Started. |
| 80 void Cancel(const Handle& handle); | 81 void Cancel(const Handle& handle); |
| 81 | 82 |
| 82 // Removes and returns the oldest-lowest Job from the queue invalidating any | 83 // Removes and returns the oldest-lowest Job from the queue invalidating any |
| 83 // handles to it. Returns NULL if the queue is empty. | 84 // handles to it. Returns NULL if the queue is empty. |
| 84 Job* EvictOldestLowest(); | 85 Job* EvictOldestLowest(); |
| 85 | 86 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 104 // another job cannot be started. | 105 // another job cannot be started. |
| 105 std::vector<size_t> max_running_jobs_; | 106 std::vector<size_t> max_running_jobs_; |
| 106 // Total number of running jobs. | 107 // Total number of running jobs. |
| 107 size_t num_running_jobs_; | 108 size_t num_running_jobs_; |
| 108 | 109 |
| 109 DISALLOW_COPY_AND_ASSIGN(PrioritizedDispatcher); | 110 DISALLOW_COPY_AND_ASSIGN(PrioritizedDispatcher); |
| 110 }; | 111 }; |
| 111 | 112 |
| 112 } // namespace net | 113 } // namespace net |
| 113 | 114 |
| 114 #endif // NET_BASE_PRIORITY_DISPATCH_H_ | 115 #endif // NET_BASE_PRIORITIZED_DISPATCHER_H_ |
| 115 | 116 |
| 117 | |
|
mmenke
2012/06/01 17:45:34
nit: Remove extra blank line (Goes for other file
szym
2012/06/01 18:15:01
Done.
| |
| OLD | NEW |