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 <limits> | |
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::PriorityQueue(const Closure& sequence_inserted_callback) | |
22 : PriorityQueue(sequence_inserted_callback, nullptr) {} | |
23 | |
24 PriorityQueue::PriorityQueue(const Closure& sequence_inserted_callback, | |
25 PriorityQueue* predecessor_priority_queue) | |
26 : lock_(&predecessor_priority_queue->lock_), | |
robliao
2016/02/23 02:10:58
Won't this line crash since predecessor_priority_q
fdoray
2016/02/23 17:02:14
Done. It didn't crash but it was definitely wrong.
| |
27 sequence_inserted_callback_(sequence_inserted_callback) { | |
gab
2016/02/23 03:02:10
Same order for initializer list, parameter order,
fdoray
2016/02/23 17:02:14
Done.
| |
28 DCHECK(!sequence_inserted_callback_.is_null()); | |
29 } | |
30 | |
31 PriorityQueue::~PriorityQueue() = default; | |
32 | |
33 PriorityQueue::Transaction::~Transaction() { | |
34 DCHECK(CalledOnValidThread()); | |
35 auto_lock_.reset(); | |
gab
2016/02/23 03:02:10
Add a comment about what's happening here (unlock
fdoray
2016/02/23 17:02:14
Done.
| |
36 for (size_t i = 0; i < num_pushed_sequences_; ++i) | |
37 priority_queue_->sequence_inserted_callback_.Run(); | |
38 } | |
39 | |
40 void PriorityQueue::Transaction::PushSequence(scoped_refptr<Sequence> sequence, | |
41 const SequenceSortKey& sort_key) { | |
42 DCHECK(CalledOnValidThread()); | |
43 priority_queue_->container_.push( | |
44 make_scoped_ptr(new SequenceAndSortKey(sequence, sort_key))); | |
45 ++num_pushed_sequences_; | |
46 } | |
47 | |
48 PriorityQueue::SequenceAndSortKey PriorityQueue::Transaction::PeekSequence() | |
49 const { | |
50 DCHECK(CalledOnValidThread()); | |
51 | |
52 if (priority_queue_->container_.empty()) { | |
53 return SequenceAndSortKey( | |
54 scoped_refptr<Sequence>(), | |
55 SequenceSortKey( | |
56 TaskPriority::LOWEST, | |
57 TimeTicks::FromInternalValue(std::numeric_limits<int64_t>::max()))); | |
gab
2016/02/23 03:02:10
Should SequenceAndSortKey instead have a default c
fdoray
2016/02/23 17:02:14
Done.
| |
58 } | |
59 | |
60 return *priority_queue_->container_.top(); | |
61 } | |
62 | |
63 void PriorityQueue::Transaction::PopSequence() { | |
64 DCHECK(CalledOnValidThread()); | |
65 DCHECK(!priority_queue_->container_.empty()); | |
66 priority_queue_->container_.pop(); | |
67 } | |
68 | |
69 PriorityQueue::Transaction::Transaction(PriorityQueue* priority_queue) | |
70 : priority_queue_(priority_queue), | |
71 auto_lock_(new AutoSchedulerLock(priority_queue->lock_)), | |
72 num_pushed_sequences_(0) { | |
73 DCHECK(CalledOnValidThread()); | |
74 } | |
75 | |
76 scoped_ptr<PriorityQueue::Transaction> PriorityQueue::BeginTransaction() { | |
77 return make_scoped_ptr(new Transaction(this)); | |
78 } | |
79 | |
80 bool PriorityQueue::SequenceAndSortKeyComparator::operator()( | |
81 const scoped_ptr<SequenceAndSortKey>& left, | |
82 const scoped_ptr<SequenceAndSortKey>& right) const { | |
83 return left->sort_key < right->sort_key; | |
84 } | |
85 | |
86 } // namespace internal | |
87 } // namespace base | |
OLD | NEW |