Chromium Code Reviews| 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 "base/logging.h" | |
| 8 #include "base/task_scheduler/sequence_sort_key.h" | |
| 9 | |
| 10 namespace base { | |
| 11 namespace task_scheduler { | |
| 12 | |
| 13 PriorityQueue::PriorityQueue(const Closure& sequence_inserted_callback) | |
| 14 : sequence_inserted_callback_(sequence_inserted_callback) { | |
| 15 DCHECK(!sequence_inserted_callback_.is_null()); | |
| 16 } | |
| 17 | |
| 18 PriorityQueue::PriorityQueue(const Closure& sequence_inserted_callback, | |
| 19 PriorityQueue* predecessor_priority_queue) | |
| 20 : lock_(&predecessor_priority_queue->lock_), | |
| 21 sequence_inserted_callback_(sequence_inserted_callback) { | |
| 22 DCHECK(!sequence_inserted_callback_.is_null()); | |
| 23 } | |
| 24 | |
| 25 PriorityQueue::~PriorityQueue() = default; | |
| 26 | |
| 27 bool PriorityQueue::UnsynchronizedEmpty() const { | |
| 28 return container_.empty(); | |
| 29 } | |
| 30 | |
| 31 PriorityQueue::Transaction::~Transaction() { | |
| 32 auto_lock_.reset(); | |
| 33 for (size_t i = 0; i < num_pushed_sequences_; ++i) | |
| 34 priority_queue_->sequence_inserted_callback_.Run(); | |
| 35 } | |
| 36 | |
| 37 void PriorityQueue::Transaction::PushSequence(scoped_refptr<Sequence> sequence, | |
| 38 const SequenceSortKey& sort_key) { | |
| 39 priority_queue_->container_.push( | |
| 40 std::make_pair(std::move(sequence), sort_key)); | |
| 41 ++num_pushed_sequences_; | |
| 42 } | |
| 43 | |
| 44 scoped_refptr<Sequence> PriorityQueue::Transaction::PeekSequence( | |
| 45 SequenceSortKey* sort_key) const { | |
|
fdoray
2016/02/11 17:30:33
DCHECK(sort_key);
fdoray
2016/02/12 04:16:19
Done.
| |
| 46 if (priority_queue_->container_.empty()) | |
| 47 return scoped_refptr<Sequence>(); | |
| 48 | |
| 49 *sort_key = priority_queue_->container_.top().second; | |
| 50 return priority_queue_->container_.top().first; | |
| 51 } | |
| 52 | |
| 53 void PriorityQueue::Transaction::PopSequence() { | |
| 54 DCHECK(!priority_queue_->container_.empty()); | |
| 55 priority_queue_->container_.pop(); | |
| 56 } | |
| 57 | |
| 58 PriorityQueue::Transaction::Transaction(PriorityQueue* priority_queue) | |
| 59 : priority_queue_(priority_queue), | |
| 60 auto_lock_(new AutoSchedulerLock(priority_queue->lock_)), | |
| 61 num_pushed_sequences_(0) {} | |
| 62 | |
| 63 scoped_ptr<PriorityQueue::Transaction> PriorityQueue::BeginTransaction() { | |
| 64 return make_scoped_ptr(new Transaction(this)); | |
| 65 } | |
| 66 | |
| 67 bool PriorityQueue::SequenceAndSortKeyPairComparator::operator()( | |
| 68 const SequenceAndSortKeyPair& left, | |
| 69 const SequenceAndSortKeyPair& right) const { | |
| 70 return left.second < right.second; | |
| 71 } | |
| 72 | |
| 73 } // namespace task_scheduler | |
| 74 } // namespace base | |
| OLD | NEW |