| 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 } allow |
| 33 // allow for at most 30 running jobs in total. If there are already 24 jobs | 33 // for at most 30 running jobs in total. Jobs at priority 0 can't use slots |
| 34 // running, then there can be 6 more jobs started of which at most 1 can be | 34 // reserved for higher priorities, so they are limited to 10. |
| 35 // at priority 1 or 2, but the rest have to be at 2. | 35 // If there are already 24 jobs running, then only 6 more jobs can start. No |
| 36 // jobs at priority 1 or below can start. After one more job starts, no jobs |
| 37 // at priority 2 or below can start, since the remaining 5 slots are reserved |
| 38 // for priority 3 or above. |
| 36 struct NET_EXPORT_PRIVATE Limits { | 39 struct NET_EXPORT_PRIVATE Limits { |
| 37 Limits(Priority num_priorities, size_t total_jobs); | 40 Limits(Priority num_priorities, size_t total_jobs); |
| 38 ~Limits(); | 41 ~Limits(); |
| 39 | 42 |
| 40 // Total allowed running jobs. | 43 // Total allowed running jobs. |
| 41 size_t total_jobs; | 44 size_t total_jobs; |
| 42 // Number of slots reserved for each priority and higher. | 45 // Number of slots reserved for each priority and higher. |
| 43 // Sum of |reserved_slots| must be no greater than |total_jobs|. | 46 // Sum of |reserved_slots| must be no greater than |total_jobs|. |
| 44 std::vector<size_t> reserved_slots; | 47 std::vector<size_t> reserved_slots; |
| 45 }; | 48 }; |
| 46 | 49 |
| 47 // An interface to the job dispatched by PrioritizedDispatcher. The dispatcher | 50 // 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. | 51 // 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 | 52 // 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. | 53 // deleted after it is dispatched or canceled, or the dispatcher is destroyed. |
| 51 class Job { | 54 class Job { |
| 52 public: | 55 public: |
| 53 // Note: PriorityDispatch will never delete a Job. | 56 // Note: PrioritizedDispatcher will never delete a Job. |
| 54 virtual ~Job() {} | 57 virtual ~Job() {} |
| 55 // Called when the dispatcher starts the job. Must call OnJobFinished when | 58 // Called when the dispatcher starts the job. Once the job finishes, it must |
| 56 // done. | 59 // call OnJobFinished. |
| 57 virtual void Start() = 0; | 60 virtual void Start() = 0; |
| 58 }; | 61 }; |
| 59 | 62 |
| 60 // A handle to the enqueued job. The handle becomes invalid when the job is | 63 // A handle to the enqueued job. The handle becomes invalid when the job is |
| 61 // canceled, updated, or started. | 64 // canceled, updated, or started. |
| 62 typedef PriorityQueue<Job*>::Pointer Handle; | 65 typedef PriorityQueue<Job*>::Pointer Handle; |
| 63 | 66 |
| 64 // Creates a dispatcher enforcing |limits| on number of running jobs. | 67 // Creates a dispatcher enforcing |limits| on number of running jobs. |
| 65 PrioritizedDispatcher(const Limits& limits); | 68 explicit PrioritizedDispatcher(const Limits& limits); |
| 66 | 69 |
| 67 ~PrioritizedDispatcher(); | 70 ~PrioritizedDispatcher(); |
| 68 | 71 |
| 69 size_t num_running_jobs() const { return num_running_jobs_; } | 72 size_t num_running_jobs() const { return num_running_jobs_; } |
| 70 size_t num_queued_jobs() const { return queue_.size(); } | 73 size_t num_queued_jobs() const { return queue_.size(); } |
| 71 size_t num_priorities() const { return max_running_jobs_.size(); } | 74 size_t num_priorities() const { return max_running_jobs_.size(); } |
| 72 | 75 |
| 73 // Adds |job| with |priority| to the dispatcher. If limits permit, |job| is | 76 // 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 | 77 // started immediately. Returns handle to the job or null-handle if the job is |
| 75 // started. | 78 // started. The dispatcher does not own |job|, but |job| must live as long as |
| 79 // it is queued in the dispatcher. |
| 76 Handle Add(Job* job, Priority priority); | 80 Handle Add(Job* job, Priority priority); |
| 77 | 81 |
| 78 // Removes the job with |handle| from the queue. Invalidates |handle|. | 82 // 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. | 83 // Note: a Handle is valid iff the job is in the queue, i.e. has not Started. |
| 80 void Cancel(const Handle& handle); | 84 void Cancel(const Handle& handle); |
| 81 | 85 |
| 82 // Removes and returns the oldest-lowest Job from the queue invalidating any | 86 // Cancels and returns the oldest-lowest-priority Job invalidating any |
| 83 // handles to it. Returns NULL if the queue is empty. | 87 // handles to it. Returns NULL if the queue is empty. |
| 84 Job* EvictOldestLowest(); | 88 Job* EvictOldestLowest(); |
| 85 | 89 |
| 86 // Moves the queued job with |handle| to the end of all values with priority | 90 // Moves the queued job with |handle| to the end of all values with priority |
| 87 // |priority| and returns the updated handle, or null-handle if it starts the | 91 // |priority| and returns the updated handle, or null-handle if it starts the |
| 88 // job. Invalidates |handle|. No-op if priority did not change. | 92 // job. Invalidates |handle|. No-op if priority did not change. |
| 89 Handle ChangePriority(const Handle& handle, Priority priority); | 93 Handle ChangePriority(const Handle& handle, Priority priority); |
| 90 | 94 |
| 91 // Notifies the dispatcher that a running job has finished. Could start a job. | 95 // Notifies the dispatcher that a running job has finished. Could start a job. |
| 92 void OnJobFinished(); | 96 void OnJobFinished(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 104 // another job cannot be started. | 108 // another job cannot be started. |
| 105 std::vector<size_t> max_running_jobs_; | 109 std::vector<size_t> max_running_jobs_; |
| 106 // Total number of running jobs. | 110 // Total number of running jobs. |
| 107 size_t num_running_jobs_; | 111 size_t num_running_jobs_; |
| 108 | 112 |
| 109 DISALLOW_COPY_AND_ASSIGN(PrioritizedDispatcher); | 113 DISALLOW_COPY_AND_ASSIGN(PrioritizedDispatcher); |
| 110 }; | 114 }; |
| 111 | 115 |
| 112 } // namespace net | 116 } // namespace net |
| 113 | 117 |
| 114 #endif // NET_BASE_PRIORITY_DISPATCH_H_ | 118 #endif // NET_BASE_PRIORITIZED_DISPATCHER_H_ |
| 115 | |
| OLD | NEW |