Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / base / message_loop / incoming_task_queue.cc
blob9bd12eed9636ff1eff56cb76271eeeacc21d9790
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/message_loop/incoming_task_queue.h"
7 #include "base/location.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/synchronization/waitable_event.h"
11 namespace base {
12 namespace internal {
14 IncomingTaskQueue::IncomingTaskQueue(MessageLoop* message_loop)
15 : message_loop_(message_loop),
16 next_sequence_num_(0) {
19 bool IncomingTaskQueue::AddToIncomingQueue(
20 const tracked_objects::Location& from_here,
21 const Closure& task,
22 TimeDelta delay,
23 bool nestable) {
24 AutoLock locked(incoming_queue_lock_);
25 PendingTask pending_task(
26 from_here, task, CalculateDelayedRuntime(delay), nestable);
27 return PostPendingTask(&pending_task);
30 bool IncomingTaskQueue::IsHighResolutionTimerEnabledForTesting() {
31 #if defined(OS_WIN)
32 return !high_resolution_timer_expiration_.is_null();
33 #else
34 return true;
35 #endif
38 bool IncomingTaskQueue::IsIdleForTesting() {
39 AutoLock lock(incoming_queue_lock_);
40 return incoming_queue_.empty();
43 void IncomingTaskQueue::ReloadWorkQueue(TaskQueue* work_queue) {
44 // Make sure no tasks are lost.
45 DCHECK(work_queue->empty());
47 // Acquire all we can from the inter-thread queue with one lock acquisition.
48 AutoLock lock(incoming_queue_lock_);
49 if (!incoming_queue_.empty())
50 incoming_queue_.Swap(work_queue); // Constant time
52 DCHECK(incoming_queue_.empty());
55 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() {
56 #if defined(OS_WIN)
57 // If we left the high-resolution timer activated, deactivate it now.
58 // Doing this is not-critical, it is mainly to make sure we track
59 // the high resolution timer activations properly in our unit tests.
60 if (!high_resolution_timer_expiration_.is_null()) {
61 Time::ActivateHighResolutionTimer(false);
62 high_resolution_timer_expiration_ = TimeTicks();
64 #endif
66 AutoLock lock(incoming_queue_lock_);
67 message_loop_ = NULL;
70 IncomingTaskQueue::~IncomingTaskQueue() {
71 // Verify that WillDestroyCurrentMessageLoop() has been called.
72 DCHECK(!message_loop_);
75 TimeTicks IncomingTaskQueue::CalculateDelayedRuntime(TimeDelta delay) {
76 TimeTicks delayed_run_time;
77 if (delay > TimeDelta()) {
78 delayed_run_time = TimeTicks::Now() + delay;
80 #if defined(OS_WIN)
81 if (high_resolution_timer_expiration_.is_null()) {
82 // Windows timers are granular to 15.6ms. If we only set high-res
83 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms,
84 // which as a percentage is pretty inaccurate. So enable high
85 // res timers for any timer which is within 2x of the granularity.
86 // This is a tradeoff between accuracy and power management.
87 bool needs_high_res_timers = delay.InMilliseconds() <
88 (2 * Time::kMinLowResolutionThresholdMs);
89 if (needs_high_res_timers) {
90 if (Time::ActivateHighResolutionTimer(true)) {
91 high_resolution_timer_expiration_ = TimeTicks::Now() +
92 TimeDelta::FromMilliseconds(
93 MessageLoop::kHighResolutionTimerModeLeaseTimeMs);
97 #endif
98 } else {
99 DCHECK_EQ(delay.InMilliseconds(), 0) << "delay should not be negative";
102 #if defined(OS_WIN)
103 if (!high_resolution_timer_expiration_.is_null()) {
104 if (TimeTicks::Now() > high_resolution_timer_expiration_) {
105 Time::ActivateHighResolutionTimer(false);
106 high_resolution_timer_expiration_ = TimeTicks();
109 #endif
111 return delayed_run_time;
114 bool IncomingTaskQueue::PostPendingTask(PendingTask* pending_task) {
115 // Warning: Don't try to short-circuit, and handle this thread's tasks more
116 // directly, as it could starve handling of foreign threads. Put every task
117 // into this queue.
119 // This should only be called while the lock is taken.
120 incoming_queue_lock_.AssertAcquired();
122 if (!message_loop_) {
123 pending_task->task.Reset();
124 return false;
127 // Initialize the sequence number. The sequence number is used for delayed
128 // tasks (to faciliate FIFO sorting when two tasks have the same
129 // delayed_run_time value) and for identifying the task in about:tracing.
130 pending_task->sequence_num = next_sequence_num_++;
132 message_loop_->task_annotator()->DidQueueTask("MessageLoop::PostTask",
133 *pending_task);
135 bool was_empty = incoming_queue_.empty();
136 incoming_queue_.push(*pending_task);
137 pending_task->task.Reset();
139 // Wake up the pump.
140 message_loop_->ScheduleWork(was_empty);
142 return true;
145 } // namespace internal
146 } // namespace base