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

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

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

Powered by Google App Engine
This is Rietveld 408576698