Remove DnsConfigServiceTest.GetSystemConfig test
[chromium-blink-merge.git] / base / message_loop / incoming_task_queue.cc
blob5e9a4613da1cad2a28369aa78b69e87001ad9e9f
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 <limits>
9 #include "base/location.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/metrics/histogram.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/time/time.h"
15 namespace base {
16 namespace internal {
18 namespace {
20 #ifndef NDEBUG
21 // Delays larger than this are often bogus, and a warning should be emitted in
22 // debug builds to warn developers. http://crbug.com/450045
23 const int kTaskDelayWarningThresholdInSeconds =
24 14 * 24 * 60 * 60; // 14 days.
25 #endif
27 // Returns true if MessagePump::ScheduleWork() must be called one
28 // time for every task that is added to the MessageLoop incoming queue.
29 bool AlwaysNotifyPump(MessageLoop::Type type) {
30 #if defined(OS_ANDROID)
31 // The Android UI message loop needs to get notified each time a task is
32 // added
33 // to the incoming queue.
34 return type == MessageLoop::TYPE_UI || type == MessageLoop::TYPE_JAVA;
35 #else
36 return false;
37 #endif
40 } // namespace
42 IncomingTaskQueue::IncomingTaskQueue(MessageLoop* message_loop)
43 : high_res_task_count_(0),
44 message_loop_(message_loop),
45 next_sequence_num_(0),
46 message_loop_scheduled_(false),
47 always_schedule_work_(AlwaysNotifyPump(message_loop_->type())),
48 is_ready_for_scheduling_(false) {
51 bool IncomingTaskQueue::AddToIncomingQueue(
52 const tracked_objects::Location& from_here,
53 const Closure& task,
54 TimeDelta delay,
55 bool nestable) {
56 DLOG_IF(WARNING,
57 delay.InSeconds() > kTaskDelayWarningThresholdInSeconds)
58 << "Requesting super-long task delay period of " << delay.InSeconds()
59 << " seconds from here: " << from_here.ToString();
61 AutoLock locked(incoming_queue_lock_);
62 PendingTask pending_task(
63 from_here, task, CalculateDelayedRuntime(delay), nestable);
64 #if defined(OS_WIN)
65 // We consider the task needs a high resolution timer if the delay is
66 // more than 0 and less than 32ms. This caps the relative error to
67 // less than 50% : a 33ms wait can wake at 48ms since the default
68 // resolution on Windows is between 10 and 15ms.
69 if (delay > TimeDelta() &&
70 delay.InMilliseconds() < (2 * Time::kMinLowResolutionThresholdMs)) {
71 ++high_res_task_count_;
72 pending_task.is_high_res = true;
74 #endif
75 return PostPendingTask(&pending_task);
78 bool IncomingTaskQueue::HasHighResolutionTasks() {
79 AutoLock lock(incoming_queue_lock_);
80 return high_res_task_count_ > 0;
83 bool IncomingTaskQueue::IsIdleForTesting() {
84 AutoLock lock(incoming_queue_lock_);
85 return incoming_queue_.empty();
88 int IncomingTaskQueue::ReloadWorkQueue(TaskQueue* work_queue) {
89 // Make sure no tasks are lost.
90 DCHECK(work_queue->empty());
92 // Acquire all we can from the inter-thread queue with one lock acquisition.
93 AutoLock lock(incoming_queue_lock_);
94 if (incoming_queue_.empty()) {
95 // If the loop attempts to reload but there are no tasks in the incoming
96 // queue, that means it will go to sleep waiting for more work. If the
97 // incoming queue becomes nonempty we need to schedule it again.
98 message_loop_scheduled_ = false;
99 } else {
100 incoming_queue_.Swap(work_queue);
102 // Reset the count of high resolution tasks since our queue is now empty.
103 int high_res_tasks = high_res_task_count_;
104 high_res_task_count_ = 0;
105 return high_res_tasks;
108 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() {
109 AutoLock lock(incoming_queue_lock_);
110 message_loop_ = NULL;
113 void IncomingTaskQueue::StartScheduling() {
114 AutoLock lock(incoming_queue_lock_);
115 DCHECK(!is_ready_for_scheduling_);
116 DCHECK(!message_loop_scheduled_);
117 is_ready_for_scheduling_ = true;
118 if (!incoming_queue_.empty())
119 ScheduleWork();
122 IncomingTaskQueue::~IncomingTaskQueue() {
123 // Verify that WillDestroyCurrentMessageLoop() has been called.
124 DCHECK(!message_loop_);
127 TimeTicks IncomingTaskQueue::CalculateDelayedRuntime(TimeDelta delay) {
128 TimeTicks delayed_run_time;
129 if (delay > TimeDelta())
130 delayed_run_time = TimeTicks::Now() + delay;
131 else
132 DCHECK_EQ(delay.InMilliseconds(), 0) << "delay should not be negative";
133 return delayed_run_time;
136 bool IncomingTaskQueue::PostPendingTask(PendingTask* pending_task) {
137 // Warning: Don't try to short-circuit, and handle this thread's tasks more
138 // directly, as it could starve handling of foreign threads. Put every task
139 // into this queue.
141 // This should only be called while the lock is taken.
142 incoming_queue_lock_.AssertAcquired();
144 if (!message_loop_) {
145 pending_task->task.Reset();
146 return false;
149 // Initialize the sequence number. The sequence number is used for delayed
150 // tasks (to faciliate FIFO sorting when two tasks have the same
151 // delayed_run_time value) and for identifying the task in about:tracing.
152 pending_task->sequence_num = next_sequence_num_++;
154 message_loop_->task_annotator()->DidQueueTask("MessageLoop::PostTask",
155 *pending_task);
157 bool was_empty = incoming_queue_.empty();
158 incoming_queue_.push(*pending_task);
159 pending_task->task.Reset();
161 if (is_ready_for_scheduling_ &&
162 (always_schedule_work_ || (!message_loop_scheduled_ && was_empty))) {
163 ScheduleWork();
166 return true;
169 void IncomingTaskQueue::ScheduleWork() {
170 DCHECK(is_ready_for_scheduling_);
171 // Wake up the message loop.
172 message_loop_->ScheduleWork();
173 // After we've scheduled the message loop, we do not need to do so again
174 // until we know it has processed all of the work in our queue and is
175 // waiting for more work again. The message loop will always attempt to
176 // reload from the incoming queue before waiting again so we clear this flag
177 // in ReloadWorkQueue().
178 message_loop_scheduled_ = true;
181 } // namespace internal
182 } // namespace base