Chromium Code Reviews| Index: net/base/priority_dispatch.h |
| diff --git a/net/base/priority_dispatch.h b/net/base/priority_dispatch.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b4013f650a8e8f7b0b7cc489d701312e6b62b8d9 |
| --- /dev/null |
| +++ b/net/base/priority_dispatch.h |
| @@ -0,0 +1,98 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef NET_BASE_PRIORITY_DISPATCH_H_ |
| +#define NET_BASE_PRIORITY_DISPATCH_H_ |
| +#pragma once |
| + |
| +#include "net/base/net_export.h" |
| +#include "net/base/priority_queue.h" |
| +#include "net/base/request_priority.h" |
| + |
| +namespace net { |
| + |
| +// A priority-based dispatcher of jobs. The dispatcher enforces limits on |
| +// the number of running jobs. It never revokes a job once started. The job |
| +// must call OnJobFinished once it finishes in order to dispatch further jobs. |
| +// All operations are O(1) time. The class is fully reentrant: it is safe to |
| +// execute any method (incl. destructor) from within Job callbacks. |
| +class NET_EXPORT_PRIVATE PriorityDispatch { |
| + public: |
| + // Describes the number of slots reserved for each priority (and higher). |
| + // For example, with |reserved_slots| = { 5, 10, 5, 0, 0 }, there can be at |
| + // most 20 running jobs in total. If there are already 14 jobs running, then |
| + // there can be 6 more jobs started of which at most 1 can be MEDIUM but the |
| + // rest has to be HIGHEST. { 0, 0, 0, 0, 100 } allows up to 100 running jobs |
| + // regardless of priority. |
| + struct NET_EXPORT_PRIVATE Limits { |
| + // Constructs Limits that permit |max_idle| jobs regardless of priority. |
| + static Limits MakeAny(size_t max_idle); |
| + // Sum of all |reserved_slots|. |
| + size_t Total() const; |
| + size_t reserved_slots[NUM_PRIORITIES]; |
| + }; |
| + |
| + // An interface to the job dispatched by PriorityDispatch. A job enqueued in |
| + // 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
|
| + // Cancel to remove job from queue. |
| + class Job { |
| + public: |
| + // Note: PriorityDispatch will never delete a Job. |
| + virtual ~Job() {} |
| + // Called when the dispatcher starts the job. Must call OnJobFinished when |
| + // done. |
| + virtual void Start() = 0; |
| + // Called when the job is evicted due to |max_queued|. |
| + virtual void OnEvicted() = 0; |
| + }; |
|
mmenke
2011/12/21 16:22:58
nit: Please add a linebreak here.
szym
2011/12/28 01:24:10
Done.
|
| + // A handle to the enqueued job. The handle becomes invalid when the job is |
| + // canceled, updated, evicted, or started. |
| + typedef PriorityQueue<Job*>::Pointer Handle; |
| + |
| + // Creates a dispatcher enforcing |limits| on number of running jobs and |
| + // |max_queued| on queued jobs. |
| + PriorityDispatch(const Limits& limits, size_t max_queued); |
| + |
| + ~PriorityDispatch(); |
| + |
| + size_t num_running_jobs() const { return num_running_jobs_; } |
| + size_t num_queued_jobs() const { return queue_.size(); } |
| + |
| + // Configures |max_queued|. Allowed only when queue is empty. |
| + void SetMaxQueued(size_t max_queued); |
| + |
| + // Adds |job| with |priority| to the dispatcher. If limits permit, |job| is |
| + // started immediately. Returns handle to the job or null-handle if the job is |
| + // started or evicted. |
| + Handle Add(Job* job, RequestPriority priority); |
| + |
| + // Removes the job at |handle| from the queue. |
| + void Cancel(const Handle& handle); |
| + |
| + // Moves the queued job at |handle| to the end of all values with priority |
| + // |priority| and returns the updated handle or null-handle if the job is |
| + // started. |
| + Handle Update(const Handle& handle, RequestPriority priority); |
| + |
| + // Notifies the dispatcher that a running job has finished. Could start a job. |
| + void OnJobFinished(); |
| + |
| + private: |
| + // Queue for jobs that need to wait for a spare slot. |
| + PriorityQueue<Job*> queue_; |
| + // Maximum total number of running jobs permitting to start a new job at each |
| + // priority. |
| + size_t max_running_jobs_[NUM_PRIORITIES]; |
| + // Total number of running jobs. |
| + size_t num_running_jobs_; |
| + // Maximum number of jobs in |queue_|. |
| + size_t max_queued_jobs_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PriorityDispatch); |
| +}; |
| + |
| +} // namespace net |
| + |
| +#endif // NET_BASE_PRIORITY_DISPATCH_H_ |
| + |