OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/task_scheduler/priority_queue.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/logging.h" |
| 10 |
| 11 namespace base { |
| 12 namespace internal { |
| 13 |
| 14 PriorityQueue::SequenceAndSortKey::SequenceAndSortKey() |
| 15 : sort_key(TaskPriority::LOWEST, TimeTicks()) {} |
| 16 |
| 17 PriorityQueue::SequenceAndSortKey::SequenceAndSortKey( |
| 18 scoped_refptr<Sequence> sequence, |
| 19 SequenceSortKey sort_key) |
| 20 : sequence(sequence), sort_key(sort_key) {} |
| 21 |
| 22 PriorityQueue::SequenceAndSortKey::~SequenceAndSortKey() = default; |
| 23 |
| 24 bool PriorityQueue::SequenceAndSortKey::is_null() const { |
| 25 return sequence.get() == nullptr; |
| 26 } |
| 27 |
| 28 PriorityQueue::PriorityQueue(const Closure& sequence_inserted_callback) |
| 29 : sequence_inserted_callback_(sequence_inserted_callback) { |
| 30 DCHECK(!sequence_inserted_callback_.is_null()); |
| 31 } |
| 32 |
| 33 PriorityQueue::PriorityQueue(const Closure& sequence_inserted_callback, |
| 34 PriorityQueue* predecessor_priority_queue) |
| 35 : lock_(&predecessor_priority_queue->lock_), |
| 36 sequence_inserted_callback_(sequence_inserted_callback) { |
| 37 DCHECK(!sequence_inserted_callback_.is_null()); |
| 38 DCHECK(predecessor_priority_queue); |
| 39 } |
| 40 |
| 41 PriorityQueue::~PriorityQueue() = default; |
| 42 |
| 43 PriorityQueue::Transaction::~Transaction() { |
| 44 DCHECK(CalledOnValidThread()); |
| 45 |
| 46 // Run the sequence insertion callback once for each sequence that was |
| 47 // inserted in the PriorityQueue during the lifetime of this Transaction. |
| 48 // Perform this outside the scope of PriorityQueue's lock to avoid imposing an |
| 49 // unnecessary lock dependency on |sequence_inserted_callback_|'s destination. |
| 50 auto_lock_.reset(); |
| 51 for (size_t i = 0; i < num_pushed_sequences_; ++i) |
| 52 outer_->sequence_inserted_callback_.Run(); |
| 53 } |
| 54 |
| 55 void PriorityQueue::Transaction::Push( |
| 56 scoped_ptr<SequenceAndSortKey> sequence_and_sort_key) { |
| 57 DCHECK(CalledOnValidThread()); |
| 58 DCHECK(!sequence_and_sort_key->is_null()); |
| 59 |
| 60 outer_->container_.push(std::move(sequence_and_sort_key)); |
| 61 ++num_pushed_sequences_; |
| 62 } |
| 63 |
| 64 PriorityQueue::SequenceAndSortKey PriorityQueue::Transaction::Peek() const { |
| 65 DCHECK(CalledOnValidThread()); |
| 66 |
| 67 if (outer_->container_.empty()) |
| 68 return SequenceAndSortKey(); |
| 69 |
| 70 return *outer_->container_.top(); |
| 71 } |
| 72 |
| 73 void PriorityQueue::Transaction::Pop() { |
| 74 DCHECK(CalledOnValidThread()); |
| 75 DCHECK(!outer_->container_.empty()); |
| 76 outer_->container_.pop(); |
| 77 } |
| 78 |
| 79 PriorityQueue::Transaction::Transaction(PriorityQueue* outer) |
| 80 : outer_(outer), |
| 81 auto_lock_(new AutoSchedulerLock(outer_->lock_)), |
| 82 num_pushed_sequences_(0) { |
| 83 DCHECK(CalledOnValidThread()); |
| 84 } |
| 85 |
| 86 scoped_ptr<PriorityQueue::Transaction> PriorityQueue::BeginTransaction() { |
| 87 return make_scoped_ptr(new Transaction(this)); |
| 88 } |
| 89 |
| 90 bool PriorityQueue::SequenceAndSortKeyComparator::operator()( |
| 91 const scoped_ptr<SequenceAndSortKey>& left, |
| 92 const scoped_ptr<SequenceAndSortKey>& right) const { |
| 93 return left->sort_key < right->sort_key; |
| 94 } |
| 95 |
| 96 } // namespace internal |
| 97 } // namespace base |
OLD | NEW |