1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
base / task / sequence_manager / work_queue_sets.cc [blame]
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/task/sequence_manager/work_queue_sets.h"
#include <optional>
#include "base/check_op.h"
#include "base/task/sequence_manager/task_order.h"
#include "base/task/sequence_manager/work_queue.h"
namespace base {
namespace sequence_manager {
namespace internal {
WorkQueueSets::WorkQueueSets(const char* name,
Observer* observer,
const SequenceManager::Settings& settings)
: name_(name),
work_queue_heaps_(settings.priority_settings.priority_count()),
#if DCHECK_IS_ON()
last_rand_(settings.random_task_selection_seed),
#endif
observer_(observer) {
}
WorkQueueSets::~WorkQueueSets() = default;
void WorkQueueSets::AddQueue(WorkQueue* work_queue, size_t set_index) {
DCHECK(!work_queue->work_queue_sets());
DCHECK_LT(set_index, work_queue_heaps_.size());
DCHECK(!work_queue->heap_handle().IsValid());
std::optional<TaskOrder> key = work_queue->GetFrontTaskOrder();
work_queue->AssignToWorkQueueSets(this);
work_queue->AssignSetIndex(set_index);
if (!key)
return;
bool was_empty = work_queue_heaps_[set_index].empty();
work_queue_heaps_[set_index].insert({*key, work_queue});
if (was_empty)
observer_->WorkQueueSetBecameNonEmpty(set_index);
}
void WorkQueueSets::RemoveQueue(WorkQueue* work_queue) {
DCHECK_EQ(this, work_queue->work_queue_sets());
work_queue->AssignToWorkQueueSets(nullptr);
if (!work_queue->heap_handle().IsValid())
return;
size_t set_index = work_queue->work_queue_set_index();
DCHECK_LT(set_index, work_queue_heaps_.size());
work_queue_heaps_[set_index].erase(work_queue->heap_handle());
if (work_queue_heaps_[set_index].empty())
observer_->WorkQueueSetBecameEmpty(set_index);
DCHECK(!work_queue->heap_handle().IsValid());
}
void WorkQueueSets::ChangeSetIndex(WorkQueue* work_queue, size_t set_index) {
DCHECK_EQ(this, work_queue->work_queue_sets());
DCHECK_LT(set_index, work_queue_heaps_.size());
std::optional<TaskOrder> key = work_queue->GetFrontTaskOrder();
size_t old_set = work_queue->work_queue_set_index();
DCHECK_LT(old_set, work_queue_heaps_.size());
DCHECK_NE(old_set, set_index);
work_queue->AssignSetIndex(set_index);
DCHECK_EQ(key.has_value(), work_queue->heap_handle().IsValid());
if (!key)
return;
work_queue_heaps_[old_set].erase(work_queue->heap_handle());
bool was_empty = work_queue_heaps_[set_index].empty();
work_queue_heaps_[set_index].insert({*key, work_queue});
// Invoke `WorkQueueSetBecameNonEmpty()` before `WorkQueueSetBecameEmpty()` so
// `observer_` doesn't momentarily observe that all work queue sets are empty.
// TaskQueueSelectorTest.TestDisableEnable will fail if the order changes.
if (was_empty)
observer_->WorkQueueSetBecameNonEmpty(set_index);
if (work_queue_heaps_[old_set].empty()) {
observer_->WorkQueueSetBecameEmpty(old_set);
}
}
void WorkQueueSets::OnQueuesFrontTaskChanged(WorkQueue* work_queue) {
size_t set_index = work_queue->work_queue_set_index();
DCHECK_EQ(this, work_queue->work_queue_sets());
DCHECK_LT(set_index, work_queue_heaps_.size());
DCHECK(work_queue->heap_handle().IsValid());
DCHECK(!work_queue_heaps_[set_index].empty()) << " set_index = " << set_index;
if (auto key = work_queue->GetFrontTaskOrder()) {
// O(log n)
work_queue_heaps_[set_index].Replace(work_queue->heap_handle(),
{*key, work_queue});
} else {
// O(log n)
work_queue_heaps_[set_index].erase(work_queue->heap_handle());
DCHECK(!work_queue->heap_handle().IsValid());
if (work_queue_heaps_[set_index].empty())
observer_->WorkQueueSetBecameEmpty(set_index);
}
}
void WorkQueueSets::OnTaskPushedToEmptyQueue(WorkQueue* work_queue) {
// NOTE if this function changes, we need to keep |WorkQueueSets::AddQueue| in
// sync.
DCHECK_EQ(this, work_queue->work_queue_sets());
std::optional<TaskOrder> key = work_queue->GetFrontTaskOrder();
DCHECK(key);
size_t set_index = work_queue->work_queue_set_index();
DCHECK_LT(set_index, work_queue_heaps_.size())
<< " set_index = " << set_index;
// |work_queue| should not be in work_queue_heaps_[set_index].
DCHECK(!work_queue->heap_handle().IsValid());
bool was_empty = work_queue_heaps_[set_index].empty();
work_queue_heaps_[set_index].insert({*key, work_queue});
if (was_empty)
observer_->WorkQueueSetBecameNonEmpty(set_index);
}
void WorkQueueSets::OnPopMinQueueInSet(WorkQueue* work_queue) {
// Assume that `work_queue` contains the lowest `TaskOrder`.
size_t set_index = work_queue->work_queue_set_index();
DCHECK_EQ(this, work_queue->work_queue_sets());
DCHECK_LT(set_index, work_queue_heaps_.size());
DCHECK(!work_queue_heaps_[set_index].empty()) << " set_index = " << set_index;
DCHECK_EQ(work_queue_heaps_[set_index].top().value, work_queue)
<< " set_index = " << set_index;
DCHECK(work_queue->heap_handle().IsValid());
if (auto key = work_queue->GetFrontTaskOrder()) {
// O(log n)
work_queue_heaps_[set_index].ReplaceTop({*key, work_queue});
} else {
// O(log n)
work_queue_heaps_[set_index].pop();
DCHECK(!work_queue->heap_handle().IsValid());
DCHECK(work_queue_heaps_[set_index].empty() ||
work_queue_heaps_[set_index].top().value != work_queue);
if (work_queue_heaps_[set_index].empty()) {
observer_->WorkQueueSetBecameEmpty(set_index);
}
}
}
void WorkQueueSets::OnQueueBlocked(WorkQueue* work_queue) {
DCHECK_EQ(this, work_queue->work_queue_sets());
HeapHandle heap_handle = work_queue->heap_handle();
if (!heap_handle.IsValid())
return;
size_t set_index = work_queue->work_queue_set_index();
DCHECK_LT(set_index, work_queue_heaps_.size());
work_queue_heaps_[set_index].erase(heap_handle);
if (work_queue_heaps_[set_index].empty())
observer_->WorkQueueSetBecameEmpty(set_index);
}
std::optional<WorkQueueAndTaskOrder>
WorkQueueSets::GetOldestQueueAndTaskOrderInSet(size_t set_index) const {
DCHECK_LT(set_index, work_queue_heaps_.size());
if (work_queue_heaps_[set_index].empty())
return std::nullopt;
const OldestTaskOrder& oldest = work_queue_heaps_[set_index].top();
DCHECK(oldest.value->heap_handle().IsValid());
#if DCHECK_IS_ON()
std::optional<TaskOrder> order = oldest.value->GetFrontTaskOrder();
DCHECK(order && oldest.key == *order);
#endif
return WorkQueueAndTaskOrder(*oldest.value, oldest.key);
}
#if DCHECK_IS_ON()
std::optional<WorkQueueAndTaskOrder>
WorkQueueSets::GetRandomQueueAndTaskOrderInSet(size_t set_index) const {
DCHECK_LT(set_index, work_queue_heaps_.size());
if (work_queue_heaps_[set_index].empty())
return std::nullopt;
const OldestTaskOrder& chosen =
work_queue_heaps_[set_index].begin()[static_cast<long>(
Random() % work_queue_heaps_[set_index].size())];
#if DCHECK_IS_ON()
std::optional<TaskOrder> key = chosen.value->GetFrontTaskOrder();
DCHECK(key && chosen.key == *key);
#endif
return WorkQueueAndTaskOrder(*chosen.value, chosen.key);
}
#endif
bool WorkQueueSets::IsSetEmpty(size_t set_index) const {
DCHECK_LT(set_index, work_queue_heaps_.size())
<< " set_index = " << set_index;
return work_queue_heaps_[set_index].empty();
}
#if DCHECK_IS_ON() || !defined(NDEBUG)
bool WorkQueueSets::ContainsWorkQueueForTest(
const WorkQueue* work_queue) const {
std::optional<TaskOrder> task_order = work_queue->GetFrontTaskOrder();
for (const auto& heap : work_queue_heaps_) {
for (const OldestTaskOrder& heap_value_pair : heap) {
if (heap_value_pair.value == work_queue) {
DCHECK(task_order);
DCHECK(heap_value_pair.key == *task_order);
DCHECK_EQ(this, work_queue->work_queue_sets());
return true;
}
}
}
if (work_queue->work_queue_sets() == this) {
DCHECK(!task_order);
return true;
}
return false;
}
#endif
void WorkQueueSets::CollectSkippedOverLowerPriorityTasks(
const internal::WorkQueue* selected_work_queue,
std::vector<const Task*>* result) const {
std::optional<TaskOrder> task_order =
selected_work_queue->GetFrontTaskOrder();
CHECK(task_order);
for (size_t priority = selected_work_queue->work_queue_set_index() + 1;
priority < work_queue_heaps_.size(); priority++) {
for (const OldestTaskOrder& pair : work_queue_heaps_[priority]) {
pair.value->CollectTasksOlderThan(*task_order, result);
}
}
}
} // namespace internal
} // namespace sequence_manager
} // namespace base