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) {} | |
danakj
2016/03/17 21:32:14
move the sequence
fdoray
2016/03/18 16:06:55
Done.
| |
21 | |
22 PriorityQueue::SequenceAndSortKey::~SequenceAndSortKey() = default; | |
23 | |
24 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.
| |
25 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.
| |
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 const 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 { | |
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
| |
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) | |
danakj
2016/03/17 21:32:14
can you put this constructor above the destructor
fdoray
2016/03/18 16:06:55
Done.
| |
80 : outer_(outer), auto_lock_(new AutoSchedulerLock(outer_->lock_)) { | |
81 DCHECK(CalledOnValidThread()); | |
82 } | |
83 | |
84 scoped_ptr<PriorityQueue::Transaction> PriorityQueue::BeginTransaction() { | |
85 return make_scoped_ptr(new Transaction(this)); | |
86 } | |
87 | |
88 bool PriorityQueue::SequenceAndSortKeyComparator::operator()( | |
89 const scoped_ptr<SequenceAndSortKey>& left, | |
90 const scoped_ptr<SequenceAndSortKey>& right) const { | |
91 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.
| |
92 } | |
93 | |
94 } // namespace internal | |
95 } // namespace base | |
OLD | NEW |