Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: base/message_loop/incoming_task_queue.cc

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/message_loop/incoming_task_queue.h ('k') | base/message_loop/message_loop.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/message_loop/incoming_task_queue.h"
6
7 #include <limits>
8
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"
14
15 namespace base {
16 namespace internal {
17
18 namespace {
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
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
32 // added
33 // to the incoming queue.
34 return type == MessageLoop::TYPE_UI || type == MessageLoop::TYPE_JAVA;
35 #else
36 return false;
37 #endif
38 }
39
40 } // namespace
41
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())),
48 is_ready_for_scheduling_(false) {
49 }
50
51 bool IncomingTaskQueue::AddToIncomingQueue(
52 const tracked_objects::Location& from_here,
53 const Closure& task,
54 TimeDelta delay,
55 bool nestable) {
56 DLOG_IF(WARNING,
57 delay.InSeconds() > kTaskDelayWarningThresholdInSeconds)
58 << "Requesting super-long task delay period of " << delay.InSeconds()
59 << " seconds from here: " << from_here.ToString();
60
61 AutoLock locked(incoming_queue_lock_);
62 PendingTask pending_task(
63 from_here, task, CalculateDelayedRuntime(delay), nestable);
64 #if defined(OS_WIN)
65 // We consider the task needs a high resolution timer if the delay is
66 // more than 0 and less than 32ms. This caps the relative error to
67 // less than 50% : a 33ms wait can wake at 48ms since the default
68 // resolution on Windows is between 10 and 15ms.
69 if (delay > TimeDelta() &&
70 delay.InMilliseconds() < (2 * Time::kMinLowResolutionThresholdMs)) {
71 ++high_res_task_count_;
72 pending_task.is_high_res = true;
73 }
74 #endif
75 return PostPendingTask(&pending_task);
76 }
77
78 bool IncomingTaskQueue::HasHighResolutionTasks() {
79 AutoLock lock(incoming_queue_lock_);
80 return high_res_task_count_ > 0;
81 }
82
83 bool IncomingTaskQueue::IsIdleForTesting() {
84 AutoLock lock(incoming_queue_lock_);
85 return incoming_queue_.empty();
86 }
87
88 int IncomingTaskQueue::ReloadWorkQueue(TaskQueue* work_queue) {
89 // Make sure no tasks are lost.
90 DCHECK(work_queue->empty());
91
92 // Acquire all we can from the inter-thread queue with one lock acquisition.
93 AutoLock lock(incoming_queue_lock_);
94 if (incoming_queue_.empty()) {
95 // If the loop attempts to reload but there are no tasks in the incoming
96 // queue, that means it will go to sleep waiting for more work. If the
97 // incoming queue becomes nonempty we need to schedule it again.
98 message_loop_scheduled_ = false;
99 } else {
100 incoming_queue_.Swap(work_queue);
101 }
102 // Reset the count of high resolution tasks since our queue is now empty.
103 int high_res_tasks = high_res_task_count_;
104 high_res_task_count_ = 0;
105 return high_res_tasks;
106 }
107
108 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() {
109 AutoLock lock(incoming_queue_lock_);
110 message_loop_ = NULL;
111 }
112
113 void IncomingTaskQueue::StartScheduling() {
114 AutoLock lock(incoming_queue_lock_);
115 DCHECK(!is_ready_for_scheduling_);
116 DCHECK(!message_loop_scheduled_);
117 is_ready_for_scheduling_ = true;
118 if (!incoming_queue_.empty())
119 ScheduleWork();
120 }
121
122 IncomingTaskQueue::~IncomingTaskQueue() {
123 // Verify that WillDestroyCurrentMessageLoop() has been called.
124 DCHECK(!message_loop_);
125 }
126
127 TimeTicks IncomingTaskQueue::CalculateDelayedRuntime(TimeDelta delay) {
128 TimeTicks delayed_run_time;
129 if (delay > TimeDelta())
130 delayed_run_time = TimeTicks::Now() + delay;
131 else
132 DCHECK_EQ(delay.InMilliseconds(), 0) << "delay should not be negative";
133 return delayed_run_time;
134 }
135
136 bool IncomingTaskQueue::PostPendingTask(PendingTask* pending_task) {
137 // Warning: Don't try to short-circuit, and handle this thread's tasks more
138 // directly, as it could starve handling of foreign threads. Put every task
139 // into this queue.
140
141 // This should only be called while the lock is taken.
142 incoming_queue_lock_.AssertAcquired();
143
144 if (!message_loop_) {
145 pending_task->task.Reset();
146 return false;
147 }
148
149 // Initialize the sequence number. The sequence number is used for delayed
150 // tasks (to faciliate FIFO sorting when two tasks have the same
151 // delayed_run_time value) and for identifying the task in about:tracing.
152 pending_task->sequence_num = next_sequence_num_++;
153
154 message_loop_->task_annotator()->DidQueueTask("MessageLoop::PostTask",
155 *pending_task);
156
157 bool was_empty = incoming_queue_.empty();
158 incoming_queue_.push(*pending_task);
159 pending_task->task.Reset();
160
161 if (is_ready_for_scheduling_ &&
162 (always_schedule_work_ || (!message_loop_scheduled_ && was_empty))) {
163 ScheduleWork();
164 }
165
166 return true;
167 }
168
169 void IncomingTaskQueue::ScheduleWork() {
170 DCHECK(is_ready_for_scheduling_);
171 // Wake up the message loop.
172 message_loop_->ScheduleWork();
173 // After we've scheduled the message loop, we do not need to do so again
174 // until we know it has processed all of the work in our queue and is
175 // waiting for more work again. The message loop will always attempt to
176 // reload from the incoming queue before waiting again so we clear this flag
177 // in ReloadWorkQueue().
178 message_loop_scheduled_ = true;
179 }
180
181 } // namespace internal
182 } // namespace base
OLDNEW
« no previous file with comments | « base/message_loop/incoming_task_queue.h ('k') | base/message_loop/message_loop.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698