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 <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/lazy_instance.h" | |
13 #include "base/logging.h" | 12 #include "base/logging.h" |
14 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
15 #include "base/message_loop/message_pump_default.h" | 14 #include "base/message_loop/message_pump_default.h" |
16 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
17 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 16 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
18 #include "base/threading/thread_id_name_manager.h" | 17 #include "base/threading/thread_id_name_manager.h" |
19 #include "base/threading/thread_local.h" | 18 #include "base/threading/thread_local.h" |
20 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
21 #include "base/trace_event/trace_event.h" | 20 #include "base/trace_event/trace_event.h" |
22 | 21 |
23 #if defined(OS_MACOSX) | 22 #if defined(OS_MACOSX) |
24 #include "base/message_loop/message_pump_mac.h" | 23 #include "base/message_loop/message_pump_mac.h" |
25 #endif | 24 #endif |
26 #if defined(OS_POSIX) && !defined(OS_IOS) | 25 #if defined(OS_POSIX) && !defined(OS_IOS) |
27 #include "base/message_loop/message_pump_libevent.h" | 26 #include "base/message_loop/message_pump_libevent.h" |
28 #endif | 27 #endif |
29 #if defined(OS_ANDROID) | 28 #if defined(OS_ANDROID) |
30 #include "base/message_loop/message_pump_android.h" | 29 #include "base/message_loop/message_pump_android.h" |
31 #endif | 30 #endif |
32 #if defined(USE_GLIB) | 31 #if defined(USE_GLIB) |
33 #include "base/message_loop/message_pump_glib.h" | 32 #include "base/message_loop/message_pump_glib.h" |
34 #endif | 33 #endif |
35 | 34 |
36 namespace base { | 35 namespace base { |
37 | 36 |
38 namespace { | 37 namespace { |
39 | 38 |
40 // A lazily created thread local storage for quick access to a thread's message | 39 // A lazily created thread local storage for quick access to a thread's message |
41 // loop, if one exists. This should be safe and free of static constructors. | 40 // loop, if one exists. |
42 LazyInstance<base::ThreadLocalPointer<MessageLoop> >::Leaky lazy_tls_ptr = | 41 base::ThreadLocalPointer<MessageLoop>* GetTLSMessageLoop() { |
43 LAZY_INSTANCE_INITIALIZER; | 42 static auto lazy_tls_ptr = new base::ThreadLocalPointer<MessageLoop>(); |
44 | 43 return lazy_tls_ptr; |
| 44 } |
45 MessageLoop::MessagePumpFactory* message_pump_for_ui_factory_ = NULL; | 45 MessageLoop::MessagePumpFactory* message_pump_for_ui_factory_ = NULL; |
46 | 46 |
47 #if defined(OS_IOS) | 47 #if defined(OS_IOS) |
48 typedef MessagePumpIOSForIO MessagePumpForIO; | 48 typedef MessagePumpIOSForIO MessagePumpForIO; |
49 #elif defined(OS_NACL_SFI) | 49 #elif defined(OS_NACL_SFI) |
50 typedef MessagePumpDefault MessagePumpForIO; | 50 typedef MessagePumpDefault MessagePumpForIO; |
51 #elif defined(OS_POSIX) | 51 #elif defined(OS_POSIX) |
52 typedef MessagePumpLibevent MessagePumpForIO; | 52 typedef MessagePumpLibevent MessagePumpForIO; |
53 #endif | 53 #endif |
54 | 54 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 thread_task_runner_handle_.reset(); | 130 thread_task_runner_handle_.reset(); |
131 | 131 |
132 // Tell the incoming queue that we are dying. | 132 // Tell the incoming queue that we are dying. |
133 incoming_task_queue_->WillDestroyCurrentMessageLoop(); | 133 incoming_task_queue_->WillDestroyCurrentMessageLoop(); |
134 incoming_task_queue_ = NULL; | 134 incoming_task_queue_ = NULL; |
135 unbound_task_runner_ = NULL; | 135 unbound_task_runner_ = NULL; |
136 task_runner_ = NULL; | 136 task_runner_ = NULL; |
137 | 137 |
138 // OK, now make it so that no one can find us. | 138 // OK, now make it so that no one can find us. |
139 if (current() == this) | 139 if (current() == this) |
140 lazy_tls_ptr.Pointer()->Set(nullptr); | 140 GetTLSMessageLoop()->Set(nullptr); |
141 } | 141 } |
142 | 142 |
143 // static | 143 // static |
144 MessageLoop* MessageLoop::current() { | 144 MessageLoop* MessageLoop::current() { |
145 // TODO(darin): sadly, we cannot enable this yet since people call us even | 145 // TODO(darin): sadly, we cannot enable this yet since people call us even |
146 // when they have no intention of using us. | 146 // when they have no intention of using us. |
147 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; | 147 // DCHECK(loop) << "Ouch, did you forget to initialize me?"; |
148 return lazy_tls_ptr.Pointer()->Get(); | 148 return GetTLSMessageLoop()->Get(); |
149 } | 149 } |
150 | 150 |
151 // static | 151 // static |
152 bool MessageLoop::InitMessagePumpForUIFactory(MessagePumpFactory* factory) { | 152 bool MessageLoop::InitMessagePumpForUIFactory(MessagePumpFactory* factory) { |
153 if (message_pump_for_ui_factory_) | 153 if (message_pump_for_ui_factory_) |
154 return false; | 154 return false; |
155 | 155 |
156 message_pump_for_ui_factory_ = factory; | 156 message_pump_for_ui_factory_ = factory; |
157 return true; | 157 return true; |
158 } | 158 } |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 } | 331 } |
332 | 332 |
333 void MessageLoop::BindToCurrentThread() { | 333 void MessageLoop::BindToCurrentThread() { |
334 DCHECK(!pump_); | 334 DCHECK(!pump_); |
335 if (!pump_factory_.is_null()) | 335 if (!pump_factory_.is_null()) |
336 pump_ = pump_factory_.Run(); | 336 pump_ = pump_factory_.Run(); |
337 else | 337 else |
338 pump_ = CreateMessagePumpForType(type_); | 338 pump_ = CreateMessagePumpForType(type_); |
339 | 339 |
340 DCHECK(!current()) << "should only have one message loop per thread"; | 340 DCHECK(!current()) << "should only have one message loop per thread"; |
341 lazy_tls_ptr.Pointer()->Set(this); | 341 GetTLSMessageLoop()->Set(this); |
342 | 342 |
343 incoming_task_queue_->StartScheduling(); | 343 incoming_task_queue_->StartScheduling(); |
344 unbound_task_runner_->BindToCurrentThread(); | 344 unbound_task_runner_->BindToCurrentThread(); |
345 unbound_task_runner_ = nullptr; | 345 unbound_task_runner_ = nullptr; |
346 SetThreadTaskRunnerHandle(); | 346 SetThreadTaskRunnerHandle(); |
347 thread_id_ = PlatformThread::CurrentId(); | 347 thread_id_ = PlatformThread::CurrentId(); |
348 } | 348 } |
349 | 349 |
350 std::string MessageLoop::GetThreadName() const { | 350 std::string MessageLoop::GetThreadName() const { |
351 DCHECK_NE(kInvalidThreadId, thread_id_) | 351 DCHECK_NE(kInvalidThreadId, thread_id_) |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
662 persistent, | 662 persistent, |
663 mode, | 663 mode, |
664 controller, | 664 controller, |
665 delegate); | 665 delegate); |
666 } | 666 } |
667 #endif | 667 #endif |
668 | 668 |
669 #endif // !defined(OS_NACL_SFI) | 669 #endif // !defined(OS_NACL_SFI) |
670 | 670 |
671 } // namespace base | 671 } // namespace base |
OLD | NEW |