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

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

Issue 637983003: Add support to base::Timer for custom task runners. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update for review feedback Created 6 years, 2 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 | base/timer/timer.cc » ('j') | base/timer/timer.cc » ('J')
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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 // disabled ones) from timer_unittests.cc pass locally. Some are disabled 48 // 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 49 // because they're flaky on the buildbot, but when you run them locally you
50 // should be able to tell the difference. 50 // should be able to tell the difference.
51 51
52 #include "base/base_export.h" 52 #include "base/base_export.h"
53 #include "base/basictypes.h" 53 #include "base/basictypes.h"
54 #include "base/bind.h" 54 #include "base/bind.h"
55 #include "base/bind_helpers.h" 55 #include "base/bind_helpers.h"
56 #include "base/callback.h" 56 #include "base/callback.h"
57 #include "base/location.h" 57 #include "base/location.h"
58 #include "base/single_thread_task_runner.h"
58 #include "base/time/time.h" 59 #include "base/time/time.h"
59 60
60 namespace base { 61 namespace base {
61 62
62 class BaseTimerTaskInternal; 63 class BaseTimerTaskInternal;
63 64
64 //----------------------------------------------------------------------------- 65 //-----------------------------------------------------------------------------
65 // This class wraps MessageLoop::PostDelayedTask to manage delayed and repeating 66 // This class wraps MessageLoop::PostDelayedTask to manage delayed and repeating
66 // tasks. It must be destructed on the same thread that starts tasks. There are 67 // tasks. It must be destructed on the same thread that starts tasks. There are
67 // DCHECKs in place to verify this. 68 // DCHECKs in place to verify this.
(...skipping 12 matching lines...) Expand all
80 bool is_repeating); 81 bool is_repeating);
81 82
82 virtual ~Timer(); 83 virtual ~Timer();
83 84
84 // Returns true if the timer is running (i.e., not stopped). 85 // Returns true if the timer is running (i.e., not stopped).
85 virtual bool IsRunning() const; 86 virtual bool IsRunning() const;
86 87
87 // Returns the current delay for this timer. 88 // Returns the current delay for this timer.
88 virtual TimeDelta GetCurrentDelay() const; 89 virtual TimeDelta GetCurrentDelay() const;
89 90
91 // Set the task runner on which the task should be scheduled. This method can
92 // only be called before any tasks have been scheduled.
93 virtual void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner);
94
90 // Start the timer to run at the given |delay| from now. If the timer is 95 // Start the timer to run at the given |delay| from now. If the timer is
91 // already running, it will be replaced to call the given |user_task|. 96 // already running, it will be replaced to call the given |user_task|.
92 virtual void Start(const tracked_objects::Location& posted_from, 97 virtual void Start(const tracked_objects::Location& posted_from,
93 TimeDelta delay, 98 TimeDelta delay,
94 const base::Closure& user_task); 99 const base::Closure& user_task);
95 100
96 // Call this method to stop and cancel the timer. It is a no-op if the timer 101 // Call this method to stop and cancel the timer. It is a no-op if the timer
97 // is not running. 102 // is not running.
98 virtual void Stop(); 103 virtual void Stop();
99 104
(...skipping 15 matching lines...) Expand all
115 bool is_repeating() const { return is_repeating_; } 120 bool is_repeating() const { return is_repeating_; }
116 121
117 private: 122 private:
118 friend class BaseTimerTaskInternal; 123 friend class BaseTimerTaskInternal;
119 124
120 // Allocates a new scheduled_task_ and posts it on the current MessageLoop 125 // Allocates a new scheduled_task_ and posts it on the current MessageLoop
121 // with the given |delay|. scheduled_task_ must be NULL. scheduled_run_time_ 126 // with the given |delay|. scheduled_task_ must be NULL. scheduled_run_time_
122 // and desired_run_time_ are reset to Now() + delay. 127 // and desired_run_time_ are reset to Now() + delay.
123 void PostNewScheduledTask(TimeDelta delay); 128 void PostNewScheduledTask(TimeDelta delay);
124 129
130 // Returns the task runner on which the task should be scheduled. If the
131 // corresponding task_runner_ field is NULL, the task runner for the current
Avi (use Gerrit) 2014/10/16 18:02:55 Here you say if the field is "NULL", but below you
petrcermak 2014/10/17 15:43:15 Done (use "is NULL").
132 // thread is set and returned.
133 scoped_refptr<SingleThreadTaskRunner> GetTaskRunner();
134
125 // Disable scheduled_task_ and abandon it so that it no longer refers back to 135 // Disable scheduled_task_ and abandon it so that it no longer refers back to
126 // this object. 136 // this object.
127 void AbandonScheduledTask(); 137 void AbandonScheduledTask();
128 138
129 // Called by BaseTimerTaskInternal when the MessageLoop runs it. 139 // Called by BaseTimerTaskInternal when the MessageLoop runs it.
130 void RunScheduledTask(); 140 void RunScheduledTask();
131 141
132 // Stop running task (if any) and abandon scheduled task (if any). 142 // Stop running task (if any) and abandon scheduled task (if any).
133 void StopAndAbandon() { 143 void StopAndAbandon() {
134 Stop(); 144 Stop();
135 AbandonScheduledTask(); 145 AbandonScheduledTask();
136 } 146 }
137 147
138 // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call 148 // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call
139 // RunScheduledTask() at scheduled_run_time_. 149 // RunScheduledTask() at scheduled_run_time_.
140 BaseTimerTaskInternal* scheduled_task_; 150 BaseTimerTaskInternal* scheduled_task_;
141 151
152 // The task runner on which the task should be scheduled. If null, the
153 // task runner for the current thread should be used.
154 scoped_refptr<SingleThreadTaskRunner> task_runner_;
155
142 // Location in user code. 156 // Location in user code.
143 tracked_objects::Location posted_from_; 157 tracked_objects::Location posted_from_;
144 // Delay requested by user. 158 // Delay requested by user.
145 TimeDelta delay_; 159 TimeDelta delay_;
146 // user_task_ is what the user wants to be run at desired_run_time_. 160 // user_task_ is what the user wants to be run at desired_run_time_.
147 base::Closure user_task_; 161 base::Closure user_task_;
148 162
149 // The estimated time that the MessageLoop will run the scheduled_task_ that 163 // The estimated time that the MessageLoop will run the scheduled_task_ that
150 // will call RunScheduledTask(). This time can be a "zero" TimeTicks if the 164 // will call RunScheduledTask(). This time can be a "zero" TimeTicks if the
151 // task must be run immediately. 165 // task must be run immediately.
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 : Timer(posted_from, delay, 252 : Timer(posted_from, delay,
239 base::Bind(method, base::Unretained(receiver)), 253 base::Bind(method, base::Unretained(receiver)),
240 false) {} 254 false) {}
241 255
242 void Reset() { Timer::Reset(); } 256 void Reset() { Timer::Reset(); }
243 }; 257 };
244 258
245 } // namespace base 259 } // namespace base
246 260
247 #endif // BASE_TIMER_TIMER_H_ 261 #endif // BASE_TIMER_TIMER_H_
OLDNEW
« no previous file with comments | « no previous file | base/timer/timer.cc » ('j') | base/timer/timer.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698