Chromium Code Reviews| 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/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/message_loop/message_pump_default.h" | 15 #include "base/message_loop/message_pump_default.h" |
| 16 #include "base/metrics/histogram.h" | |
| 17 #include "base/metrics/statistics_recorder.h" | |
| 18 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 19 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 17 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 20 #include "base/threading/thread_id_name_manager.h" | 18 #include "base/threading/thread_id_name_manager.h" |
| 21 #include "base/threading/thread_local.h" | 19 #include "base/threading/thread_local.h" |
| 22 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
| 23 #include "base/trace_event/trace_event.h" | 21 #include "base/trace_event/trace_event.h" |
| 24 | 22 |
| 25 #if defined(OS_MACOSX) | 23 #if defined(OS_MACOSX) |
| 26 #include "base/message_loop/message_pump_mac.h" | 24 #include "base/message_loop/message_pump_mac.h" |
| 27 #endif | 25 #endif |
| 28 #if defined(OS_POSIX) && !defined(OS_IOS) | 26 #if defined(OS_POSIX) && !defined(OS_IOS) |
| 29 #include "base/message_loop/message_pump_libevent.h" | 27 #include "base/message_loop/message_pump_libevent.h" |
| 30 #endif | 28 #endif |
| 31 #if defined(OS_ANDROID) | 29 #if defined(OS_ANDROID) |
| 32 #include "base/message_loop/message_pump_android.h" | 30 #include "base/message_loop/message_pump_android.h" |
| 33 #endif | 31 #endif |
| 34 #if defined(USE_GLIB) | 32 #if defined(USE_GLIB) |
| 35 #include "base/message_loop/message_pump_glib.h" | 33 #include "base/message_loop/message_pump_glib.h" |
| 36 #endif | 34 #endif |
| 37 | 35 |
| 38 namespace base { | 36 namespace base { |
| 39 | 37 |
| 40 namespace { | 38 namespace { |
| 41 | 39 |
| 42 // A lazily created thread local storage for quick access to a thread's message | 40 // A lazily created thread local storage for quick access to a thread's message |
| 43 // loop, if one exists. This should be safe and free of static constructors. | 41 // loop, if one exists. This should be safe and free of static constructors. |
| 44 LazyInstance<base::ThreadLocalPointer<MessageLoop> >::Leaky lazy_tls_ptr = | 42 LazyInstance<base::ThreadLocalPointer<MessageLoop> >::Leaky lazy_tls_ptr = |
| 45 LAZY_INSTANCE_INITIALIZER; | 43 LAZY_INSTANCE_INITIALIZER; |
| 46 | 44 |
| 47 // Logical events for Histogram profiling. Run with --message-loop-histogrammer | |
| 48 // to get an accounting of messages and actions taken on each thread. | |
| 49 const int kTaskRunEvent = 0x1; | |
| 50 #if !defined(OS_NACL) | |
| 51 const int kTimerEvent = 0x2; | |
| 52 | |
| 53 // Provide range of message IDs for use in histogramming and debug display. | |
| 54 const int kLeastNonZeroMessageId = 1; | |
| 55 const int kMaxMessageId = 1099; | |
| 56 const int kNumberOfDistinctMessagesDisplayed = 1100; | |
| 57 | |
| 58 // Provide a macro that takes an expression (such as a constant, or macro | |
| 59 // constant) and creates a pair to initialize an array of pairs. In this case, | |
| 60 // our pair consists of the expressions value, and the "stringized" version | |
| 61 // of the expression (i.e., the expression put in quotes). For example, if | |
| 62 // we have: | |
| 63 // #define FOO 2 | |
| 64 // #define BAR 5 | |
| 65 // then the following: | |
| 66 // VALUE_TO_NUMBER_AND_NAME(FOO + BAR) | |
| 67 // will expand to: | |
| 68 // {7, "FOO + BAR"} | |
| 69 // We use the resulting array as an argument to our histogram, which reads the | |
| 70 // number as a bucket identifier, and proceeds to use the corresponding name | |
| 71 // in the pair (i.e., the quoted string) when printing out a histogram. | |
| 72 #define VALUE_TO_NUMBER_AND_NAME(name) {name, #name}, | |
| 73 | |
| 74 const LinearHistogram::DescriptionPair event_descriptions_[] = { | |
| 75 // Provide some pretty print capability in our histogram for our internal | |
| 76 // messages. | |
| 77 | |
| 78 // A few events we handle (kindred to messages), and used to profile actions. | |
| 79 VALUE_TO_NUMBER_AND_NAME(kTaskRunEvent) | |
| 80 VALUE_TO_NUMBER_AND_NAME(kTimerEvent) | |
| 81 | |
| 82 {-1, NULL} // The list must be null-terminated, per API to histogram. | |
| 83 }; | |
| 84 #endif // !defined(OS_NACL) | |
| 85 | |
| 86 bool enable_histogrammer_ = false; | |
| 87 | |
| 88 MessageLoop::MessagePumpFactory* message_pump_for_ui_factory_ = NULL; | 45 MessageLoop::MessagePumpFactory* message_pump_for_ui_factory_ = NULL; |
| 89 | 46 |
| 90 #if defined(OS_IOS) | 47 #if defined(OS_IOS) |
| 91 typedef MessagePumpIOSForIO MessagePumpForIO; | 48 typedef MessagePumpIOSForIO MessagePumpForIO; |
| 92 #elif defined(OS_NACL_SFI) | 49 #elif defined(OS_NACL_SFI) |
| 93 typedef MessagePumpDefault MessagePumpForIO; | 50 typedef MessagePumpDefault MessagePumpForIO; |
| 94 #elif defined(OS_POSIX) | 51 #elif defined(OS_POSIX) |
| 95 typedef MessagePumpLibevent MessagePumpForIO; | 52 typedef MessagePumpLibevent MessagePumpForIO; |
| 96 #endif | 53 #endif |
| 97 | 54 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 185 | 142 |
| 186 // static | 143 // static |
| 187 MessageLoop* MessageLoop::current() { | 144 MessageLoop* MessageLoop::current() { |
| 188 // TODO(darin): sadly, we cannot enable this yet since people call us even | 145 // TODO(darin): sadly, we cannot enable this yet since people call us even |
| 189 // when they have no intention of using us. | 146 // when they have no intention of using us. |
| 190 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; | 147 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; |
| 191 return lazy_tls_ptr.Pointer()->Get(); | 148 return lazy_tls_ptr.Pointer()->Get(); |
| 192 } | 149 } |
| 193 | 150 |
| 194 // static | 151 // static |
| 195 void MessageLoop::EnableHistogrammer(bool enable) { | |
| 196 enable_histogrammer_ = enable; | |
| 197 } | |
| 198 | |
| 199 // static | |
| 200 bool MessageLoop::InitMessagePumpForUIFactory(MessagePumpFactory* factory) { | 152 bool MessageLoop::InitMessagePumpForUIFactory(MessagePumpFactory* factory) { |
| 201 if (message_pump_for_ui_factory_) | 153 if (message_pump_for_ui_factory_) |
| 202 return false; | 154 return false; |
| 203 | 155 |
| 204 message_pump_for_ui_factory_ = factory; | 156 message_pump_for_ui_factory_ = factory; |
| 205 return true; | 157 return true; |
| 206 } | 158 } |
| 207 | 159 |
| 208 // static | 160 // static |
| 209 std::unique_ptr<MessagePump> MessageLoop::CreateMessagePumpForType(Type type) { | 161 std::unique_ptr<MessagePump> MessageLoop::CreateMessagePumpForType(Type type) { |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 355 } | 307 } |
| 356 | 308 |
| 357 MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory) | 309 MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory) |
| 358 : type_(type), | 310 : type_(type), |
| 359 #if defined(OS_WIN) | 311 #if defined(OS_WIN) |
| 360 pending_high_res_tasks_(0), | 312 pending_high_res_tasks_(0), |
| 361 in_high_res_mode_(false), | 313 in_high_res_mode_(false), |
| 362 #endif | 314 #endif |
| 363 nestable_tasks_allowed_(true), | 315 nestable_tasks_allowed_(true), |
| 364 pump_factory_(pump_factory), | 316 pump_factory_(pump_factory), |
| 365 message_histogram_(NULL), | |
| 366 run_loop_(NULL), | 317 run_loop_(NULL), |
| 367 incoming_task_queue_(new internal::IncomingTaskQueue(this)), | 318 incoming_task_queue_(new internal::IncomingTaskQueue(this)), |
| 368 unbound_task_runner_( | 319 unbound_task_runner_( |
| 369 new internal::MessageLoopTaskRunner(incoming_task_queue_)), | 320 new internal::MessageLoopTaskRunner(incoming_task_queue_)), |
| 370 task_runner_(unbound_task_runner_), | 321 task_runner_(unbound_task_runner_), |
| 371 thread_id_(kInvalidThreadId) { | 322 thread_id_(kInvalidThreadId) { |
| 372 // If type is TYPE_CUSTOM non-null pump_factory must be given. | 323 // If type is TYPE_CUSTOM non-null pump_factory must be given. |
| 373 DCHECK(type_ != TYPE_CUSTOM || !pump_factory_.is_null()); | 324 DCHECK(type_ != TYPE_CUSTOM || !pump_factory_.is_null()); |
| 374 } | 325 } |
| 375 | 326 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 409 void MessageLoop::SetThreadTaskRunnerHandle() { | 360 void MessageLoop::SetThreadTaskRunnerHandle() { |
| 410 DCHECK_EQ(this, current()); | 361 DCHECK_EQ(this, current()); |
| 411 // Clear the previous thread task runner first, because only one can exist at | 362 // Clear the previous thread task runner first, because only one can exist at |
| 412 // a time. | 363 // a time. |
| 413 thread_task_runner_handle_.reset(); | 364 thread_task_runner_handle_.reset(); |
| 414 thread_task_runner_handle_.reset(new ThreadTaskRunnerHandle(task_runner_)); | 365 thread_task_runner_handle_.reset(new ThreadTaskRunnerHandle(task_runner_)); |
| 415 } | 366 } |
| 416 | 367 |
| 417 void MessageLoop::RunHandler() { | 368 void MessageLoop::RunHandler() { |
| 418 DCHECK_EQ(this, current()); | 369 DCHECK_EQ(this, current()); |
| 419 StartHistogrammer(); | |
| 420 pump_->Run(this); | 370 pump_->Run(this); |
| 421 } | 371 } |
| 422 | 372 |
| 423 bool MessageLoop::ProcessNextDelayedNonNestableTask() { | 373 bool MessageLoop::ProcessNextDelayedNonNestableTask() { |
| 424 if (run_loop_->run_depth_ != 1) | 374 if (run_loop_->run_depth_ != 1) |
| 425 return false; | 375 return false; |
| 426 | 376 |
| 427 if (deferred_non_nestable_work_queue_.empty()) | 377 if (deferred_non_nestable_work_queue_.empty()) |
| 428 return false; | 378 return false; |
| 429 | 379 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 441 #if defined(OS_WIN) | 391 #if defined(OS_WIN) |
| 442 if (pending_task.is_high_res) { | 392 if (pending_task.is_high_res) { |
| 443 pending_high_res_tasks_--; | 393 pending_high_res_tasks_--; |
| 444 CHECK_GE(pending_high_res_tasks_, 0); | 394 CHECK_GE(pending_high_res_tasks_, 0); |
| 445 } | 395 } |
| 446 #endif | 396 #endif |
| 447 | 397 |
| 448 // Execute the task and assume the worst: It is probably not reentrant. | 398 // Execute the task and assume the worst: It is probably not reentrant. |
| 449 nestable_tasks_allowed_ = false; | 399 nestable_tasks_allowed_ = false; |
| 450 | 400 |
| 451 HistogramEvent(kTaskRunEvent); | |
| 452 | |
| 453 TRACE_TASK_EXECUTION("MessageLoop::RunTask", pending_task); | 401 TRACE_TASK_EXECUTION("MessageLoop::RunTask", pending_task); |
| 454 | 402 |
| 455 FOR_EACH_OBSERVER(TaskObserver, task_observers_, | 403 FOR_EACH_OBSERVER(TaskObserver, task_observers_, |
| 456 WillProcessTask(pending_task)); | 404 WillProcessTask(pending_task)); |
| 457 task_annotator_.RunTask("MessageLoop::PostTask", pending_task); | 405 task_annotator_.RunTask("MessageLoop::PostTask", pending_task); |
| 458 FOR_EACH_OBSERVER(TaskObserver, task_observers_, | 406 FOR_EACH_OBSERVER(TaskObserver, task_observers_, |
| 459 DidProcessTask(pending_task)); | 407 DidProcessTask(pending_task)); |
| 460 | 408 |
| 461 nestable_tasks_allowed_ = true; | 409 nestable_tasks_allowed_ = true; |
| 462 } | 410 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 527 void MessageLoop::ScheduleWork() { | 475 void MessageLoop::ScheduleWork() { |
| 528 pump_->ScheduleWork(); | 476 pump_->ScheduleWork(); |
| 529 } | 477 } |
| 530 | 478 |
| 531 #if defined(OS_WIN) | 479 #if defined(OS_WIN) |
| 532 bool MessageLoop::MessagePumpWasSignaled() { | 480 bool MessageLoop::MessagePumpWasSignaled() { |
| 533 return pump_->WasSignaled(); | 481 return pump_->WasSignaled(); |
| 534 } | 482 } |
| 535 #endif | 483 #endif |
| 536 | 484 |
| 537 //------------------------------------------------------------------------------ | |
| 538 // Method and data for histogramming events and actions taken by each instance | |
| 539 // on each thread. | |
| 540 | |
| 541 void MessageLoop::StartHistogrammer() { | |
| 542 #if !defined(OS_NACL) // NaCl build has no metrics code. | |
| 543 if (enable_histogrammer_ && !message_histogram_ | |
| 544 && StatisticsRecorder::IsActive()) { | |
| 545 std::string thread_name = GetThreadName(); | |
| 546 DCHECK(!thread_name.empty()); | |
| 547 message_histogram_ = LinearHistogram::FactoryGetWithRangeDescription( | |
| 548 "MsgLoop:" + thread_name, kLeastNonZeroMessageId, kMaxMessageId, | |
| 549 kNumberOfDistinctMessagesDisplayed, | |
| 550 HistogramBase::kHexRangePrintingFlag, event_descriptions_); | |
| 551 } | |
| 552 #endif | |
| 553 } | |
| 554 | |
| 555 void MessageLoop::HistogramEvent(int event) { | |
| 556 #if !defined(OS_NACL) | |
| 557 if (message_histogram_) | |
| 558 message_histogram_->Add(event); | |
| 559 #endif | |
| 560 } | |
| 561 | 485 |
|
Alexei Svitkine (slow)
2016/09/28 19:27:48
Nit: Remove extra line.
rkaplow
2016/09/28 19:40:36
Done.
| |
| 562 void MessageLoop::NotifyBeginNestedLoop() { | 486 void MessageLoop::NotifyBeginNestedLoop() { |
| 563 FOR_EACH_OBSERVER(NestingObserver, nesting_observers_, | 487 FOR_EACH_OBSERVER(NestingObserver, nesting_observers_, |
| 564 OnBeginNestedMessageLoop()); | 488 OnBeginNestedMessageLoop()); |
| 565 } | 489 } |
| 566 | 490 |
| 567 bool MessageLoop::DoWork() { | 491 bool MessageLoop::DoWork() { |
| 568 if (!nestable_tasks_allowed_) { | 492 if (!nestable_tasks_allowed_) { |
| 569 // Task can't be executed right now. | 493 // Task can't be executed right now. |
| 570 return false; | 494 return false; |
| 571 } | 495 } |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 729 persistent, | 653 persistent, |
| 730 mode, | 654 mode, |
| 731 controller, | 655 controller, |
| 732 delegate); | 656 delegate); |
| 733 } | 657 } |
| 734 #endif | 658 #endif |
| 735 | 659 |
| 736 #endif // !defined(OS_NACL_SFI) | 660 #endif // !defined(OS_NACL_SFI) |
| 737 | 661 |
| 738 } // namespace base | 662 } // namespace base |
| OLD | NEW |