| Index: base/task_scheduler/priority_queue.cc
|
| diff --git a/base/task_scheduler/priority_queue.cc b/base/task_scheduler/priority_queue.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..e937e65323d9f221480c6b308494af55e0b3ffc4
|
| --- /dev/null
|
| +++ b/base/task_scheduler/priority_queue.cc
|
| @@ -0,0 +1,97 @@
|
| +// 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.
|
| +
|
| +#include "base/task_scheduler/priority_queue.h"
|
| +
|
| +#include <utility>
|
| +
|
| +#include "base/logging.h"
|
| +
|
| +namespace base {
|
| +namespace internal {
|
| +
|
| +PriorityQueue::SequenceAndSortKey::SequenceAndSortKey()
|
| + : sort_key(TaskPriority::LOWEST, TimeTicks()) {}
|
| +
|
| +PriorityQueue::SequenceAndSortKey::SequenceAndSortKey(
|
| + scoped_refptr<Sequence> sequence,
|
| + SequenceSortKey sort_key)
|
| + : sequence(sequence), sort_key(sort_key) {}
|
| +
|
| +PriorityQueue::SequenceAndSortKey::~SequenceAndSortKey() = default;
|
| +
|
| +bool PriorityQueue::SequenceAndSortKey::is_null() const {
|
| + return sequence.get() == nullptr;
|
| +}
|
| +
|
| +PriorityQueue::PriorityQueue(const Closure& sequence_inserted_callback)
|
| + : sequence_inserted_callback_(sequence_inserted_callback) {
|
| + DCHECK(!sequence_inserted_callback_.is_null());
|
| +}
|
| +
|
| +PriorityQueue::PriorityQueue(const Closure& sequence_inserted_callback,
|
| + PriorityQueue* predecessor_priority_queue)
|
| + : lock_(&predecessor_priority_queue->lock_),
|
| + sequence_inserted_callback_(sequence_inserted_callback) {
|
| + DCHECK(!sequence_inserted_callback_.is_null());
|
| + DCHECK(predecessor_priority_queue);
|
| +}
|
| +
|
| +PriorityQueue::~PriorityQueue() = default;
|
| +
|
| +PriorityQueue::Transaction::~Transaction() {
|
| + DCHECK(CalledOnValidThread());
|
| +
|
| + // Run the sequence insertion callback once for each sequence that was
|
| + // inserted in the PriorityQueue during the lifetime of this Transaction.
|
| + // Perform this outside the scope of PriorityQueue's lock to avoid imposing an
|
| + // unnecessary lock dependency on |sequence_inserted_callback_|'s destination.
|
| + auto_lock_.reset();
|
| + for (size_t i = 0; i < num_pushed_sequences_; ++i)
|
| + outer_->sequence_inserted_callback_.Run();
|
| +}
|
| +
|
| +void PriorityQueue::Transaction::Push(
|
| + scoped_ptr<SequenceAndSortKey> sequence_and_sort_key) {
|
| + DCHECK(CalledOnValidThread());
|
| + DCHECK(!sequence_and_sort_key->is_null());
|
| +
|
| + outer_->container_.push(std::move(sequence_and_sort_key));
|
| + ++num_pushed_sequences_;
|
| +}
|
| +
|
| +PriorityQueue::SequenceAndSortKey PriorityQueue::Transaction::Peek() const {
|
| + DCHECK(CalledOnValidThread());
|
| +
|
| + if (outer_->container_.empty())
|
| + return SequenceAndSortKey();
|
| +
|
| + return *outer_->container_.top();
|
| +}
|
| +
|
| +void PriorityQueue::Transaction::Pop() {
|
| + DCHECK(CalledOnValidThread());
|
| + DCHECK(!outer_->container_.empty());
|
| + outer_->container_.pop();
|
| +}
|
| +
|
| +PriorityQueue::Transaction::Transaction(PriorityQueue* outer)
|
| + : outer_(outer),
|
| + auto_lock_(new AutoSchedulerLock(outer_->lock_)),
|
| + num_pushed_sequences_(0) {
|
| + DCHECK(CalledOnValidThread());
|
| +}
|
| +
|
| +scoped_ptr<PriorityQueue::Transaction> PriorityQueue::BeginTransaction() {
|
| + return make_scoped_ptr(new Transaction(this));
|
| +}
|
| +
|
| +bool PriorityQueue::SequenceAndSortKeyComparator::operator()(
|
| + const scoped_ptr<SequenceAndSortKey>& left,
|
| + const scoped_ptr<SequenceAndSortKey>& right) const {
|
| + return left->sort_key < right->sort_key;
|
| +}
|
| +
|
| +} // namespace internal
|
| +} // namespace base
|
|
|