This source file includes following definitions.
- Run
- Abandon
- is_running_
- is_running_
- IsRunning
- GetCurrentDelay
- Start
- Stop
- Reset
- SetTaskInfo
- PostNewScheduledTask
- AbandonScheduledTask
- RunScheduledTask
#include "base/timer/timer.h"
#include <stddef.h>
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/platform_thread.h"
namespace base {
class BaseTimerTaskInternal {
public:
explicit BaseTimerTaskInternal(Timer* timer)
: timer_(timer) {
}
~BaseTimerTaskInternal() {
if (timer_)
timer_->StopAndAbandon();
}
void Run() {
if (!timer_)
return;
timer_->scheduled_task_ = NULL;
Timer* timer = timer_;
timer_ = NULL;
timer->RunScheduledTask();
}
void Abandon() {
timer_ = NULL;
}
private:
Timer* timer_;
};
Timer::Timer(bool retain_user_task, bool is_repeating)
: scheduled_task_(NULL),
thread_id_(0),
is_repeating_(is_repeating),
retain_user_task_(retain_user_task),
is_running_(false) {
}
Timer::Timer(const tracked_objects::Location& posted_from,
TimeDelta delay,
const base::Closure& user_task,
bool is_repeating)
: scheduled_task_(NULL),
posted_from_(posted_from),
delay_(delay),
user_task_(user_task),
thread_id_(0),
is_repeating_(is_repeating),
retain_user_task_(true),
is_running_(false) {
}
Timer::~Timer() {
StopAndAbandon();
}
bool Timer::IsRunning() const {
return is_running_;
}
TimeDelta Timer::GetCurrentDelay() const {
return delay_;
}
void Timer::Start(const tracked_objects::Location& posted_from,
TimeDelta delay,
const base::Closure& user_task) {
SetTaskInfo(posted_from, delay, user_task);
Reset();
}
void Timer::Stop() {
is_running_ = false;
if (!retain_user_task_)
user_task_.Reset();
}
void Timer::Reset() {
DCHECK(!user_task_.is_null());
if (!scheduled_task_) {
PostNewScheduledTask(delay_);
return;
}
if (delay_ > TimeDelta::FromMicroseconds(0))
desired_run_time_ = TimeTicks::Now() + delay_;
else
desired_run_time_ = TimeTicks();
if (desired_run_time_ >= scheduled_run_time_) {
is_running_ = true;
return;
}
AbandonScheduledTask();
PostNewScheduledTask(delay_);
}
void Timer::SetTaskInfo(const tracked_objects::Location& posted_from,
TimeDelta delay,
const base::Closure& user_task) {
posted_from_ = posted_from;
delay_ = delay;
user_task_ = user_task;
}
void Timer::PostNewScheduledTask(TimeDelta delay) {
DCHECK(scheduled_task_ == NULL);
is_running_ = true;
scheduled_task_ = new BaseTimerTaskInternal(this);
if (delay > TimeDelta::FromMicroseconds(0)) {
ThreadTaskRunnerHandle::Get()->PostDelayedTask(posted_from_,
base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)),
delay);
scheduled_run_time_ = desired_run_time_ = TimeTicks::Now() + delay;
} else {
ThreadTaskRunnerHandle::Get()->PostTask(posted_from_,
base::Bind(&BaseTimerTaskInternal::Run, base::Owned(scheduled_task_)));
scheduled_run_time_ = desired_run_time_ = TimeTicks();
}
if (!thread_id_)
thread_id_ = static_cast<int>(PlatformThread::CurrentId());
}
void Timer::AbandonScheduledTask() {
DCHECK(thread_id_ == 0 ||
thread_id_ == static_cast<int>(PlatformThread::CurrentId()));
if (scheduled_task_) {
scheduled_task_->Abandon();
scheduled_task_ = NULL;
}
}
void Timer::RunScheduledTask() {
if (!is_running_)
return;
if (desired_run_time_ > scheduled_run_time_) {
TimeTicks now = TimeTicks::Now();
if (desired_run_time_ > now) {
PostNewScheduledTask(desired_run_time_ - now);
return;
}
}
base::Closure task = user_task_;
if (is_repeating_)
PostNewScheduledTask(delay_);
else
Stop();
task.Run();
}
}