Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Unified Diff: third_party/WebKit/Source/platform/scheduler/base/work_queue.cc

Issue 2258713004: Make tasks cancellable inside the blink scheduler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Various comment nits addressed Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/platform/scheduler/base/work_queue.cc
diff --git a/third_party/WebKit/Source/platform/scheduler/base/work_queue.cc b/third_party/WebKit/Source/platform/scheduler/base/work_queue.cc
index 1864f581dd8fec1861f0811c50774ece0eadd4a3..7274802b8b202c321eb01879d80d1e3d7a8a4148 100644
--- a/third_party/WebKit/Source/platform/scheduler/base/work_queue.cc
+++ b/third_party/WebKit/Source/platform/scheduler/base/work_queue.cc
@@ -10,24 +10,19 @@ namespace blink {
namespace scheduler {
namespace internal {
-WorkQueue::WorkQueue(TaskQueueImpl* task_queue, const char* name)
- : work_queue_sets_(nullptr),
+WorkQueue::WorkQueue(TaskQueueImpl* task_queue,
+ const char* name,
+ TaskQueueImpl::Task::ComparatorFn comparator)
+ : work_queue_(comparator),
+ work_queue_sets_(nullptr),
task_queue_(task_queue),
work_queue_set_index_(0),
name_(name) {}
void WorkQueue::AsValueInto(base::trace_event::TracedValue* state) const {
- // Remove const to search |work_queue_| in the destructive manner. Restore the
- // content from |visited| later.
- std::queue<TaskQueueImpl::Task>* mutable_queue =
- const_cast<std::queue<TaskQueueImpl::Task>*>(&work_queue_);
- std::queue<TaskQueueImpl::Task> visited;
- while (!mutable_queue->empty()) {
- TaskQueueImpl::TaskAsValueInto(mutable_queue->front(), state);
- visited.push(std::move(mutable_queue->front()));
- mutable_queue->pop();
+ for (const TaskQueueImpl::Task& task : work_queue_) {
+ TaskQueueImpl::TaskAsValueInto(task, state);
}
- *mutable_queue = std::move(visited);
}
WorkQueue::~WorkQueue() {
@@ -38,40 +33,74 @@ WorkQueue::~WorkQueue() {
const TaskQueueImpl::Task* WorkQueue::GetFrontTask() const {
if (work_queue_.empty())
return nullptr;
- return &work_queue_.front();
+ return &*work_queue_.begin();
}
bool WorkQueue::GetFrontTaskEnqueueOrder(EnqueueOrder* enqueue_order) const {
if (work_queue_.empty())
return false;
- *enqueue_order = work_queue_.front().enqueue_order();
+ // Quick sanity check.
+ DCHECK_LE(work_queue_.begin()->enqueue_order(),
+ work_queue_.rbegin()->enqueue_order())
+ << task_queue_->GetName() << " : "
+ << work_queue_sets_->name() << " : " << name_;
+ *enqueue_order = work_queue_.begin()->enqueue_order();
return true;
}
void WorkQueue::Push(TaskQueueImpl::Task task) {
bool was_empty = work_queue_.empty();
- work_queue_.push(std::move(task));
- if (was_empty && work_queue_sets_)
+#ifndef NDEBUG
+ DCHECK(task.enqueue_order_set());
+#endif
+
+ // We expect |task| to be inserted at the end. Amoritized O(1).
+ work_queue_.insert(work_queue_.end(), std::move(task));
+ DCHECK_EQ(task.enqueue_order(), work_queue_.rbegin()->enqueue_order())
+ << task_queue_->GetName() << " : "
+ << work_queue_sets_->name() << " : " << name_
+ << "task [scheduled_run_time_" << task.delayed_run_time << ", "
+ << task.sequence_num <<
+ "] rbegin() [" << work_queue_.rbegin()->delayed_run_time << ", "
+ << work_queue_.rbegin()->sequence_num << "]";
+
+ if (was_empty && work_queue_sets_) {
work_queue_sets_->OnPushQueue(this);
+ }
}
-void WorkQueue::PushAndSetEnqueueOrder(TaskQueueImpl::Task task,
- EnqueueOrder enqueue_order) {
- bool was_empty = work_queue_.empty();
- work_queue_.push(std::move(task));
- work_queue_.back().set_enqueue_order(enqueue_order);
+bool WorkQueue::CancelTask(const TaskQueueImpl::Task& key) {
+ TaskQueueImpl::ComparatorQueue::iterator it = work_queue_.find(key);
+ if (it == work_queue_.end())
+ return false;
- if (was_empty && work_queue_sets_)
- work_queue_sets_->OnPushQueue(this);
+ if (it == work_queue_.begin()) {
+ EnqueueOrder erased_task_enqueue_order = it->enqueue_order();
+ work_queue_.erase(it);
+ // We can't guarantee this WorkQueue is the lowest value in the WorkQueueSet
+ // so we need to use OnQueueHeadChanged instead of OnPopQueue for
+ // correctness.
+ work_queue_sets_->OnQueueHeadChanged(this, erased_task_enqueue_order);
+ } else {
+ work_queue_.erase(it);
+ }
+ task_queue_->TraceQueueSize(false);
+ return true;
+}
+
+bool WorkQueue::IsTaskPending(const TaskQueueImpl::Task& key) const {
+ return work_queue_.find(key) != work_queue_.end();
}
void WorkQueue::PopTaskForTest() {
- work_queue_.pop();
+ if (work_queue_.empty())
+ return;
+ work_queue_.erase(work_queue_.begin());
}
-void WorkQueue::SwapLocked(std::queue<TaskQueueImpl::Task>& incoming_queue) {
+void WorkQueue::SwapLocked(TaskQueueImpl::ComparatorQueue& incoming_queue) {
+ DCHECK(work_queue_.empty());
std::swap(work_queue_, incoming_queue);
-
if (!work_queue_.empty() && work_queue_sets_)
work_queue_sets_->OnPushQueue(this);
task_queue_->TraceQueueSize(true);
@@ -80,8 +109,10 @@ void WorkQueue::SwapLocked(std::queue<TaskQueueImpl::Task>& incoming_queue) {
TaskQueueImpl::Task WorkQueue::TakeTaskFromWorkQueue() {
DCHECK(work_queue_sets_);
DCHECK(!work_queue_.empty());
- TaskQueueImpl::Task pending_task = std::move(work_queue_.front());
- work_queue_.pop();
+ TaskQueueImpl::ComparatorQueue::iterator it = work_queue_.begin();
+ TaskQueueImpl::Task pending_task =
+ std::move(const_cast<TaskQueueImpl::Task&>(*it));
+ work_queue_.erase(it);
work_queue_sets_->OnPopQueue(this);
task_queue_->TraceQueueSize(false);
return pending_task;

Powered by Google App Engine
This is Rietveld 408576698