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

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

Issue 2958003002: Expose Timer::AbandonAndStop, so it can safely be destroyed on another thread. (Closed)
Patch Set: Expose AbandonAndStop and use it in AudioInputController. Created 3 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 unified diff | Download patch
« no previous file with comments | « no previous file | media/audio/audio_input_controller.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // OneShotTimer and RepeatingTimer provide a simple timer API. As the names 5 // OneShotTimer and RepeatingTimer provide a simple timer API. As the names
6 // suggest, OneShotTimer calls you back once after a time delay expires. 6 // suggest, OneShotTimer calls you back once after a time delay expires.
7 // RepeatingTimer on the other hand calls you back periodically with the 7 // RepeatingTimer on the other hand calls you back periodically with the
8 // prescribed time interval. 8 // prescribed time interval.
9 // 9 //
10 // OneShotTimer and RepeatingTimer both cancel the timer when they go out of 10 // OneShotTimer and RepeatingTimer both cancel the timer when they go out of
(...skipping 23 matching lines...) Expand all
34 // 34 //
35 // Both OneShotTimer and RepeatingTimer also support a Reset method, which 35 // Both OneShotTimer and RepeatingTimer also support a Reset method, which
36 // allows you to easily defer the timer event until the timer delay passes once 36 // allows you to easily defer the timer event until the timer delay passes once
37 // again. So, in the above example, if 0.5 seconds have already passed, 37 // again. So, in the above example, if 0.5 seconds have already passed,
38 // calling Reset on |timer_| would postpone DoStuff by another 1 second. In 38 // calling Reset on |timer_| would postpone DoStuff by another 1 second. In
39 // other words, Reset is shorthand for calling Stop and then Start again with 39 // other words, Reset is shorthand for calling Stop and then Start again with
40 // the same arguments. 40 // the same arguments.
41 // 41 //
42 // These APIs are not thread safe. All methods must be called from the same 42 // These APIs are not thread safe. All methods must be called from the same
43 // sequence (not necessarily the construction sequence), except for the 43 // sequence (not necessarily the construction sequence), except for the
44 // destructor and SetTaskRunner() which may be called from any sequence when the 44 // destructor and SetTaskRunner().
45 // timer is not running (i.e. when Start() has never been called or Stop() has 45 // - The destructor may be called from any sequence when the timer is not
46 // been called since the last Start()). By default, the scheduled tasks will be 46 // running and there is no scheduled task active, i.e. when Start() has never
47 // run on the same sequence that the Timer was *started on*, but this can be 47 // been called or after AbandonAndStop() has been called.
48 // changed *prior* to Start() via SetTaskRunner(). 48 // - SetTaskRunner() may be called from any sequence when the timer is not
49 // running, i.e. when Start() has never been called or Stop() has been called
50 // since the last Start().
51 //
52 // By default, the scheduled tasks will be run on the same sequence that the
53 // Timer was *started on*, but this can be changed *prior* to Start() via
54 // SetTaskRunner().
49 55
50 #ifndef BASE_TIMER_TIMER_H_ 56 #ifndef BASE_TIMER_TIMER_H_
51 #define BASE_TIMER_TIMER_H_ 57 #define BASE_TIMER_TIMER_H_
52 58
53 // IMPORTANT: If you change timer code, make sure that all tests (including 59 // IMPORTANT: If you change timer code, make sure that all tests (including
54 // disabled ones) from timer_unittests.cc pass locally. Some are disabled 60 // disabled ones) from timer_unittests.cc pass locally. Some are disabled
55 // because they're flaky on the buildbot, but when you run them locally you 61 // because they're flaky on the buildbot, but when you run them locally you
56 // should be able to tell the difference. 62 // should be able to tell the difference.
57 63
58 #include <memory> 64 #include <memory>
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 // Start the timer to run at the given |delay| from now. If the timer is 125 // Start the timer to run at the given |delay| from now. If the timer is
120 // already running, it will be replaced to call the given |user_task|. 126 // already running, it will be replaced to call the given |user_task|.
121 virtual void Start(const tracked_objects::Location& posted_from, 127 virtual void Start(const tracked_objects::Location& posted_from,
122 TimeDelta delay, 128 TimeDelta delay,
123 const base::Closure& user_task); 129 const base::Closure& user_task);
124 130
125 // Call this method to stop and cancel the timer. It is a no-op if the timer 131 // Call this method to stop and cancel the timer. It is a no-op if the timer
126 // is not running. 132 // is not running.
127 virtual void Stop(); 133 virtual void Stop();
128 134
135 // Stop running task (if any) and abandon scheduled task (if any).
136 void AbandonAndStop() {
137 AbandonScheduledTask();
138
139 Stop();
140 // No more member accesses here: |this| could be deleted at this point.
141 }
142
129 // Call this method to reset the timer delay. The |user_task_| must be set. If 143 // Call this method to reset the timer delay. The |user_task_| must be set. If
130 // the timer is not running, this will start it by posting a task. 144 // the timer is not running, this will start it by posting a task.
131 virtual void Reset(); 145 virtual void Reset();
132 146
133 const base::Closure& user_task() const { return user_task_; } 147 const base::Closure& user_task() const { return user_task_; }
134 const TimeTicks& desired_run_time() const { return desired_run_time_; } 148 const TimeTicks& desired_run_time() const { return desired_run_time_; }
135 149
136 protected: 150 protected:
137 // Returns the current tick count. 151 // Returns the current tick count.
138 TimeTicks Now() const; 152 TimeTicks Now() const;
(...skipping 20 matching lines...) Expand all
159 // sequence is returned. 173 // sequence is returned.
160 scoped_refptr<SequencedTaskRunner> GetTaskRunner(); 174 scoped_refptr<SequencedTaskRunner> GetTaskRunner();
161 175
162 // Disable |scheduled_task_| and abandon it so that it no longer refers back 176 // Disable |scheduled_task_| and abandon it so that it no longer refers back
163 // to this object. 177 // to this object.
164 void AbandonScheduledTask(); 178 void AbandonScheduledTask();
165 179
166 // Called by BaseTimerTaskInternal when the delayed task fires. 180 // Called by BaseTimerTaskInternal when the delayed task fires.
167 void RunScheduledTask(); 181 void RunScheduledTask();
168 182
169 // Stop running task (if any) and abandon scheduled task (if any).
170 void AbandonAndStop() {
171 AbandonScheduledTask();
172
173 Stop();
174 // No more member accesses here: |this| could be deleted at this point.
175 }
176
177 // When non-null, the |scheduled_task_| was posted to call RunScheduledTask() 183 // When non-null, the |scheduled_task_| was posted to call RunScheduledTask()
178 // at |scheduled_run_time_|. 184 // at |scheduled_run_time_|.
179 BaseTimerTaskInternal* scheduled_task_; 185 BaseTimerTaskInternal* scheduled_task_;
180 186
181 // The task runner on which the task should be scheduled. If it is null, the 187 // The task runner on which the task should be scheduled. If it is null, the
182 // task runner for the current sequence will be used. 188 // task runner for the current sequence will be used.
183 scoped_refptr<SequencedTaskRunner> task_runner_; 189 scoped_refptr<SequencedTaskRunner> task_runner_;
184 190
185 // Location in user code. 191 // Location in user code.
186 tracked_objects::Location posted_from_; 192 tracked_objects::Location posted_from_;
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 // to link in MSVC. But clang-plugin does not allow inline definitions of 315 // to link in MSVC. But clang-plugin does not allow inline definitions of
310 // virtual methods, so the inline definition lives in the header file here 316 // virtual methods, so the inline definition lives in the header file here
311 // to satisfy both. 317 // to satisfy both.
312 inline void DelayTimer::Reset() { 318 inline void DelayTimer::Reset() {
313 Timer::Reset(); 319 Timer::Reset();
314 } 320 }
315 321
316 } // namespace base 322 } // namespace base
317 323
318 #endif // BASE_TIMER_TIMER_H_ 324 #endif // BASE_TIMER_TIMER_H_
OLDNEW
« no previous file with comments | « no previous file | media/audio/audio_input_controller.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698