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"
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"
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.
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
33 // to the incoming queue.
34 return type
== MessageLoop::TYPE_UI
|| type
== MessageLoop::TYPE_JAVA
;
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())) {
50 bool IncomingTaskQueue::AddToIncomingQueue(
51 const tracked_objects::Location
& from_here
,
56 delay
.InSeconds() > kTaskDelayWarningThresholdInSeconds
)
57 << "Requesting super-long task delay period of " << delay
.InSeconds()
58 << " seconds from here: " << from_here
.ToString();
60 AutoLock
locked(incoming_queue_lock_
);
61 PendingTask
pending_task(
62 from_here
, task
, CalculateDelayedRuntime(delay
), nestable
);
64 // We consider the task needs a high resolution timer if the delay is
65 // more than 0 and less than 32ms. This caps the relative error to
66 // less than 50% : a 33ms wait can wake at 48ms since the default
67 // resolution on Windows is between 10 and 15ms.
68 if (delay
> TimeDelta() &&
69 delay
.InMilliseconds() < (2 * Time::kMinLowResolutionThresholdMs
)) {
70 ++high_res_task_count_
;
71 pending_task
.is_high_res
= true;
74 return PostPendingTask(&pending_task
);
77 bool IncomingTaskQueue::HasHighResolutionTasks() {
78 AutoLock
lock(incoming_queue_lock_
);
79 return high_res_task_count_
> 0;
82 bool IncomingTaskQueue::IsIdleForTesting() {
83 AutoLock
lock(incoming_queue_lock_
);
84 return incoming_queue_
.empty();
87 int IncomingTaskQueue::ReloadWorkQueue(TaskQueue
* work_queue
) {
88 // Make sure no tasks are lost.
89 DCHECK(work_queue
->empty());
91 // Acquire all we can from the inter-thread queue with one lock acquisition.
92 AutoLock
lock(incoming_queue_lock_
);
93 if (incoming_queue_
.empty()) {
94 // If the loop attempts to reload but there are no tasks in the incoming
95 // queue, that means it will go to sleep waiting for more work. If the
96 // incoming queue becomes nonempty we need to schedule it again.
97 message_loop_scheduled_
= false;
99 incoming_queue_
.Swap(work_queue
);
101 // Reset the count of high resolution tasks since our queue is now empty.
102 int high_res_tasks
= high_res_task_count_
;
103 high_res_task_count_
= 0;
104 return high_res_tasks
;
107 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() {
108 AutoLock
lock(incoming_queue_lock_
);
109 message_loop_
= NULL
;
112 IncomingTaskQueue::~IncomingTaskQueue() {
113 // Verify that WillDestroyCurrentMessageLoop() has been called.
114 DCHECK(!message_loop_
);
117 TimeTicks
IncomingTaskQueue::CalculateDelayedRuntime(TimeDelta delay
) {
118 TimeTicks delayed_run_time
;
119 if (delay
> TimeDelta())
120 delayed_run_time
= TimeTicks::Now() + delay
;
122 DCHECK_EQ(delay
.InMilliseconds(), 0) << "delay should not be negative";
123 return delayed_run_time
;
126 bool IncomingTaskQueue::PostPendingTask(PendingTask
* pending_task
) {
127 // Warning: Don't try to short-circuit, and handle this thread's tasks more
128 // directly, as it could starve handling of foreign threads. Put every task
131 // This should only be called while the lock is taken.
132 incoming_queue_lock_
.AssertAcquired();
134 if (!message_loop_
) {
135 pending_task
->task
.Reset();
139 // Initialize the sequence number. The sequence number is used for delayed
140 // tasks (to faciliate FIFO sorting when two tasks have the same
141 // delayed_run_time value) and for identifying the task in about:tracing.
142 pending_task
->sequence_num
= next_sequence_num_
++;
144 message_loop_
->task_annotator()->DidQueueTask("MessageLoop::PostTask",
147 bool was_empty
= incoming_queue_
.empty();
148 incoming_queue_
.push(*pending_task
);
149 pending_task
->task
.Reset();
151 if (always_schedule_work_
|| (!message_loop_scheduled_
&& was_empty
)) {
152 // Wake up the message loop.
153 message_loop_
->ScheduleWork();
154 // After we've scheduled the message loop, we do not need to do so again
155 // until we know it has processed all of the work in our queue and is
156 // waiting for more work again. The message loop will always attempt to
157 // reload from the incoming queue before waiting again so we clear this flag
158 // in ReloadWorkQueue().
159 message_loop_scheduled_
= true;
165 } // namespace internal