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

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

Issue 1991623002: Avoid holding |incoming_queue_lock_| while waking up the message loop. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@base-rw-lock
Patch Set: Rebase Created 4 years, 7 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
OLDNEW
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> 7 #include <limits>
8 8
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 19 matching lines...) Expand all
30 #if defined(OS_ANDROID) 30 #if defined(OS_ANDROID)
31 // 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
32 // added 32 // added
33 // to the incoming queue. 33 // to the incoming queue.
34 return type == MessageLoop::TYPE_UI || type == MessageLoop::TYPE_JAVA; 34 return type == MessageLoop::TYPE_UI || type == MessageLoop::TYPE_JAVA;
35 #else 35 #else
36 return false; 36 return false;
37 #endif 37 #endif
38 } 38 }
39 39
40 // Calculates the time at which a PendingTask should run.
41 TimeTicks CalculateDelayedRuntime(TimeDelta delay) {
42 TimeTicks delayed_run_time;
43 if (delay > TimeDelta())
44 delayed_run_time = TimeTicks::Now() + delay;
45 else
46 DCHECK_EQ(delay.InMilliseconds(), 0) << "delay should not be negative";
47 return delayed_run_time;
48 }
49
40 } // namespace 50 } // namespace
41 51
42 IncomingTaskQueue::IncomingTaskQueue(MessageLoop* message_loop) 52 IncomingTaskQueue::IncomingTaskQueue(MessageLoop* message_loop)
43 : high_res_task_count_(0), 53 : high_res_task_count_(0),
44 message_loop_(message_loop), 54 message_loop_(message_loop),
45 next_sequence_num_(0), 55 next_sequence_num_(0),
46 message_loop_scheduled_(false), 56 message_loop_scheduled_(false),
47 always_schedule_work_(AlwaysNotifyPump(message_loop_->type())), 57 always_schedule_work_(AlwaysNotifyPump(message_loop_->type())),
48 is_ready_for_scheduling_(false) { 58 is_ready_for_scheduling_(false) {
49 } 59 }
50 60
51 bool IncomingTaskQueue::AddToIncomingQueue( 61 bool IncomingTaskQueue::AddToIncomingQueue(
52 const tracked_objects::Location& from_here, 62 const tracked_objects::Location& from_here,
53 const Closure& task, 63 const Closure& task,
54 TimeDelta delay, 64 TimeDelta delay,
55 bool nestable) { 65 bool nestable) {
56 DLOG_IF(WARNING, 66 DLOG_IF(WARNING,
57 delay.InSeconds() > kTaskDelayWarningThresholdInSeconds) 67 delay.InSeconds() > kTaskDelayWarningThresholdInSeconds)
58 << "Requesting super-long task delay period of " << delay.InSeconds() 68 << "Requesting super-long task delay period of " << delay.InSeconds()
59 << " seconds from here: " << from_here.ToString(); 69 << " seconds from here: " << from_here.ToString();
60 70
61 AutoLock locked(incoming_queue_lock_);
62 PendingTask pending_task( 71 PendingTask pending_task(
63 from_here, task, CalculateDelayedRuntime(delay), nestable); 72 from_here, task, CalculateDelayedRuntime(delay), nestable);
64 #if defined(OS_WIN) 73 #if defined(OS_WIN)
65 // We consider the task needs a high resolution timer if the delay is 74 // 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 75 // 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 76 // less than 50% : a 33ms wait can wake at 48ms since the default
68 // resolution on Windows is between 10 and 15ms. 77 // resolution on Windows is between 10 and 15ms.
69 if (delay > TimeDelta() && 78 if (delay > TimeDelta() &&
70 delay.InMilliseconds() < (2 * Time::kMinLowResolutionThresholdMs)) { 79 delay.InMilliseconds() < (2 * Time::kMinLowResolutionThresholdMs)) {
71 ++high_res_task_count_;
72 pending_task.is_high_res = true; 80 pending_task.is_high_res = true;
73 } 81 }
74 #endif 82 #endif
75 return PostPendingTask(&pending_task); 83 return PostPendingTask(&pending_task);
76 } 84 }
77 85
78 bool IncomingTaskQueue::HasHighResolutionTasks() { 86 bool IncomingTaskQueue::HasHighResolutionTasks() {
79 AutoLock lock(incoming_queue_lock_); 87 AutoLock lock(incoming_queue_lock_);
80 return high_res_task_count_ > 0; 88 return high_res_task_count_ > 0;
81 } 89 }
(...skipping 17 matching lines...) Expand all
99 } else { 107 } else {
100 incoming_queue_.Swap(work_queue); 108 incoming_queue_.Swap(work_queue);
101 } 109 }
102 // Reset the count of high resolution tasks since our queue is now empty. 110 // Reset the count of high resolution tasks since our queue is now empty.
103 int high_res_tasks = high_res_task_count_; 111 int high_res_tasks = high_res_task_count_;
104 high_res_task_count_ = 0; 112 high_res_task_count_ = 0;
105 return high_res_tasks; 113 return high_res_tasks;
106 } 114 }
107 115
108 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() { 116 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() {
109 AutoLock lock(incoming_queue_lock_); 117 AutoWriteLock lock(message_loop_lock_);
110 message_loop_ = NULL; 118 message_loop_ = NULL;
111 } 119 }
112 120
113 void IncomingTaskQueue::StartScheduling() { 121 void IncomingTaskQueue::StartScheduling() {
114 AutoLock lock(incoming_queue_lock_); 122 bool schedule_work;
115 DCHECK(!is_ready_for_scheduling_); 123 {
116 DCHECK(!message_loop_scheduled_); 124 AutoLock lock(incoming_queue_lock_);
117 is_ready_for_scheduling_ = true; 125 DCHECK(!is_ready_for_scheduling_);
118 if (!incoming_queue_.empty()) 126 DCHECK(!message_loop_scheduled_);
119 ScheduleWork(); 127 is_ready_for_scheduling_ = true;
128 schedule_work = !incoming_queue_.empty();
129 }
130 if (schedule_work) {
131 AutoReadLock lock(message_loop_lock_);
danakj 2016/05/19 22:29:37 nit: name "hold_message_loop"
Anand Mistry (off Chromium) 2016/05/20 04:45:38 Removed as per comment below.
132 DCHECK(message_loop_);
danakj 2016/05/19 22:29:37 This method is called from the MessageLoop which i
Anand Mistry (off Chromium) 2016/05/20 04:45:38 Done. Thinking about it a bit more, calling Schedu
133 message_loop_->ScheduleWork();
danakj 2016/05/19 22:29:37 How come this doesn't set message_loop_scheduled_
Anand Mistry (off Chromium) 2016/05/20 04:45:38 It's not necessary. The worst that can happen is a
134 }
120 } 135 }
121 136
122 IncomingTaskQueue::~IncomingTaskQueue() { 137 IncomingTaskQueue::~IncomingTaskQueue() {
123 // Verify that WillDestroyCurrentMessageLoop() has been called. 138 // Verify that WillDestroyCurrentMessageLoop() has been called.
124 DCHECK(!message_loop_); 139 DCHECK(!message_loop_);
125 } 140 }
126 141
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) { 142 bool IncomingTaskQueue::PostPendingTask(PendingTask* pending_task) {
137 // Warning: Don't try to short-circuit, and handle this thread's tasks more 143 // 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 144 // directly, as it could starve handling of foreign threads. Put every task
139 // into this queue. 145 // into this queue.
140 146
141 // This should only be called while the lock is taken. 147 // Ensures |message_loop_| isn't null'd while running.
danakj 2016/05/19 22:29:37 "nulled". tho it's more like ensures the message l
Anand Mistry (off Chromium) 2016/05/20 04:45:37 yeha, changed to destroyed. The nulling happens in
142 incoming_queue_lock_.AssertAcquired(); 148 AutoReadLock ml_lock(message_loop_lock_);
danakj 2016/05/19 22:29:37 nit: name this variable "hold_message_loop"
Anand Mistry (off Chromium) 2016/05/20 04:45:38 Done.
143 149
144 if (!message_loop_) { 150 if (!message_loop_) {
145 pending_task->task.Reset(); 151 pending_task->task.Reset();
146 return false; 152 return false;
147 } 153 }
148 154
149 // Initialize the sequence number. The sequence number is used for delayed 155 bool schedule_work = false;
150 // tasks (to facilitate FIFO sorting when two tasks have the same 156 {
151 // delayed_run_time value) and for identifying the task in about:tracing. 157 AutoLock locked(incoming_queue_lock_);
danakj 2016/05/19 22:29:37 nit: name this variable "hold"
Anand Mistry (off Chromium) 2016/05/20 04:45:37 Done.
152 pending_task->sequence_num = next_sequence_num_++;
153 158
154 message_loop_->task_annotator()->DidQueueTask("MessageLoop::PostTask", 159 #if defined(OS_WIN)
155 *pending_task); 160 if (pending_task->is_high_res)
161 ++high_res_task_count_;
162 #endif
156 163
157 bool was_empty = incoming_queue_.empty(); 164 // Initialize the sequence number. The sequence number is used for delayed
158 incoming_queue_.push(*pending_task); 165 // tasks (to facilitate FIFO sorting when two tasks have the same
159 pending_task->task.Reset(); 166 // delayed_run_time value) and for identifying the task in about:tracing.
167 pending_task->sequence_num = next_sequence_num_++;
160 168
161 if (is_ready_for_scheduling_ && 169 message_loop_->task_annotator()->DidQueueTask("MessageLoop::PostTask",
162 (always_schedule_work_ || (!message_loop_scheduled_ && was_empty))) { 170 *pending_task);
163 ScheduleWork(); 171
172 bool was_empty = incoming_queue_.empty();
173 incoming_queue_.push(std::move(*pending_task));
174
175 if (is_ready_for_scheduling_ &&
176 (always_schedule_work_ || (!message_loop_scheduled_ && was_empty))) {
177 schedule_work = true;
178 // After we've scheduled the message loop, we do not need to do so again
179 // until we know it has processed all of the work in our queue and is
180 // waiting for more work again. The message loop will always attempt to
181 // reload from the incoming queue before waiting again so we clear this
182 // flag in ReloadWorkQueue().
183 message_loop_scheduled_ = true;
184 }
164 } 185 }
165 186
187 // Wake up the message loop and schedule work. This is done outside
188 // |incoming_queue_lock_| because signalling the message loop may cause this
189 // thread to be switched. If |incoming_queue_lock_| is held, any other thread
190 // that wants to post a task will be blocked until this thread switches back
191 // in and releases |incoming_queue_lock_|.
192 if (schedule_work)
193 message_loop_->ScheduleWork();
194
166 return true; 195 return true;
167 } 196 }
168 197
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 198 } // namespace internal
182 } // namespace base 199 } // namespace base
OLDNEW
« base/message_loop/incoming_task_queue.h ('K') | « base/message_loop/incoming_task_queue.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698