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 #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 // An immutable struct combining a Sequence and the sort key that determines | |
28 // its position in a PriorityQueue. | |
29 struct BASE_EXPORT SequenceAndSortKey { | |
30 // Constructs a null SequenceAndSortKey. | |
31 SequenceAndSortKey(); | |
32 | |
33 // Constructs a SequenceAndSortKey with the given |sequence| and |sort_key|. | |
34 SequenceAndSortKey(scoped_refptr<Sequence> sequence, | |
35 SequenceSortKey sort_key); | |
danakj
2016/03/17 21:32:14
const&
fdoray
2016/03/18 16:06:55
Done.
| |
36 | |
37 ~SequenceAndSortKey(); | |
38 | |
39 // Returns true if this is a null SequenceAndSortKey. | |
40 bool is_null() const; | |
41 | |
42 const scoped_refptr<Sequence> sequence; | |
43 const SequenceSortKey sort_key; | |
44 }; | |
45 | |
46 // |sequence_inserted_callback| is a non-null callback invoked when a sequence | |
47 // is added to the PriorityQueue. | |
danakj
2016/03/17 21:32:14
Can you document this happens when the transaction
fdoray
2016/03/18 16:06:55
Done.
| |
48 explicit PriorityQueue(const Closure& sequence_inserted_callback); | |
49 | |
50 // |sequence_inserted_callback| is a non-null callback invoked when a sequence | |
51 // is added to the PriorityQueue. |predecessor_priority_queue| is a | |
52 // PriorityQueue for which a thread is allowed to have an active Transaction | |
53 // when it creates a Transaction for this PriorityQueue. | |
54 PriorityQueue(const Closure& sequence_inserted_callback, | |
55 const PriorityQueue* predecessor_priority_queue); | |
56 | |
57 ~PriorityQueue(); | |
58 | |
59 class BASE_EXPORT Transaction : public NonThreadSafe { | |
robliao
2016/03/17 17:48:10
Move this after SequenceAndSortKey's definition.
fdoray
2016/03/17 19:12:02
Done.
danakj
2016/03/17 21:32:15
Put a comment on this class, what is it?
fdoray
2016/03/18 16:06:55
Done.
| |
60 public: | |
61 ~Transaction(); | |
62 | |
63 // Inserts |sequence_and_sort_key| in the PriorityQueue. | |
64 void Push(scoped_ptr<SequenceAndSortKey> sequence_and_sort_key); | |
65 | |
66 // Returns the SequenceAndSortKey with the highest priority or a null | |
67 // SequenceAndSortKey if the PriorityQueue is empty. | |
68 SequenceAndSortKey Peek() const; | |
69 | |
70 // Removes the SequenceAndSortKey with the highest priority from the | |
71 // PriorityQueue. Cannot be called on an empty PriorityQueue. | |
72 void Pop(); | |
73 | |
74 private: | |
75 friend class PriorityQueue; | |
76 | |
77 explicit Transaction(PriorityQueue* outer); | |
78 | |
79 PriorityQueue* const outer_; | |
danakj
2016/03/17 21:32:15
outer_queue_?
fdoray
2016/03/18 16:06:55
Done.
| |
80 | |
81 // Holds the lock of |outer_| for most of the lifetime of this Transaction. | |
danakj
2016/03/17 21:32:14
Why is it okay that while a Transaction is alive t
danakj
2016/03/17 21:32:14
usually locks sit right above the things they lock
fdoray
2016/03/18 16:06:55
Done. Added an explanation above the declaration o
fdoray
2016/03/18 16:06:55
This is not a lock. It's an auto-lock to hold oute
danakj
2016/03/18 20:24:05
Yeah, but it's holding a lock in the auto lock. Wh
fdoray
2016/03/18 21:22:18
Ah ok! Done.
| |
82 // Using a scoped_ptr allows the destructor to release the lock before | |
83 // performing internal operations which have to be done outside of its | |
84 // scope. | |
85 scoped_ptr<AutoSchedulerLock> auto_lock_; | |
86 | |
87 // Number of times that Push() has been called on this Transaction. | |
88 size_t num_pushed_sequences_ = 0; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(Transaction); | |
91 }; | |
92 | |
93 // Begins a Transaction which allows multiple operations to be performed | |
94 // atomically on the PriorityQueue. It is guaranteed that nothing else will | |
95 // modify the PriorityQueue while the returned Transaction is alive. This | |
96 // method cannot be called on a thread which has an active Transaction unless | |
97 // the last Transaction created on the thread was for the allowed predecessor | |
98 // specified in the constructor of this PriorityQueue. | |
99 scoped_ptr<Transaction> BeginTransaction(); | |
100 | |
101 private: | |
102 struct SequenceAndSortKeyComparator { | |
103 bool operator()(const scoped_ptr<SequenceAndSortKey>& left, | |
104 const scoped_ptr<SequenceAndSortKey>& right) const; | |
105 }; | |
106 using ContainerType = | |
107 std::priority_queue<scoped_ptr<SequenceAndSortKey>, | |
108 std::vector<scoped_ptr<SequenceAndSortKey>>, | |
109 SequenceAndSortKeyComparator>; | |
110 | |
111 // Synchronizes access to |container_|. | |
112 SchedulerLock lock_; | |
danakj
2016/03/17 21:32:14
container_lock_?
fdoray
2016/03/18 16:06:55
Done.
| |
113 | |
114 ContainerType container_; | |
115 | |
116 const Closure sequence_inserted_callback_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(PriorityQueue); | |
119 }; | |
120 | |
121 } // namespace internal | |
122 } // namespace base | |
123 | |
124 #endif // BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_ | |
OLD | NEW |