This source file includes following definitions.
- CalcTimeDelta
- stopped_
- DetachAndRunShutdownLoop
- Quit
- PostDelayedTask
- PostNonNestableDelayedTask
- RunsTasksOnCurrentThread
- PostRunTasks
- PostDelayedRunTasks
- ProcessIncomingTasks
- RunDelayedTasks
- RunDueTasks
- RunTasks
- TaskSpringboard
#include "remoting/base/plugin_thread_task_runner.h"
#include "base/bind.h"
namespace {
base::TimeDelta CalcTimeDelta(base::TimeTicks when) {
return std::max(when - base::TimeTicks::Now(), base::TimeDelta());
}
}
namespace remoting {
PluginThreadTaskRunner::Delegate::~Delegate() {
}
PluginThreadTaskRunner::PluginThreadTaskRunner(Delegate* delegate)
: plugin_thread_id_(base::PlatformThread::CurrentId()),
event_(false, false),
delegate_(delegate),
next_sequence_num_(0),
quit_received_(false),
stopped_(false) {
}
PluginThreadTaskRunner::~PluginThreadTaskRunner() {
DCHECK(delegate_ == NULL);
DCHECK(stopped_);
}
void PluginThreadTaskRunner::DetachAndRunShutdownLoop() {
DCHECK(BelongsToCurrentThread());
{
base::AutoLock auto_lock(lock_);
DCHECK(delegate_ != NULL);
DCHECK(!stopped_);
delegate_ = NULL;
stopped_ = quit_received_;
}
scheduled_timers_.clear();
ProcessIncomingTasks();
RunDueTasks(base::TimeTicks::Now());
while (!stopped_) {
if (delayed_queue_.empty()) {
event_.Wait();
} else {
event_.TimedWait(CalcTimeDelta(delayed_queue_.top().delayed_run_time));
}
ProcessIncomingTasks();
RunDueTasks(base::TimeTicks::Now());
base::AutoLock auto_lock(lock_);
stopped_ = quit_received_;
}
}
void PluginThreadTaskRunner::Quit() {
base::AutoLock auto_lock(lock_);
if (!quit_received_) {
quit_received_ = true;
event_.Signal();
}
}
bool PluginThreadTaskRunner::PostDelayedTask(
const tracked_objects::Location& from_here,
const base::Closure& task,
base::TimeDelta delay) {
base::TimeTicks delayed_run_time;
if (delay > base::TimeDelta()) {
delayed_run_time = base::TimeTicks::Now() + delay;
} else {
DCHECK_EQ(delay.InMilliseconds(), 0) << "delay should not be negative";
}
base::PendingTask pending_task(from_here, task, delayed_run_time, false);
base::AutoLock locked(lock_);
pending_task.sequence_num = next_sequence_num_++;
if (incoming_queue_.empty()) {
PostRunTasks();
}
incoming_queue_.push(pending_task);
pending_task.task.Reset();
DCHECK(!quit_received_);
return true;
}
bool PluginThreadTaskRunner::PostNonNestableDelayedTask(
const tracked_objects::Location& from_here,
const base::Closure& task,
base::TimeDelta delay) {
return PostDelayedTask(from_here, task, delay);
}
bool PluginThreadTaskRunner::RunsTasksOnCurrentThread() const {
return base::PlatformThread::CurrentId() == plugin_thread_id_;
}
void PluginThreadTaskRunner::PostRunTasks() {
if (delegate_ != NULL) {
base::Closure closure = base::Bind(&PluginThreadTaskRunner::RunTasks, this);
delegate_->RunOnPluginThread(
base::TimeDelta(),
&PluginThreadTaskRunner::TaskSpringboard,
new base::Closure(closure));
} else {
event_.Signal();
}
}
void PluginThreadTaskRunner::PostDelayedRunTasks(base::TimeTicks when) {
DCHECK(BelongsToCurrentThread());
if (delegate_ != NULL) {
if (scheduled_timers_.insert(when).second) {
base::TimeDelta delay = CalcTimeDelta(when);
base::Closure closure =
base::Bind(&PluginThreadTaskRunner::RunDelayedTasks, this, when);
delegate_->RunOnPluginThread(
delay,
&PluginThreadTaskRunner::TaskSpringboard,
new base::Closure(closure));
}
} else {
event_.Signal();
}
}
void PluginThreadTaskRunner::ProcessIncomingTasks() {
DCHECK(BelongsToCurrentThread());
base::TaskQueue work_queue;
{
base::AutoLock locked(lock_);
incoming_queue_.Swap(&work_queue);
}
while (!work_queue.empty()) {
base::PendingTask pending_task = work_queue.front();
work_queue.pop();
if (pending_task.delayed_run_time.is_null()) {
pending_task.task.Run();
} else {
delayed_queue_.push(pending_task);
}
}
}
void PluginThreadTaskRunner::RunDelayedTasks(base::TimeTicks when) {
DCHECK(BelongsToCurrentThread());
scheduled_timers_.erase(when);
if (!stopped_) {
ProcessIncomingTasks();
RunDueTasks(base::TimeTicks::Now());
}
}
void PluginThreadTaskRunner::RunDueTasks(base::TimeTicks now) {
DCHECK(BelongsToCurrentThread());
while (!delayed_queue_.empty() &&
delayed_queue_.top().delayed_run_time <= now) {
delayed_queue_.top().task.Run();
delayed_queue_.pop();
}
if (!delayed_queue_.empty()) {
base::TimeTicks when = delayed_queue_.top().delayed_run_time;
if (scheduled_timers_.empty() || when < *scheduled_timers_.begin()) {
PostDelayedRunTasks(when);
}
}
}
void PluginThreadTaskRunner::RunTasks() {
DCHECK(BelongsToCurrentThread());
if (!stopped_) {
ProcessIncomingTasks();
RunDueTasks(base::TimeTicks::Now());
}
}
void PluginThreadTaskRunner::TaskSpringboard(void* data) {
base::Closure* task = reinterpret_cast<base::Closure*>(data);
task->Run();
delete task;
}
}