OLD | NEW |
---|---|
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 21 matching lines...) Expand all Loading... | |
32 // base::RepeatingTimer timer_; | 32 // base::RepeatingTimer timer_; |
33 // }; | 33 // }; |
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 // NOTE: These APIs are not thread safe. Always call from the same thread. | 42 // These APIs are not thread safe. Always call from the same sequenced task |
43 // runner - thread or worker pool. By default the scheduled tasks will be run | |
44 // on the same sequence that the APIs are called from, but this can be changed | |
45 // prior to any tasks being scheduled using SetTaskRunner(). | |
43 | 46 |
44 #ifndef BASE_TIMER_TIMER_H_ | 47 #ifndef BASE_TIMER_TIMER_H_ |
45 #define BASE_TIMER_TIMER_H_ | 48 #define BASE_TIMER_TIMER_H_ |
46 | 49 |
47 // IMPORTANT: If you change timer code, make sure that all tests (including | 50 // IMPORTANT: If you change timer code, make sure that all tests (including |
48 // disabled ones) from timer_unittests.cc pass locally. Some are disabled | 51 // disabled ones) from timer_unittests.cc pass locally. Some are disabled |
49 // because they're flaky on the buildbot, but when you run them locally you | 52 // because they're flaky on the buildbot, but when you run them locally you |
50 // should be able to tell the difference. | 53 // should be able to tell the difference. |
51 | 54 |
52 #include "base/base_export.h" | 55 #include "base/base_export.h" |
53 #include "base/basictypes.h" | 56 #include "base/basictypes.h" |
54 #include "base/bind.h" | 57 #include "base/bind.h" |
55 #include "base/bind_helpers.h" | 58 #include "base/bind_helpers.h" |
56 #include "base/callback.h" | 59 #include "base/callback.h" |
57 #include "base/location.h" | 60 #include "base/location.h" |
61 #include "base/sequence_checker.h" | |
58 #include "base/time/time.h" | 62 #include "base/time/time.h" |
59 | 63 |
60 namespace base { | 64 namespace base { |
61 | 65 |
62 class BaseTimerTaskInternal; | 66 class BaseTimerTaskInternal; |
63 class SingleThreadTaskRunner; | 67 class SequencedTaskRunner; |
64 | 68 |
65 //----------------------------------------------------------------------------- | 69 //----------------------------------------------------------------------------- |
66 // This class wraps MessageLoop::PostDelayedTask to manage delayed and repeating | 70 // This class wraps TaskRunner::PostDelayedTask to manage delayed and |
67 // tasks. It must be destructed on the same thread that starts tasks. There are | 71 // repeating tasks. It must be destructed on the same task runner that starts |
68 // DCHECKs in place to verify this. | 72 // tasks. There are DCHECKs in place to verify this. |
69 // | 73 // |
70 class BASE_EXPORT Timer { | 74 class BASE_EXPORT Timer { |
71 public: | 75 public: |
72 // Construct a timer in repeating or one-shot mode. Start or SetTaskInfo must | 76 // Construct a timer in repeating or one-shot mode. Start or SetTaskInfo must |
73 // be called later to set task info. |retain_user_task| determines whether the | 77 // be called later to set task info. |retain_user_task| determines whether the |
74 // user_task is retained or reset when it runs or stops. | 78 // user_task is retained or reset when it runs or stops. |
75 Timer(bool retain_user_task, bool is_repeating); | 79 Timer(bool retain_user_task, bool is_repeating); |
76 | 80 |
77 // Construct a timer with retained task info. | 81 // Construct a timer with retained task info. |
78 Timer(const tracked_objects::Location& posted_from, | 82 Timer(const tracked_objects::Location& posted_from, |
79 TimeDelta delay, | 83 TimeDelta delay, |
80 const base::Closure& user_task, | 84 const base::Closure& user_task, |
81 bool is_repeating); | 85 bool is_repeating); |
82 | 86 |
83 virtual ~Timer(); | 87 virtual ~Timer(); |
84 | 88 |
85 // Returns true if the timer is running (i.e., not stopped). | 89 // Returns true if the timer is running (i.e., not stopped). |
86 virtual bool IsRunning() const; | 90 virtual bool IsRunning() const; |
87 | 91 |
88 // Returns the current delay for this timer. | 92 // Returns the current delay for this timer. |
89 virtual TimeDelta GetCurrentDelay() const; | 93 virtual TimeDelta GetCurrentDelay() const; |
90 | 94 |
91 // Set the task runner on which the task should be scheduled. This method can | 95 // Set the task runner on which the task should be scheduled. This method can |
92 // only be called before any tasks have been scheduled. The task runner must | 96 // only be called before any tasks have been scheduled. |task_runner| must |
93 // run tasks on the same thread the timer is used on. | 97 // run tasks on the same sequenced the timer is used on. |
94 virtual void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner); | 98 virtual void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner); |
95 | 99 |
96 // Start the timer to run at the given |delay| from now. If the timer is | 100 // Start the timer to run at the given |delay| from now. If the timer is |
97 // already running, it will be replaced to call the given |user_task|. | 101 // already running, it will be replaced to call the given |user_task|. |
98 virtual void Start(const tracked_objects::Location& posted_from, | 102 virtual void Start(const tracked_objects::Location& posted_from, |
99 TimeDelta delay, | 103 TimeDelta delay, |
100 const base::Closure& user_task); | 104 const base::Closure& user_task); |
101 | 105 |
102 // Call this method to stop and cancel the timer. It is a no-op if the timer | 106 // Call this method to stop and cancel the timer. It is a no-op if the timer |
103 // is not running. | 107 // is not running. |
104 virtual void Stop(); | 108 virtual void Stop(); |
(...skipping 22 matching lines...) Expand all Loading... | |
127 bool is_running() const { return is_running_; } | 131 bool is_running() const { return is_running_; } |
128 | 132 |
129 private: | 133 private: |
130 friend class BaseTimerTaskInternal; | 134 friend class BaseTimerTaskInternal; |
131 | 135 |
132 // Allocates a new scheduled_task_ and posts it on the current MessageLoop | 136 // Allocates a new scheduled_task_ and posts it on the current MessageLoop |
133 // with the given |delay|. scheduled_task_ must be NULL. scheduled_run_time_ | 137 // with the given |delay|. scheduled_task_ must be NULL. scheduled_run_time_ |
134 // and desired_run_time_ are reset to Now() + delay. | 138 // and desired_run_time_ are reset to Now() + delay. |
135 void PostNewScheduledTask(TimeDelta delay); | 139 void PostNewScheduledTask(TimeDelta delay); |
136 | 140 |
137 // Returns the task runner on which the task should be scheduled. If the | 141 // Returns the task runner on which the task should be scheduled. If |
138 // corresponding task_runner_ field is null, the task runner for the current | 142 // |task_runner_| field is null, returns the SequencedTaskRunner for the |
139 // thread is returned. | 143 // current thread. |
140 scoped_refptr<SingleThreadTaskRunner> GetTaskRunner(); | 144 scoped_refptr<SequencedTaskRunner> GetTaskRunner(); |
141 | 145 |
142 // Disable scheduled_task_ and abandon it so that it no longer refers back to | 146 // Disable scheduled_task_ and abandon it so that it no longer refers back to |
143 // this object. | 147 // this object. |
144 void AbandonScheduledTask(); | 148 void AbandonScheduledTask(); |
145 | 149 |
146 // Called by BaseTimerTaskInternal when the MessageLoop runs it. | 150 // Called by BaseTimerTaskInternal when the MessageLoop runs it. |
147 void RunScheduledTask(); | 151 void RunScheduledTask(); |
148 | 152 |
149 // Stop running task (if any) and abandon scheduled task (if any). | 153 // Stop running task (if any) and abandon scheduled task (if any). |
150 void StopAndAbandon() { | 154 void StopAndAbandon() { |
151 Stop(); | 155 Stop(); |
152 AbandonScheduledTask(); | 156 AbandonScheduledTask(); |
153 } | 157 } |
154 | 158 |
155 // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call | 159 // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call |
156 // RunScheduledTask() at scheduled_run_time_. | 160 // RunScheduledTask() at scheduled_run_time_. |
157 BaseTimerTaskInternal* scheduled_task_; | 161 BaseTimerTaskInternal* scheduled_task_; |
158 | 162 |
159 // The task runner on which the task should be scheduled. If it is null, the | 163 // The task runner on which the task should be scheduled. If it is null, the |
160 // task runner for the current thread should be used. | 164 // task runner for the current thread is used. |
161 scoped_refptr<SingleThreadTaskRunner> task_runner_; | 165 scoped_refptr<SequencedTaskRunner> task_runner_; |
gab
2015/12/03 22:26:08
Maybe we should call this |destination_task_runner
jsbell
2015/12/05 01:15:39
Done.
| |
162 | 166 |
163 // Location in user code. | 167 // Location in user code. |
164 tracked_objects::Location posted_from_; | 168 tracked_objects::Location posted_from_; |
165 // Delay requested by user. | 169 // Delay requested by user. |
166 TimeDelta delay_; | 170 TimeDelta delay_; |
167 // user_task_ is what the user wants to be run at desired_run_time_. | 171 // user_task_ is what the user wants to be run at desired_run_time_. |
168 base::Closure user_task_; | 172 base::Closure user_task_; |
169 | 173 |
170 // The estimated time that the MessageLoop will run the scheduled_task_ that | 174 // The estimated time that the MessageLoop will run the scheduled_task_ that |
171 // will call RunScheduledTask(). This time can be a "zero" TimeTicks if the | 175 // will call RunScheduledTask(). This time can be a "zero" TimeTicks if the |
172 // task must be run immediately. | 176 // task must be run immediately. |
173 TimeTicks scheduled_run_time_; | 177 TimeTicks scheduled_run_time_; |
174 | 178 |
175 // The desired run time of user_task_. The user may update this at any time, | 179 // The desired run time of user_task_. The user may update this at any time, |
176 // even if their previous request has not run yet. If desired_run_time_ is | 180 // even if their previous request has not run yet. If desired_run_time_ is |
177 // greater than scheduled_run_time_, a continuation task will be posted to | 181 // greater than scheduled_run_time_, a continuation task will be posted to |
178 // wait for the remaining time. This allows us to reuse the pending task so as | 182 // wait for the remaining time. This allows us to reuse the pending task so as |
179 // not to flood the MessageLoop with orphaned tasks when the user code | 183 // not to flood the MessageLoop with orphaned tasks when the user code |
180 // excessively Stops and Starts the timer. This time can be a "zero" TimeTicks | 184 // excessively Stops and Starts the timer. This time can be a "zero" TimeTicks |
181 // if the task must be run immediately. | 185 // if the task must be run immediately. |
182 TimeTicks desired_run_time_; | 186 TimeTicks desired_run_time_; |
183 | 187 |
184 // Thread ID of current MessageLoop for verifying single-threaded usage. | 188 // Calls to the timer are not thread safe, so a checker is used detect |
185 int thread_id_; | 189 // incorrect usage in debug builds. Note that this verifies the timer API |
190 // calls; the tasks may be scheduled by the timer on a different sequence if | |
191 // SetTaskRunner() is used. | |
192 SequenceChecker sequence_checker_; | |
193 | |
194 // Once the timer has been scheduled, the task runner may not be changed. | |
gab
2015/12/03 22:26:08
s/the task runner/task_runner_/
jsbell
2015/12/05 01:15:39
Done.
| |
195 bool was_scheduled_; | |
186 | 196 |
187 // Repeating timers automatically post the task again before calling the task | 197 // Repeating timers automatically post the task again before calling the task |
188 // callback. | 198 // callback. |
189 const bool is_repeating_; | 199 const bool is_repeating_; |
190 | 200 |
191 // If true, hold on to the user_task_ closure object for reuse. | 201 // If true, hold on to the user_task_ closure object for reuse. |
192 const bool retain_user_task_; | 202 const bool retain_user_task_; |
193 | 203 |
194 // If true, user_task_ is scheduled to run sometime in the future. | 204 // If true, user_task_ is scheduled to run sometime in the future. |
195 bool is_running_; | 205 bool is_running_; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
235 | 245 |
236 //----------------------------------------------------------------------------- | 246 //----------------------------------------------------------------------------- |
237 // A simple, repeating timer. See usage notes at the top of the file. | 247 // A simple, repeating timer. See usage notes at the top of the file. |
238 class RepeatingTimer : public BaseTimerMethodPointer { | 248 class RepeatingTimer : public BaseTimerMethodPointer { |
239 public: | 249 public: |
240 RepeatingTimer() : BaseTimerMethodPointer(REPEATING) {} | 250 RepeatingTimer() : BaseTimerMethodPointer(REPEATING) {} |
241 }; | 251 }; |
242 | 252 |
243 //----------------------------------------------------------------------------- | 253 //----------------------------------------------------------------------------- |
244 // A Delay timer is like The Button from Lost. Once started, you have to keep | 254 // A Delay timer is like The Button from Lost. Once started, you have to keep |
245 // calling Reset otherwise it will call the given method in the MessageLoop | 255 // calling Reset otherwise it will call the given method on the task runner. |
246 // thread. | |
247 // | 256 // |
248 // Once created, it is inactive until Reset is called. Once |delay| seconds have | 257 // Once created, it is inactive until Reset is called. Once |delay| seconds have |
249 // passed since the last call to Reset, the callback is made. Once the callback | 258 // passed since the last call to Reset, the callback is made. Once the callback |
250 // has been made, it's inactive until Reset is called again. | 259 // has been made, it's inactive until Reset is called again. |
251 // | 260 // |
252 // If destroyed, the timeout is canceled and will not occur even if already | 261 // If destroyed, the timeout is canceled and will not occur even if already |
253 // inflight. | 262 // inflight. |
254 class DelayTimer : protected Timer { | 263 class DelayTimer : protected Timer { |
255 public: | 264 public: |
256 template <class Receiver> | 265 template <class Receiver> |
(...skipping 13 matching lines...) Expand all Loading... | |
270 // to link in MSVC. But clang-plugin does not allow inline definitions of | 279 // to link in MSVC. But clang-plugin does not allow inline definitions of |
271 // virtual methods, so the inline definition lives in the header file here | 280 // virtual methods, so the inline definition lives in the header file here |
272 // to satisfy both. | 281 // to satisfy both. |
273 inline void DelayTimer::Reset() { | 282 inline void DelayTimer::Reset() { |
274 Timer::Reset(); | 283 Timer::Reset(); |
275 } | 284 } |
276 | 285 |
277 } // namespace base | 286 } // namespace base |
278 | 287 |
279 #endif // BASE_TIMER_TIMER_H_ | 288 #endif // BASE_TIMER_TIMER_H_ |
OLD | NEW |