1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors 2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker #ifndef NET_BASE_PRIORITIZED_DISPATCHER_H_ 6*6777b538SAndroid Build Coastguard Worker #define NET_BASE_PRIORITIZED_DISPATCHER_H_ 7*6777b538SAndroid Build Coastguard Worker 8*6777b538SAndroid Build Coastguard Worker #include <stddef.h> 9*6777b538SAndroid Build Coastguard Worker 10*6777b538SAndroid Build Coastguard Worker #include <vector> 11*6777b538SAndroid Build Coastguard Worker 12*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h" 13*6777b538SAndroid Build Coastguard Worker #include "net/base/priority_queue.h" 14*6777b538SAndroid Build Coastguard Worker 15*6777b538SAndroid Build Coastguard Worker namespace net { 16*6777b538SAndroid Build Coastguard Worker 17*6777b538SAndroid Build Coastguard Worker // A priority-based dispatcher of jobs. Dispatch order is by priority (highest 18*6777b538SAndroid Build Coastguard Worker // first) and then FIFO. The dispatcher enforces limits on the number of running 19*6777b538SAndroid Build Coastguard Worker // jobs. It never revokes a job once started. The job must call OnJobFinished 20*6777b538SAndroid Build Coastguard Worker // once it finishes in order to dispatch further jobs. 21*6777b538SAndroid Build Coastguard Worker // 22*6777b538SAndroid Build Coastguard Worker // This class is NOT thread-safe which is enforced by the underlying 23*6777b538SAndroid Build Coastguard Worker // non-thread-safe PriorityQueue. All operations are O(p) time for p priority 24*6777b538SAndroid Build Coastguard Worker // levels. It is safe to execute any method, including destructor, from within 25*6777b538SAndroid Build Coastguard Worker // Job::Start. 26*6777b538SAndroid Build Coastguard Worker // 27*6777b538SAndroid Build Coastguard Worker class NET_EXPORT_PRIVATE PrioritizedDispatcher { 28*6777b538SAndroid Build Coastguard Worker public: 29*6777b538SAndroid Build Coastguard Worker class Job; 30*6777b538SAndroid Build Coastguard Worker typedef PriorityQueue<Job*>::Priority Priority; 31*6777b538SAndroid Build Coastguard Worker 32*6777b538SAndroid Build Coastguard Worker // Describes the limits for the number of jobs started by the dispatcher. 33*6777b538SAndroid Build Coastguard Worker // For example, |total_jobs| = 30 and |reserved_slots| = { 0, 5, 10, 5 } allow 34*6777b538SAndroid Build Coastguard Worker // for at most 30 running jobs in total. Jobs at priority 0 can't use slots 35*6777b538SAndroid Build Coastguard Worker // reserved for higher priorities, so they are limited to 10. 36*6777b538SAndroid Build Coastguard Worker // If there are already 24 jobs running, then only 6 more jobs can start. No 37*6777b538SAndroid Build Coastguard Worker // jobs at priority 1 or below can start. After one more job starts, no jobs 38*6777b538SAndroid Build Coastguard Worker // at priority 2 or below can start, since the remaining 5 slots are reserved 39*6777b538SAndroid Build Coastguard Worker // for priority 3 or above. 40*6777b538SAndroid Build Coastguard Worker struct NET_EXPORT_PRIVATE Limits { 41*6777b538SAndroid Build Coastguard Worker Limits(Priority num_priorities, size_t total_jobs); 42*6777b538SAndroid Build Coastguard Worker Limits(const Limits& other); 43*6777b538SAndroid Build Coastguard Worker ~Limits(); 44*6777b538SAndroid Build Coastguard Worker 45*6777b538SAndroid Build Coastguard Worker // Total allowed running jobs. 46*6777b538SAndroid Build Coastguard Worker size_t total_jobs; 47*6777b538SAndroid Build Coastguard Worker // Number of slots reserved for each priority and higher. 48*6777b538SAndroid Build Coastguard Worker // Sum of |reserved_slots| must be no greater than |total_jobs|. 49*6777b538SAndroid Build Coastguard Worker std::vector<size_t> reserved_slots; 50*6777b538SAndroid Build Coastguard Worker }; 51*6777b538SAndroid Build Coastguard Worker 52*6777b538SAndroid Build Coastguard Worker // An interface to the job dispatched by PrioritizedDispatcher. The dispatcher 53*6777b538SAndroid Build Coastguard Worker // does not own the Job but expects it to live as long as the Job is queued. 54*6777b538SAndroid Build Coastguard Worker // Use Cancel to remove Job from queue before it is dispatched. The Job can be 55*6777b538SAndroid Build Coastguard Worker // deleted after it is dispatched or canceled, or the dispatcher is destroyed. 56*6777b538SAndroid Build Coastguard Worker class Job { 57*6777b538SAndroid Build Coastguard Worker public: 58*6777b538SAndroid Build Coastguard Worker // Note: PrioritizedDispatcher will never delete a Job. 59*6777b538SAndroid Build Coastguard Worker virtual ~Job() = default; 60*6777b538SAndroid Build Coastguard Worker // Called when the dispatcher starts the job. Once the job finishes, it must 61*6777b538SAndroid Build Coastguard Worker // call OnJobFinished. 62*6777b538SAndroid Build Coastguard Worker virtual void Start() = 0; 63*6777b538SAndroid Build Coastguard Worker }; 64*6777b538SAndroid Build Coastguard Worker 65*6777b538SAndroid Build Coastguard Worker // A handle to the enqueued job. The handle becomes invalid when the job is 66*6777b538SAndroid Build Coastguard Worker // canceled, updated, or started. 67*6777b538SAndroid Build Coastguard Worker typedef PriorityQueue<Job*>::Pointer Handle; 68*6777b538SAndroid Build Coastguard Worker 69*6777b538SAndroid Build Coastguard Worker // Creates a dispatcher enforcing |limits| on number of running jobs. 70*6777b538SAndroid Build Coastguard Worker explicit PrioritizedDispatcher(const Limits& limits); 71*6777b538SAndroid Build Coastguard Worker 72*6777b538SAndroid Build Coastguard Worker PrioritizedDispatcher(const PrioritizedDispatcher&) = delete; 73*6777b538SAndroid Build Coastguard Worker PrioritizedDispatcher& operator=(const PrioritizedDispatcher&) = delete; 74*6777b538SAndroid Build Coastguard Worker ~PrioritizedDispatcher(); 75*6777b538SAndroid Build Coastguard Worker num_running_jobs()76*6777b538SAndroid Build Coastguard Worker size_t num_running_jobs() const { return num_running_jobs_; } num_queued_jobs()77*6777b538SAndroid Build Coastguard Worker size_t num_queued_jobs() const { return queue_.size(); } num_priorities()78*6777b538SAndroid Build Coastguard Worker size_t num_priorities() const { return max_running_jobs_.size(); } 79*6777b538SAndroid Build Coastguard Worker 80*6777b538SAndroid Build Coastguard Worker // Adds |job| with |priority| to the dispatcher. If limits permit, |job| is 81*6777b538SAndroid Build Coastguard Worker // started immediately. Returns handle to the job or null-handle if the job is 82*6777b538SAndroid Build Coastguard Worker // started. The dispatcher does not own |job|, but |job| must live as long as 83*6777b538SAndroid Build Coastguard Worker // it is queued in the dispatcher. 84*6777b538SAndroid Build Coastguard Worker Handle Add(Job* job, Priority priority); 85*6777b538SAndroid Build Coastguard Worker 86*6777b538SAndroid Build Coastguard Worker // Just like Add, except that it adds Job at the font of queue of jobs with 87*6777b538SAndroid Build Coastguard Worker // priorities of |priority|. 88*6777b538SAndroid Build Coastguard Worker Handle AddAtHead(Job* job, Priority priority); 89*6777b538SAndroid Build Coastguard Worker 90*6777b538SAndroid Build Coastguard Worker // Removes the job with |handle| from the queue. Invalidates |handle|. 91*6777b538SAndroid Build Coastguard Worker // Note: a Handle is valid iff the job is in the queue, i.e. has not Started. 92*6777b538SAndroid Build Coastguard Worker void Cancel(const Handle& handle); 93*6777b538SAndroid Build Coastguard Worker 94*6777b538SAndroid Build Coastguard Worker // Cancels and returns the oldest-lowest-priority Job invalidating any 95*6777b538SAndroid Build Coastguard Worker // handles to it. Returns NULL if the queue is empty. 96*6777b538SAndroid Build Coastguard Worker Job* EvictOldestLowest(); 97*6777b538SAndroid Build Coastguard Worker 98*6777b538SAndroid Build Coastguard Worker // Moves the queued job with |handle| to the end of all values with priority 99*6777b538SAndroid Build Coastguard Worker // |priority| and returns the updated handle, or null-handle if it starts the 100*6777b538SAndroid Build Coastguard Worker // job. Invalidates |handle|. No-op if priority did not change. 101*6777b538SAndroid Build Coastguard Worker Handle ChangePriority(const Handle& handle, Priority priority); 102*6777b538SAndroid Build Coastguard Worker 103*6777b538SAndroid Build Coastguard Worker // Notifies the dispatcher that a running job has finished. Could start a job. 104*6777b538SAndroid Build Coastguard Worker void OnJobFinished(); 105*6777b538SAndroid Build Coastguard Worker 106*6777b538SAndroid Build Coastguard Worker // Retrieves the Limits that |this| is currently using. This may not exactly 107*6777b538SAndroid Build Coastguard Worker // match the Limits this was created with. In particular, the number of slots 108*6777b538SAndroid Build Coastguard Worker // reserved for the lowest priority will always be 0, even if it was non-zero 109*6777b538SAndroid Build Coastguard Worker // in the Limits passed to the constructor or to SetLimits. 110*6777b538SAndroid Build Coastguard Worker Limits GetLimits() const; 111*6777b538SAndroid Build Coastguard Worker 112*6777b538SAndroid Build Coastguard Worker // Updates |max_running_jobs_| to match |limits|. Starts jobs if new limit 113*6777b538SAndroid Build Coastguard Worker // allows. Does not stop jobs if the new limits are lower than the old ones. 114*6777b538SAndroid Build Coastguard Worker void SetLimits(const Limits& limits); 115*6777b538SAndroid Build Coastguard Worker 116*6777b538SAndroid Build Coastguard Worker // Set the limits to zero for all priorities, allowing no new jobs to start. 117*6777b538SAndroid Build Coastguard Worker void SetLimitsToZero(); 118*6777b538SAndroid Build Coastguard Worker 119*6777b538SAndroid Build Coastguard Worker private: 120*6777b538SAndroid Build Coastguard Worker // Attempts to dispatch the job with |handle| at priority |priority| (might be 121*6777b538SAndroid Build Coastguard Worker // different than |handle.priority()|. Returns true if successful. If so 122*6777b538SAndroid Build Coastguard Worker // the |handle| becomes invalid. 123*6777b538SAndroid Build Coastguard Worker bool MaybeDispatchJob(const Handle& handle, Priority priority); 124*6777b538SAndroid Build Coastguard Worker 125*6777b538SAndroid Build Coastguard Worker // Attempts to dispatch the next highest priority job in the queue. Returns 126*6777b538SAndroid Build Coastguard Worker // true if successful, and all handles to that job become invalid. 127*6777b538SAndroid Build Coastguard Worker bool MaybeDispatchNextJob(); 128*6777b538SAndroid Build Coastguard Worker 129*6777b538SAndroid Build Coastguard Worker // Queue for jobs that need to wait for a spare slot. 130*6777b538SAndroid Build Coastguard Worker PriorityQueue<Job*> queue_; 131*6777b538SAndroid Build Coastguard Worker // Maximum total number of running jobs allowed after a job at a particular 132*6777b538SAndroid Build Coastguard Worker // priority is started. If a greater or equal number of jobs are running, then 133*6777b538SAndroid Build Coastguard Worker // another job cannot be started. 134*6777b538SAndroid Build Coastguard Worker std::vector<size_t> max_running_jobs_; 135*6777b538SAndroid Build Coastguard Worker // Total number of running jobs. 136*6777b538SAndroid Build Coastguard Worker size_t num_running_jobs_ = 0; 137*6777b538SAndroid Build Coastguard Worker }; 138*6777b538SAndroid Build Coastguard Worker 139*6777b538SAndroid Build Coastguard Worker } // namespace net 140*6777b538SAndroid Build Coastguard Worker 141*6777b538SAndroid Build Coastguard Worker #endif // NET_BASE_PRIORITIZED_DISPATCHER_H_ 142