| Index: base/timer/timer.h
|
| diff --git a/base/timer/timer.h b/base/timer/timer.h
|
| index 50aedbd4cec28e97c408b2acd8b896c70526732c..f6117336e54b8aa5a591787d82acfc249f30f0a0 100644
|
| --- a/base/timer/timer.h
|
| +++ b/base/timer/timer.h
|
| @@ -39,7 +39,10 @@
|
| // other words, Reset is shorthand for calling Stop and then Start again with
|
| // the same arguments.
|
| //
|
| -// NOTE: These APIs are not thread safe. Always call from the same thread.
|
| +// These APIs are not thread safe. Always call from the same sequenced task
|
| +// runner - thread or worker pool. By default the scheduled tasks will be run
|
| +// on the same sequence that the APIs are called from, but this can be changed
|
| +// prior to any tasks being scheduled using SetTaskRunner().
|
|
|
| #ifndef BASE_TIMER_TIMER_H_
|
| #define BASE_TIMER_TIMER_H_
|
| @@ -57,26 +60,30 @@
|
| #include "base/callback.h"
|
| #include "base/location.h"
|
| #include "base/macros.h"
|
| +#include "base/memory/weak_ptr.h"
|
| +#include "base/sequence_checker.h"
|
| #include "base/time/time.h"
|
|
|
| namespace base {
|
|
|
| class BaseTimerTaskInternal;
|
| -class SingleThreadTaskRunner;
|
| +class SequencedTaskRunner;
|
| class TickClock;
|
|
|
| //-----------------------------------------------------------------------------
|
| -// This class wraps MessageLoop::PostDelayedTask to manage delayed and repeating
|
| -// tasks. It must be destructed on the same thread that starts tasks. There are
|
| -// DCHECKs in place to verify this.
|
| +// This class wraps TaskRunner::PostDelayedTask to manage delayed and repeating
|
| +// tasks. A Timer is bound to the sequence on which it is started. To destroy it
|
| +// on its initial sequence (shall it differ) it must first be stopped on the
|
| +// sequence it's bound to. There are DCHECKs in place to verify this. Note:
|
| +// Timer is sequence-affine, but may become thread-affine if coupled with a
|
| +// thread-affine TickClock.
|
| //
|
| class BASE_EXPORT Timer {
|
| public:
|
| - // Construct a timer in repeating or one-shot mode. Start or SetTaskInfo must
|
| - // be called later to set task info. |retain_user_task| determines whether the
|
| - // user_task is retained or reset when it runs or stops. If |tick_clock| is
|
| - // provided, it is used instead of TimeTicks::Now() to get TimeTicks when
|
| - // scheduling tasks.
|
| + // Construct a timer in repeating or one-shot mode. Start must be called later
|
| + // to set task info. |retain_user_task| determines whether the user_task is
|
| + // retained or reset when it runs or stops. If |tick_clock| is provided, it is
|
| + // used instead of TimeTicks::Now() to get TimeTicks when scheduling tasks.
|
| Timer(bool retain_user_task, bool is_repeating);
|
| Timer(bool retain_user_task, bool is_repeating, TickClock* tick_clock);
|
|
|
| @@ -101,9 +108,10 @@ class BASE_EXPORT Timer {
|
| virtual TimeDelta GetCurrentDelay() const;
|
|
|
| // Set the task runner on which the task should be scheduled. This method can
|
| - // only be called before any tasks have been scheduled. The task runner must
|
| - // run tasks on the same thread the timer is used on.
|
| - virtual void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner);
|
| + // only be called while this Timer is stopped. If a TickClock is set, it must
|
| + // support being used on both the "origin sequence" for this Timer and
|
| + // |task_runner|.
|
| + virtual void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner);
|
|
|
| // Start the timer to run at the given |delay| from now. If the timer is
|
| // already running, it will be replaced to call the given |user_task|.
|
| @@ -123,15 +131,6 @@ class BASE_EXPORT Timer {
|
| const TimeTicks& desired_run_time() const { return desired_run_time_; }
|
|
|
| protected:
|
| - // Returns the current tick count.
|
| - TimeTicks Now() const;
|
| -
|
| - // Used to initiate a new delayed task. This has the side-effect of disabling
|
| - // scheduled_task_ if it is non-null.
|
| - void SetTaskInfo(const tracked_objects::Location& posted_from,
|
| - TimeDelta delay,
|
| - const base::Closure& user_task);
|
| -
|
| void set_user_task(const Closure& task) { user_task_ = task; }
|
| void set_desired_run_time(TimeTicks desired) { desired_run_time_ = desired; }
|
| void set_is_running(bool running) { is_running_ = running; }
|
| @@ -142,18 +141,10 @@ class BASE_EXPORT Timer {
|
| bool is_running() const { return is_running_; }
|
|
|
| private:
|
| - friend class BaseTimerTaskInternal;
|
| -
|
| // Allocates a new scheduled_task_ and posts it on the current MessageLoop
|
| - // with the given |delay|. scheduled_task_ must be NULL. scheduled_run_time_
|
| - // and desired_run_time_ are reset to Now() + delay.
|
| + // with the given |delay|. This Timer must not be running when this is called.
|
| void PostNewScheduledTask(TimeDelta delay);
|
|
|
| - // Returns the task runner on which the task should be scheduled. If the
|
| - // corresponding task_runner_ field is null, the task runner for the current
|
| - // thread is returned.
|
| - scoped_refptr<SingleThreadTaskRunner> GetTaskRunner();
|
| -
|
| // Disable scheduled_task_ and abandon it so that it no longer refers back to
|
| // this object.
|
| void AbandonScheduledTask();
|
| @@ -161,19 +152,17 @@ class BASE_EXPORT Timer {
|
| // Called by BaseTimerTaskInternal when the MessageLoop runs it.
|
| void RunScheduledTask();
|
|
|
| - // Stop running task (if any) and abandon scheduled task (if any).
|
| - void StopAndAbandon() {
|
| - Stop();
|
| - AbandonScheduledTask();
|
| - }
|
| -
|
| - // When non-NULL, the scheduled_task_ is waiting in the MessageLoop to call
|
| - // RunScheduledTask() at scheduled_run_time_.
|
| - BaseTimerTaskInternal* scheduled_task_;
|
| + // A weak pointer to the BaseTimerTaskInternal associated with this Timer
|
| + // while it is running. It must only be used to post tasks to that
|
| + // BaseTimerTaskInternal and must not be dereferenced nor even used to check
|
| + // for null on the origin task runner (|is_running_| is used instead to know
|
| + // whether there is a pending task). Note: for performance benefits it *can*
|
| + // be dereferenced inline iff GetTaskRunner()->RunsTasksOnCurrentThread().
|
| + WeakPtr<BaseTimerTaskInternal> scheduled_task_weak_ref_;
|
|
|
| // The task runner on which the task should be scheduled. If it is null, the
|
| - // task runner for the current thread should be used.
|
| - scoped_refptr<SingleThreadTaskRunner> task_runner_;
|
| + // task runner for the current sequence is used.
|
| + scoped_refptr<SequencedTaskRunner> destination_task_runner_;
|
|
|
| // Location in user code.
|
| tracked_objects::Location posted_from_;
|
| @@ -196,8 +185,12 @@ class BASE_EXPORT Timer {
|
| // if the task must be run immediately.
|
| TimeTicks desired_run_time_;
|
|
|
| - // Thread ID of current MessageLoop for verifying single-threaded usage.
|
| - int thread_id_;
|
| + // Calls to the timer are not thread safe, so a checker is used detect
|
| + // incorrect usage in debug builds. Uses SequenceCheckerImpl to allow usage in
|
| + // non-DCHECK logic in ~Timer(). Note that this only verifies interactions
|
| + // with this Timer's API; tasks themselves may be scheduled to run on a
|
| + // different sequence through SetTaskRunner().
|
| + SequenceCheckerImpl origin_sequence_checker_;
|
|
|
| // Repeating timers automatically post the task again before calling the task
|
| // callback.
|
| @@ -212,6 +205,8 @@ class BASE_EXPORT Timer {
|
| // If true, user_task_ is scheduled to run sometime in the future.
|
| bool is_running_;
|
|
|
| + WeakPtrFactory<Timer> weak_ptr_factory_;
|
| +
|
| DISALLOW_COPY_AND_ASSIGN(Timer);
|
| };
|
|
|
| @@ -264,8 +259,7 @@ class RepeatingTimer : public BaseTimerMethodPointer {
|
|
|
| //-----------------------------------------------------------------------------
|
| // A Delay timer is like The Button from Lost. Once started, you have to keep
|
| -// calling Reset otherwise it will call the given method in the MessageLoop
|
| -// thread.
|
| +// calling Reset otherwise it will call the given method on the task runner.
|
| //
|
| // Once created, it is inactive until Reset is called. Once |delay| seconds have
|
| // passed since the last call to Reset, the callback is made. Once the callback
|
|
|