| 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/threading/thread.h" | 5 #include "base/threading/thread.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 8 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 9 #include "base/threading/thread_local.h" | 9 #include "base/threading/thread_local.h" |
| 10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| 11 #include "base/tracked_objects.h" | |
| 12 | 11 |
| 13 namespace base { | 12 namespace base { |
| 14 | 13 |
| 15 namespace { | 14 namespace { |
| 16 | 15 |
| 17 // We use this thread-local variable to record whether or not a thread exited | 16 // We use this thread-local variable to record whether or not a thread exited |
| 18 // because its Stop method was called. This allows us to catch cases where | 17 // because its Stop method was called. This allows us to catch cases where |
| 19 // MessageLoop::Quit() is called directly, which is unexpected when using a | 18 // MessageLoop::Quit() is called directly, which is unexpected when using a |
| 20 // Thread to setup and run a MessageLoop. | 19 // Thread to setup and run a MessageLoop. |
| 21 base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool( | 20 base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool( |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 { | 144 { |
| 146 // The message loop for this thread. | 145 // The message loop for this thread. |
| 147 MessageLoop message_loop(startup_data_->options.message_loop_type); | 146 MessageLoop message_loop(startup_data_->options.message_loop_type); |
| 148 | 147 |
| 149 // Complete the initialization of our Thread object. | 148 // Complete the initialization of our Thread object. |
| 150 thread_id_ = PlatformThread::CurrentId(); | 149 thread_id_ = PlatformThread::CurrentId(); |
| 151 PlatformThread::SetName(name_.c_str()); | 150 PlatformThread::SetName(name_.c_str()); |
| 152 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. | 151 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. |
| 153 message_loop.set_thread_name(name_); | 152 message_loop.set_thread_name(name_); |
| 154 message_loop_ = &message_loop; | 153 message_loop_ = &message_loop; |
| 155 tracked_objects::ThreadData::InitializeThreadContext(name_); | |
| 156 | 154 |
| 157 // Let the thread do extra initialization. | 155 // Let the thread do extra initialization. |
| 158 // Let's do this before signaling we are started. | 156 // Let's do this before signaling we are started. |
| 159 Init(); | 157 Init(); |
| 160 | 158 |
| 161 startup_data_->event.Signal(); | 159 startup_data_->event.Signal(); |
| 162 // startup_data_ can't be touched anymore since the starting thread is now | 160 // startup_data_ can't be touched anymore since the starting thread is now |
| 163 // unlocked. | 161 // unlocked. |
| 164 | 162 |
| 165 Run(message_loop_); | 163 Run(message_loop_); |
| 166 | 164 |
| 167 // Let the thread do extra cleanup. | 165 // Let the thread do extra cleanup. |
| 168 CleanUp(); | 166 CleanUp(); |
| 169 | 167 |
| 170 // Assert that MessageLoop::Quit was called by ThreadQuitTask. | 168 // Assert that MessageLoop::Quit was called by ThreadQuitTask. |
| 171 DCHECK(GetThreadWasQuitProperly()); | 169 DCHECK(GetThreadWasQuitProperly()); |
| 172 | 170 |
| 173 // We can't receive messages anymore. | 171 // We can't receive messages anymore. |
| 174 message_loop_ = NULL; | 172 message_loop_ = NULL; |
| 175 } | 173 } |
| 176 thread_id_ = kInvalidThreadId; | 174 thread_id_ = kInvalidThreadId; |
| 177 } | 175 } |
| 178 | 176 |
| 179 } // namespace base | 177 } // namespace base |
| OLD | NEW |