| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "base/message_loop.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/debug/alias.h" | 11 #include "base/debug/alias.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/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/message_pump_default.h" | 15 #include "base/message_pump_default.h" |
| 16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 17 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 17 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 18 #include "base/threading/platform_thread.h" |
| 18 #include "base/threading/thread_local.h" | 19 #include "base/threading/thread_local.h" |
| 19 #include "base/time.h" | 20 #include "base/time.h" |
| 20 #include "base/tracked_objects.h" | 21 #include "base/tracked_objects.h" |
| 21 | 22 |
| 22 #if defined(OS_MACOSX) | 23 #if defined(OS_MACOSX) |
| 23 #include "base/message_pump_mac.h" | 24 #include "base/message_pump_mac.h" |
| 24 #endif | 25 #endif |
| 25 #if defined(OS_POSIX) | 26 #if defined(OS_POSIX) |
| 26 #include "base/message_pump_libevent.h" | 27 #include "base/message_pump_libevent.h" |
| 27 #endif | 28 #endif |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 } | 116 } |
| 116 | 117 |
| 117 MessageLoop::TaskObserver::~TaskObserver() { | 118 MessageLoop::TaskObserver::~TaskObserver() { |
| 118 } | 119 } |
| 119 | 120 |
| 120 MessageLoop::DestructionObserver::~DestructionObserver() { | 121 MessageLoop::DestructionObserver::~DestructionObserver() { |
| 121 } | 122 } |
| 122 | 123 |
| 123 //------------------------------------------------------------------------------ | 124 //------------------------------------------------------------------------------ |
| 124 | 125 |
| 125 MessageLoop::MessageLoop(Type type) | 126 MessageLoop::MessageLoop(Type type, const char* thread_name) |
| 126 : type_(type), | 127 : type_(type), |
| 127 nestable_tasks_allowed_(true), | 128 nestable_tasks_allowed_(true), |
| 128 exception_restoration_(false), | 129 exception_restoration_(false), |
| 130 thread_name_(thread_name ? thread_name : ""), |
| 129 message_histogram_(NULL), | 131 message_histogram_(NULL), |
| 130 state_(NULL), | 132 state_(NULL), |
| 131 should_leak_tasks_(true), | 133 should_leak_tasks_(true), |
| 132 #ifdef OS_WIN | 134 #ifdef OS_WIN |
| 133 os_modal_loop_(false), | 135 os_modal_loop_(false), |
| 134 #endif // OS_WIN | 136 #endif // OS_WIN |
| 135 next_sequence_num_(0) { | 137 next_sequence_num_(0) { |
| 136 DCHECK(!current()) << "should only have one message loop per thread"; | 138 DCHECK(!current()) << "should only have one message loop per thread"; |
| 137 lazy_tls_ptr.Pointer()->Set(this); | 139 lazy_tls_ptr.Pointer()->Set(this); |
| 138 | 140 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 159 #endif | 161 #endif |
| 160 | 162 |
| 161 if (type_ == TYPE_UI) { | 163 if (type_ == TYPE_UI) { |
| 162 pump_ = MESSAGE_PUMP_UI; | 164 pump_ = MESSAGE_PUMP_UI; |
| 163 } else if (type_ == TYPE_IO) { | 165 } else if (type_ == TYPE_IO) { |
| 164 pump_ = MESSAGE_PUMP_IO; | 166 pump_ = MESSAGE_PUMP_IO; |
| 165 } else { | 167 } else { |
| 166 DCHECK_EQ(TYPE_DEFAULT, type_); | 168 DCHECK_EQ(TYPE_DEFAULT, type_); |
| 167 pump_ = new base::MessagePumpDefault(); | 169 pump_ = new base::MessagePumpDefault(); |
| 168 } | 170 } |
| 171 if (!thread_name_.empty()) |
| 172 base::PlatformThread::SetName(thread_name_.c_str()); |
| 173 // Tell the name to race detector. |
| 174 ANNOTATE_THREAD_NAME(thread_name_.c_str()); |
| 169 } | 175 } |
| 170 | 176 |
| 171 MessageLoop::~MessageLoop() { | 177 MessageLoop::~MessageLoop() { |
| 172 DCHECK_EQ(this, current()); | 178 DCHECK_EQ(this, current()); |
| 173 | 179 |
| 174 DCHECK(!state_); | 180 DCHECK(!state_); |
| 175 | 181 |
| 176 // Clean up any unprocessed tasks, but take care: deleting a task could | 182 // Clean up any unprocessed tasks, but take care: deleting a task could |
| 177 // result in the addition of more tasks (e.g., via DeleteSoon). We set a | 183 // result in the addition of more tasks (e.g., via DeleteSoon). We set a |
| 178 // limit on the number of times we will allow a deleted task to generate more | 184 // limit on the number of times we will allow a deleted task to generate more |
| (...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 818 Watcher *delegate) { | 824 Watcher *delegate) { |
| 819 return pump_libevent()->WatchFileDescriptor( | 825 return pump_libevent()->WatchFileDescriptor( |
| 820 fd, | 826 fd, |
| 821 persistent, | 827 persistent, |
| 822 static_cast<base::MessagePumpLibevent::Mode>(mode), | 828 static_cast<base::MessagePumpLibevent::Mode>(mode), |
| 823 controller, | 829 controller, |
| 824 delegate); | 830 delegate); |
| 825 } | 831 } |
| 826 | 832 |
| 827 #endif | 833 #endif |
| OLD | NEW |