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 <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 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 391 nestable_tasks_allowed_(true), | 392 nestable_tasks_allowed_(true), |
| 392 #if defined(OS_WIN) | 393 #if defined(OS_WIN) |
| 393 os_modal_loop_(false), | 394 os_modal_loop_(false), |
| 394 #endif // OS_WIN | 395 #endif // OS_WIN |
| 395 pump_factory_(pump_factory), | 396 pump_factory_(pump_factory), |
| 396 message_histogram_(NULL), | 397 message_histogram_(NULL), |
| 397 run_loop_(NULL), | 398 run_loop_(NULL), |
| 398 incoming_task_queue_(new internal::IncomingTaskQueue(this)), | 399 incoming_task_queue_(new internal::IncomingTaskQueue(this)), |
| 399 unbound_task_runner_( | 400 unbound_task_runner_( |
| 400 new internal::MessageLoopTaskRunner(incoming_task_queue_)), | 401 new internal::MessageLoopTaskRunner(incoming_task_queue_)), |
| 401 task_runner_(unbound_task_runner_) { | 402 task_runner_(unbound_task_runner_), |
| 403 thread_id_(kInvalidThreadId) { | |
| 402 // If type is TYPE_CUSTOM non-null pump_factory must be given. | 404 // If type is TYPE_CUSTOM non-null pump_factory must be given. |
| 403 DCHECK(type_ != TYPE_CUSTOM || !pump_factory_.is_null()); | 405 DCHECK(type_ != TYPE_CUSTOM || !pump_factory_.is_null()); |
| 404 } | 406 } |
| 405 | 407 |
| 406 void MessageLoop::BindToCurrentThread() { | 408 void MessageLoop::BindToCurrentThread() { |
| 407 DCHECK(!pump_); | 409 DCHECK(!pump_); |
| 408 if (!pump_factory_.is_null()) | 410 if (!pump_factory_.is_null()) |
| 409 pump_ = pump_factory_.Run(); | 411 pump_ = pump_factory_.Run(); |
| 410 else | 412 else |
| 411 pump_ = CreateMessagePumpForType(type_); | 413 pump_ = CreateMessagePumpForType(type_); |
| 412 | 414 |
| 413 DCHECK(!current()) << "should only have one message loop per thread"; | 415 DCHECK(!current()) << "should only have one message loop per thread"; |
| 414 lazy_tls_ptr.Pointer()->Set(this); | 416 lazy_tls_ptr.Pointer()->Set(this); |
| 415 | 417 |
| 416 incoming_task_queue_->StartScheduling(); | 418 incoming_task_queue_->StartScheduling(); |
| 417 unbound_task_runner_->BindToCurrentThread(); | 419 unbound_task_runner_->BindToCurrentThread(); |
| 418 unbound_task_runner_ = nullptr; | 420 unbound_task_runner_ = nullptr; |
| 421 thread_id_ = PlatformThread::CurrentId(); | |
|
gab
2016/05/20 20:25:39
This needs to be synchronized as it's set outside
alokp
2016/06/01 19:23:18
Done. Can you please check the barrier usage and m
| |
| 419 SetThreadTaskRunnerHandle(); | 422 SetThreadTaskRunnerHandle(); |
| 420 } | 423 } |
| 421 | 424 |
| 425 std::string MessageLoop::GetThreadName() const { | |
| 426 return ThreadIdNameManager::GetInstance()->GetName(thread_id_); | |
| 427 } | |
| 428 | |
| 422 void MessageLoop::SetTaskRunner( | 429 void MessageLoop::SetTaskRunner( |
| 423 scoped_refptr<SingleThreadTaskRunner> task_runner) { | 430 scoped_refptr<SingleThreadTaskRunner> task_runner) { |
| 424 DCHECK_EQ(this, current()); | 431 DCHECK_EQ(this, current()); |
| 425 DCHECK(task_runner->BelongsToCurrentThread()); | 432 DCHECK(task_runner->BelongsToCurrentThread()); |
| 426 DCHECK(!unbound_task_runner_); | 433 DCHECK(!unbound_task_runner_); |
| 427 task_runner_ = std::move(task_runner); | 434 task_runner_ = std::move(task_runner); |
| 428 SetThreadTaskRunnerHandle(); | 435 SetThreadTaskRunnerHandle(); |
| 429 } | 436 } |
| 430 | 437 |
| 431 void MessageLoop::SetThreadTaskRunnerHandle() { | 438 void MessageLoop::SetThreadTaskRunnerHandle() { |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 550 } | 557 } |
| 551 | 558 |
| 552 //------------------------------------------------------------------------------ | 559 //------------------------------------------------------------------------------ |
| 553 // Method and data for histogramming events and actions taken by each instance | 560 // Method and data for histogramming events and actions taken by each instance |
| 554 // on each thread. | 561 // on each thread. |
| 555 | 562 |
| 556 void MessageLoop::StartHistogrammer() { | 563 void MessageLoop::StartHistogrammer() { |
| 557 #if !defined(OS_NACL) // NaCl build has no metrics code. | 564 #if !defined(OS_NACL) // NaCl build has no metrics code. |
| 558 if (enable_histogrammer_ && !message_histogram_ | 565 if (enable_histogrammer_ && !message_histogram_ |
| 559 && StatisticsRecorder::IsActive()) { | 566 && StatisticsRecorder::IsActive()) { |
| 560 DCHECK(!thread_name_.empty()); | 567 std::string thread_name = GetThreadName(); |
| 568 DCHECK(!thread_name.empty()); | |
| 561 message_histogram_ = LinearHistogram::FactoryGetWithRangeDescription( | 569 message_histogram_ = LinearHistogram::FactoryGetWithRangeDescription( |
| 562 "MsgLoop:" + thread_name_, | 570 "MsgLoop:" + thread_name, |
| 563 kLeastNonZeroMessageId, kMaxMessageId, | 571 kLeastNonZeroMessageId, kMaxMessageId, |
| 564 kNumberOfDistinctMessagesDisplayed, | 572 kNumberOfDistinctMessagesDisplayed, |
| 565 HistogramBase::kHexRangePrintingFlag, | 573 HistogramBase::kHexRangePrintingFlag, |
| 566 event_descriptions_); | 574 event_descriptions_); |
| 567 } | 575 } |
| 568 #endif | 576 #endif |
| 569 } | 577 } |
| 570 | 578 |
| 571 void MessageLoop::HistogramEvent(int event) { | 579 void MessageLoop::HistogramEvent(int event) { |
| 572 #if !defined(OS_NACL) | 580 #if !defined(OS_NACL) |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 743 persistent, | 751 persistent, |
| 744 mode, | 752 mode, |
| 745 controller, | 753 controller, |
| 746 delegate); | 754 delegate); |
| 747 } | 755 } |
| 748 #endif | 756 #endif |
| 749 | 757 |
| 750 #endif // !defined(OS_NACL_SFI) | 758 #endif // !defined(OS_NACL_SFI) |
| 751 | 759 |
| 752 } // namespace base | 760 } // namespace base |
| OLD | NEW |