Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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(1) time. The class is fully reentrant: it is safe to | |
|
cbentzel
2012/01/06 16:03:05
Aren't some O(p) where p is the number of levels.
| |
| 22 // execute any method (incl. destructor) from within Job callbacks. | |
| 23 // However, this class is NOT thread-safe. | |
|
cbentzel
2012/01/06 16:03:05
Why not derive from NonThreadSafe here like the Pr
szym
2012/01/06 17:52:30
The underlying PriorityQueue will enforce this. Ad
| |
| 24 // | |
| 25 class NET_EXPORT_PRIVATE PrioritizedDispatcher { | |
| 26 public: | |
| 27 class Job; | |
| 28 typedef PriorityQueue<Job*>::Priority Priority; | |
| 29 | |
| 30 // Describes the limits for the number of jobs started by the dispatcher. | |
| 31 // For example, |total_jobs| = 30 and |reserved_slots| = { 5, 10, 5 } | |
| 32 // allow for at most 30 running jobs in total. If there are already 24 jobs | |
| 33 // running, then there can be 6 more jobs started of which at most 1 can be | |
| 34 // at priority 1 or 0, but the rest has to be at 0. | |
| 35 struct NET_EXPORT_PRIVATE Limits { | |
| 36 Limits(Priority num_priorities, size_t total_jobs); | |
| 37 ~Limits(); | |
| 38 | |
| 39 // Total allowed running jobs. | |
| 40 size_t total_jobs; | |
| 41 // Number of slots reserved for each priority and higher. | |
| 42 // Sum of |reserved_slots| must be no greater than |total_jobs|. | |
| 43 std::vector<size_t> reserved_slots; | |
| 44 }; | |
| 45 | |
| 46 // An interface to the job dispatched by PrioritizedDispatcher. The dispatcher | |
| 47 // does not own the Job but expects it to live as long as the Job is queued. | |
| 48 // Use Cancel to remove Job from queue before it is dispatched. The Job can be | |
| 49 // deleted after it is dispatched or canceled, or the dispatcher is destroyed. | |
| 50 class Job { | |
| 51 public: | |
| 52 // Note: PriorityDispatch will never delete a Job. | |
| 53 virtual ~Job() {} | |
| 54 // Called when the dispatcher starts the job. Must call OnJobFinished when | |
| 55 // done. | |
| 56 virtual void Start() = 0; | |
| 57 }; | |
| 58 | |
| 59 // A handle to the enqueued job. The handle becomes invalid when the job is | |
| 60 // canceled, updated, or started. | |
| 61 typedef PriorityQueue<Job*>::Pointer Handle; | |
| 62 | |
| 63 // Creates a dispatcher enforcing |limits| on number of running jobs. | |
| 64 PrioritizedDispatcher(const Limits& limits); | |
| 65 | |
| 66 ~PrioritizedDispatcher(); | |
| 67 | |
| 68 size_t num_running_jobs() const { return num_running_jobs_; } | |
| 69 size_t num_queued_jobs() const { return queue_.size(); } | |
| 70 size_t num_priorities() const { return max_running_jobs_.size(); } | |
| 71 | |
| 72 // Adds |job| with |priority| to the dispatcher. If limits permit, |job| is | |
| 73 // started immediately. Returns handle to the job or null-handle if the job is | |
| 74 // started. | |
| 75 Handle Add(Job* job, Priority priority); | |
| 76 | |
| 77 // Removes the job with |handle| from the queue. Invalidates |handle|. | |
| 78 // Note: a Handle is valid iff the job is in the queue, i.e. has not Started. | |
| 79 void Cancel(const Handle& handle); | |
| 80 | |
| 81 // Removes and returns the oldest-lowest Job from the queue invalidating any | |
| 82 // handles to it. Returns NULL if the queue is empty. | |
| 83 Job* EvictOldestLowest(); | |
| 84 | |
| 85 // Moves the queued job with |handle| to the end of all values with priority | |
| 86 // |priority| and returns the updated handle, or null-handle if it starts the | |
| 87 // job. Invalidates |handle|. No-op if priority did not change. | |
| 88 Handle ChangePriority(const Handle& handle, Priority priority); | |
| 89 | |
| 90 // Notifies the dispatcher that a running job has finished. Could start a job. | |
| 91 void OnJobFinished(); | |
| 92 | |
| 93 private: | |
| 94 // Attempts to dispatch the job with |handle| at priority |priority| (might be | |
| 95 // different than |handle.priority()|. Returns true if successful. If so | |
| 96 // the |handle| becomes invalid. | |
| 97 bool DispatchJob(const Handle& handle, Priority priority); | |
| 98 | |
| 99 // Queue for jobs that need to wait for a spare slot. | |
| 100 PriorityQueue<Job*> queue_; | |
| 101 // Maximum total number of running jobs allowed after a job at a particular | |
| 102 // priority is started. If a greater or equal number of jobs are running, then | |
| 103 // another job cannot be started. | |
| 104 std::vector<size_t> max_running_jobs_; | |
| 105 // Total number of running jobs. | |
| 106 size_t num_running_jobs_; | |
| 107 // Maximum number of jobs in |queue_|. | |
| 108 size_t max_queued_jobs_; | |
| 109 | |
| 110 DISALLOW_COPY_AND_ASSIGN(PrioritizedDispatcher); | |
| 111 }; | |
| 112 | |
| 113 } // namespace net | |
| 114 | |
| 115 #endif // NET_BASE_PRIORITY_DISPATCH_H_ | |
| 116 | |
| OLD | NEW |