| 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 <queue> | 9 #include <queue> |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/base_export.h" | 12 #include "base/base_export.h" |
| 12 #include "base/callback.h" | 13 #include "base/callback.h" |
| 13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/task_scheduler/scheduler_lock.h" | 16 #include "base/task_scheduler/scheduler_lock.h" |
| 17 #include "base/task_scheduler/sequence.h" | 17 #include "base/task_scheduler/sequence.h" |
| 18 #include "base/task_scheduler/sequence_sort_key.h" | 18 #include "base/task_scheduler/sequence_sort_key.h" |
| 19 #include "base/threading/non_thread_safe.h" | 19 #include "base/threading/non_thread_safe.h" |
| 20 | 20 |
| 21 namespace base { | 21 namespace base { |
| 22 namespace internal { | 22 namespace internal { |
| 23 | 23 |
| 24 // A PriorityQueue holds Sequences of Tasks. This class is thread-safe. | 24 // A PriorityQueue holds Sequences of Tasks. This class is thread-safe. |
| 25 class BASE_EXPORT PriorityQueue { | 25 class BASE_EXPORT PriorityQueue { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 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. |
| 60 void Push(scoped_ptr<SequenceAndSortKey> sequence_and_sort_key); | 60 void Push(std::unique_ptr<SequenceAndSortKey> sequence_and_sort_key); |
| 61 | 61 |
| 62 // Returns the SequenceAndSortKey with the highest priority or a null | 62 // Returns the SequenceAndSortKey with the highest priority or a null |
| 63 // SequenceAndSortKey if the PriorityQueue is empty. The reference becomes | 63 // SequenceAndSortKey if the PriorityQueue is empty. The reference becomes |
| 64 // invalid the next time that a Sequence is popped from the PriorityQueue. | 64 // invalid the next time that a Sequence is popped from the PriorityQueue. |
| 65 const SequenceAndSortKey& Peek() const; | 65 const SequenceAndSortKey& Peek() const; |
| 66 | 66 |
| 67 // Removes the SequenceAndSortKey with the highest priority from the | 67 // Removes the SequenceAndSortKey with the highest priority from the |
| 68 // PriorityQueue. Cannot be called on an empty PriorityQueue. | 68 // PriorityQueue. Cannot be called on an empty PriorityQueue. |
| 69 void Pop(); | 69 void Pop(); |
| 70 | 70 |
| 71 private: | 71 private: |
| 72 friend class PriorityQueue; | 72 friend class PriorityQueue; |
| 73 | 73 |
| 74 explicit Transaction(PriorityQueue* outer_queue); | 74 explicit Transaction(PriorityQueue* outer_queue); |
| 75 | 75 |
| 76 // Holds the lock of |outer_queue_| for most of the lifetime of this | 76 // 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 | 77 // Transaction. Using a scoped_ptr allows the destructor to release the lock |
| 78 // before performing internal operations which have to be done outside of | 78 // before performing internal operations which have to be done outside of |
| 79 // its scope. | 79 // its scope. |
| 80 scoped_ptr<AutoSchedulerLock> auto_lock_; | 80 std::unique_ptr<AutoSchedulerLock> auto_lock_; |
| 81 | 81 |
| 82 PriorityQueue* const outer_queue_; | 82 PriorityQueue* const outer_queue_; |
| 83 | 83 |
| 84 // Number of times that Push() has been called on this Transaction. | 84 // Number of times that Push() has been called on this Transaction. |
| 85 size_t num_pushed_sequences_ = 0; | 85 size_t num_pushed_sequences_ = 0; |
| 86 | 86 |
| 87 DISALLOW_COPY_AND_ASSIGN(Transaction); | 87 DISALLOW_COPY_AND_ASSIGN(Transaction); |
| 88 }; | 88 }; |
| 89 | 89 |
| 90 // |sequence_inserted_callback| is a non-null callback invoked when the | 90 // |sequence_inserted_callback| is a non-null callback invoked when the |
| 91 // Transaction is done for each Push that was performed with the Transaction. | 91 // Transaction is done for each Push that was performed with the Transaction. |
| 92 explicit PriorityQueue(const Closure& sequence_inserted_callback); | 92 explicit PriorityQueue(const Closure& sequence_inserted_callback); |
| 93 | 93 |
| 94 // |sequence_inserted_callback| is a non-null callback invoked when the | 94 // |sequence_inserted_callback| is a non-null callback invoked when the |
| 95 // Transaction is done for each Push that was performed with the Transaction. | 95 // Transaction is done for each Push that was performed with the Transaction. |
| 96 // |predecessor_priority_queue| is a PriorityQueue for which a thread is | 96 // |predecessor_priority_queue| is a PriorityQueue for which a thread is |
| 97 // allowed to have an active Transaction when it creates a Transaction for | 97 // allowed to have an active Transaction when it creates a Transaction for |
| 98 // this PriorityQueue. | 98 // this PriorityQueue. |
| 99 PriorityQueue(const Closure& sequence_inserted_callback, | 99 PriorityQueue(const Closure& sequence_inserted_callback, |
| 100 const PriorityQueue* predecessor_priority_queue); | 100 const PriorityQueue* predecessor_priority_queue); |
| 101 | 101 |
| 102 ~PriorityQueue(); | 102 ~PriorityQueue(); |
| 103 | 103 |
| 104 // Begins a Transaction. This method cannot be called on a thread which has an | 104 // 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 | 105 // active Transaction unless the last Transaction created on the thread was |
| 106 // for the allowed predecessor specified in the constructor of this | 106 // for the allowed predecessor specified in the constructor of this |
| 107 // PriorityQueue. | 107 // PriorityQueue. |
| 108 scoped_ptr<Transaction> BeginTransaction(); | 108 std::unique_ptr<Transaction> BeginTransaction(); |
| 109 | 109 |
| 110 private: | 110 private: |
| 111 struct SequenceAndSortKeyComparator { | 111 struct SequenceAndSortKeyComparator { |
| 112 bool operator()(const scoped_ptr<SequenceAndSortKey>& left, | 112 bool operator()(const std::unique_ptr<SequenceAndSortKey>& left, |
| 113 const scoped_ptr<SequenceAndSortKey>& right) const { | 113 const std::unique_ptr<SequenceAndSortKey>& right) const { |
| 114 return left->sort_key < right->sort_key; | 114 return left->sort_key < right->sort_key; |
| 115 } | 115 } |
| 116 }; | 116 }; |
| 117 using ContainerType = | 117 using ContainerType = |
| 118 std::priority_queue<scoped_ptr<SequenceAndSortKey>, | 118 std::priority_queue<std::unique_ptr<SequenceAndSortKey>, |
| 119 std::vector<scoped_ptr<SequenceAndSortKey>>, | 119 std::vector<std::unique_ptr<SequenceAndSortKey>>, |
| 120 SequenceAndSortKeyComparator>; | 120 SequenceAndSortKeyComparator>; |
| 121 | 121 |
| 122 // Synchronizes access to |container_|. | 122 // Synchronizes access to |container_|. |
| 123 SchedulerLock container_lock_; | 123 SchedulerLock container_lock_; |
| 124 | 124 |
| 125 ContainerType container_; | 125 ContainerType container_; |
| 126 | 126 |
| 127 const Closure sequence_inserted_callback_; | 127 const Closure sequence_inserted_callback_; |
| 128 | 128 |
| 129 // A null SequenceAndSortKey returned by Peek() when the PriorityQueue is | 129 // A null SequenceAndSortKey returned by Peek() when the PriorityQueue is |
| 130 // empty. | 130 // empty. |
| 131 const SequenceAndSortKey empty_sequence_and_sort_key_; | 131 const SequenceAndSortKey empty_sequence_and_sort_key_; |
| 132 | 132 |
| 133 DISALLOW_COPY_AND_ASSIGN(PriorityQueue); | 133 DISALLOW_COPY_AND_ASSIGN(PriorityQueue); |
| 134 }; | 134 }; |
| 135 | 135 |
| 136 } // namespace internal | 136 } // namespace internal |
| 137 } // namespace base | 137 } // namespace base |
| 138 | 138 |
| 139 #endif // BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_ | 139 #endif // BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_ |
| OLD | NEW |