| 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..17c203d4246b2d5a664499b42132ee35b854516c
|
| --- /dev/null
|
| +++ b/base/task_scheduler/priority_queue.cc
|
| @@ -0,0 +1,82 @@
|
| +// 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 "base/logging.h"
|
| +#include "base/task_scheduler/sequence_sort_key.h"
|
| +
|
| +namespace base {
|
| +namespace internal {
|
| +
|
| +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());
|
| +}
|
| +
|
| +PriorityQueue::~PriorityQueue() = default;
|
| +
|
| +bool PriorityQueue::UnsynchronizedEmpty() const {
|
| + return container_.empty();
|
| +}
|
| +
|
| +PriorityQueue::Transaction::~Transaction() {
|
| + DCHECK(CalledOnValidThread());
|
| + auto_lock_.reset();
|
| + for (size_t i = 0; i < num_pushed_sequences_; ++i)
|
| + priority_queue_->sequence_inserted_callback_.Run();
|
| +}
|
| +
|
| +void PriorityQueue::Transaction::PushSequence(scoped_refptr<Sequence> sequence,
|
| + const SequenceSortKey& sort_key) {
|
| + DCHECK(CalledOnValidThread());
|
| + priority_queue_->container_.push(
|
| + std::make_pair(std::move(sequence), sort_key));
|
| + ++num_pushed_sequences_;
|
| +}
|
| +
|
| +scoped_refptr<Sequence> PriorityQueue::Transaction::PeekSequence(
|
| + SequenceSortKey* sort_key) const {
|
| + DCHECK(CalledOnValidThread());
|
| + DCHECK(sort_key);
|
| +
|
| + if (priority_queue_->container_.empty())
|
| + return scoped_refptr<Sequence>();
|
| +
|
| + *sort_key = priority_queue_->container_.top().second;
|
| + return priority_queue_->container_.top().first;
|
| +}
|
| +
|
| +void PriorityQueue::Transaction::PopSequence() {
|
| + DCHECK(CalledOnValidThread());
|
| + DCHECK(!priority_queue_->container_.empty());
|
| + priority_queue_->container_.pop();
|
| +}
|
| +
|
| +PriorityQueue::Transaction::Transaction(PriorityQueue* priority_queue)
|
| + : priority_queue_(priority_queue),
|
| + auto_lock_(new AutoSchedulerLock(priority_queue->lock_)),
|
| + num_pushed_sequences_(0) {
|
| + DCHECK(CalledOnValidThread());
|
| +}
|
| +
|
| +scoped_ptr<PriorityQueue::Transaction> PriorityQueue::BeginTransaction() {
|
| + return make_scoped_ptr(new Transaction(this));
|
| +}
|
| +
|
| +bool PriorityQueue::SequenceAndSortKeyPairComparator::operator()(
|
| + const SequenceAndSortKeyPair& left,
|
| + const SequenceAndSortKeyPair& right) const {
|
| + return left.second < right.second;
|
| +}
|
| +
|
| +} // namespace internal
|
| +} // namespace base
|
|
|