Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: base/task_scheduler/priority_queue.cc

Issue 1709713002: TaskScheduler [4/9] Priority Queue (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_2_sequence_and_task
Patch Set: CR danakj #31 Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/task_scheduler/priority_queue.h ('k') | base/task_scheduler/priority_queue_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 const SequenceSortKey& sort_key)
20 : sequence(std::move(sequence)), sort_key(sort_key) {}
21
22 PriorityQueue::SequenceAndSortKey::~SequenceAndSortKey() = default;
23
24 PriorityQueue::Transaction::Transaction(PriorityQueue* outer_queue)
25 : auto_lock_(new AutoSchedulerLock(outer_queue->container_lock_)),
26 outer_queue_(outer_queue) {
27 DCHECK(CalledOnValidThread());
28 }
29
30 PriorityQueue::Transaction::~Transaction() {
31 DCHECK(CalledOnValidThread());
32
33 // Run the sequence insertion callback once for each Sequence that was
34 // inserted in the PriorityQueue during the lifetime of this Transaction.
35 // Perform this outside the scope of PriorityQueue's lock to avoid imposing an
36 // unnecessary lock dependency on |sequence_inserted_callback_|'s destination.
37 auto_lock_.reset();
38 for (size_t i = 0; i < num_pushed_sequences_; ++i)
39 outer_queue_->sequence_inserted_callback_.Run();
40 }
41
42 void PriorityQueue::Transaction::Push(
43 scoped_ptr<SequenceAndSortKey> sequence_and_sort_key) {
44 DCHECK(CalledOnValidThread());
45 DCHECK(!sequence_and_sort_key->is_null());
46
47 outer_queue_->container_.push(std::move(sequence_and_sort_key));
48 ++num_pushed_sequences_;
49 }
50
51 const PriorityQueue::SequenceAndSortKey& PriorityQueue::Transaction::Peek()
52 const {
53 DCHECK(CalledOnValidThread());
54
55 // TODO(fdoray): Add an IsEmpty() method to Transaction and require Peek() to
56 // be called on a non-empty PriorityQueue only.
57 if (outer_queue_->container_.empty())
58 return outer_queue_->empty_sequence_and_sort_key_;
59
60 return *outer_queue_->container_.top();
61 }
62
63 void PriorityQueue::Transaction::Pop() {
64 DCHECK(CalledOnValidThread());
65 DCHECK(!outer_queue_->container_.empty());
66 outer_queue_->container_.pop();
67 }
68
69 PriorityQueue::PriorityQueue(const Closure& sequence_inserted_callback)
70 : sequence_inserted_callback_(sequence_inserted_callback) {
71 DCHECK(!sequence_inserted_callback_.is_null());
72 }
73
74 PriorityQueue::PriorityQueue(const Closure& sequence_inserted_callback,
75 const PriorityQueue* predecessor_priority_queue)
76 : container_lock_(&predecessor_priority_queue->container_lock_),
77 sequence_inserted_callback_(sequence_inserted_callback) {
78 DCHECK(!sequence_inserted_callback_.is_null());
79 DCHECK(predecessor_priority_queue);
80 }
81
82 PriorityQueue::~PriorityQueue() = default;
83
84 scoped_ptr<PriorityQueue::Transaction> PriorityQueue::BeginTransaction() {
85 return make_scoped_ptr(new Transaction(this));
86 }
87
88 } // namespace internal
89 } // namespace base
OLDNEW
« no previous file with comments | « base/task_scheduler/priority_queue.h ('k') | base/task_scheduler/priority_queue_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698