Index: base/task_scheduler/priority_queue.cc |
diff --git a/base/task_scheduler/priority_queue.cc b/base/task_scheduler/priority_queue.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..25326c07966947917c82002c3660b29304c1516b |
--- /dev/null |
+++ b/base/task_scheduler/priority_queue.cc |
@@ -0,0 +1,100 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/task_scheduler/priority_queue.h" |
+ |
+#include <utility> |
+ |
+#include "base/logging.h" |
+ |
+namespace base { |
+namespace internal { |
+ |
+PriorityQueue::SequenceAndSortKey::SequenceAndSortKey( |
+ scoped_refptr<Sequence> sequence, |
+ SequenceSortKey sort_key) |
+ : sequence(sequence), sort_key(sort_key) {} |
+ |
+PriorityQueue::SequenceAndSortKey::~SequenceAndSortKey() = default; |
+ |
+PriorityQueue::SequenceAndSortKey PriorityQueue::SequenceAndSortKey::Null() { |
+ return SequenceAndSortKey(scoped_refptr<Sequence>(), |
+ SequenceSortKey(TaskPriority::LOWEST, TimeTicks())); |
+} |
+ |
+bool PriorityQueue::SequenceAndSortKey::is_null() const { |
+ return sequence.get() == nullptr; |
+} |
+ |
+PriorityQueue::PriorityQueue(const Closure& sequence_inserted_callback) |
+ : sequence_inserted_callback_(sequence_inserted_callback) { |
+ DCHECK(!sequence_inserted_callback_.is_null()); |
+} |
+ |
+PriorityQueue::PriorityQueue(const Closure& sequence_inserted_callback, |
+ PriorityQueue* predecessor_priority_queue) |
+ : lock_(&predecessor_priority_queue->lock_), |
+ sequence_inserted_callback_(sequence_inserted_callback) { |
+ DCHECK(!sequence_inserted_callback_.is_null()); |
+ 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
|
+} |
+ |
+PriorityQueue::~PriorityQueue() = default; |
+ |
+PriorityQueue::Transaction::~Transaction() { |
+ DCHECK(CalledOnValidThread()); |
+ |
+ // Release the PriorityQueue's lock before running the sequence insertion |
+ // 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
|
+ auto_lock_.reset(); |
+ |
+ // Run the sequence insertion callback once for each sequence that was |
+ // 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.
|
+ for (size_t i = 0; i < num_pushed_sequences_; ++i) |
+ outer_->sequence_inserted_callback_.Run(); |
+} |
+ |
+void PriorityQueue::Transaction::Push( |
+ scoped_ptr<SequenceAndSortKey> sequence_and_sort_key) { |
+ DCHECK(CalledOnValidThread()); |
+ DCHECK(!sequence_and_sort_key->is_null()); |
+ |
+ outer_->container_.push(std::move(sequence_and_sort_key)); |
+ ++num_pushed_sequences_; |
+} |
+ |
+PriorityQueue::SequenceAndSortKey PriorityQueue::Transaction::Peek() const { |
+ DCHECK(CalledOnValidThread()); |
+ |
+ if (outer_->container_.empty()) |
+ return SequenceAndSortKey::Null(); |
+ |
+ return *outer_->container_.top(); |
+} |
+ |
+void PriorityQueue::Transaction::Pop() { |
+ DCHECK(CalledOnValidThread()); |
+ DCHECK(!outer_->container_.empty()); |
+ outer_->container_.pop(); |
+} |
+ |
+PriorityQueue::Transaction::Transaction(PriorityQueue* outer) |
+ : outer_(outer), |
+ auto_lock_(new AutoSchedulerLock(outer_->lock_)), |
+ num_pushed_sequences_(0) { |
+ DCHECK(CalledOnValidThread()); |
+} |
+ |
+scoped_ptr<PriorityQueue::Transaction> PriorityQueue::BeginTransaction() { |
+ return make_scoped_ptr(new Transaction(this)); |
+} |
+ |
+bool PriorityQueue::SequenceAndSortKeyComparator::operator()( |
+ const scoped_ptr<SequenceAndSortKey>& left, |
+ const scoped_ptr<SequenceAndSortKey>& right) const { |
+ return left->sort_key < right->sort_key; |
+} |
+ |
+} // namespace internal |
+} // namespace base |