Chromium Code Reviews| 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..071df14901790f700413c7bdfb9a6f9c781da1a5 |
| --- /dev/null |
| +++ b/base/task_scheduler/priority_queue.cc |
| @@ -0,0 +1,74 @@ |
| +// 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 task_scheduler { |
| + |
| +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() { |
| + 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) { |
| + 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 { |
|
fdoray
2016/02/11 17:30:33
DCHECK(sort_key);
fdoray
2016/02/12 04:16:19
Done.
|
| + 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(!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) {} |
| + |
| +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 task_scheduler |
| +} // namespace base |