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 thread_id_ = PlatformThread::CurrentId(); | |
alokp
2016/06/03 16:34:56
Do I need a memory barrier here as well?
gab
2016/06/07 00:30:59
Ah yes, let's put one and scope this code inline f
alokp
2016/06/10 22:13:00
Done.
| |
416 SetThreadTaskRunnerHandle(); | 419 SetThreadTaskRunnerHandle(); |
417 } | 420 } |
418 | 421 |
422 std::string MessageLoop::GetThreadName() const { | |
423 if (thread_id_ == kInvalidThreadId) { | |
424 // |thread_id_| may already have been initialized but this thread might not | |
425 // have received the update yet. | |
426 subtle::MemoryBarrier(); | |
427 } | |
428 return ThreadIdNameManager::GetInstance()->GetName(thread_id_); | |
429 } | |
430 | |
419 void MessageLoop::SetTaskRunner( | 431 void MessageLoop::SetTaskRunner( |
420 scoped_refptr<SingleThreadTaskRunner> task_runner) { | 432 scoped_refptr<SingleThreadTaskRunner> task_runner) { |
421 DCHECK_EQ(this, current()); | 433 DCHECK_EQ(this, current()); |
422 DCHECK(task_runner->BelongsToCurrentThread()); | 434 DCHECK(task_runner->BelongsToCurrentThread()); |
423 DCHECK(!unbound_task_runner_); | 435 DCHECK(!unbound_task_runner_); |
424 task_runner_ = std::move(task_runner); | 436 task_runner_ = std::move(task_runner); |
425 SetThreadTaskRunnerHandle(); | 437 SetThreadTaskRunnerHandle(); |
426 } | 438 } |
427 | 439 |
428 void MessageLoop::SetThreadTaskRunnerHandle() { | 440 void MessageLoop::SetThreadTaskRunnerHandle() { |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
547 } | 559 } |
548 | 560 |
549 //------------------------------------------------------------------------------ | 561 //------------------------------------------------------------------------------ |
550 // Method and data for histogramming events and actions taken by each instance | 562 // Method and data for histogramming events and actions taken by each instance |
551 // on each thread. | 563 // on each thread. |
552 | 564 |
553 void MessageLoop::StartHistogrammer() { | 565 void MessageLoop::StartHistogrammer() { |
554 #if !defined(OS_NACL) // NaCl build has no metrics code. | 566 #if !defined(OS_NACL) // NaCl build has no metrics code. |
555 if (enable_histogrammer_ && !message_histogram_ | 567 if (enable_histogrammer_ && !message_histogram_ |
556 && StatisticsRecorder::IsActive()) { | 568 && StatisticsRecorder::IsActive()) { |
557 DCHECK(!thread_name_.empty()); | 569 std::string thread_name = GetThreadName(); |
570 DCHECK(!thread_name.empty()); | |
558 message_histogram_ = LinearHistogram::FactoryGetWithRangeDescription( | 571 message_histogram_ = LinearHistogram::FactoryGetWithRangeDescription( |
559 "MsgLoop:" + thread_name_, | 572 "MsgLoop:" + thread_name, kLeastNonZeroMessageId, kMaxMessageId, |
560 kLeastNonZeroMessageId, kMaxMessageId, | |
561 kNumberOfDistinctMessagesDisplayed, | 573 kNumberOfDistinctMessagesDisplayed, |
562 HistogramBase::kHexRangePrintingFlag, | 574 HistogramBase::kHexRangePrintingFlag, event_descriptions_); |
563 event_descriptions_); | |
564 } | 575 } |
565 #endif | 576 #endif |
566 } | 577 } |
567 | 578 |
568 void MessageLoop::HistogramEvent(int event) { | 579 void MessageLoop::HistogramEvent(int event) { |
569 #if !defined(OS_NACL) | 580 #if !defined(OS_NACL) |
570 if (message_histogram_) | 581 if (message_histogram_) |
571 message_histogram_->Add(event); | 582 message_histogram_->Add(event); |
572 #endif | 583 #endif |
573 } | 584 } |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
740 persistent, | 751 persistent, |
741 mode, | 752 mode, |
742 controller, | 753 controller, |
743 delegate); | 754 delegate); |
744 } | 755 } |
745 #endif | 756 #endif |
746 | 757 |
747 #endif // !defined(OS_NACL_SFI) | 758 #endif // !defined(OS_NACL_SFI) |
748 | 759 |
749 } // namespace base | 760 } // namespace base |
OLD | NEW |