OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop/incoming_task_queue.h" | 5 #include "base/message_loop/incoming_task_queue.h" |
6 | 6 |
| 7 #include <limits> |
| 8 |
7 #include "base/location.h" | 9 #include "base/location.h" |
8 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
9 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
10 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
11 #include "base/time/time.h" | 13 #include "base/time/time.h" |
12 | 14 |
13 namespace base { | 15 namespace base { |
14 namespace internal { | 16 namespace internal { |
15 | 17 |
16 namespace { | 18 namespace { |
17 | 19 |
| 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 |
| 26 |
18 // Returns true if MessagePump::ScheduleWork() must be called one | 27 // Returns true if MessagePump::ScheduleWork() must be called one |
19 // time for every task that is added to the MessageLoop incoming queue. | 28 // time for every task that is added to the MessageLoop incoming queue. |
20 bool AlwaysNotifyPump(MessageLoop::Type type) { | 29 bool AlwaysNotifyPump(MessageLoop::Type type) { |
21 #if defined(OS_ANDROID) | 30 #if defined(OS_ANDROID) |
22 // The Android UI message loop needs to get notified each time a task is | 31 // The Android UI message loop needs to get notified each time a task is |
23 // added | 32 // added |
24 // to the incoming queue. | 33 // to the incoming queue. |
25 return type == MessageLoop::TYPE_UI || type == MessageLoop::TYPE_JAVA; | 34 return type == MessageLoop::TYPE_UI || type == MessageLoop::TYPE_JAVA; |
26 #else | 35 #else |
27 return false; | 36 return false; |
28 #endif | 37 #endif |
29 } | 38 } |
30 | 39 |
31 } // namespace | 40 } // namespace |
32 | 41 |
33 IncomingTaskQueue::IncomingTaskQueue(MessageLoop* message_loop) | 42 IncomingTaskQueue::IncomingTaskQueue(MessageLoop* message_loop) |
34 : high_res_task_count_(0), | 43 : high_res_task_count_(0), |
35 message_loop_(message_loop), | 44 message_loop_(message_loop), |
36 next_sequence_num_(0), | 45 next_sequence_num_(0), |
37 message_loop_scheduled_(false), | 46 message_loop_scheduled_(false), |
38 always_schedule_work_(AlwaysNotifyPump(message_loop_->type())) { | 47 always_schedule_work_(AlwaysNotifyPump(message_loop_->type())) { |
39 } | 48 } |
40 | 49 |
41 bool IncomingTaskQueue::AddToIncomingQueue( | 50 bool IncomingTaskQueue::AddToIncomingQueue( |
42 const tracked_objects::Location& from_here, | 51 const tracked_objects::Location& from_here, |
43 const Closure& task, | 52 const Closure& task, |
44 TimeDelta delay, | 53 TimeDelta delay, |
45 bool nestable) { | 54 bool nestable) { |
| 55 DLOG_IF(WARNING, |
| 56 delay.InSeconds() > kTaskDelayWarningThresholdInSeconds) |
| 57 << "Requesting super-long task delay period of " << delay.InSeconds() |
| 58 << " seconds from here: " << from_here.ToString(); |
| 59 |
46 AutoLock locked(incoming_queue_lock_); | 60 AutoLock locked(incoming_queue_lock_); |
47 PendingTask pending_task( | 61 PendingTask pending_task( |
48 from_here, task, CalculateDelayedRuntime(delay), nestable); | 62 from_here, task, CalculateDelayedRuntime(delay), nestable); |
49 #if defined(OS_WIN) | 63 #if defined(OS_WIN) |
50 // We consider the task needs a high resolution timer if the delay is | 64 // 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 | 65 // 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 | 66 // less than 50% : a 33ms wait can wake at 48ms since the default |
53 // resolution on Windows is between 10 and 15ms. | 67 // resolution on Windows is between 10 and 15ms. |
54 if (delay > TimeDelta() && | 68 if (delay > TimeDelta() && |
55 delay.InMilliseconds() < (2 * Time::kMinLowResolutionThresholdMs)) { | 69 delay.InMilliseconds() < (2 * Time::kMinLowResolutionThresholdMs)) { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 // reload from the incoming queue before waiting again so we clear this flag | 157 // reload from the incoming queue before waiting again so we clear this flag |
144 // in ReloadWorkQueue(). | 158 // in ReloadWorkQueue(). |
145 message_loop_scheduled_ = true; | 159 message_loop_scheduled_ = true; |
146 } | 160 } |
147 | 161 |
148 return true; | 162 return true; |
149 } | 163 } |
150 | 164 |
151 } // namespace internal | 165 } // namespace internal |
152 } // namespace base | 166 } // namespace base |
OLD | NEW |