| 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 <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/message_loop/message_pump_default.h" | 16 #include "base/message_loop/message_pump_default.h" |
| 17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 18 #include "base/metrics/statistics_recorder.h" | 18 #include "base/metrics/statistics_recorder.h" |
| 19 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
| 20 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 20 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 21 #include "base/threading/thread_id_name_manager.h" |
| 21 #include "base/threading/thread_local.h" | 22 #include "base/threading/thread_local.h" |
| 22 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
| 23 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 24 #include "base/trace_event/trace_event.h" | 25 #include "base/trace_event/trace_event.h" |
| 25 #include "base/tracked_objects.h" | 26 #include "base/tracked_objects.h" |
| 26 #include "build/build_config.h" | 27 #include "build/build_config.h" |
| 27 | 28 |
| 28 #if defined(OS_MACOSX) | 29 #if defined(OS_MACOSX) |
| 29 #include "base/message_loop/message_pump_mac.h" | 30 #include "base/message_loop/message_pump_mac.h" |
| 30 #endif | 31 #endif |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 pending_high_res_tasks_(0), | 389 pending_high_res_tasks_(0), |
| 389 in_high_res_mode_(false), | 390 in_high_res_mode_(false), |
| 390 #endif | 391 #endif |
| 391 nestable_tasks_allowed_(true), | 392 nestable_tasks_allowed_(true), |
| 392 pump_factory_(pump_factory), | 393 pump_factory_(pump_factory), |
| 393 message_histogram_(NULL), | 394 message_histogram_(NULL), |
| 394 run_loop_(NULL), | 395 run_loop_(NULL), |
| 395 incoming_task_queue_(new internal::IncomingTaskQueue(this)), | 396 incoming_task_queue_(new internal::IncomingTaskQueue(this)), |
| 396 unbound_task_runner_( | 397 unbound_task_runner_( |
| 397 new internal::MessageLoopTaskRunner(incoming_task_queue_)), | 398 new internal::MessageLoopTaskRunner(incoming_task_queue_)), |
| 398 task_runner_(unbound_task_runner_) { | 399 task_runner_(unbound_task_runner_), |
| 400 thread_id_(kInvalidThreadId) { |
| 399 // If type is TYPE_CUSTOM non-null pump_factory must be given. | 401 // If type is TYPE_CUSTOM non-null pump_factory must be given. |
| 400 DCHECK(type_ != TYPE_CUSTOM || !pump_factory_.is_null()); | 402 DCHECK(type_ != TYPE_CUSTOM || !pump_factory_.is_null()); |
| 401 } | 403 } |
| 402 | 404 |
| 403 void MessageLoop::BindToCurrentThread() { | 405 void MessageLoop::BindToCurrentThread() { |
| 404 DCHECK(!pump_); | 406 DCHECK(!pump_); |
| 405 if (!pump_factory_.is_null()) | 407 if (!pump_factory_.is_null()) |
| 406 pump_ = pump_factory_.Run(); | 408 pump_ = pump_factory_.Run(); |
| 407 else | 409 else |
| 408 pump_ = CreateMessagePumpForType(type_); | 410 pump_ = CreateMessagePumpForType(type_); |
| 409 | 411 |
| 410 DCHECK(!current()) << "should only have one message loop per thread"; | 412 DCHECK(!current()) << "should only have one message loop per thread"; |
| 411 lazy_tls_ptr.Pointer()->Set(this); | 413 lazy_tls_ptr.Pointer()->Set(this); |
| 412 | 414 |
| 413 incoming_task_queue_->StartScheduling(); | 415 incoming_task_queue_->StartScheduling(); |
| 414 unbound_task_runner_->BindToCurrentThread(); | 416 unbound_task_runner_->BindToCurrentThread(); |
| 415 unbound_task_runner_ = nullptr; | 417 unbound_task_runner_ = nullptr; |
| 416 SetThreadTaskRunnerHandle(); | 418 SetThreadTaskRunnerHandle(); |
| 419 { |
| 420 // Save the current thread's ID for potential use by other threads |
| 421 // later from GetThreadName(). |
| 422 thread_id_ = PlatformThread::CurrentId(); |
| 423 subtle::MemoryBarrier(); |
| 424 } |
| 425 } |
| 426 |
| 427 std::string MessageLoop::GetThreadName() const { |
| 428 if (thread_id_ == kInvalidThreadId) { |
| 429 // |thread_id_| may already have been initialized but this thread might not |
| 430 // have received the update yet. |
| 431 subtle::MemoryBarrier(); |
| 432 DCHECK_NE(kInvalidThreadId, thread_id_); |
| 433 } |
| 434 return ThreadIdNameManager::GetInstance()->GetName(thread_id_); |
| 417 } | 435 } |
| 418 | 436 |
| 419 void MessageLoop::SetTaskRunner( | 437 void MessageLoop::SetTaskRunner( |
| 420 scoped_refptr<SingleThreadTaskRunner> task_runner) { | 438 scoped_refptr<SingleThreadTaskRunner> task_runner) { |
| 421 DCHECK_EQ(this, current()); | 439 DCHECK_EQ(this, current()); |
| 422 DCHECK(task_runner->BelongsToCurrentThread()); | 440 DCHECK(task_runner->BelongsToCurrentThread()); |
| 423 DCHECK(!unbound_task_runner_); | 441 DCHECK(!unbound_task_runner_); |
| 424 task_runner_ = std::move(task_runner); | 442 task_runner_ = std::move(task_runner); |
| 425 SetThreadTaskRunnerHandle(); | 443 SetThreadTaskRunnerHandle(); |
| 426 } | 444 } |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 } | 565 } |
| 548 | 566 |
| 549 //------------------------------------------------------------------------------ | 567 //------------------------------------------------------------------------------ |
| 550 // Method and data for histogramming events and actions taken by each instance | 568 // Method and data for histogramming events and actions taken by each instance |
| 551 // on each thread. | 569 // on each thread. |
| 552 | 570 |
| 553 void MessageLoop::StartHistogrammer() { | 571 void MessageLoop::StartHistogrammer() { |
| 554 #if !defined(OS_NACL) // NaCl build has no metrics code. | 572 #if !defined(OS_NACL) // NaCl build has no metrics code. |
| 555 if (enable_histogrammer_ && !message_histogram_ | 573 if (enable_histogrammer_ && !message_histogram_ |
| 556 && StatisticsRecorder::IsActive()) { | 574 && StatisticsRecorder::IsActive()) { |
| 557 DCHECK(!thread_name_.empty()); | 575 std::string thread_name = GetThreadName(); |
| 576 DCHECK(!thread_name.empty()); |
| 558 message_histogram_ = LinearHistogram::FactoryGetWithRangeDescription( | 577 message_histogram_ = LinearHistogram::FactoryGetWithRangeDescription( |
| 559 "MsgLoop:" + thread_name_, | 578 "MsgLoop:" + thread_name, kLeastNonZeroMessageId, kMaxMessageId, |
| 560 kLeastNonZeroMessageId, kMaxMessageId, | |
| 561 kNumberOfDistinctMessagesDisplayed, | 579 kNumberOfDistinctMessagesDisplayed, |
| 562 HistogramBase::kHexRangePrintingFlag, | 580 HistogramBase::kHexRangePrintingFlag, event_descriptions_); |
| 563 event_descriptions_); | |
| 564 } | 581 } |
| 565 #endif | 582 #endif |
| 566 } | 583 } |
| 567 | 584 |
| 568 void MessageLoop::HistogramEvent(int event) { | 585 void MessageLoop::HistogramEvent(int event) { |
| 569 #if !defined(OS_NACL) | 586 #if !defined(OS_NACL) |
| 570 if (message_histogram_) | 587 if (message_histogram_) |
| 571 message_histogram_->Add(event); | 588 message_histogram_->Add(event); |
| 572 #endif | 589 #endif |
| 573 } | 590 } |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 persistent, | 757 persistent, |
| 741 mode, | 758 mode, |
| 742 controller, | 759 controller, |
| 743 delegate); | 760 delegate); |
| 744 } | 761 } |
| 745 #endif | 762 #endif |
| 746 | 763 |
| 747 #endif // !defined(OS_NACL_SFI) | 764 #endif // !defined(OS_NACL_SFI) |
| 748 | 765 |
| 749 } // namespace base | 766 } // namespace base |
| OLD | NEW |