This source file includes following definitions.
- next_sequence_num_
- AddToIncomingQueue
- IsHighResolutionTimerEnabledForTesting
- IsIdleForTesting
- ReloadWorkQueue
- WillDestroyCurrentMessageLoop
- CalculateDelayedRuntime
- PostPendingTask
#include "base/message_loop/incoming_task_queue.h"
#include "base/debug/trace_event.h"
#include "base/location.h"
#include "base/message_loop/message_loop.h"
#include "base/synchronization/waitable_event.h"
namespace base {
namespace internal {
IncomingTaskQueue::IncomingTaskQueue(MessageLoop* message_loop)
: message_loop_(message_loop),
next_sequence_num_(0) {
}
bool IncomingTaskQueue::AddToIncomingQueue(
const tracked_objects::Location& from_here,
const Closure& task,
TimeDelta delay,
bool nestable) {
AutoLock locked(incoming_queue_lock_);
PendingTask pending_task(
from_here, task, CalculateDelayedRuntime(delay), nestable);
return PostPendingTask(&pending_task);
}
bool IncomingTaskQueue::IsHighResolutionTimerEnabledForTesting() {
#if defined(OS_WIN)
return !high_resolution_timer_expiration_.is_null();
#else
return true;
#endif
}
bool IncomingTaskQueue::IsIdleForTesting() {
AutoLock lock(incoming_queue_lock_);
return incoming_queue_.empty();
}
void IncomingTaskQueue::ReloadWorkQueue(TaskQueue* work_queue) {
DCHECK(work_queue->empty());
AutoLock lock(incoming_queue_lock_);
if (!incoming_queue_.empty())
incoming_queue_.Swap(work_queue);
DCHECK(incoming_queue_.empty());
}
void IncomingTaskQueue::WillDestroyCurrentMessageLoop() {
#if defined(OS_WIN)
if (!high_resolution_timer_expiration_.is_null()) {
Time::ActivateHighResolutionTimer(false);
high_resolution_timer_expiration_ = TimeTicks();
}
#endif
AutoLock lock(incoming_queue_lock_);
message_loop_ = NULL;
}
IncomingTaskQueue::~IncomingTaskQueue() {
DCHECK(!message_loop_);
}
TimeTicks IncomingTaskQueue::CalculateDelayedRuntime(TimeDelta delay) {
TimeTicks delayed_run_time;
if (delay > TimeDelta()) {
delayed_run_time = TimeTicks::Now() + delay;
#if defined(OS_WIN)
if (high_resolution_timer_expiration_.is_null()) {
bool needs_high_res_timers = delay.InMilliseconds() <
(2 * Time::kMinLowResolutionThresholdMs);
if (needs_high_res_timers) {
if (Time::ActivateHighResolutionTimer(true)) {
high_resolution_timer_expiration_ = TimeTicks::Now() +
TimeDelta::FromMilliseconds(
MessageLoop::kHighResolutionTimerModeLeaseTimeMs);
}
}
}
#endif
} else {
DCHECK_EQ(delay.InMilliseconds(), 0) << "delay should not be negative";
}
#if defined(OS_WIN)
if (!high_resolution_timer_expiration_.is_null()) {
if (TimeTicks::Now() > high_resolution_timer_expiration_) {
Time::ActivateHighResolutionTimer(false);
high_resolution_timer_expiration_ = TimeTicks();
}
}
#endif
return delayed_run_time;
}
bool IncomingTaskQueue::PostPendingTask(PendingTask* pending_task) {
incoming_queue_lock_.AssertAcquired();
if (!message_loop_) {
pending_task->task.Reset();
return false;
}
pending_task->sequence_num = next_sequence_num_++;
TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"),
"MessageLoop::PostTask",
TRACE_ID_MANGLE(message_loop_->GetTaskTraceID(*pending_task)));
bool was_empty = incoming_queue_.empty();
incoming_queue_.push(*pending_task);
pending_task->task.Reset();
message_loop_->ScheduleWork(was_empty);
return true;
}
}
}