Commit 843872e1 authored by Sami Kyostila's avatar Sami Kyostila Committed by Chromium LUCI CQ

sequence_manager: Clean up cancelled task sweeping logic

Use STL algorithms to split out and remove cancelled tasks from the
task queue instead of reimplementing them manually. Also avoid exposing
protected fields of std::priority_queue.

No behavioral changes.

Bug: 1155905
Change-Id: I4afb25038dea1dc2114163e19ec9c100a780701f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2612900
Auto-Submit: Sami Kyöstilä <skyostil@chromium.org>
Commit-Queue: Sami Kyöstilä <skyostil@chromium.org>
Reviewed-by: default avatarCarlos Caballero <carlscab@google.com>
Cr-Commit-Position: refs/heads/master@{#841500}
parent ffb13abc
......@@ -1402,36 +1402,44 @@ void TaskQueueImpl::DelayedIncomingQueue::swap(DelayedIncomingQueue* rhs) {
void TaskQueueImpl::DelayedIncomingQueue::SweepCancelledTasks(
SequenceManagerImpl* sequence_manager) {
// Under the hood a std::priority_queue is a heap and usually it's built on
// top of a std::vector. We poke at that vector directly here to filter out
// canceled tasks in place.
pending_high_res_tasks_ -= queue_.SweepCancelledTasks(sequence_manager);
}
size_t TaskQueueImpl::DelayedIncomingQueue::PQueue::SweepCancelledTasks(
SequenceManagerImpl* sequence_manager) {
// Under the hood a std::priority_queue is a heap implemented top of a
// std::vector. We poke at that vector directly here to filter out canceled
// tasks in place.
bool task_deleted = false;
auto it = queue_.c.begin();
while (!queue_.c.empty() && it != queue_.c.end()) {
// TODO(crbug.com/1155905): Remove after figuring out the cause of the
// crash.
sequence_manager->RecordCrashKeys(*it);
if (it->task.IsCancelled()) {
if (it->is_high_res)
pending_high_res_tasks_--;
*it = std::move(queue_.c.back());
queue_.c.pop_back();
size_t num_high_res_tasks_swept = 0u;
auto is_cancelled = [sequence_manager, &num_high_res_tasks_swept,
&task_deleted](const Task& task) {
// TODO(crbug.com/1155905): Remove after figuring out
// the cause of the crash.
sequence_manager->RecordCrashKeys(task);
if (!task.task.IsCancelled())
return false;
if (task.is_high_res)
num_high_res_tasks_swept++;
task_deleted = true;
// Note: if we just removed the last task in the queue, |it| is now
// invalid and shouldn't be used.
} else {
it++;
}
}
return true;
};
c.erase(std::remove_if(c.begin(), c.end(), is_cancelled), c.end());
// If we deleted something, re-enforce the heap property.
if (task_deleted)
ranges::make_heap(queue_.c, queue_.comp);
ranges::make_heap(c, comp);
return num_high_res_tasks_swept;
}
Value TaskQueueImpl::DelayedIncomingQueue::AsValue(TimeTicks now) const {
return queue_.AsValue(now);
}
Value TaskQueueImpl::DelayedIncomingQueue::PQueue::AsValue(
TimeTicks now) const {
Value state(Value::Type::LIST);
for (const Task& task : queue_.c)
for (const Task& task : c)
state.Append(TaskAsValue(task, now));
return state;
}
......
......@@ -332,9 +332,14 @@ class BASE_EXPORT TaskQueueImpl {
private:
struct PQueue : public std::priority_queue<Task> {
// Expose the container and comparator.
using std::priority_queue<Task>::c;
using std::priority_queue<Task>::comp;
// Removes all cancelled tasks from the queue. Returns the number of
// removed high resolution tasks (which could be lower than the total
// number of removed tasks).
//
// TODO(crbug.com/1155905): we pass SequenceManager to be able to record
// crash keys. Remove this parameter after chasing down this crash.
size_t SweepCancelledTasks(SequenceManagerImpl* sequence_manager);
Value AsValue(TimeTicks now) const;
};
PQueue queue_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment