OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_ | 5 #ifndef BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_ |
6 #define BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_ | 6 #define BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <queue> | 9 #include <queue> |
10 #include <vector> | 10 #include <vector> |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 // | 49 // |
50 // A WorkerThread needs to be able to Peek sequences from both its | 50 // A WorkerThread needs to be able to Peek sequences from both its |
51 // PriorityQueues (single-threaded and shared) and then Pop the sequence with | 51 // PriorityQueues (single-threaded and shared) and then Pop the sequence with |
52 // the highest priority. If the Peek and the Pop are done through the same | 52 // the highest priority. If the Peek and the Pop are done through the same |
53 // Transaction, it is guaranteed that the PriorityQueue hasn't changed between | 53 // Transaction, it is guaranteed that the PriorityQueue hasn't changed between |
54 // the 2 operations. | 54 // the 2 operations. |
55 class BASE_EXPORT Transaction : public NonThreadSafe { | 55 class BASE_EXPORT Transaction : public NonThreadSafe { |
56 public: | 56 public: |
57 ~Transaction(); | 57 ~Transaction(); |
58 | 58 |
59 // Inserts |sequence_and_sort_key| in the PriorityQueue. | 59 // Inserts |sequence_and_sort_key| in the PriorityQueue. Each call to this |
| 60 // method will result in one invocation of the wake up callback when the |
| 61 // Transaction is destroyed. |
60 void Push(std::unique_ptr<SequenceAndSortKey> sequence_and_sort_key); | 62 void Push(std::unique_ptr<SequenceAndSortKey> sequence_and_sort_key); |
61 | 63 |
| 64 // Inserts |sequence_and_sort_key| in the PriorityQueue without invoking the |
| 65 // wake up callback. |
| 66 void PushNoWakeUp( |
| 67 std::unique_ptr<SequenceAndSortKey> sequence_and_sort_key); |
| 68 |
62 // Returns the SequenceAndSortKey with the highest priority or a null | 69 // Returns the SequenceAndSortKey with the highest priority or a null |
63 // SequenceAndSortKey if the PriorityQueue is empty. The reference becomes | 70 // SequenceAndSortKey if the PriorityQueue is empty. The reference becomes |
64 // invalid the next time that a Sequence is popped from the PriorityQueue. | 71 // invalid the next time that a Sequence is popped from the PriorityQueue. |
65 const SequenceAndSortKey& Peek() const; | 72 const SequenceAndSortKey& Peek() const; |
66 | 73 |
67 // Removes the SequenceAndSortKey with the highest priority from the | 74 // Removes the SequenceAndSortKey with the highest priority from the |
68 // PriorityQueue. Cannot be called on an empty PriorityQueue. | 75 // PriorityQueue. Cannot be called on an empty PriorityQueue. |
69 void Pop(); | 76 void Pop(); |
70 | 77 |
71 private: | 78 private: |
72 friend class PriorityQueue; | 79 friend class PriorityQueue; |
73 | 80 |
74 explicit Transaction(PriorityQueue* outer_queue); | 81 explicit Transaction(PriorityQueue* outer_queue); |
75 | 82 |
76 // Holds the lock of |outer_queue_| for most of the lifetime of this | 83 // Holds the lock of |outer_queue_| for most of the lifetime of this |
77 // Transaction. Using a scoped_ptr allows the destructor to release the lock | 84 // Transaction. Using a scoped_ptr allows the destructor to release the lock |
78 // before performing internal operations which have to be done outside of | 85 // before performing internal operations which have to be done outside of |
79 // its scope. | 86 // its scope. |
80 std::unique_ptr<AutoSchedulerLock> auto_lock_; | 87 std::unique_ptr<AutoSchedulerLock> auto_lock_; |
81 | 88 |
82 PriorityQueue* const outer_queue_; | 89 PriorityQueue* const outer_queue_; |
83 | 90 |
84 // Number of times that Push() has been called on this Transaction. | 91 // Number of times that the wake up callback should be invoked when this |
85 size_t num_pushed_sequences_ = 0; | 92 // Transaction is destroyed. |
| 93 size_t num_wake_ups_ = 0; |
86 | 94 |
87 DISALLOW_COPY_AND_ASSIGN(Transaction); | 95 DISALLOW_COPY_AND_ASSIGN(Transaction); |
88 }; | 96 }; |
89 | 97 |
90 // |sequence_inserted_callback| is a non-null callback invoked when the | 98 // |wake_up_callback| is a non-null callback invoked when a Transaction is |
91 // Transaction is done for each Push that was performed with the Transaction. | 99 // done for each call to Push() on the Transaction. |
92 explicit PriorityQueue(const Closure& sequence_inserted_callback); | 100 explicit PriorityQueue(const Closure& wake_up_callback); |
93 | 101 |
94 // |sequence_inserted_callback| is a non-null callback invoked when the | 102 // |wake_up_callback| is a non-null callback invoked when a Transaction is |
95 // Transaction is done for each Push that was performed with the Transaction. | 103 // done for each call to Push() on the Transaction. |
96 // |predecessor_priority_queue| is a PriorityQueue for which a thread is | 104 // |predecessor_priority_queue| is a PriorityQueue for which a thread is |
97 // allowed to have an active Transaction when it creates a Transaction for | 105 // allowed to have an active Transaction when it creates a Transaction for |
98 // this PriorityQueue. | 106 // this PriorityQueue. |
99 PriorityQueue(const Closure& sequence_inserted_callback, | 107 PriorityQueue(const Closure& wake_up_callback, |
100 const PriorityQueue* predecessor_priority_queue); | 108 const PriorityQueue* predecessor_priority_queue); |
101 | 109 |
102 ~PriorityQueue(); | 110 ~PriorityQueue(); |
103 | 111 |
104 // Begins a Transaction. This method cannot be called on a thread which has an | 112 // Begins a Transaction. This method cannot be called on a thread which has an |
105 // active Transaction unless the last Transaction created on the thread was | 113 // active Transaction unless the last Transaction created on the thread was |
106 // for the allowed predecessor specified in the constructor of this | 114 // for the allowed predecessor specified in the constructor of this |
107 // PriorityQueue. | 115 // PriorityQueue. |
108 std::unique_ptr<Transaction> BeginTransaction(); | 116 std::unique_ptr<Transaction> BeginTransaction(); |
109 | 117 |
110 private: | 118 private: |
111 struct SequenceAndSortKeyComparator { | 119 struct SequenceAndSortKeyComparator { |
112 bool operator()(const std::unique_ptr<SequenceAndSortKey>& left, | 120 bool operator()(const std::unique_ptr<SequenceAndSortKey>& left, |
113 const std::unique_ptr<SequenceAndSortKey>& right) const { | 121 const std::unique_ptr<SequenceAndSortKey>& right) const { |
114 return left->sort_key < right->sort_key; | 122 return left->sort_key < right->sort_key; |
115 } | 123 } |
116 }; | 124 }; |
117 using ContainerType = | 125 using ContainerType = |
118 std::priority_queue<std::unique_ptr<SequenceAndSortKey>, | 126 std::priority_queue<std::unique_ptr<SequenceAndSortKey>, |
119 std::vector<std::unique_ptr<SequenceAndSortKey>>, | 127 std::vector<std::unique_ptr<SequenceAndSortKey>>, |
120 SequenceAndSortKeyComparator>; | 128 SequenceAndSortKeyComparator>; |
121 | 129 |
122 // Synchronizes access to |container_|. | 130 // Synchronizes access to |container_|. |
123 SchedulerLock container_lock_; | 131 SchedulerLock container_lock_; |
124 | 132 |
125 ContainerType container_; | 133 ContainerType container_; |
126 | 134 |
127 const Closure sequence_inserted_callback_; | 135 const Closure wake_up_callback_; |
128 | 136 |
129 // A null SequenceAndSortKey returned by Peek() when the PriorityQueue is | 137 // A null SequenceAndSortKey returned by Peek() when the PriorityQueue is |
130 // empty. | 138 // empty. |
131 const SequenceAndSortKey empty_sequence_and_sort_key_; | 139 const SequenceAndSortKey empty_sequence_and_sort_key_; |
132 | 140 |
133 DISALLOW_COPY_AND_ASSIGN(PriorityQueue); | 141 DISALLOW_COPY_AND_ASSIGN(PriorityQueue); |
134 }; | 142 }; |
135 | 143 |
136 } // namespace internal | 144 } // namespace internal |
137 } // namespace base | 145 } // namespace base |
138 | 146 |
139 #endif // BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_ | 147 #endif // BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_ |
OLD | NEW |