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

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

Issue 17567007: Made MessagePump a non-thread safe class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: base/time.h was moved to base/time/ Created 7 years, 5 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 | Annotate | Revision Log
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/message_loop.h" 5 #include "base/message_loop/message_loop.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent) 82 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent)
83 VALUE_TO_NUMBER_AND_NAME(kTimerEvent) 83 VALUE_TO_NUMBER_AND_NAME(kTimerEvent)
84 84
85 {-1, NULL} // The list must be null terminated, per API to histogram. 85 {-1, NULL} // The list must be null terminated, per API to histogram.
86 }; 86 };
87 87
88 bool enable_histogrammer_ = false; 88 bool enable_histogrammer_ = false;
89 89
90 MessageLoop::MessagePumpFactory* message_pump_for_ui_factory_ = NULL; 90 MessageLoop::MessagePumpFactory* message_pump_for_ui_factory_ = NULL;
91 91
92 // Create a process-wide unique ID to represent this task in trace events. This
93 // will be mangled with a Process ID hash to reduce the likelyhood of colliding
94 // with MessageLoop pointers on other processes.
95 uint64 GetTaskTraceID(const PendingTask& task, MessageLoop* loop) {
96 return (static_cast<uint64>(task.sequence_num) << 32) |
97 static_cast<uint64>(reinterpret_cast<intptr_t>(loop));
98 }
99
100 } // namespace 92 } // namespace
101 93
102 //------------------------------------------------------------------------------ 94 //------------------------------------------------------------------------------
103 95
104 #if defined(OS_WIN) 96 #if defined(OS_WIN)
105 97
106 // Upon a SEH exception in this thread, it restores the original unhandled 98 // Upon a SEH exception in this thread, it restores the original unhandled
107 // exception filter. 99 // exception filter.
108 static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) { 100 static int SEHFilter(LPTOP_LEVEL_EXCEPTION_FILTER old_filter) {
109 ::SetUnhandledExceptionFilter(old_filter); 101 ::SetUnhandledExceptionFilter(old_filter);
(...skipping 19 matching lines...) Expand all
129 MessageLoop::TaskObserver::~TaskObserver() { 121 MessageLoop::TaskObserver::~TaskObserver() {
130 } 122 }
131 123
132 MessageLoop::DestructionObserver::~DestructionObserver() { 124 MessageLoop::DestructionObserver::~DestructionObserver() {
133 } 125 }
134 126
135 //------------------------------------------------------------------------------ 127 //------------------------------------------------------------------------------
136 128
137 MessageLoop::MessageLoop(Type type) 129 MessageLoop::MessageLoop(Type type)
138 : type_(type), 130 : type_(type),
131 exception_restoration_(false),
139 nestable_tasks_allowed_(true), 132 nestable_tasks_allowed_(true),
140 exception_restoration_(false),
141 message_histogram_(NULL),
142 run_loop_(NULL),
143 #if defined(OS_WIN) 133 #if defined(OS_WIN)
144 os_modal_loop_(false), 134 os_modal_loop_(false),
145 #endif // OS_WIN 135 #endif // OS_WIN
146 next_sequence_num_(0) { 136 message_histogram_(NULL),
137 run_loop_(NULL) {
147 DCHECK(!current()) << "should only have one message loop per thread"; 138 DCHECK(!current()) << "should only have one message loop per thread";
148 lazy_tls_ptr.Pointer()->Set(this); 139 lazy_tls_ptr.Pointer()->Set(this);
149 140
150 message_loop_proxy_ = new MessageLoopProxyImpl(); 141 message_loop_proxy_ = new MessageLoopProxyImpl();
151 thread_task_runner_handle_.reset( 142 thread_task_runner_handle_.reset(
152 new ThreadTaskRunnerHandle(message_loop_proxy_)); 143 new ThreadTaskRunnerHandle(message_loop_proxy_));
153 144
154 // TODO(rvargas): Get rid of the OS guards. 145 // TODO(rvargas): Get rid of the OS guards.
155 #if defined(OS_WIN) 146 #if defined(OS_WIN)
156 #define MESSAGE_PUMP_UI new MessagePumpForUI() 147 #define MESSAGE_PUMP_UI new MessagePumpForUI()
157 #define MESSAGE_PUMP_IO new MessagePumpForIO() 148 #define MESSAGE_PUMP_IO new MessagePumpForIO()
158 #elif defined(OS_IOS) 149 #elif defined(OS_IOS)
159 #define MESSAGE_PUMP_UI MessagePumpMac::Create() 150 #define MESSAGE_PUMP_UI MessagePumpMac::Create()
160 #define MESSAGE_PUMP_IO new MessagePumpIOSForIO() 151 #define MESSAGE_PUMP_IO new MessagePumpIOSForIO()
161 #elif defined(OS_MACOSX) 152 #elif defined(OS_MACOSX)
162 #define MESSAGE_PUMP_UI MessagePumpMac::Create() 153 #define MESSAGE_PUMP_UI MessagePumpMac::Create()
163 #define MESSAGE_PUMP_IO new MessagePumpLibevent() 154 #define MESSAGE_PUMP_IO new MessagePumpLibevent()
164 #elif defined(OS_NACL) 155 #elif defined(OS_NACL)
165 // Currently NaCl doesn't have a UI MessageLoop. 156 // Currently NaCl doesn't have a UI MessageLoop.
166 // TODO(abarth): Figure out if we need this. 157 // TODO(abarth): Figure out if we need this.
167 #define MESSAGE_PUMP_UI NULL 158 #define MESSAGE_PUMP_UI NULL
168 // ipc_channel_nacl.cc uses a worker thread to do socket reads currently, and 159 // ipc_channel_nacl.cc uses a worker thread to do socket reads currently, and
169 // doesn't require extra support for watching file descriptors. 160 // doesn't require extra support for watching file descriptors.
170 #define MESSAGE_PUMP_IO new MessagePumpDefault(); 161 #define MESSAGE_PUMP_IO new MessagePumpDefault()
171 #elif defined(OS_POSIX) // POSIX but not MACOSX. 162 #elif defined(OS_POSIX) // POSIX but not MACOSX.
172 #define MESSAGE_PUMP_UI new MessagePumpForUI() 163 #define MESSAGE_PUMP_UI new MessagePumpForUI()
173 #define MESSAGE_PUMP_IO new MessagePumpLibevent() 164 #define MESSAGE_PUMP_IO new MessagePumpLibevent()
174 #else 165 #else
175 #error Not implemented 166 #error Not implemented
176 #endif 167 #endif
177 168
178 if (type_ == TYPE_UI) { 169 if (type_ == TYPE_UI) {
179 if (message_pump_for_ui_factory_) 170 if (message_pump_for_ui_factory_)
180 pump_ = message_pump_for_ui_factory_(); 171 pump_.reset(message_pump_for_ui_factory_());
181 else 172 else
182 pump_ = MESSAGE_PUMP_UI; 173 pump_.reset(MESSAGE_PUMP_UI);
183 } else if (type_ == TYPE_IO) { 174 } else if (type_ == TYPE_IO) {
184 pump_ = MESSAGE_PUMP_IO; 175 pump_.reset(MESSAGE_PUMP_IO);
185 } else { 176 } else {
186 DCHECK_EQ(TYPE_DEFAULT, type_); 177 DCHECK_EQ(TYPE_DEFAULT, type_);
187 pump_ = new MessagePumpDefault(); 178 pump_.reset(new MessagePumpDefault());
188 } 179 }
189 } 180 }
190 181
191 MessageLoop::~MessageLoop() { 182 MessageLoop::~MessageLoop() {
192 DCHECK_EQ(this, current()); 183 DCHECK_EQ(this, current());
193 184
194 DCHECK(!run_loop_); 185 DCHECK(!run_loop_);
195 186
196 // Clean up any unprocessed tasks, but take care: deleting a task could 187 // Clean up any unprocessed tasks, but take care: deleting a task could
197 // result in the addition of more tasks (e.g., via DeleteSoon). We set a 188 // result in the addition of more tasks (e.g., via DeleteSoon). We set a
(...skipping 12 matching lines...) Expand all
210 } 201 }
211 DCHECK(!did_work); 202 DCHECK(!did_work);
212 203
213 // Let interested parties have one last shot at accessing this. 204 // Let interested parties have one last shot at accessing this.
214 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, 205 FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_,
215 WillDestroyCurrentMessageLoop()); 206 WillDestroyCurrentMessageLoop());
216 207
217 thread_task_runner_handle_.reset(); 208 thread_task_runner_handle_.reset();
218 209
219 // Tell the message_loop_proxy that we are dying. 210 // Tell the message_loop_proxy that we are dying.
220 static_cast<MessageLoopProxyImpl*>(message_loop_proxy_.get())-> 211 message_loop_proxy_->WillDestroyCurrentMessageLoop();
221 WillDestroyCurrentMessageLoop();
222 message_loop_proxy_ = NULL; 212 message_loop_proxy_ = NULL;
223 213
224 // OK, now make it so that no one can find us. 214 // OK, now make it so that no one can find us.
225 lazy_tls_ptr.Pointer()->Set(NULL); 215 lazy_tls_ptr.Pointer()->Set(NULL);
226
227 #if defined(OS_WIN)
228 // If we left the high-resolution timer activated, deactivate it now.
229 // Doing this is not-critical, it is mainly to make sure we track
230 // the high resolution timer activations properly in our unit tests.
231 if (!high_resolution_timer_expiration_.is_null()) {
232 Time::ActivateHighResolutionTimer(false);
233 high_resolution_timer_expiration_ = TimeTicks();
234 }
235 #endif
236 } 216 }
237 217
238 // static 218 // static
239 MessageLoop* MessageLoop::current() { 219 MessageLoop* MessageLoop::current() {
240 // TODO(darin): sadly, we cannot enable this yet since people call us even 220 // TODO(darin): sadly, we cannot enable this yet since people call us even
241 // when they have no intention of using us. 221 // when they have no intention of using us.
242 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; 222 // DCHECK(loop) << "Ouch, did you forget to initialize me?";
243 return lazy_tls_ptr.Pointer()->Get(); 223 return lazy_tls_ptr.Pointer()->Get();
244 } 224 }
245 225
(...skipping 20 matching lines...) Expand all
266 void MessageLoop::RemoveDestructionObserver( 246 void MessageLoop::RemoveDestructionObserver(
267 DestructionObserver* destruction_observer) { 247 DestructionObserver* destruction_observer) {
268 DCHECK_EQ(this, current()); 248 DCHECK_EQ(this, current());
269 destruction_observers_.RemoveObserver(destruction_observer); 249 destruction_observers_.RemoveObserver(destruction_observer);
270 } 250 }
271 251
272 void MessageLoop::PostTask( 252 void MessageLoop::PostTask(
273 const tracked_objects::Location& from_here, 253 const tracked_objects::Location& from_here,
274 const Closure& task) { 254 const Closure& task) {
275 DCHECK(!task.is_null()) << from_here.ToString(); 255 DCHECK(!task.is_null()) << from_here.ToString();
276 PendingTask pending_task( 256 message_loop_proxy_->AddToIncomingQueue(from_here, task, TimeDelta(), true);
277 from_here, task, CalculateDelayedRuntime(TimeDelta()), true);
278 AddToIncomingQueue(&pending_task, false);
279 } 257 }
280 258
281 bool MessageLoop::TryPostTask( 259 bool MessageLoop::TryPostTask(
282 const tracked_objects::Location& from_here, 260 const tracked_objects::Location& from_here,
283 const Closure& task) { 261 const Closure& task) {
284 DCHECK(!task.is_null()) << from_here.ToString(); 262 DCHECK(!task.is_null()) << from_here.ToString();
285 PendingTask pending_task( 263 return message_loop_proxy_->TryAddToIncomingQueue(from_here, task);
286 from_here, task, CalculateDelayedRuntime(TimeDelta()), true);
287 return AddToIncomingQueue(&pending_task, true);
288 } 264 }
289 265
290 void MessageLoop::PostDelayedTask( 266 void MessageLoop::PostDelayedTask(
291 const tracked_objects::Location& from_here, 267 const tracked_objects::Location& from_here,
292 const Closure& task, 268 const Closure& task,
293 TimeDelta delay) { 269 TimeDelta delay) {
294 DCHECK(!task.is_null()) << from_here.ToString(); 270 DCHECK(!task.is_null()) << from_here.ToString();
295 PendingTask pending_task( 271 message_loop_proxy_->AddToIncomingQueue(from_here, task, delay, true);
296 from_here, task, CalculateDelayedRuntime(delay), true);
297 AddToIncomingQueue(&pending_task, false);
298 } 272 }
299 273
300 void MessageLoop::PostNonNestableTask( 274 void MessageLoop::PostNonNestableTask(
301 const tracked_objects::Location& from_here, 275 const tracked_objects::Location& from_here,
302 const Closure& task) { 276 const Closure& task) {
303 DCHECK(!task.is_null()) << from_here.ToString(); 277 DCHECK(!task.is_null()) << from_here.ToString();
304 PendingTask pending_task( 278 message_loop_proxy_->AddToIncomingQueue(from_here, task, TimeDelta(), false);
305 from_here, task, CalculateDelayedRuntime(TimeDelta()), false);
306 AddToIncomingQueue(&pending_task, false);
307 } 279 }
308 280
309 void MessageLoop::PostNonNestableDelayedTask( 281 void MessageLoop::PostNonNestableDelayedTask(
310 const tracked_objects::Location& from_here, 282 const tracked_objects::Location& from_here,
311 const Closure& task, 283 const Closure& task,
312 TimeDelta delay) { 284 TimeDelta delay) {
313 DCHECK(!task.is_null()) << from_here.ToString(); 285 DCHECK(!task.is_null()) << from_here.ToString();
314 PendingTask pending_task( 286 message_loop_proxy_->AddToIncomingQueue(from_here, task, delay, false);
315 from_here, task, CalculateDelayedRuntime(delay), false);
316 AddToIncomingQueue(&pending_task, false);
317 } 287 }
318 288
319 void MessageLoop::Run() { 289 void MessageLoop::Run() {
320 RunLoop run_loop; 290 RunLoop run_loop;
321 run_loop.Run(); 291 run_loop.Run();
322 } 292 }
323 293
324 void MessageLoop::RunUntilIdle() { 294 void MessageLoop::RunUntilIdle() {
325 RunLoop run_loop; 295 RunLoop run_loop;
326 run_loop.RunUntilIdle(); 296 run_loop.RunUntilIdle();
(...skipping 23 matching lines...) Expand all
350 320
351 static void QuitCurrentWhenIdle() { 321 static void QuitCurrentWhenIdle() {
352 MessageLoop::current()->QuitWhenIdle(); 322 MessageLoop::current()->QuitWhenIdle();
353 } 323 }
354 324
355 // static 325 // static
356 Closure MessageLoop::QuitWhenIdleClosure() { 326 Closure MessageLoop::QuitWhenIdleClosure() {
357 return Bind(&QuitCurrentWhenIdle); 327 return Bind(&QuitCurrentWhenIdle);
358 } 328 }
359 329
330 scoped_refptr<MessageLoopProxy> MessageLoop::message_loop_proxy() {
331 return message_loop_proxy_;
332 }
333
360 void MessageLoop::SetNestableTasksAllowed(bool allowed) { 334 void MessageLoop::SetNestableTasksAllowed(bool allowed) {
361 if (nestable_tasks_allowed_ != allowed) { 335 if (nestable_tasks_allowed_ != allowed) {
362 nestable_tasks_allowed_ = allowed; 336 nestable_tasks_allowed_ = allowed;
363 if (!nestable_tasks_allowed_) 337 if (!nestable_tasks_allowed_)
364 return; 338 return;
365 // Start the native pump if we are not already pumping. 339 // Start the native pump if we are not already pumping.
366 pump_->ScheduleWork(); 340 WakeUpPump();
367 } 341 }
368 } 342 }
369 343
370 bool MessageLoop::NestableTasksAllowed() const { 344 bool MessageLoop::NestableTasksAllowed() const {
371 return nestable_tasks_allowed_; 345 return nestable_tasks_allowed_;
372 } 346 }
373 347
374 bool MessageLoop::IsNested() { 348 bool MessageLoop::IsNested() {
375 return run_loop_->run_depth_ > 1; 349 return run_loop_->run_depth_ > 1;
376 } 350 }
377 351
378 void MessageLoop::AddTaskObserver(TaskObserver* task_observer) { 352 void MessageLoop::AddTaskObserver(TaskObserver* task_observer) {
379 DCHECK_EQ(this, current()); 353 DCHECK_EQ(this, current());
380 task_observers_.AddObserver(task_observer); 354 task_observers_.AddObserver(task_observer);
381 } 355 }
382 356
383 void MessageLoop::RemoveTaskObserver(TaskObserver* task_observer) { 357 void MessageLoop::RemoveTaskObserver(TaskObserver* task_observer) {
384 DCHECK_EQ(this, current()); 358 DCHECK_EQ(this, current());
385 task_observers_.RemoveObserver(task_observer); 359 task_observers_.RemoveObserver(task_observer);
386 } 360 }
387 361
388 void MessageLoop::AssertIdle() const {
389 // We only check |incoming_queue_|, since we don't want to lock |work_queue_|.
390 AutoLock lock(incoming_queue_lock_);
391 DCHECK(incoming_queue_.empty());
392 }
393
394 bool MessageLoop::is_running() const { 362 bool MessageLoop::is_running() const {
395 DCHECK_EQ(this, current()); 363 DCHECK_EQ(this, current());
396 return run_loop_ != NULL; 364 return run_loop_ != NULL;
397 } 365 }
398 366
367 bool MessageLoop::IsHishResolutionTimersEnabledForTest() {
368 return message_loop_proxy_->IsHishResolutionTimersEnabledForTest();
369 }
370
371 bool MessageLoop::IsIdleForTest() {
372 // We only check the imcoming queue|, since we don't want to lock the work
373 // queue.
374 return message_loop_proxy_->IsIdleForTest();
375 }
376
399 //------------------------------------------------------------------------------ 377 //------------------------------------------------------------------------------
400 378
401 // Runs the loop in two different SEH modes: 379 // Runs the loop in two different SEH modes:
402 // enable_SEH_restoration_ = false : any unhandled exception goes to the last 380 // enable_SEH_restoration_ = false : any unhandled exception goes to the last
403 // one that calls SetUnhandledExceptionFilter(). 381 // one that calls SetUnhandledExceptionFilter().
404 // enable_SEH_restoration_ = true : any unhandled exception goes to the filter 382 // enable_SEH_restoration_ = true : any unhandled exception goes to the filter
405 // that was existed before the loop was run. 383 // that was existed before the loop was run.
406 void MessageLoop::RunHandler() { 384 void MessageLoop::RunHandler() {
407 #if defined(OS_WIN) 385 #if defined(OS_WIN)
408 if (exception_restoration_) { 386 if (exception_restoration_) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 428
451 PendingTask pending_task = deferred_non_nestable_work_queue_.front(); 429 PendingTask pending_task = deferred_non_nestable_work_queue_.front();
452 deferred_non_nestable_work_queue_.pop(); 430 deferred_non_nestable_work_queue_.pop();
453 431
454 RunTask(pending_task); 432 RunTask(pending_task);
455 return true; 433 return true;
456 } 434 }
457 435
458 void MessageLoop::RunTask(const PendingTask& pending_task) { 436 void MessageLoop::RunTask(const PendingTask& pending_task) {
459 TRACE_EVENT_FLOW_END0("task", "MessageLoop::PostTask", 437 TRACE_EVENT_FLOW_END0("task", "MessageLoop::PostTask",
460 TRACE_ID_MANGLE(GetTaskTraceID(pending_task, this))); 438 TRACE_ID_MANGLE(GetTaskTraceID(pending_task)));
461 TRACE_EVENT2("task", "MessageLoop::RunTask", 439 TRACE_EVENT2("task", "MessageLoop::RunTask",
462 "src_file", pending_task.posted_from.file_name(), 440 "src_file", pending_task.posted_from.file_name(),
463 "src_func", pending_task.posted_from.function_name()); 441 "src_func", pending_task.posted_from.function_name());
464 DCHECK(nestable_tasks_allowed_); 442 DCHECK(nestable_tasks_allowed_);
465 // Execute the task and assume the worst: It is probably not reentrant. 443 // Execute the task and assume the worst: It is probably not reentrant.
466 nestable_tasks_allowed_ = false; 444 nestable_tasks_allowed_ = false;
467 445
468 // Before running the task, store the program counter where it was posted 446 // Before running the task, store the program counter where it was posted
469 // and deliberately alias it to ensure it is on the stack if the task 447 // and deliberately alias it to ensure it is on the stack if the task
470 // crashes. Be careful not to assume that the variable itself will have the 448 // crashes. Be careful not to assume that the variable itself will have the
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 // and the task isn't nestable. 481 // and the task isn't nestable.
504 deferred_non_nestable_work_queue_.push(pending_task); 482 deferred_non_nestable_work_queue_.push(pending_task);
505 return false; 483 return false;
506 } 484 }
507 485
508 void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) { 486 void MessageLoop::AddToDelayedWorkQueue(const PendingTask& pending_task) {
509 // Move to the delayed work queue. 487 // Move to the delayed work queue.
510 delayed_work_queue_.push(pending_task); 488 delayed_work_queue_.push(pending_task);
511 } 489 }
512 490
513 void MessageLoop::ReloadWorkQueue() {
514 // We can improve performance of our loading tasks from incoming_queue_ to
515 // work_queue_ by waiting until the last minute (work_queue_ is empty) to
516 // load. That reduces the number of locks-per-task significantly when our
517 // queues get large.
518 if (!work_queue_.empty())
519 return; // Wait till we *really* need to lock and load.
520
521 // Acquire all we can from the inter-thread queue with one lock acquisition.
522 {
523 AutoLock lock(incoming_queue_lock_);
524 if (incoming_queue_.empty())
525 return;
526 incoming_queue_.Swap(&work_queue_); // Constant time
527 DCHECK(incoming_queue_.empty());
528 }
529 }
530
531 bool MessageLoop::DeletePendingTasks() { 491 bool MessageLoop::DeletePendingTasks() {
532 bool did_work = !work_queue_.empty(); 492 bool did_work = !work_queue_.empty();
533 while (!work_queue_.empty()) { 493 while (!work_queue_.empty()) {
534 PendingTask pending_task = work_queue_.front(); 494 PendingTask pending_task = work_queue_.front();
535 work_queue_.pop(); 495 work_queue_.pop();
536 if (!pending_task.delayed_run_time.is_null()) { 496 if (!pending_task.delayed_run_time.is_null()) {
537 // We want to delete delayed tasks in the same order in which they would 497 // We want to delete delayed tasks in the same order in which they would
538 // normally be deleted in case of any funny dependencies between delayed 498 // normally be deleted in case of any funny dependencies between delayed
539 // tasks. 499 // tasks.
540 AddToDelayedWorkQueue(pending_task); 500 AddToDelayedWorkQueue(pending_task);
541 } 501 }
542 } 502 }
543 did_work |= !deferred_non_nestable_work_queue_.empty(); 503 did_work |= !deferred_non_nestable_work_queue_.empty();
544 while (!deferred_non_nestable_work_queue_.empty()) { 504 while (!deferred_non_nestable_work_queue_.empty()) {
545 deferred_non_nestable_work_queue_.pop(); 505 deferred_non_nestable_work_queue_.pop();
546 } 506 }
547 did_work |= !delayed_work_queue_.empty(); 507 did_work |= !delayed_work_queue_.empty();
548 508
549 // Historically, we always delete the task regardless of valgrind status. It's 509 // Historically, we always delete the task regardless of valgrind status. It's
550 // not completely clear why we want to leak them in the loops above. This 510 // not completely clear why we want to leak them in the loops above. This
551 // code is replicating legacy behavior, and should not be considered 511 // code is replicating legacy behavior, and should not be considered
552 // absolutely "correct" behavior. See TODO above about deleting all tasks 512 // absolutely "correct" behavior. See TODO above about deleting all tasks
553 // when it's safe. 513 // when it's safe.
554 while (!delayed_work_queue_.empty()) { 514 while (!delayed_work_queue_.empty()) {
555 delayed_work_queue_.pop(); 515 delayed_work_queue_.pop();
556 } 516 }
557 return did_work; 517 return did_work;
558 } 518 }
559 519
560 TimeTicks MessageLoop::CalculateDelayedRuntime(TimeDelta delay) { 520 uint64 MessageLoop::GetTaskTraceID(const PendingTask& task) {
561 TimeTicks delayed_run_time; 521 return (static_cast<uint64>(task.sequence_num) << 32) |
562 if (delay > TimeDelta()) { 522 static_cast<uint64>(reinterpret_cast<intptr_t>(this));
563 delayed_run_time = TimeTicks::Now() + delay;
564
565 #if defined(OS_WIN)
566 if (high_resolution_timer_expiration_.is_null()) {
567 // Windows timers are granular to 15.6ms. If we only set high-res
568 // timers for those under 15.6ms, then a 18ms timer ticks at ~32ms,
569 // which as a percentage is pretty inaccurate. So enable high
570 // res timers for any timer which is within 2x of the granularity.
571 // This is a tradeoff between accuracy and power management.
572 bool needs_high_res_timers = delay.InMilliseconds() <
573 (2 * Time::kMinLowResolutionThresholdMs);
574 if (needs_high_res_timers) {
575 if (Time::ActivateHighResolutionTimer(true)) {
576 high_resolution_timer_expiration_ = TimeTicks::Now() +
577 TimeDelta::FromMilliseconds(kHighResolutionTimerModeLeaseTimeMs);
578 }
579 }
580 }
581 #endif
582 } else {
583 DCHECK_EQ(delay.InMilliseconds(), 0) << "delay should not be negative";
584 }
585
586 #if defined(OS_WIN)
587 if (!high_resolution_timer_expiration_.is_null()) {
588 if (TimeTicks::Now() > high_resolution_timer_expiration_) {
589 Time::ActivateHighResolutionTimer(false);
590 high_resolution_timer_expiration_ = TimeTicks();
591 }
592 }
593 #endif
594
595 return delayed_run_time;
596 } 523 }
597 524
598 // Possibly called on a background thread! 525 void MessageLoop::ReloadWorkQueue() {
599 bool MessageLoop::AddToIncomingQueue(PendingTask* pending_task, 526 // We can improve performance of our loading tasks from |incoming_queue_| to
600 bool use_try_lock) { 527 // |*work_queue| by waiting until the last minute (|*work_queue| is empty) to
601 // Warning: Don't try to short-circuit, and handle this thread's tasks more 528 // load. That reduces the number of locks-per-task significantly when our
602 // directly, as it could starve handling of foreign threads. Put every task 529 // queues get large.
603 // into this queue. 530 if (work_queue_.empty())
531 message_loop_proxy_->ReloadWorkQueue(&work_queue_);
532 }
604 533
605 scoped_refptr<MessagePump> pump; 534 void MessageLoop::WakeUpPump() {
606 { 535 pump_->ScheduleWork();
607 if (use_try_lock) {
608 if (!incoming_queue_lock_.Try()) {
609 pending_task->task.Reset();
610 return false;
611 }
612 } else {
613 incoming_queue_lock_.Acquire();
614 }
615 AutoLock locked(incoming_queue_lock_, AutoLock::AlreadyAcquired());
616 // Initialize the sequence number. The sequence number is used for delayed
617 // tasks (to faciliate FIFO sorting when two tasks have the same
618 // delayed_run_time value) and for identifying the task in about:tracing.
619 pending_task->sequence_num = next_sequence_num_++;
620
621 TRACE_EVENT_FLOW_BEGIN0("task", "MessageLoop::PostTask",
622 TRACE_ID_MANGLE(GetTaskTraceID(*pending_task, this)));
623
624 bool was_empty = incoming_queue_.empty();
625 incoming_queue_.push(*pending_task);
626 pending_task->task.Reset();
627 if (!was_empty)
628 return true; // Someone else should have started the sub-pump.
629
630 pump = pump_;
631 }
632 // Since the incoming_queue_ may contain a task that destroys this message
633 // loop, we cannot exit incoming_queue_lock_ until we are done with |this|.
634 // We use a stack-based reference to the message pump so that we can call
635 // ScheduleWork outside of incoming_queue_lock_.
636
637 pump->ScheduleWork();
638 return true;
639 } 536 }
640 537
641 //------------------------------------------------------------------------------ 538 //------------------------------------------------------------------------------
642 // Method and data for histogramming events and actions taken by each instance 539 // Method and data for histogramming events and actions taken by each instance
643 // on each thread. 540 // on each thread.
644 541
645 void MessageLoop::StartHistogrammer() { 542 void MessageLoop::StartHistogrammer() {
646 #if !defined(OS_NACL) // NaCl build has no metrics code. 543 #if !defined(OS_NACL) // NaCl build has no metrics code.
647 if (enable_histogrammer_ && !message_histogram_ 544 if (enable_histogrammer_ && !message_histogram_
648 && StatisticsRecorder::IsActive()) { 545 && StatisticsRecorder::IsActive()) {
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
825 fd, 722 fd,
826 persistent, 723 persistent,
827 mode, 724 mode,
828 controller, 725 controller,
829 delegate); 726 delegate);
830 } 727 }
831 728
832 #endif 729 #endif
833 730
834 } // namespace base 731 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698