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

Unified Diff: base/task_scheduler/sequence.cc

Issue 2164103002: TaskScheduler: Don't delete Tasks in the scope of a Sequence's lock. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR robliao #6 (initialize variable at declaration) Created 4 years, 5 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
« no previous file with comments | « no previous file | base/task_scheduler/sequence_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/task_scheduler/sequence.cc
diff --git a/base/task_scheduler/sequence.cc b/base/task_scheduler/sequence.cc
index 4ecb60568cb88a6c013a3fb23d94c629fa22135e..86e99f0a4c390907f101f99cf6a08d29b86f08a7 100644
--- a/base/task_scheduler/sequence.cc
+++ b/base/task_scheduler/sequence.cc
@@ -36,16 +36,27 @@ const Task* Sequence::PeekTask() const {
}
bool Sequence::PopTask() {
- AutoSchedulerLock auto_lock(lock_);
- DCHECK(!queue_.empty());
+ // Delete the popped task outside the scope of |lock_|. This prevents a double
+ // acquisition of |lock_| if the task's destructor tries to post a task to
+ // this Sequence and reduces contention.
+ std::unique_ptr<Task> delete_outside_lock_scope;
+ bool sequence_empty_after_pop = false;
- const int priority_index =
- static_cast<int>(queue_.front()->traits.priority());
- DCHECK_GT(num_tasks_per_priority_[priority_index], 0U);
- --num_tasks_per_priority_[priority_index];
+ {
+ AutoSchedulerLock auto_lock(lock_);
+ DCHECK(!queue_.empty());
+
+ const int priority_index =
+ static_cast<int>(queue_.front()->traits.priority());
+ DCHECK_GT(num_tasks_per_priority_[priority_index], 0U);
+ --num_tasks_per_priority_[priority_index];
+
+ delete_outside_lock_scope = std::move(queue_.front());
+ queue_.pop();
+ sequence_empty_after_pop = queue_.empty();
+ }
- queue_.pop();
- return queue_.empty();
+ return sequence_empty_after_pop;
}
SequenceSortKey Sequence::GetSortKey() const {
« no previous file with comments | « no previous file | base/task_scheduler/sequence_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698