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 <utility> | |
10 #include <vector> | |
11 | |
12 #include "base/base_export.h" | |
13 #include "base/callback.h" | |
14 #include "base/macros.h" | |
15 #include "base/memory/ref_counted.h" | |
16 #include "base/memory/scoped_ptr.h" | |
17 #include "base/task_scheduler/scheduler_lock.h" | |
18 #include "base/task_scheduler/sequence.h" | |
19 #include "base/task_scheduler/sequence_sort_key.h" | |
20 | |
21 namespace base { | |
22 namespace task_scheduler { | |
23 | |
24 // A priority queue holds sequences of tasks. This class is thread-safe. | |
25 class BASE_EXPORT PriorityQueue { | |
26 public: | |
27 // |sequence_inserted_callback| is invoked whenever a sequence is added to the | |
fdoray
2016/02/11 17:30:33
is *pushed* to the priority queue
fdoray
2016/02/12 04:16:19
WontFix
| |
28 // priority queue. |predecessor_priority_queue| is a priority queue for which | |
29 // a thread is allowed to have an active transaction alive when it calls | |
30 // BeginTransaction() on this priority queue. | |
31 PriorityQueue(const Closure& sequence_inserted_callback); | |
32 PriorityQueue(const Closure& sequence_inserted_callback, | |
33 PriorityQueue* predecessor_priority_queue); | |
34 | |
35 ~PriorityQueue(); | |
36 | |
37 // Returns true if the priority queue is empty. Use a memory barrier before | |
38 // calling this to avoid getting a stale result. The returned result can be | |
fdoray
2016/02/11 17:30:33
to *mitigate the chances* of getting a stale resul
fdoray
2016/02/12 04:16:19
Done.
| |
39 // invalidated at any time by sequences being pushed or popped from the | |
40 // priority queue. | |
41 bool UnsynchronizedEmpty() const; | |
42 | |
43 class BASE_EXPORT Transaction { | |
fdoray
2016/02/11 17:30:33
: public NonThreadSafe
fdoray
2016/02/12 04:16:19
Done.
| |
44 public: | |
45 ~Transaction(); | |
46 | |
47 // Adds |sequence| to the priority queue. The position of |sequence| in the | |
48 // priority queue is determined by |sort_key|. | |
49 void PushSequence(scoped_refptr<Sequence> sequence, | |
50 const SequenceSortKey& sort_key); | |
51 | |
52 // Returns the sequence with the lowest sort key in the priority queue and | |
53 // sets |sort_key| to the sort key provided when that sequence was inserted | |
54 // in the priority queue. Returns nullptr if the priority queue is empty. | |
55 scoped_refptr<Sequence> PeekSequence(SequenceSortKey* sort_key) const; | |
56 | |
57 // Removes the sequence with the lowest sort key in the priority queue. | |
58 // Cannot be called on an empty priority queue. | |
59 void PopSequence(); | |
60 | |
61 private: | |
62 friend class PriorityQueue; | |
63 | |
64 Transaction(PriorityQueue* priority_queue); | |
65 | |
66 PriorityQueue* priority_queue_; | |
67 | |
68 // Holds the lock of |priority_queue_| for the lifetime of the transaction. | |
69 scoped_ptr<AutoSchedulerLock> auto_lock_; | |
70 | |
71 // Number of times that PushSequence() has been called on this transaction. | |
72 size_t num_pushed_sequences_; | |
73 | |
74 DISALLOW_COPY_AND_ASSIGN(Transaction); | |
75 }; | |
76 | |
77 // Begins a Transaction that can be used to perform peek and pop operations | |
78 // atomically on the priority queue. It is guaranteed that nothing else will | |
79 // modify the priority queue while the returned Transaction is alive. The | |
fdoray
2016/02/11 17:30:33
The *returned Transaction
fdoray
2016/02/12 04:16:19
Done.
| |
80 // Transaction can only be used on the thread on which it was created. It is | |
81 // illegal to call this on a thread that has an active transaction for another | |
82 // priority queue than the one specified in the constructor. | |
83 scoped_ptr<Transaction> BeginTransaction(); | |
84 | |
85 private: | |
86 // Controls access to |priority_queue_|. | |
fdoray
2016/02/11 17:30:33
to |container_|
fdoray
2016/02/12 04:16:19
Done.
| |
87 SchedulerLock lock_; | |
88 | |
89 using SequenceAndSortKeyPair = | |
90 std::pair<scoped_refptr<Sequence>, SequenceSortKey>; | |
91 struct SequenceAndSortKeyPairComparator { | |
92 bool operator()(const SequenceAndSortKeyPair& left, | |
93 const SequenceAndSortKeyPair& right) const; | |
94 }; | |
95 using ContainerType = std::priority_queue<SequenceAndSortKeyPair, | |
96 std::vector<SequenceAndSortKeyPair>, | |
97 SequenceAndSortKeyPairComparator>; | |
98 ContainerType container_; | |
99 | |
100 const Closure sequence_inserted_callback_; | |
101 | |
102 DISALLOW_COPY_AND_ASSIGN(PriorityQueue); | |
103 }; | |
104 | |
105 } // namespace task_scheduler | |
106 } // namespace base | |
107 | |
108 #endif // BASE_TASK_SCHEDULER_PRIORITY_QUEUE_H_ | |
OLD | NEW |