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 // A Sequence and the sort key that determines its position in the | |
28 // PriorityQueue. | |
29 struct SequenceAndSortKey { | |
30 SequenceAndSortKey(scoped_refptr<Sequence> sequence, | |
31 SequenceSortKey sort_key); | |
32 ~SequenceAndSortKey(); | |
33 | |
34 // Returns a null SequenceAndSortKey. | |
35 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.
| |
36 | |
37 // Returns true if this is a null SequenceAndSortKey. | |
38 bool is_null() const; | |
39 | |
40 scoped_refptr<Sequence> sequence; | |
41 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.
| |
42 }; | |
43 | |
44 // |sequence_inserted_callback| is a callback invoked when a sequence is added | |
45 // to the PriorityQueue. | |
46 explicit PriorityQueue(const Closure& sequence_inserted_callback); | |
47 | |
48 // |sequence_inserted_callback| is a callback invoked when a sequence is added | |
49 // to the PriorityQueue. |predecessor_priority_queue| is a PriorityQueue for | |
50 // which a thread is allowed to have an active Transaction when it creates a | |
51 // Transaction for this PriorityQueue. | |
52 PriorityQueue(const Closure& sequence_inserted_callback, | |
53 PriorityQueue* predecessor_priority_queue); | |
54 | |
55 ~PriorityQueue(); | |
56 | |
57 class Transaction : public NonThreadSafe { | |
58 public: | |
59 ~Transaction(); | |
60 | |
61 // Inserts |sequence_and_sort_key| in the PriorityQueue. | |
62 void Push(scoped_ptr<SequenceAndSortKey> sequence_and_sort_key); | |
63 | |
64 // Returns the SequenceAndSortKey with the highest priority or a null | |
65 // SequenceAndSortKey if the PriorityQueue is empty. | |
66 SequenceAndSortKey Peek() const; | |
67 | |
68 // Removes the SequenceAndSortKey with the highest priority from the | |
69 // PriorityQueue. Cannot be called on an empty PriorityQueue. | |
70 void Pop(); | |
71 | |
72 private: | |
73 friend class PriorityQueue; | |
74 | |
75 Transaction(PriorityQueue* outer); | |
76 | |
77 PriorityQueue* const outer_; | |
78 | |
79 // Holds the lock of |outer_| for most of the lifetime of this Transaction. | |
80 // Using a scoped_ptr allows the destructor to release the lock before | |
81 // 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.
| |
82 scoped_ptr<AutoSchedulerLock> auto_lock_; | |
83 | |
84 // Number of times that Push() has been called on this Transaction. | |
85 size_t num_pushed_sequences_; | |
86 | |
87 DISALLOW_COPY_AND_ASSIGN(Transaction); | |
88 }; | |
89 | |
90 // Begins a Transaction which allows multiple operations to be performed | |
91 // atomically on the PriorityQueue. It is guaranteed that nothing else will | |
92 // modify the PriorityQueue while the returned Transaction is alive. This | |
93 // method cannot be called on a thread which has an active Transaction unless | |
94 // the last Transaction created on the thread was for the allowed predecessor | |
95 // specified in the constructor of this PriorityQueue. | |
96 scoped_ptr<Transaction> BeginTransaction(); | |
97 | |
98 private: | |
99 struct SequenceAndSortKeyComparator { | |
100 bool operator()(const scoped_ptr<SequenceAndSortKey>& left, | |
101 const scoped_ptr<SequenceAndSortKey>& right) const; | |
102 }; | |
103 using ContainerType = | |
104 std::priority_queue<scoped_ptr<SequenceAndSortKey>, | |
105 std::vector<scoped_ptr<SequenceAndSortKey>>, | |
106 SequenceAndSortKeyComparator>; | |
107 | |
108 // Synchronizes access to |container_|. | |
109 SchedulerLock lock_; | |
110 | |
111 ContainerType container_; | |
112 | |
113 const Closure sequence_inserted_callback_; | |
114 | |
115 DISALLOW_COPY_AND_ASSIGN(PriorityQueue); | |
116 }; | |
117 | |
118 } // namespace internal | |
119 } // namespace base | |
120 | |
121 #endif // BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_ | |
OLD | NEW |