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/debug/trace_event.h"
8 #include "base/location.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/synchronization/waitable_event.h"
15 IncomingTaskQueue::IncomingTaskQueue(MessageLoop
* message_loop
)
16 : message_loop_(message_loop
),
17 next_sequence_num_(0) {
20 bool IncomingTaskQueue::AddToIncomingQueue(
21 const tracked_objects::Location
& from_here
,
25 AutoLock
locked(incoming_queue_lock_
);
26 PendingTask
pending_task(
27 from_here
, task
, CalculateDelayedRuntime(delay
), nestable
);
28 return PostPendingTask(&pending_task
);
31 bool IncomingTaskQueue::TryAddToIncomingQueue(
32 const tracked_objects::Location
& from_here
,
33 const Closure
& task
) {
34 if (!incoming_queue_lock_
.Try()) {
36 Closure local_task
= task
;
40 AutoLock
locked(incoming_queue_lock_
, AutoLock::AlreadyAcquired());
41 PendingTask
pending_task(
42 from_here
, task
, CalculateDelayedRuntime(TimeDelta()), true);
43 return PostPendingTask(&pending_task
);
46 bool IncomingTaskQueue::IsHighResolutionTimerEnabledForTesting() {
48 return !high_resolution_timer_expiration_
.is_null();
54 bool IncomingTaskQueue::IsIdleForTesting() {
55 AutoLock
lock(incoming_queue_lock_
);
56 return incoming_queue_
.empty();
59 void IncomingTaskQueue::LockWaitUnLockForTesting(WaitableEvent
* caller_wait
,
60 WaitableEvent
* caller_signal
) {
61 AutoLock
lock(incoming_queue_lock_
);
62 caller_wait
->Signal();
63 caller_signal
->Wait();
66 void IncomingTaskQueue::ReloadWorkQueue(TaskQueue
* work_queue
) {
67 // Make sure no tasks are lost.
68 DCHECK(work_queue
->empty());
70 // Acquire all we can from the inter-thread queue with one lock acquisition.
71 AutoLock
lock(incoming_queue_lock_
);
72 if (!incoming_queue_
.empty())
73 incoming_queue_
.Swap(work_queue
); // Constant time
75 DCHECK(incoming_queue_
.empty());
78 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() {
80 // If we left the high-resolution timer activated, deactivate it now.
81 // Doing this is not-critical, it is mainly to make sure we track
82 // the high resolution timer activations properly in our unit tests.
83 if (!high_resolution_timer_expiration_
.is_null()) {
84 Time::ActivateHighResolutionTimer(false);
85 high_resolution_timer_expiration_
= TimeTicks();
89 AutoLock
lock(incoming_queue_lock_
);
93 IncomingTaskQueue::~IncomingTaskQueue() {
94 // Verify that WillDestroyCurrentMessageLoop() has been called.
95 DCHECK(!message_loop_
);
98 TimeTicks
IncomingTaskQueue::CalculateDelayedRuntime(TimeDelta delay
) {
99 TimeTicks delayed_run_time
;
100 if (delay
> TimeDelta()) {
101 delayed_run_time
= TimeTicks::Now() + delay
;
104 if (high_resolution_timer_expiration_
.is_null()) {
105 // Windows timers are granular to 15.6ms. If we only set high-res
106 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms,
107 // which as a percentage is pretty inaccurate. So enable high
108 // res timers for any timer which is within 2x of the granularity.
109 // This is a tradeoff between accuracy and power management.
110 bool needs_high_res_timers
= delay
.InMilliseconds() <
111 (2 * Time::kMinLowResolutionThresholdMs
);
112 if (needs_high_res_timers
) {
113 if (Time::ActivateHighResolutionTimer(true)) {
114 high_resolution_timer_expiration_
= TimeTicks::Now() +
115 TimeDelta::FromMilliseconds(
116 MessageLoop::kHighResolutionTimerModeLeaseTimeMs
);
122 DCHECK_EQ(delay
.InMilliseconds(), 0) << "delay should not be negative";
126 if (!high_resolution_timer_expiration_
.is_null()) {
127 if (TimeTicks::Now() > high_resolution_timer_expiration_
) {
128 Time::ActivateHighResolutionTimer(false);
129 high_resolution_timer_expiration_
= TimeTicks();
134 return delayed_run_time
;
137 bool IncomingTaskQueue::PostPendingTask(PendingTask
* pending_task
) {
138 // Warning: Don't try to short-circuit, and handle this thread's tasks more
139 // directly, as it could starve handling of foreign threads. Put every task
142 // This should only be called while the lock is taken.
143 incoming_queue_lock_
.AssertAcquired();
145 if (!message_loop_
) {
146 pending_task
->task
.Reset();
150 // Initialize the sequence number. The sequence number is used for delayed
151 // tasks (to faciliate FIFO sorting when two tasks have the same
152 // delayed_run_time value) and for identifying the task in about:tracing.
153 pending_task
->sequence_num
= next_sequence_num_
++;
155 TRACE_EVENT_FLOW_BEGIN0("task", "MessageLoop::PostTask",
156 TRACE_ID_MANGLE(message_loop_
->GetTaskTraceID(*pending_task
)));
158 bool was_empty
= incoming_queue_
.empty();
159 incoming_queue_
.push(*pending_task
);
160 pending_task
->task
.Reset();
163 message_loop_
->ScheduleWork(was_empty
);
168 } // namespace internal