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"
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
,
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() {
32 return !high_resolution_timer_expiration_
.is_null();
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() {
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();
66 AutoLock
lock(incoming_queue_lock_
);
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
;
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
);
99 DCHECK_EQ(delay
.InMilliseconds(), 0) << "delay should not be negative";
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();
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
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();
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",
135 bool was_empty
= incoming_queue_
.empty();
136 incoming_queue_
.push(*pending_task
);
137 pending_task
->task
.Reset();
140 message_loop_
->ScheduleWork(was_empty
);
145 } // namespace internal