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

Unified Diff: base/timer/timer.cc

Issue 2912433002: Fix race in base::Timer when SetTaskRunner() is used
Patch Set: rebase Created 3 years, 6 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 | « base/timer/timer.h ('k') | build/sanitizers/tsan_suppressions.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/timer/timer.cc
diff --git a/base/timer/timer.cc b/base/timer/timer.cc
index abcd70aefa1c31212945b01867e6c039c27a3b9e..09f0300282f62eeca8d7ad0a69586d2ee08f0ee8 100644
--- a/base/timer/timer.cc
+++ b/base/timer/timer.cc
@@ -139,9 +139,7 @@ void Timer::Start(const tracked_objects::Location& posted_from,
}
void Timer::Stop() {
- // TODO(gab): Enable this when it's no longer called racily from
- // RunScheduledTask(): https://crbug.com/587199.
- // DCHECK(origin_sequence_checker_.CalledOnValidSequence());
+ DCHECK(origin_sequence_checker_.CalledOnValidSequence());
is_running_ = false;
if (!retain_user_task_)
@@ -179,44 +177,32 @@ void Timer::Reset() {
}
TimeTicks Timer::Now() const {
- // TODO(gab): Enable this when it's no longer called racily from
- // RunScheduledTask(): https://crbug.com/587199.
- // DCHECK(origin_sequence_checker_.CalledOnValidSequence());
+ DCHECK(origin_sequence_checker_.CalledOnValidSequence());
return tick_clock_ ? tick_clock_->NowTicks() : TimeTicks::Now();
}
void Timer::PostNewScheduledTask(TimeDelta delay) {
- // TODO(gab): Enable this when it's no longer called racily from
- // RunScheduledTask(): https://crbug.com/587199.
- // DCHECK(origin_sequence_checker_.CalledOnValidSequence());
+ DCHECK(origin_sequence_checker_.CalledOnValidSequence());
DCHECK(!scheduled_task_);
is_running_ = true;
scheduled_task_ = new BaseTimerTaskInternal(this);
if (delay > TimeDelta::FromMicroseconds(0)) {
- // TODO(gab): Posting BaseTimerTaskInternal::Run to another sequence makes
- // this code racy. https://crbug.com/587199
- GetTaskRunner()->PostDelayedTask(
+ SequencedTaskRunnerHandle::Get()->PostDelayedTask(
posted_from_,
base::BindOnce(&BaseTimerTaskInternal::Run,
base::Owned(scheduled_task_)),
delay);
scheduled_run_time_ = desired_run_time_ = Now() + delay;
} else {
- GetTaskRunner()->PostTask(posted_from_,
- base::BindOnce(&BaseTimerTaskInternal::Run,
- base::Owned(scheduled_task_)));
+ SequencedTaskRunnerHandle::Get()->PostTask(
+ posted_from_, base::BindOnce(&BaseTimerTaskInternal::Run,
+ base::Owned(scheduled_task_)));
scheduled_run_time_ = desired_run_time_ = TimeTicks();
}
}
-scoped_refptr<SequencedTaskRunner> Timer::GetTaskRunner() {
- return task_runner_.get() ? task_runner_ : SequencedTaskRunnerHandle::Get();
-}
-
void Timer::AbandonScheduledTask() {
- // TODO(gab): Enable this when it's no longer called racily from
- // RunScheduledTask() -> Stop(): https://crbug.com/587199.
- // DCHECK(origin_sequence_checker_.CalledOnValidSequence());
+ DCHECK(origin_sequence_checker_.CalledOnValidSequence());
if (scheduled_task_) {
scheduled_task_->Abandon();
scheduled_task_ = nullptr;
@@ -224,9 +210,7 @@ void Timer::AbandonScheduledTask() {
}
void Timer::RunScheduledTask() {
- // TODO(gab): Enable this when it's no longer called racily:
- // https://crbug.com/587199.
- // DCHECK(origin_sequence_checker_.CalledOnValidSequence());
+ DCHECK(origin_sequence_checker_.CalledOnValidSequence());
// Task may have been disabled.
if (!is_running_)
@@ -255,7 +239,10 @@ void Timer::RunScheduledTask() {
else
Stop();
- task.Run();
+ if (task_runner_ && !task_runner_->RunsTasksOnCurrentThread())
+ task_runner_->PostTask(posted_from_, std::move(task));
+ else
+ task.Run();
// No more member accesses here: |this| could be deleted at this point.
}
« no previous file with comments | « base/timer/timer.h ('k') | build/sanitizers/tsan_suppressions.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698