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 PriorityDispatch { | |
21 public: | |
22 // Describes the number of slots reserved for each priority (and higher). | |
23 // For example, with |reserved_slots| = { 5, 10, 5, 0, 0 }, there can be at | |
24 // most 20 running jobs in total. If there are already 14 jobs running, then | |
25 // there can be 6 more jobs started of which at most 1 can be MEDIUM but the | |
26 // rest has to be HIGHEST. { 0, 0, 0, 0, 100 } allows up to 100 running jobs | |
27 // regardless of priority. | |
28 struct NET_EXPORT_PRIVATE Limits { | |
29 // Constructs Limits that permit |max_idle| jobs regardless of priority. | |
30 static Limits MakeAny(size_t max_idle); | |
31 // Sum of all |reserved_slots|. | |
32 size_t Total() const; | |
33 size_t reserved_slots[NUM_PRIORITIES]; | |
34 }; | |
35 | |
36 // An interface to the job dispatched by PriorityDispatch. A job enqueued in | |
37 // the dispatcher is not owned by the dispatcher but must outlive it. Use | |
mmenke
2011/12/21 16:22:58
I think you mean must not outlive it. Or must not
szym
2011/12/21 22:19:34
I wasn't clear: the dispatcher expects the Job to
| |
38 // Cancel to remove job from queue. | |
39 class Job { | |
40 public: | |
41 // Note: PriorityDispatch will never delete a Job. | |
42 virtual ~Job() {} | |
43 // Called when the dispatcher starts the job. Must call OnJobFinished when | |
44 // done. | |
45 virtual void Start() = 0; | |
46 // Called when the job is evicted due to |max_queued|. | |
47 virtual void OnEvicted() = 0; | |
48 }; | |
mmenke
2011/12/21 16:22:58
nit: Please add a linebreak here.
szym
2011/12/28 01:24:10
Done.
| |
49 // A handle to the enqueued job. The handle becomes invalid when the job is | |
50 // canceled, updated, evicted, or started. | |
51 typedef PriorityQueue<Job*>::Pointer Handle; | |
52 | |
53 // Creates a dispatcher enforcing |limits| on number of running jobs and | |
54 // |max_queued| on queued jobs. | |
55 PriorityDispatch(const Limits& limits, size_t max_queued); | |
56 | |
57 ~PriorityDispatch(); | |
58 | |
59 size_t num_running_jobs() const { return num_running_jobs_; } | |
60 size_t num_queued_jobs() const { return queue_.size(); } | |
61 | |
62 // Configures |max_queued|. Allowed only when queue is empty. | |
63 void SetMaxQueued(size_t max_queued); | |
64 | |
65 // Adds |job| with |priority| to the dispatcher. If limits permit, |job| is | |
66 // started immediately. Returns handle to the job or null-handle if the job is | |
67 // started or evicted. | |
68 Handle Add(Job* job, RequestPriority priority); | |
69 | |
70 // Removes the job at |handle| from the queue. | |
71 void Cancel(const Handle& handle); | |
72 | |
73 // Moves the queued job at |handle| to the end of all values with priority | |
74 // |priority| and returns the updated handle or null-handle if the job is | |
75 // started. | |
76 Handle Update(const Handle& handle, RequestPriority priority); | |
77 | |
78 // Notifies the dispatcher that a running job has finished. Could start a job. | |
79 void OnJobFinished(); | |
80 | |
81 private: | |
82 // Queue for jobs that need to wait for a spare slot. | |
83 PriorityQueue<Job*> queue_; | |
84 // Maximum total number of running jobs permitting to start a new job at each | |
85 // priority. | |
86 size_t max_running_jobs_[NUM_PRIORITIES]; | |
87 // Total number of running jobs. | |
88 size_t num_running_jobs_; | |
89 // Maximum number of jobs in |queue_|. | |
90 size_t max_queued_jobs_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(PriorityDispatch); | |
93 }; | |
94 | |
95 } // namespace net | |
96 | |
97 #endif // NET_BASE_PRIORITY_DISPATCH_H_ | |
98 | |
OLD | NEW |