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/metrics/histogram.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "base/time/time.h"
18 // Returns true if MessagePump::ScheduleWork() must be called one
19 // time for every task that is added to the MessageLoop incoming queue.
20 bool AlwaysNotifyPump(MessageLoop::Type type
) {
21 #if defined(OS_ANDROID)
22 // The Android UI message loop needs to get notified each time a task is
24 // to the incoming queue.
25 return type
== MessageLoop::TYPE_UI
|| type
== MessageLoop::TYPE_JAVA
;
33 IncomingTaskQueue::IncomingTaskQueue(MessageLoop
* message_loop
)
34 : high_res_task_count_(0),
35 message_loop_(message_loop
),
36 next_sequence_num_(0),
37 message_loop_scheduled_(false),
38 always_schedule_work_(AlwaysNotifyPump(message_loop_
->type())) {
41 bool IncomingTaskQueue::AddToIncomingQueue(
42 const tracked_objects::Location
& from_here
,
46 AutoLock
locked(incoming_queue_lock_
);
47 PendingTask
pending_task(
48 from_here
, task
, CalculateDelayedRuntime(delay
), nestable
);
50 // We consider the task needs a high resolution timer if the delay is
51 // more than 0 and less than 32ms. This caps the relative error to
52 // less than 50% : a 33ms wait can wake at 48ms since the default
53 // resolution on Windows is between 10 and 15ms.
54 if (delay
> TimeDelta() &&
55 delay
.InMilliseconds() < (2 * Time::kMinLowResolutionThresholdMs
)) {
56 ++high_res_task_count_
;
57 pending_task
.is_high_res
= true;
60 return PostPendingTask(&pending_task
);
63 bool IncomingTaskQueue::HasHighResolutionTasks() {
64 AutoLock
lock(incoming_queue_lock_
);
65 return high_res_task_count_
> 0;
68 bool IncomingTaskQueue::IsIdleForTesting() {
69 AutoLock
lock(incoming_queue_lock_
);
70 return incoming_queue_
.empty();
73 int IncomingTaskQueue::ReloadWorkQueue(TaskQueue
* work_queue
) {
74 // Make sure no tasks are lost.
75 DCHECK(work_queue
->empty());
77 // Acquire all we can from the inter-thread queue with one lock acquisition.
78 AutoLock
lock(incoming_queue_lock_
);
79 if (incoming_queue_
.empty()) {
80 // If the loop attempts to reload but there are no tasks in the incoming
81 // queue, that means it will go to sleep waiting for more work. If the
82 // incoming queue becomes nonempty we need to schedule it again.
83 message_loop_scheduled_
= false;
85 incoming_queue_
.Swap(work_queue
);
87 // Reset the count of high resolution tasks since our queue is now empty.
88 int high_res_tasks
= high_res_task_count_
;
89 high_res_task_count_
= 0;
90 return high_res_tasks
;
93 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() {
94 AutoLock
lock(incoming_queue_lock_
);
98 IncomingTaskQueue::~IncomingTaskQueue() {
99 // Verify that WillDestroyCurrentMessageLoop() has been called.
100 DCHECK(!message_loop_
);
103 TimeTicks
IncomingTaskQueue::CalculateDelayedRuntime(TimeDelta delay
) {
104 TimeTicks delayed_run_time
;
105 if (delay
> TimeDelta())
106 delayed_run_time
= TimeTicks::Now() + delay
;
108 DCHECK_EQ(delay
.InMilliseconds(), 0) << "delay should not be negative";
109 return delayed_run_time
;
112 bool IncomingTaskQueue::PostPendingTask(PendingTask
* pending_task
) {
113 // Warning: Don't try to short-circuit, and handle this thread's tasks more
114 // directly, as it could starve handling of foreign threads. Put every task
117 // This should only be called while the lock is taken.
118 incoming_queue_lock_
.AssertAcquired();
120 if (!message_loop_
) {
121 pending_task
->task
.Reset();
125 // Initialize the sequence number. The sequence number is used for delayed
126 // tasks (to faciliate FIFO sorting when two tasks have the same
127 // delayed_run_time value) and for identifying the task in about:tracing.
128 pending_task
->sequence_num
= next_sequence_num_
++;
130 message_loop_
->task_annotator()->DidQueueTask("MessageLoop::PostTask",
133 bool was_empty
= incoming_queue_
.empty();
134 incoming_queue_
.push(*pending_task
);
135 pending_task
->task
.Reset();
137 if (always_schedule_work_
|| (!message_loop_scheduled_
&& was_empty
)) {
138 // Wake up the message loop.
139 message_loop_
->ScheduleWork();
140 // After we've scheduled the message loop, we do not need to do so again
141 // until we know it has processed all of the work in our queue and is
142 // waiting for more work again. The message loop will always attempt to
143 // reload from the incoming queue before waiting again so we clear this flag
144 // in ReloadWorkQueue().
145 message_loop_scheduled_
= true;
151 } // namespace internal