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

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

Issue 1709713002: TaskScheduler [4/9] Priority Queue (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_2_sequence_and_task
Patch Set: self review. Created 4 years, 10 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
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 #ifndef BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_
6 #define BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_
7
8 #include <queue>
9 #include <vector>
10
11 #include "base/base_export.h"
12 #include "base/callback.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/task_scheduler/scheduler_lock.h"
17 #include "base/task_scheduler/sequence.h"
18 #include "base/task_scheduler/sequence_sort_key.h"
19 #include "base/threading/non_thread_safe.h"
20
21 namespace base {
22 namespace internal {
23
24 // A PriorityQueue holds Sequences of Tasks. This class is thread-safe.
25 class BASE_EXPORT PriorityQueue {
26 public:
27 // A Sequence and the sort key with which it was inserted in the
28 // PriorityQueue.
29 struct SequenceAndSortKey {
30 SequenceAndSortKey(scoped_refptr<Sequence> sequence,
31 SequenceSortKey sort_key);
32 ~SequenceAndSortKey();
33 scoped_refptr<Sequence> sequence;
34 SequenceSortKey sort_key;
35 };
36
37 // |sequence_inserted_callback| is a callback invoked when a sequence is added
38 // to the PriorityQueue. In the second constructor,
39 // |predecessor_priority_queue| is a PriorityQueue for which a thread is
robliao 2016/02/23 02:10:58 Let's separate this comment and have one per const
fdoray 2016/02/23 17:02:14 Done.
40 // allowed to have an active Transaction when it creates a Transaction for
41 // this PriorityQueue.
42 PriorityQueue(const Closure& sequence_inserted_callback);
robliao 2016/02/23 02:10:58 Add explicit constructor keyword
fdoray 2016/02/23 17:02:14 Done.
43 PriorityQueue(const Closure& sequence_inserted_callback,
44 PriorityQueue* predecessor_priority_queue);
45
46 ~PriorityQueue();
47
48 class BASE_EXPORT Transaction : public NonThreadSafe {
49 public:
50 ~Transaction();
51
52 // Adds |sequence| to the PriorityQueue. The position of |sequence| in the
53 // PriorityQueue is determined by |sort_key|.
54 void PushSequence(scoped_refptr<Sequence> sequence,
robliao 2016/02/23 02:10:58 Should this be a SequenceAndSortKey?
fdoray 2016/02/23 17:02:14 Done.
55 const SequenceSortKey& sort_key);
56
57 // Returns a SequenceAndSortKey combining the Sequence with the highest
58 // priority in the PriorityQueue and its sort key. If the PriorityQueue is
59 // empty, the |sequence| field of the returned SequenceSortKey is nullptr.
60 SequenceAndSortKey PeekSequence() const;
61
62 // Removes the Sequence with the highest priority from the PriorityQueue.
63 // Cannot be called on an empty PriorityQueue.
64 void PopSequence();
65
66 private:
67 friend class PriorityQueue;
68
69 Transaction(PriorityQueue* priority_queue);
70
71 PriorityQueue* const priority_queue_;
gab 2016/02/23 03:02:10 s/priority_queue_/outer_/ (I tend to prefer using
fdoray 2016/02/23 17:02:14 Done.
72
73 // Holds the lock of |priority_queue_| for the lifetime of the transaction.
74 scoped_ptr<AutoSchedulerLock> auto_lock_;
robliao 2016/02/23 02:10:58 Worth noting why we're using a scoped_ptr rather t
gab 2016/02/23 03:02:10 Right, something like appending "Using a scoped_pt
fdoray 2016/02/23 17:02:14 Done.
75
76 // Number of times that PushSequence() has been called on this transaction.
77 size_t num_pushed_sequences_;
78
79 DISALLOW_COPY_AND_ASSIGN(Transaction);
80 };
81
82 // Begins a Transaction which allows multiple operations to be performed
83 // atomically on the PriorityQueue. It is guaranteed that nothing else will
84 // modify the PriorityQueue while the returned Transaction is alive. This
85 // method cannot be called on a thread which has an active Transaction unless
86 // the last Transaction created on the thread was for the allowed predecessor
87 // specified in the constructor of this PriorityQueue.
88 scoped_ptr<Transaction> BeginTransaction();
89
90 private:
91 // Synchronizes access to |container_|.
92 SchedulerLock lock_;
robliao 2016/02/23 02:10:58 Move below the type definitions.
fdoray 2016/02/23 17:02:14 Done.
93
94 struct SequenceAndSortKeyComparator {
95 bool operator()(const scoped_ptr<SequenceAndSortKey>& left,
96 const scoped_ptr<SequenceAndSortKey>& right) const;
97 };
98 using ContainerType =
99 std::priority_queue<SequenceAndSortKey,
gab 2016/02/23 03:02:10 s/SequenceAndSortKey/scoped_ptr<SequenceAndSortKey
fdoray 2016/02/23 17:02:14 Done. I'm surprised that the code compiled with th
100 std::vector<scoped_ptr<SequenceAndSortKey>>,
101 SequenceAndSortKeyComparator>;
102 ContainerType container_;
103
104 const Closure sequence_inserted_callback_;
105
106 DISALLOW_COPY_AND_ASSIGN(PriorityQueue);
107 };
108
109 } // namespace internal
110 } // namespace base
111
112 #endif // BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698