Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_BASE_PRIORITY_DISPATCH_H_ | 5 #ifndef NET_BASE_PRIORITY_DISPATCH_H_ |
| 6 #define NET_BASE_PRIORITY_DISPATCH_H_ | 6 #define NET_BASE_PRIORITY_DISPATCH_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "net/base/net_export.h" | 11 #include "net/base/net_export.h" |
| 12 #include "net/base/priority_queue.h" | 12 #include "net/base/priority_queue.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 // A priority-based dispatcher of jobs. Dispatch order is by priority (lowest | 16 // A priority-based dispatcher of jobs. Dispatch order is by priority (lowest |
|
stevez
2012/04/11 16:13:58
If it's possible to reverse the decision of lowest
szym
2012/05/14 22:56:02
Took some work, but done.
| |
| 17 // first) and then FIFO. The dispatcher enforces limits on the number of running | 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 | 18 // jobs. It never revokes a job once started. The job must call OnJobFinished |
| 19 // once it finishes in order to dispatch further jobs. | 19 // once it finishes in order to dispatch further jobs. |
| 20 // | 20 // |
| 21 // All operations are O(p) time for p priority levels. The class is fully | 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 | 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 | 23 // Job callbacks. However, this class is NOT thread-safe, which is enforced |
|
stevez
2012/04/11 16:13:58
It's not clear what all this means.
Perhaps give
szym
2012/05/14 22:56:02
That's not allowed. The class is NOT thread-safe.
| |
| 24 // by the underlying non-thread-safe PriorityQueue. | 24 // by the underlying non-thread-safe PriorityQueue. |
| 25 // | 25 // |
| 26 class NET_EXPORT_PRIVATE PrioritizedDispatcher { | 26 class NET_EXPORT_PRIVATE PrioritizedDispatcher { |
| 27 public: | 27 public: |
| 28 class Job; | 28 class Job; |
| 29 typedef PriorityQueue<Job*>::Priority Priority; | 29 typedef PriorityQueue<Job*>::Priority Priority; |
| 30 | 30 |
| 31 // Describes the limits for the number of jobs started by the dispatcher. | 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 } | 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 | 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 | 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. | 35 // at priority 1 or 0, but the rest has to be at 0. |
|
stevez
2012/04/11 16:13:58
Can you explain this comment more, it doesn't make
szym
2012/05/14 22:56:02
There's no typo, I updated the comment after prior
stevez
2012/05/17 17:57:07
The typo was 'at priority 1 or 0' -> 'at priority
szym
2012/05/17 19:52:01
That was not a typo. That one slot could be used b
| |
| 36 struct NET_EXPORT_PRIVATE Limits { | 36 struct NET_EXPORT_PRIVATE Limits { |
| 37 Limits(Priority num_priorities, size_t total_jobs); | 37 Limits(Priority num_priorities, size_t total_jobs); |
| 38 ~Limits(); | 38 ~Limits(); |
| 39 | 39 |
| 40 // Total allowed running jobs. | 40 // Total allowed running jobs. |
| 41 size_t total_jobs; | 41 size_t total_jobs; |
| 42 // Number of slots reserved for each priority and higher. | 42 // Number of slots reserved for each priority and higher. |
| 43 // Sum of |reserved_slots| must be no greater than |total_jobs|. | 43 // Sum of |reserved_slots| must be no greater than |total_jobs|. |
| 44 std::vector<size_t> reserved_slots; | 44 std::vector<size_t> reserved_slots; |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 // An interface to the job dispatched by PrioritizedDispatcher. The dispatcher | 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. | 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 | 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. | 50 // deleted after it is dispatched or canceled, or the dispatcher is destroyed. |
| 51 class Job { | 51 class Job { |
| 52 public: | 52 public: |
| 53 // Note: PriorityDispatch will never delete a Job. | 53 // Note: PriorityDispatch will never delete a Job. |
|
stevez
2012/04/11 16:13:58
What is PriorityDispatch?
szym
2012/05/14 22:56:02
Done.
| |
| 54 virtual ~Job() {} | 54 virtual ~Job() {} |
| 55 // Called when the dispatcher starts the job. Must call OnJobFinished when | 55 // Called when the dispatcher starts the job. Must call OnJobFinished when |
| 56 // done. | 56 // done. |
|
stevez
2012/04/11 16:13:58
When done what? done starting?
szym
2012/05/14 22:56:02
Fixed wording.
| |
| 57 virtual void Start() = 0; | 57 virtual void Start() = 0; |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 // A handle to the enqueued job. The handle becomes invalid when the job is | 60 // A handle to the enqueued job. The handle becomes invalid when the job is |
| 61 // canceled, updated, or started. | 61 // canceled, updated, or started. |
| 62 typedef PriorityQueue<Job*>::Pointer Handle; | 62 typedef PriorityQueue<Job*>::Pointer Handle; |
| 63 | 63 |
| 64 // Creates a dispatcher enforcing |limits| on number of running jobs. | 64 // Creates a dispatcher enforcing |limits| on number of running jobs. |
| 65 PrioritizedDispatcher(const Limits& limits); | 65 PrioritizedDispatcher(const Limits& limits); |
|
stevez
2012/04/11 16:13:58
Single arg constructors must be declared explicit.
szym
2012/05/14 22:56:02
Done.
| |
| 66 | 66 |
| 67 ~PrioritizedDispatcher(); | 67 ~PrioritizedDispatcher(); |
| 68 | 68 |
| 69 size_t num_running_jobs() const { return num_running_jobs_; } | 69 size_t num_running_jobs() const { return num_running_jobs_; } |
| 70 size_t num_queued_jobs() const { return queue_.size(); } | 70 size_t num_queued_jobs() const { return queue_.size(); } |
| 71 size_t num_priorities() const { return max_running_jobs_.size(); } | 71 size_t num_priorities() const { return max_running_jobs_.size(); } |
| 72 | 72 |
| 73 // Adds |job| with |priority| to the dispatcher. If limits permit, |job| is | 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 | 74 // started immediately. Returns handle to the job or null-handle if the job is |
| 75 // started. | 75 // started. |
| 76 Handle Add(Job* job, Priority priority); | 76 Handle Add(Job* job, Priority priority); |
|
stevez
2012/04/11 16:13:58
Document ownership semantics when passing mutable
szym
2012/05/14 22:56:02
Done.
| |
| 77 | 77 |
| 78 // Removes the job with |handle| from the queue. Invalidates |handle|. | 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. | 79 // Note: a Handle is valid iff the job is in the queue, i.e. has not Started. |
| 80 void Cancel(const Handle& handle); | 80 void Cancel(const Handle& handle); |
| 81 | 81 |
| 82 // Removes and returns the oldest-lowest Job from the queue invalidating any | 82 // Removes and returns the oldest-lowest Job from the queue invalidating any |
| 83 // handles to it. Returns NULL if the queue is empty. | 83 // handles to it. Returns NULL if the queue is empty. |
| 84 Job* EvictOldestLowest(); | 84 Job* EvictOldestLowest(); |
| 85 | 85 |
| 86 // Moves the queued job with |handle| to the end of all values with priority | 86 // Moves the queued job with |handle| to the end of all values with priority |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 106 // Total number of running jobs. | 106 // Total number of running jobs. |
| 107 size_t num_running_jobs_; | 107 size_t num_running_jobs_; |
| 108 | 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(PrioritizedDispatcher); | 109 DISALLOW_COPY_AND_ASSIGN(PrioritizedDispatcher); |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 } // namespace net | 112 } // namespace net |
| 113 | 113 |
| 114 #endif // NET_BASE_PRIORITY_DISPATCH_H_ | 114 #endif // NET_BASE_PRIORITY_DISPATCH_H_ |
| 115 | 115 |
| 116 | |
| OLD | NEW |