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 <utility> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 | |
| 11 namespace base { | |
| 12 namespace internal { | |
| 13 | |
| 14 PriorityQueue::SequenceAndSortKey::SequenceAndSortKey( | |
| 15 scoped_refptr<Sequence> sequence, | |
| 16 SequenceSortKey sort_key) | |
| 17 : sequence(sequence), sort_key(sort_key) {} | |
| 18 | |
| 19 PriorityQueue::SequenceAndSortKey::~SequenceAndSortKey() = default; | |
| 20 | |
| 21 PriorityQueue::SequenceAndSortKey PriorityQueue::SequenceAndSortKey::Null() { | |
| 22 return SequenceAndSortKey(scoped_refptr<Sequence>(), | |
| 23 SequenceSortKey(TaskPriority::LOWEST, TimeTicks())); | |
| 24 } | |
| 25 | |
| 26 bool PriorityQueue::SequenceAndSortKey::is_null() const { | |
| 27 return sequence.get() == nullptr; | |
| 28 } | |
| 29 | |
| 30 PriorityQueue::PriorityQueue(const Closure& sequence_inserted_callback) | |
| 31 : sequence_inserted_callback_(sequence_inserted_callback) { | |
| 32 DCHECK(!sequence_inserted_callback_.is_null()); | |
| 33 } | |
| 34 | |
| 35 PriorityQueue::PriorityQueue(const Closure& sequence_inserted_callback, | |
| 36 PriorityQueue* predecessor_priority_queue) | |
| 37 : lock_(&predecessor_priority_queue->lock_), | |
| 38 sequence_inserted_callback_(sequence_inserted_callback) { | |
| 39 DCHECK(!sequence_inserted_callback_.is_null()); | |
| 40 DCHECK(predecessor_priority_queue); | |
|
robliao
2016/02/23 21:00:35
Remove DCHECK(predecessory_priorit_queue) since we
fdoray
2016/02/24 15:03:53
On my machine, there is no crash when the address
gab
2016/02/24 18:07:38
Interesting, @ Rob maybe this goes to show that ex
fdoray
2016/02/24 18:18:42
explanation from etienneb@: It doesn't crash becau
robliao
2016/02/24 18:46:33
This sounds wrong to me. We are dereferencing the
robliao
2016/02/24 20:04:52
Alright. Looks like I get to stand corrected here
| |
| 41 } | |
| 42 | |
| 43 PriorityQueue::~PriorityQueue() = default; | |
| 44 | |
| 45 PriorityQueue::Transaction::~Transaction() { | |
| 46 DCHECK(CalledOnValidThread()); | |
| 47 | |
| 48 // Release the PriorityQueue's lock before running the sequence insertion | |
| 49 // callback to avoid holding multiple locks at the same time. | |
|
gab
2016/02/23 20:58:42
s/to avoid holding multiple locks at the same time
fdoray
2016/02/24 15:03:53
I did what you suggest in your next comment instea
| |
| 50 auto_lock_.reset(); | |
| 51 | |
| 52 // Run the sequence insertion callback once for each sequence that was | |
| 53 // inserted in the PriorityQueue during the lifetime of this Transaction. | |
|
gab
2016/02/23 20:58:42
Actually I'd say append [1] to this comment and fo
fdoray
2016/02/24 15:03:53
Done.
| |
| 54 for (size_t i = 0; i < num_pushed_sequences_; ++i) | |
| 55 outer_->sequence_inserted_callback_.Run(); | |
| 56 } | |
| 57 | |
| 58 void PriorityQueue::Transaction::Push( | |
| 59 scoped_ptr<SequenceAndSortKey> sequence_and_sort_key) { | |
| 60 DCHECK(CalledOnValidThread()); | |
| 61 DCHECK(!sequence_and_sort_key->is_null()); | |
| 62 | |
| 63 outer_->container_.push(std::move(sequence_and_sort_key)); | |
| 64 ++num_pushed_sequences_; | |
| 65 } | |
| 66 | |
| 67 PriorityQueue::SequenceAndSortKey PriorityQueue::Transaction::Peek() const { | |
| 68 DCHECK(CalledOnValidThread()); | |
| 69 | |
| 70 if (outer_->container_.empty()) | |
| 71 return SequenceAndSortKey::Null(); | |
| 72 | |
| 73 return *outer_->container_.top(); | |
| 74 } | |
| 75 | |
| 76 void PriorityQueue::Transaction::Pop() { | |
| 77 DCHECK(CalledOnValidThread()); | |
| 78 DCHECK(!outer_->container_.empty()); | |
| 79 outer_->container_.pop(); | |
| 80 } | |
| 81 | |
| 82 PriorityQueue::Transaction::Transaction(PriorityQueue* outer) | |
| 83 : outer_(outer), | |
| 84 auto_lock_(new AutoSchedulerLock(outer_->lock_)), | |
| 85 num_pushed_sequences_(0) { | |
| 86 DCHECK(CalledOnValidThread()); | |
| 87 } | |
| 88 | |
| 89 scoped_ptr<PriorityQueue::Transaction> PriorityQueue::BeginTransaction() { | |
| 90 return make_scoped_ptr(new Transaction(this)); | |
| 91 } | |
| 92 | |
| 93 bool PriorityQueue::SequenceAndSortKeyComparator::operator()( | |
| 94 const scoped_ptr<SequenceAndSortKey>& left, | |
| 95 const scoped_ptr<SequenceAndSortKey>& right) const { | |
| 96 return left->sort_key < right->sort_key; | |
| 97 } | |
| 98 | |
| 99 } // namespace internal | |
| 100 } // namespace base | |
| OLD | NEW |