| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef NET_BASE_PRIORITY_DISPATCH_H_ |
| 6 #define NET_BASE_PRIORITY_DISPATCH_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <vector> |
| 10 |
| 11 #include "net/base/net_export.h" |
| 12 #include "net/base/priority_queue.h" |
| 13 |
| 14 namespace net { |
| 15 |
| 16 // A priority-based dispatcher of jobs. Dispatch order is by priority (lowest |
| 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 |
| 19 // once it finishes in order to dispatch further jobs. |
| 20 // |
| 21 // All operations are O(p) time for p priority levels. The class is fully |
| 22 // reentrant: it is safe to execute any method (incl. destructor) from within |
| 23 // Job callbacks. However, this class is NOT thread-safe, which is enforced |
| 24 // by the underlying non-thread-safe PriorityQueue. |
| 25 // |
| 26 class NET_EXPORT_PRIVATE PrioritizedDispatcher { |
| 27 public: |
| 28 class Job; |
| 29 typedef PriorityQueue<Job*>::Priority Priority; |
| 30 |
| 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 } |
| 33 // allow for at most 30 running jobs in total. If there are already 24 jobs |
| 34 // running, then there can be 6 more jobs started of which at most 1 can be |
| 35 // at priority 1 or 0, but the rest has to be at 0. |
| 36 struct NET_EXPORT_PRIVATE Limits { |
| 37 Limits(Priority num_priorities, size_t total_jobs); |
| 38 ~Limits(); |
| 39 |
| 40 // Total allowed running jobs. |
| 41 size_t total_jobs; |
| 42 // Number of slots reserved for each priority and higher. |
| 43 // Sum of |reserved_slots| must be no greater than |total_jobs|. |
| 44 std::vector<size_t> reserved_slots; |
| 45 }; |
| 46 |
| 47 // 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 // 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 class Job { |
| 52 public: |
| 53 // Note: PriorityDispatch will never delete a Job. |
| 54 virtual ~Job() {} |
| 55 // Called when the dispatcher starts the job. Must call OnJobFinished when |
| 56 // done. |
| 57 virtual void Start() = 0; |
| 58 }; |
| 59 |
| 60 // A handle to the enqueued job. The handle becomes invalid when the job is |
| 61 // canceled, updated, or started. |
| 62 typedef PriorityQueue<Job*>::Pointer Handle; |
| 63 |
| 64 // Creates a dispatcher enforcing |limits| on number of running jobs. |
| 65 PrioritizedDispatcher(const Limits& limits); |
| 66 |
| 67 ~PrioritizedDispatcher(); |
| 68 |
| 69 size_t num_running_jobs() const { return num_running_jobs_; } |
| 70 size_t num_queued_jobs() const { return queue_.size(); } |
| 71 size_t num_priorities() const { return max_running_jobs_.size(); } |
| 72 |
| 73 // 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. |
| 76 Handle Add(Job* job, Priority priority); |
| 77 |
| 78 // 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 void Cancel(const Handle& handle); |
| 81 |
| 82 // Removes and returns the oldest-lowest Job from the queue invalidating any |
| 83 // handles to it. Returns NULL if the queue is empty. |
| 84 Job* EvictOldestLowest(); |
| 85 |
| 86 // 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 |
| 88 // job. Invalidates |handle|. No-op if priority did not change. |
| 89 Handle ChangePriority(const Handle& handle, Priority priority); |
| 90 |
| 91 // Notifies the dispatcher that a running job has finished. Could start a job. |
| 92 void OnJobFinished(); |
| 93 |
| 94 private: |
| 95 // Attempts to dispatch the job with |handle| at priority |priority| (might be |
| 96 // different than |handle.priority()|. Returns true if successful. If so |
| 97 // the |handle| becomes invalid. |
| 98 bool MaybeDispatchJob(const Handle& handle, Priority priority); |
| 99 |
| 100 // Queue for jobs that need to wait for a spare slot. |
| 101 PriorityQueue<Job*> queue_; |
| 102 // Maximum total number of running jobs allowed after a job at a particular |
| 103 // priority is started. If a greater or equal number of jobs are running, then |
| 104 // another job cannot be started. |
| 105 std::vector<size_t> max_running_jobs_; |
| 106 // Total number of running jobs. |
| 107 size_t num_running_jobs_; |
| 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(PrioritizedDispatcher); |
| 110 }; |
| 111 |
| 112 } // namespace net |
| 113 |
| 114 #endif // NET_BASE_PRIORITY_DISPATCH_H_ |
| 115 |
| OLD | NEW |