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; |
418 subtle::Release_Store(&thread_id_, PlatformThread::CurrentId()); | |
416 SetThreadTaskRunnerHandle(); | 419 SetThreadTaskRunnerHandle(); |
417 } | 420 } |
418 | 421 |
422 std::string MessageLoop::GetThreadName() const { | |
423 return ThreadIdNameManager::GetInstance()->GetName( | |
424 subtle::Release_Load(&thread_id_)); | |
gab
2016/06/03 15:31:17
I don't think you need to use an Atomic32. Instead
alokp
2016/06/03 16:34:56
Used subtle::MemoryBarrier() in the new patch. I d
gab
2016/06/07 00:30:59
I think it's reasonable to document in the header
alokp
2016/06/10 22:12:59
Done.
| |
425 } | |
426 | |
419 void MessageLoop::SetTaskRunner( | 427 void MessageLoop::SetTaskRunner( |
420 scoped_refptr<SingleThreadTaskRunner> task_runner) { | 428 scoped_refptr<SingleThreadTaskRunner> task_runner) { |
421 DCHECK_EQ(this, current()); | 429 DCHECK_EQ(this, current()); |
422 DCHECK(task_runner->BelongsToCurrentThread()); | 430 DCHECK(task_runner->BelongsToCurrentThread()); |
423 DCHECK(!unbound_task_runner_); | 431 DCHECK(!unbound_task_runner_); |
424 task_runner_ = std::move(task_runner); | 432 task_runner_ = std::move(task_runner); |
425 SetThreadTaskRunnerHandle(); | 433 SetThreadTaskRunnerHandle(); |
426 } | 434 } |
427 | 435 |
428 void MessageLoop::SetThreadTaskRunnerHandle() { | 436 void MessageLoop::SetThreadTaskRunnerHandle() { |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
547 } | 555 } |
548 | 556 |
549 //------------------------------------------------------------------------------ | 557 //------------------------------------------------------------------------------ |
550 // Method and data for histogramming events and actions taken by each instance | 558 // Method and data for histogramming events and actions taken by each instance |
551 // on each thread. | 559 // on each thread. |
552 | 560 |
553 void MessageLoop::StartHistogrammer() { | 561 void MessageLoop::StartHistogrammer() { |
554 #if !defined(OS_NACL) // NaCl build has no metrics code. | 562 #if !defined(OS_NACL) // NaCl build has no metrics code. |
555 if (enable_histogrammer_ && !message_histogram_ | 563 if (enable_histogrammer_ && !message_histogram_ |
556 && StatisticsRecorder::IsActive()) { | 564 && StatisticsRecorder::IsActive()) { |
557 DCHECK(!thread_name_.empty()); | 565 std::string thread_name = GetThreadName(); |
566 DCHECK(!thread_name.empty()); | |
558 message_histogram_ = LinearHistogram::FactoryGetWithRangeDescription( | 567 message_histogram_ = LinearHistogram::FactoryGetWithRangeDescription( |
559 "MsgLoop:" + thread_name_, | 568 "MsgLoop:" + thread_name, kLeastNonZeroMessageId, kMaxMessageId, |
560 kLeastNonZeroMessageId, kMaxMessageId, | |
561 kNumberOfDistinctMessagesDisplayed, | 569 kNumberOfDistinctMessagesDisplayed, |
562 HistogramBase::kHexRangePrintingFlag, | 570 HistogramBase::kHexRangePrintingFlag, event_descriptions_); |
563 event_descriptions_); | |
564 } | 571 } |
565 #endif | 572 #endif |
566 } | 573 } |
567 | 574 |
568 void MessageLoop::HistogramEvent(int event) { | 575 void MessageLoop::HistogramEvent(int event) { |
569 #if !defined(OS_NACL) | 576 #if !defined(OS_NACL) |
570 if (message_histogram_) | 577 if (message_histogram_) |
571 message_histogram_->Add(event); | 578 message_histogram_->Add(event); |
572 #endif | 579 #endif |
573 } | 580 } |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
740 persistent, | 747 persistent, |
741 mode, | 748 mode, |
742 controller, | 749 controller, |
743 delegate); | 750 delegate); |
744 } | 751 } |
745 #endif | 752 #endif |
746 | 753 |
747 #endif // !defined(OS_NACL_SFI) | 754 #endif // !defined(OS_NACL_SFI) |
748 | 755 |
749 } // namespace base | 756 } // namespace base |
OLD | NEW |