Index: base/task_scheduler/priority_queue.h |
diff --git a/base/task_scheduler/priority_queue.h b/base/task_scheduler/priority_queue.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9face7fe61f3d525bf09d36f396a4fd25a2348cd |
--- /dev/null |
+++ b/base/task_scheduler/priority_queue.h |
@@ -0,0 +1,121 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_ |
+#define BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_ |
+ |
+#include <queue> |
+#include <vector> |
+ |
+#include "base/base_export.h" |
+#include "base/callback.h" |
+#include "base/macros.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/task_scheduler/scheduler_lock.h" |
+#include "base/task_scheduler/sequence.h" |
+#include "base/task_scheduler/sequence_sort_key.h" |
+#include "base/threading/non_thread_safe.h" |
+ |
+namespace base { |
+namespace internal { |
+ |
+// A PriorityQueue holds Sequences of Tasks. This class is thread-safe. |
+class BASE_EXPORT PriorityQueue { |
+ public: |
+ // A Sequence and the sort key that determines its position in the |
+ // PriorityQueue. |
+ struct SequenceAndSortKey { |
+ SequenceAndSortKey(scoped_refptr<Sequence> sequence, |
+ SequenceSortKey sort_key); |
+ ~SequenceAndSortKey(); |
+ |
+ // Returns a null SequenceAndSortKey. |
+ static SequenceAndSortKey Null(); |
gab
2016/02/23 20:58:42
I meant to have a default constructor instead, jus
robliao
2016/02/23 21:00:35
Could this instead be the default constructor?
fdoray
2016/02/24 15:03:53
Done.
|
+ |
+ // Returns true if this is a null SequenceAndSortKey. |
+ bool is_null() const; |
+ |
+ scoped_refptr<Sequence> sequence; |
+ SequenceSortKey sort_key; |
gab
2016/02/23 20:58:42
Should these be const? (and this class be marked "
fdoray
2016/02/24 15:03:53
Done.
|
+ }; |
+ |
+ // |sequence_inserted_callback| is a callback invoked when a sequence is added |
+ // to the PriorityQueue. |
+ explicit PriorityQueue(const Closure& sequence_inserted_callback); |
+ |
+ // |sequence_inserted_callback| is a callback invoked when a sequence is added |
+ // to the PriorityQueue. |predecessor_priority_queue| is a PriorityQueue for |
+ // which a thread is allowed to have an active Transaction when it creates a |
+ // Transaction for this PriorityQueue. |
+ PriorityQueue(const Closure& sequence_inserted_callback, |
+ PriorityQueue* predecessor_priority_queue); |
+ |
+ ~PriorityQueue(); |
+ |
+ class Transaction : public NonThreadSafe { |
+ public: |
+ ~Transaction(); |
+ |
+ // Inserts |sequence_and_sort_key| in the PriorityQueue. |
+ void Push(scoped_ptr<SequenceAndSortKey> sequence_and_sort_key); |
+ |
+ // Returns the SequenceAndSortKey with the highest priority or a null |
+ // SequenceAndSortKey if the PriorityQueue is empty. |
+ SequenceAndSortKey Peek() const; |
+ |
+ // Removes the SequenceAndSortKey with the highest priority from the |
+ // PriorityQueue. Cannot be called on an empty PriorityQueue. |
+ void Pop(); |
+ |
+ private: |
+ friend class PriorityQueue; |
+ |
+ Transaction(PriorityQueue* outer); |
+ |
+ PriorityQueue* const outer_; |
+ |
+ // Holds the lock of |outer_| for most of the lifetime of this Transaction. |
+ // Using a scoped_ptr allows the destructor to release the lock before |
+ // performing internal operations which don't require synchronization. |
gab
2016/02/23 20:58:42
s/don't require synchronization/have to be done ou
fdoray
2016/02/24 15:03:53
Done.
|
+ scoped_ptr<AutoSchedulerLock> auto_lock_; |
+ |
+ // Number of times that Push() has been called on this Transaction. |
+ size_t num_pushed_sequences_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(Transaction); |
+ }; |
+ |
+ // Begins a Transaction which allows multiple operations to be performed |
+ // atomically on the PriorityQueue. It is guaranteed that nothing else will |
+ // modify the PriorityQueue while the returned Transaction is alive. This |
+ // method cannot be called on a thread which has an active Transaction unless |
+ // the last Transaction created on the thread was for the allowed predecessor |
+ // specified in the constructor of this PriorityQueue. |
+ scoped_ptr<Transaction> BeginTransaction(); |
+ |
+ private: |
+ struct SequenceAndSortKeyComparator { |
+ bool operator()(const scoped_ptr<SequenceAndSortKey>& left, |
+ const scoped_ptr<SequenceAndSortKey>& right) const; |
+ }; |
+ using ContainerType = |
+ std::priority_queue<scoped_ptr<SequenceAndSortKey>, |
+ std::vector<scoped_ptr<SequenceAndSortKey>>, |
+ SequenceAndSortKeyComparator>; |
+ |
+ // Synchronizes access to |container_|. |
+ SchedulerLock lock_; |
+ |
+ ContainerType container_; |
+ |
+ const Closure sequence_inserted_callback_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(PriorityQueue); |
+}; |
+ |
+} // namespace internal |
+} // namespace base |
+ |
+#endif // BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_ |