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..180371f649adf7462f8fc38b12ed09af01ee9edf |
| --- /dev/null |
| +++ b/base/task_scheduler/priority_queue.cc |
| @@ -0,0 +1,95 @@ |
| +// 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) {} |
|
danakj
2016/03/17 21:32:14
move the sequence
fdoray
2016/03/18 16:06:55
Done.
|
| + |
| +PriorityQueue::SequenceAndSortKey::~SequenceAndSortKey() = default; |
| + |
| +bool PriorityQueue::SequenceAndSortKey::is_null() const { |
|
danakj
2016/03/17 21:32:14
hacker_style methods should be inline. otherwise i
fdoray
2016/03/18 16:06:55
Done.
|
| + return sequence.get() == nullptr; |
|
danakj
2016/03/17 21:32:14
fwiw, return !!sequence also works, either way.
fdoray
2016/03/18 16:06:55
Done.
|
| +} |
| + |
| +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, |
| + const 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 { |
|
danakj
2016/03/17 21:32:14
why not return a const reference?
fdoray
2016/03/18 16:06:55
I changed the code to return a const reference. Ho
robliao
2016/03/18 17:11:55
Is the cost of having the Sequence with the sort k
danakj
2016/03/18 17:44:17
If you look at container_.top(), it returns a cons
fdoray
2016/03/18 19:35:28
OK, makes sense. In the latest patch set, Peek() r
|
| + 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) |
|
danakj
2016/03/17 21:32:14
can you put this constructor above the destructor
fdoray
2016/03/18 16:06:55
Done.
|
| + : outer_(outer), auto_lock_(new AutoSchedulerLock(outer_->lock_)) { |
| + 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; |
|
danakj
2016/03/17 21:32:14
i think you could inline this in the header
fdoray
2016/03/18 16:06:55
Done.
|
| +} |
| + |
| +} // namespace internal |
| +} // namespace base |