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

Side by Side Diff: base/timer/timer.cc

Issue 2491613004: Make base::Timer sequence-friendly. (Closed)
Patch Set: rebase on dependency Created 3 years, 11 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/timer/timer.h" 5 #include "base/timer/timer.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/threading/platform_thread.h" 14 #include "base/threading/platform_thread.h"
16 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/sequenced_task_runner_handle.h"
17 #include "base/time/tick_clock.h" 16 #include "base/time/tick_clock.h"
18 17
19 namespace base { 18 namespace base {
20 19
21 // BaseTimerTaskInternal is a simple delegate for scheduling a callback to 20 // BaseTimerTaskInternal is a simple delegate for scheduling a callback to Timer
22 // Timer in the thread's default task runner. It also handles the following 21 // on the current sequence. It also handles the following edge cases:
23 // edge cases:
24 // - deleted by the task runner. 22 // - deleted by the task runner.
25 // - abandoned (orphaned) by Timer. 23 // - abandoned (orphaned) by Timer.
26 class BaseTimerTaskInternal { 24 class BaseTimerTaskInternal {
27 public: 25 public:
28 explicit BaseTimerTaskInternal(Timer* timer) 26 explicit BaseTimerTaskInternal(Timer* timer)
29 : timer_(timer) { 27 : timer_(timer) {
30 } 28 }
31 29
32 ~BaseTimerTaskInternal() { 30 ~BaseTimerTaskInternal() {
33 // This task may be getting cleared because the task runner has been 31 // This task may be getting cleared because the task runner has been
34 // destructed. If so, don't leave Timer with a dangling pointer 32 // destructed. If so, don't leave Timer with a dangling pointer
35 // to this. 33 // to this.
36 if (timer_) 34 if (timer_)
37 timer_->StopAndAbandon(); 35 timer_->Stop();
38 } 36 }
39 37
40 void Run() { 38 void Run() {
41 // timer_ is NULL if we were abandoned. 39 // |timer_| is nullptr if we were abandoned.
42 if (!timer_) 40 if (!timer_)
43 return; 41 return;
44 42
45 // *this will be deleted by the task runner, so Timer needs to 43 // *this will be deleted by the task runner, so Timer needs to
vmpstr 2017/02/01 21:13:11 nit: |this|
gab 2017/02/13 19:57:00 Done.
46 // forget us: 44 // forget us:
47 timer_->scheduled_task_ = NULL; 45 timer_->scheduled_task_ = nullptr;
48 46
49 // Although Timer should not call back into *this, let's clear 47 // Although Timer should not call back into *this, let's clear
50 // the timer_ member first to be pedantic. 48 // |timer_| first to be pedantic.
51 Timer* timer = timer_; 49 Timer* timer = timer_;
52 timer_ = NULL; 50 timer_ = nullptr;
53 timer->RunScheduledTask(); 51 timer->RunScheduledTask();
54 } 52 }
55 53
56 // The task remains in the MessageLoop queue, but nothing will happen when it 54 // The task remains in the queue, but nothing will happen when it runs.
57 // runs. 55 void Abandon() { timer_ = nullptr; }
58 void Abandon() {
59 timer_ = NULL;
60 }
61 56
62 private: 57 private:
63 Timer* timer_; 58 Timer* timer_;
59
60 DISALLOW_COPY_AND_ASSIGN(BaseTimerTaskInternal);
64 }; 61 };
65 62
66 Timer::Timer(bool retain_user_task, bool is_repeating) 63 Timer::Timer(bool retain_user_task, bool is_repeating)
67 : Timer(retain_user_task, is_repeating, nullptr) {} 64 : Timer(retain_user_task, is_repeating, nullptr) {}
68 65
69 Timer::Timer(bool retain_user_task, bool is_repeating, TickClock* tick_clock) 66 Timer::Timer(bool retain_user_task, bool is_repeating, TickClock* tick_clock)
70 : scheduled_task_(nullptr), 67 : scheduled_task_(nullptr),
71 thread_id_(0),
72 is_repeating_(is_repeating), 68 is_repeating_(is_repeating),
73 retain_user_task_(retain_user_task), 69 retain_user_task_(retain_user_task),
74 tick_clock_(tick_clock), 70 tick_clock_(tick_clock),
75 is_running_(false) {} 71 is_running_(false) {
72 // It is safe for the timer to be created on a different thread/sequence than
73 // the one from which the timer APIs are called. The first call to the
74 // checker's CalledOnValidSequence() method will re-bind the checker, and
75 // later calls will verify that the same task runner is used.
76 origin_sequence_checker_.DetachFromSequence();
77 }
76 78
77 Timer::Timer(const tracked_objects::Location& posted_from, 79 Timer::Timer(const tracked_objects::Location& posted_from,
78 TimeDelta delay, 80 TimeDelta delay,
79 const base::Closure& user_task, 81 const base::Closure& user_task,
80 bool is_repeating) 82 bool is_repeating)
81 : Timer(posted_from, delay, user_task, is_repeating, nullptr) {} 83 : Timer(posted_from, delay, user_task, is_repeating, nullptr) {}
82 84
83 Timer::Timer(const tracked_objects::Location& posted_from, 85 Timer::Timer(const tracked_objects::Location& posted_from,
84 TimeDelta delay, 86 TimeDelta delay,
85 const base::Closure& user_task, 87 const base::Closure& user_task,
86 bool is_repeating, 88 bool is_repeating,
87 TickClock* tick_clock) 89 TickClock* tick_clock)
88 : scheduled_task_(nullptr), 90 : scheduled_task_(nullptr),
89 posted_from_(posted_from), 91 posted_from_(posted_from),
90 delay_(delay), 92 delay_(delay),
91 user_task_(user_task), 93 user_task_(user_task),
92 thread_id_(0),
93 is_repeating_(is_repeating), 94 is_repeating_(is_repeating),
94 retain_user_task_(true), 95 retain_user_task_(true),
95 tick_clock_(tick_clock), 96 tick_clock_(tick_clock),
96 is_running_(false) {} 97 is_running_(false) {
98 // See comment in other constructor.
99 origin_sequence_checker_.DetachFromSequence();
100 }
97 101
98 Timer::~Timer() { 102 Timer::~Timer() {
99 StopAndAbandon(); 103 // As highlighted in the constructor. It's okay to start the Timer on a
104 // different sequence but it must then be sequentially stopped on that
105 // sequence as well before it can be deleted on its owning sequence.
106 DCHECK(origin_sequence_checker_.CalledOnValidSequence() || !is_running_);
107
108 // Don't call Stop() if |!origin_sequence_checker_.CalledOnValidSequence()|
109 // (which, per the above, implies |!is_running|).
110 if (is_running_) {
vmpstr 2017/02/01 21:13:11 This block can be rewritten as DCHECK(is_running_
gab 2017/02/13 19:57:00 Done.
111 Stop();
112 } else {
113 // There shouldn't be a pending |scheduled_task_| pointing at |this| if
114 // |!is_running|.
115 DCHECK(!scheduled_task_);
116 }
100 } 117 }
101 118
102 bool Timer::IsRunning() const { 119 bool Timer::IsRunning() const {
120 DCHECK(origin_sequence_checker_.CalledOnValidSequence());
103 return is_running_; 121 return is_running_;
104 } 122 }
105 123
106 TimeDelta Timer::GetCurrentDelay() const { 124 TimeDelta Timer::GetCurrentDelay() const {
125 DCHECK(origin_sequence_checker_.CalledOnValidSequence());
107 return delay_; 126 return delay_;
108 } 127 }
109 128
110 void Timer::SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) { 129 void Timer::SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) {
111 // Do not allow changing the task runner once something has been scheduled. 130 // Do not allow changing the task runner once something has been scheduled.
112 DCHECK_EQ(thread_id_, 0); 131 // Don't check for |origin_sequence_checker_.CalledOnValidSequence()| here to
132 // allow the use case of constructing the Timer and immediatetly invoking
133 // SetTaskRunner() before starting it (CalledOnValidSequence() would undo the
134 // DetachFromSequence() from the constructor). The |!is_running| check kind of
135 // verifies the same thing (and TSAN should catch callers that do it wrong but
136 // somehow evade all debug checks).
137 DCHECK(!is_running_);
113 task_runner_.swap(task_runner); 138 task_runner_.swap(task_runner);
114 } 139 }
115 140
116 void Timer::Start(const tracked_objects::Location& posted_from, 141 void Timer::Start(const tracked_objects::Location& posted_from,
117 TimeDelta delay, 142 TimeDelta delay,
118 const base::Closure& user_task) { 143 const base::Closure& user_task) {
119 SetTaskInfo(posted_from, delay, user_task); 144 DCHECK(origin_sequence_checker_.CalledOnValidSequence());
145
146 posted_from_ = posted_from;
147 delay_ = delay;
148 user_task_ = user_task;
149
120 Reset(); 150 Reset();
121 } 151 }
122 152
123 void Timer::Stop() { 153 void Timer::Stop() {
154 DCHECK(origin_sequence_checker_.CalledOnValidSequence());
155
156 // While a pending scheduled task could in theory be left in the queue in the
157 // hope of recycling it should the Timer be restarted before it fires, it's
158 // simpler to unconditionally abandon it as it helps avoid races between it
159 // and ~Timer() should its owning sequence (where it's constructed/destroyed)
160 // differ from its running sequence (where it was started -- bound to
161 // |origin_sequence_checker_|). Hopefully repeated fast start/stop of the
162 // same Timer is rare...
163 // Note: It's also important to abandon before |user_task_.Reset()| below as
164 // |user_task_| may indirectly own this Timer.
165 AbandonScheduledTask();
166
124 is_running_ = false; 167 is_running_ = false;
125 if (!retain_user_task_) 168 if (!retain_user_task_)
126 user_task_.Reset(); 169 user_task_.Reset();
170 // No more member accesses here: |this| could be deleted after freeing
171 // |user_task_|.
127 } 172 }
128 173
129 void Timer::Reset() { 174 void Timer::Reset() {
175 DCHECK(origin_sequence_checker_.CalledOnValidSequence());
130 DCHECK(!user_task_.is_null()); 176 DCHECK(!user_task_.is_null());
131 177
132 // If there's no pending task, start one up and return. 178 // If there's no pending task, start one up and return.
133 if (!scheduled_task_) { 179 if (!scheduled_task_) {
134 PostNewScheduledTask(delay_); 180 PostNewScheduledTask(delay_);
135 return; 181 return;
136 } 182 }
137 183
138 // Set the new desired_run_time_. 184 // Set the new |desired_run_time_|.
139 if (delay_ > TimeDelta::FromMicroseconds(0)) 185 if (delay_ > TimeDelta::FromMicroseconds(0))
140 desired_run_time_ = Now() + delay_; 186 desired_run_time_ = Now() + delay_;
141 else 187 else
142 desired_run_time_ = TimeTicks(); 188 desired_run_time_ = TimeTicks();
143 189
144 // We can use the existing scheduled task if it arrives before the new 190 // We can use the existing scheduled task if it arrives before the new
145 // desired_run_time_. 191 // |desired_run_time_|.
146 if (desired_run_time_ >= scheduled_run_time_) { 192 if (desired_run_time_ >= scheduled_run_time_) {
147 is_running_ = true; 193 is_running_ = true;
148 return; 194 return;
149 } 195 }
150 196
151 // We can't reuse the scheduled_task_, so abandon it and post a new one. 197 // We can't reuse the |scheduled_task_|, so abandon it and post a new one.
152 AbandonScheduledTask(); 198 AbandonScheduledTask();
153 PostNewScheduledTask(delay_); 199 PostNewScheduledTask(delay_);
154 } 200 }
155 201
156 TimeTicks Timer::Now() const { 202 TimeTicks Timer::Now() const {
203 DCHECK(origin_sequence_checker_.CalledOnValidSequence());
157 return tick_clock_ ? tick_clock_->NowTicks() : TimeTicks::Now(); 204 return tick_clock_ ? tick_clock_->NowTicks() : TimeTicks::Now();
158 } 205 }
159 206
160 void Timer::SetTaskInfo(const tracked_objects::Location& posted_from,
161 TimeDelta delay,
162 const base::Closure& user_task) {
163 posted_from_ = posted_from;
164 delay_ = delay;
165 user_task_ = user_task;
166 }
167
168 void Timer::PostNewScheduledTask(TimeDelta delay) { 207 void Timer::PostNewScheduledTask(TimeDelta delay) {
169 DCHECK(scheduled_task_ == NULL); 208 DCHECK(origin_sequence_checker_.CalledOnValidSequence());
209 DCHECK(!scheduled_task_);
170 is_running_ = true; 210 is_running_ = true;
171 scheduled_task_ = new BaseTimerTaskInternal(this); 211 scheduled_task_ = new BaseTimerTaskInternal(this);
172 if (delay > TimeDelta::FromMicroseconds(0)) { 212 if (delay > TimeDelta::FromMicroseconds(0)) {
173 GetTaskRunner()->PostDelayedTask(posted_from_, 213 SequencedTaskRunnerHandle::Get()->PostDelayedTask(
214 posted_from_,
174 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)), 215 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)),
175 delay); 216 delay);
176 scheduled_run_time_ = desired_run_time_ = Now() + delay; 217 scheduled_run_time_ = desired_run_time_ = Now() + delay;
177 } else { 218 } else {
178 GetTaskRunner()->PostTask(posted_from_, 219 SequencedTaskRunnerHandle::Get()->PostTask(
220 posted_from_,
179 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_))); 221 base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)));
180 scheduled_run_time_ = desired_run_time_ = TimeTicks(); 222 scheduled_run_time_ = desired_run_time_ = TimeTicks();
181 } 223 }
182 // Remember the thread ID that posts the first task -- this will be verified
183 // later when the task is abandoned to detect misuse from multiple threads.
184 if (!thread_id_)
185 thread_id_ = static_cast<int>(PlatformThread::CurrentId());
186 }
187
188 scoped_refptr<SingleThreadTaskRunner> Timer::GetTaskRunner() {
189 return task_runner_.get() ? task_runner_ : ThreadTaskRunnerHandle::Get();
190 } 224 }
191 225
192 void Timer::AbandonScheduledTask() { 226 void Timer::AbandonScheduledTask() {
193 DCHECK(thread_id_ == 0 || 227 DCHECK(origin_sequence_checker_.CalledOnValidSequence());
194 thread_id_ == static_cast<int>(PlatformThread::CurrentId()));
195 if (scheduled_task_) { 228 if (scheduled_task_) {
196 scheduled_task_->Abandon(); 229 scheduled_task_->Abandon();
197 scheduled_task_ = NULL; 230 scheduled_task_ = nullptr;
198 } 231 }
199 } 232 }
200 233
201 void Timer::RunScheduledTask() { 234 void Timer::RunScheduledTask() {
235 DCHECK(origin_sequence_checker_.CalledOnValidSequence());
236
202 // Task may have been disabled. 237 // Task may have been disabled.
203 if (!is_running_) 238 if (!is_running_)
204 return; 239 return;
205 240
206 // First check if we need to delay the task because of a new target time. 241 // First check if we need to delay the task because of a new target time.
207 if (desired_run_time_ > scheduled_run_time_) { 242 if (desired_run_time_ > scheduled_run_time_) {
208 // Now() can be expensive, so only call it if we know the user has changed 243 // Now() can be expensive, so only call it if we know the user has changed
209 // the desired_run_time_. 244 // the |desired_run_time_|.
210 TimeTicks now = Now(); 245 TimeTicks now = Now();
211 // Task runner may have called us late anyway, so only post a continuation 246 // Task runner may have called us late anyway, so only post a continuation
212 // task if the desired_run_time_ is in the future. 247 // task if the |desired_run_time_| is in the future.
213 if (desired_run_time_ > now) { 248 if (desired_run_time_ > now) {
214 // Post a new task to span the remaining time. 249 // Post a new task to span the remaining time.
215 PostNewScheduledTask(desired_run_time_ - now); 250 PostNewScheduledTask(desired_run_time_ - now);
216 return; 251 return;
217 } 252 }
218 } 253 }
219 254
220 // Make a local copy of the task to run. The Stop method will reset the 255 // Make a local copy of the task to run. The Stop method will reset the
221 // user_task_ member if retain_user_task_ is false. 256 // |user_task_| member if |retain_user_task_| is false.
222 base::Closure task = user_task_; 257 base::Closure task = user_task_;
223 258
224 if (is_repeating_) 259 if (is_repeating_)
225 PostNewScheduledTask(delay_); 260 PostNewScheduledTask(delay_);
226 else 261 else
227 Stop(); 262 Stop();
228 263
229 task.Run(); 264 if (task_runner_ && !task_runner_->RunsTasksOnCurrentThread())
265 task_runner_->PostTask(posted_from_, std::move(task));
266 else
267 task.Run();
230 268
231 // No more member accesses here: *this could be deleted at this point. 269 // No more member accesses here: *this could be deleted at this point.
232 } 270 }
233 271
234 } // namespace base 272 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698