| Index: base/task_scheduler/priority_queue.h
|
| diff --git a/base/task_scheduler/priority_queue.h b/base/task_scheduler/priority_queue.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..aa337a49a52a54178aaa6416c0dc806ae49a90a4
|
| --- /dev/null
|
| +++ b/base/task_scheduler/priority_queue.h
|
| @@ -0,0 +1,124 @@
|
| +// Copyright 2016 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 BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_
|
| +#define BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_
|
| +
|
| +#include <queue>
|
| +#include <vector>
|
| +
|
| +#include "base/base_export.h"
|
| +#include "base/callback.h"
|
| +#include "base/macros.h"
|
| +#include "base/memory/ref_counted.h"
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "base/task_scheduler/scheduler_lock.h"
|
| +#include "base/task_scheduler/sequence.h"
|
| +#include "base/task_scheduler/sequence_sort_key.h"
|
| +#include "base/threading/non_thread_safe.h"
|
| +
|
| +namespace base {
|
| +namespace internal {
|
| +
|
| +// A PriorityQueue holds Sequences of Tasks. This class is thread-safe.
|
| +class BASE_EXPORT PriorityQueue {
|
| + public:
|
| + // An immutable struct combining a Sequence and the sort key that determines
|
| + // its position in a PriorityQueue.
|
| + struct SequenceAndSortKey {
|
| + // Constructs a null SequenceAndSortKey.
|
| + SequenceAndSortKey();
|
| +
|
| + // Constructs a SequenceAndSortKey with the given |sequence| and |sort_key|.
|
| + SequenceAndSortKey(scoped_refptr<Sequence> sequence,
|
| + SequenceSortKey sort_key);
|
| +
|
| + ~SequenceAndSortKey();
|
| +
|
| + // Returns true if this is a null SequenceAndSortKey.
|
| + bool is_null() const;
|
| +
|
| + const scoped_refptr<Sequence> sequence;
|
| + const SequenceSortKey sort_key;
|
| + };
|
| +
|
| + // |sequence_inserted_callback| is a non-null callback invoked when a sequence
|
| + // is added to the PriorityQueue.
|
| + explicit PriorityQueue(const Closure& sequence_inserted_callback);
|
| +
|
| + // |sequence_inserted_callback| is a non-null callback invoked when a sequence
|
| + // is added to the PriorityQueue. |predecessor_priority_queue| is a
|
| + // PriorityQueue for which a thread is allowed to have an active Transaction
|
| + // when it creates a Transaction for this PriorityQueue.
|
| + PriorityQueue(const Closure& sequence_inserted_callback,
|
| + PriorityQueue* predecessor_priority_queue);
|
| +
|
| + ~PriorityQueue();
|
| +
|
| + class Transaction : public NonThreadSafe {
|
| + public:
|
| + ~Transaction();
|
| +
|
| + // Inserts |sequence_and_sort_key| in the PriorityQueue.
|
| + void Push(scoped_ptr<SequenceAndSortKey> sequence_and_sort_key);
|
| +
|
| + // Returns the SequenceAndSortKey with the highest priority or a null
|
| + // SequenceAndSortKey if the PriorityQueue is empty.
|
| + SequenceAndSortKey Peek() const;
|
| +
|
| + // Removes the SequenceAndSortKey with the highest priority from the
|
| + // PriorityQueue. Cannot be called on an empty PriorityQueue.
|
| + void Pop();
|
| +
|
| + private:
|
| + friend class PriorityQueue;
|
| +
|
| + explicit Transaction(PriorityQueue* outer);
|
| +
|
| + PriorityQueue* const outer_;
|
| +
|
| + // Holds the lock of |outer_| for most of the lifetime of this Transaction.
|
| + // Using a scoped_ptr allows the destructor to release the lock before
|
| + // performing internal operations which have to be done outside of its
|
| + // scope.
|
| + scoped_ptr<AutoSchedulerLock> auto_lock_;
|
| +
|
| + // Number of times that Push() has been called on this Transaction.
|
| + size_t num_pushed_sequences_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(Transaction);
|
| + };
|
| +
|
| + // Begins a Transaction which allows multiple operations to be performed
|
| + // atomically on the PriorityQueue. It is guaranteed that nothing else will
|
| + // modify the PriorityQueue while the returned Transaction is alive. This
|
| + // method cannot be called on a thread which has an active Transaction unless
|
| + // the last Transaction created on the thread was for the allowed predecessor
|
| + // specified in the constructor of this PriorityQueue.
|
| + scoped_ptr<Transaction> BeginTransaction();
|
| +
|
| + private:
|
| + struct SequenceAndSortKeyComparator {
|
| + bool operator()(const scoped_ptr<SequenceAndSortKey>& left,
|
| + const scoped_ptr<SequenceAndSortKey>& right) const;
|
| + };
|
| + using ContainerType =
|
| + std::priority_queue<scoped_ptr<SequenceAndSortKey>,
|
| + std::vector<scoped_ptr<SequenceAndSortKey>>,
|
| + SequenceAndSortKeyComparator>;
|
| +
|
| + // Synchronizes access to |container_|.
|
| + SchedulerLock lock_;
|
| +
|
| + ContainerType container_;
|
| +
|
| + const Closure sequence_inserted_callback_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(PriorityQueue);
|
| +};
|
| +
|
| +} // namespace internal
|
| +} // namespace base
|
| +
|
| +#endif // BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_
|
|
|