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 "net/base/net_export.h" | |
| 10 #include "net/base/priority_queue.h" | |
| 11 #include "net/base/request_priority.h" | |
| 12 | |
| 13 namespace net { | |
| 14 | |
| 15 // A priority-based dispatcher of jobs. The dispatcher enforces limits on | |
| 16 // the number of running jobs. It never revokes a job once started. The job | |
| 17 // must call OnJobFinished once it finishes in order to dispatch further jobs. | |
| 18 // All operations are O(1) time. The class is fully reentrant: it is safe to | |
| 19 // execute any method (incl. destructor) from within Job callbacks. | |
| 20 class NET_EXPORT_PRIVATE PrioritizedDispatcher { | |
| 21 public: | |
| 22 // Describes the limits for the number of jobs started by the dispatcher. | |
| 23 // For example, |total_jobs| = 30 and |reserved_slots| = { 5, 10, 5, 0, 0 } | |
| 24 // allow for at most 30 running jobs in total. If there are already 24 jobs | |
| 25 // running, then there can be 6 more jobs started of which at most 1 can be | |
| 26 // MEDIUM but the rest has to be HIGHEST. | |
| 27 struct NET_EXPORT_PRIVATE Limits { | |
| 28 // Total allowed running jobs. | |
| 29 size_t total_jobs; | |
| 30 // Number of slots reserved for each priority and higher. | |
| 31 // Sum of |reserved_slots| must be no greater than |total_jobs|. | |
| 32 size_t reserved_slots[NUM_PRIORITIES]; | |
|
cbentzel
2012/01/03 19:39:09
Like PriorityQueue, it feels like this should be d
| |
| 33 }; | |
| 34 | |
| 35 // An interface to the job dispatched by PrioritizedDispatcher. The dispatcher | |
| 36 // does not own the Job but expects it to live as long as the Job is queued. | |
| 37 // Use Cancel to remove Job from queue before it is dispatched (with either | |
| 38 // Start or OnEvicted). The Job can be deleted after it is dispatched, | |
| 39 // canceled or the dispatcher is destroyed. | |
| 40 class Job { | |
| 41 public: | |
| 42 // Note: PriorityDispatch will never delete a Job. | |
| 43 virtual ~Job() {} | |
| 44 // Called when the dispatcher starts the job. Must call OnJobFinished when | |
| 45 // done. | |
| 46 virtual void Start() = 0; | |
| 47 // Called when the job is evicted due to |max_queued|. | |
| 48 virtual void OnEvicted() = 0; | |
| 49 }; | |
| 50 | |
| 51 // A handle to the enqueued job. The handle becomes invalid when the job is | |
| 52 // canceled, updated, evicted, or started. | |
| 53 typedef PriorityQueue<Job*>::Pointer Handle; | |
| 54 | |
| 55 // Creates a dispatcher enforcing |limits| on number of running jobs and | |
| 56 // |max_queued| on queued jobs. | |
| 57 PrioritizedDispatcher(const Limits& limits, size_t max_queued); | |
| 58 | |
| 59 ~PrioritizedDispatcher(); | |
| 60 | |
| 61 size_t num_running_jobs() const { return num_running_jobs_; } | |
| 62 size_t num_queued_jobs() const { return queue_.size(); } | |
| 63 | |
| 64 // Configures |max_queued|. May be called only when queue is empty. | |
|
cbentzel
2012/01/03 19:39:09
Why isn't this part of Limits?
szym
2012/01/03 20:48:52
It makes sense, but note my concern about enforcin
| |
| 65 void SetMaxQueued(size_t max_queued); | |
| 66 | |
| 67 // Returns true iff a new job with |priority| would itself be evicted on Add. | |
| 68 bool WouldEvict(RequestPriority priority) const; | |
|
cbentzel
2012/01/03 19:39:09
From the one caller I saw, it looks like this meth
szym
2012/01/03 20:48:52
We must not call OnEvicted on the Job that is bein
| |
| 69 | |
| 70 // Adds |job| with |priority| to the dispatcher. If limits permit, |job| is | |
| 71 // started immediately. Returns handle to the job or null-handle if the job is | |
| 72 // started or evicted. | |
| 73 Handle Add(Job* job, RequestPriority priority); | |
| 74 | |
| 75 // Removes the job with |handle| from the queue. Invalidates |handle|. | |
| 76 void Cancel(const Handle& handle); | |
|
cbentzel
2012/01/03 19:39:09
I think this needs to be a bit more defensive abou
szym
2012/01/03 20:48:52
Agreed, although I don't really have a better cont
| |
| 77 | |
| 78 // Moves the queued job with |handle| to the end of all values with priority | |
| 79 // |priority| and returns the updated handle, or null-handle if it starts the | |
| 80 // job. Invalidates |handle|. No-op if priority did not change. | |
| 81 Handle Update(const Handle& handle, RequestPriority priority); | |
| 82 | |
| 83 // Notifies the dispatcher that a running job has finished. Could start a job. | |
| 84 void OnJobFinished(); | |
| 85 | |
| 86 private: | |
| 87 // Attempts to dispatch the job with |handle| at priority |priority| (might be | |
| 88 // different than |handle.priority()|. Returns true if successful. If so | |
| 89 // the |handle| becomes invalid. | |
| 90 bool DispatchJob(const Handle& handle, RequestPriority priority); | |
| 91 | |
| 92 // Queue for jobs that need to wait for a spare slot. | |
| 93 PriorityQueue<Job*> queue_; | |
| 94 // Maximum total number of running jobs allowed after a job at a particular | |
| 95 // priority is started. If a greater or equal number of jobs are running, then | |
| 96 // another job cannot be started. | |
| 97 size_t max_running_jobs_[NUM_PRIORITIES]; | |
| 98 // Total number of running jobs. | |
| 99 size_t num_running_jobs_; | |
| 100 // Maximum number of jobs in |queue_|. | |
| 101 size_t max_queued_jobs_; | |
| 102 | |
| 103 DISALLOW_COPY_AND_ASSIGN(PrioritizedDispatcher); | |
| 104 }; | |
| 105 | |
| 106 } // namespace net | |
| 107 | |
| 108 #endif // NET_BASE_PRIORITY_DISPATCH_H_ | |
| 109 | |
| OLD | NEW |