OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 } | 55 } |
56 | 56 |
57 Thread::Thread(const std::string& name) | 57 Thread::Thread(const std::string& name) |
58 : | 58 : |
59 #if defined(OS_WIN) | 59 #if defined(OS_WIN) |
60 com_status_(NONE), | 60 com_status_(NONE), |
61 #endif | 61 #endif |
62 stopping_(false), | 62 stopping_(false), |
63 running_(false), | 63 running_(false), |
64 thread_(0), | 64 thread_(0), |
| 65 id_(kInvalidThreadId), |
| 66 id_event_(true, false), |
65 message_loop_(nullptr), | 67 message_loop_(nullptr), |
66 message_loop_timer_slack_(TIMER_SLACK_NONE), | 68 message_loop_timer_slack_(TIMER_SLACK_NONE), |
67 name_(name) { | 69 name_(name) { |
68 } | 70 } |
69 | 71 |
70 Thread::~Thread() { | 72 Thread::~Thread() { |
71 Stop(); | 73 Stop(); |
72 } | 74 } |
73 | 75 |
74 bool Thread::Start() { | 76 bool Thread::Start() { |
75 Options options; | 77 Options options; |
76 #if defined(OS_WIN) | 78 #if defined(OS_WIN) |
77 if (com_status_ == STA) | 79 if (com_status_ == STA) |
78 options.message_loop_type = MessageLoop::TYPE_UI; | 80 options.message_loop_type = MessageLoop::TYPE_UI; |
79 #endif | 81 #endif |
80 return StartWithOptions(options); | 82 return StartWithOptions(options); |
81 } | 83 } |
82 | 84 |
83 bool Thread::StartWithOptions(const Options& options) { | 85 bool Thread::StartWithOptions(const Options& options) { |
84 DCHECK(!message_loop_); | 86 DCHECK(!message_loop_); |
85 #if defined(OS_WIN) | 87 #if defined(OS_WIN) |
86 DCHECK((com_status_ != STA) || | 88 DCHECK((com_status_ != STA) || |
87 (options.message_loop_type == MessageLoop::TYPE_UI)); | 89 (options.message_loop_type == MessageLoop::TYPE_UI)); |
88 #endif | 90 #endif |
89 | 91 |
| 92 // Reset |id_| here to support restarting the thread. |
| 93 id_event_.Reset(); |
| 94 id_ = kInvalidThreadId; |
| 95 |
90 SetThreadWasQuitProperly(false); | 96 SetThreadWasQuitProperly(false); |
91 | 97 |
92 MessageLoop::Type type = options.message_loop_type; | 98 MessageLoop::Type type = options.message_loop_type; |
93 if (!options.message_pump_factory.is_null()) | 99 if (!options.message_pump_factory.is_null()) |
94 type = MessageLoop::TYPE_CUSTOM; | 100 type = MessageLoop::TYPE_CUSTOM; |
95 | 101 |
96 message_loop_timer_slack_ = options.timer_slack; | 102 message_loop_timer_slack_ = options.timer_slack; |
97 scoped_ptr<MessageLoop> message_loop = MessageLoop::CreateUnbound( | 103 scoped_ptr<MessageLoop> message_loop = MessageLoop::CreateUnbound( |
98 type, options.message_pump_factory); | 104 type, options.message_pump_factory); |
99 message_loop_ = message_loop.get(); | 105 message_loop_ = message_loop.get(); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 DCHECK_NE(thread_id(), PlatformThread::CurrentId()); | 176 DCHECK_NE(thread_id(), PlatformThread::CurrentId()); |
171 | 177 |
172 if (stopping_ || !message_loop_) | 178 if (stopping_ || !message_loop_) |
173 return; | 179 return; |
174 | 180 |
175 stopping_ = true; | 181 stopping_ = true; |
176 task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); | 182 task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); |
177 } | 183 } |
178 | 184 |
179 PlatformThreadId Thread::thread_id() const { | 185 PlatformThreadId Thread::thread_id() const { |
180 AutoLock lock(thread_lock_); | 186 // If the thread is created but not started yet, wait for |id_| being ready. |
181 return thread_.id(); | 187 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 188 id_event_.Wait(); |
| 189 return id_; |
182 } | 190 } |
183 | 191 |
184 bool Thread::IsRunning() const { | 192 bool Thread::IsRunning() const { |
185 // If the thread's already started (i.e. message_loop_ is non-null) and | 193 // If the thread's already started (i.e. message_loop_ is non-null) and |
186 // not yet requested to stop (i.e. stopping_ is false) we can just return | 194 // not yet requested to stop (i.e. stopping_ is false) we can just return |
187 // true. (Note that stopping_ is touched only on the same thread that | 195 // true. (Note that stopping_ is touched only on the same thread that |
188 // starts / started the new thread so we need no locking here.) | 196 // starts / started the new thread so we need no locking here.) |
189 if (message_loop_ && !stopping_) | 197 if (message_loop_ && !stopping_) |
190 return true; | 198 return true; |
191 // Otherwise check the running_ flag, which is set to true by the new thread | 199 // Otherwise check the running_ flag, which is set to true by the new thread |
(...skipping 12 matching lines...) Expand all Loading... |
204 | 212 |
205 bool Thread::GetThreadWasQuitProperly() { | 213 bool Thread::GetThreadWasQuitProperly() { |
206 bool quit_properly = true; | 214 bool quit_properly = true; |
207 #ifndef NDEBUG | 215 #ifndef NDEBUG |
208 quit_properly = lazy_tls_bool.Pointer()->Get(); | 216 quit_properly = lazy_tls_bool.Pointer()->Get(); |
209 #endif | 217 #endif |
210 return quit_properly; | 218 return quit_properly; |
211 } | 219 } |
212 | 220 |
213 void Thread::ThreadMain() { | 221 void Thread::ThreadMain() { |
| 222 // First, make thread_id() available to avoid deadlocks. It could be called |
| 223 // any place in the following thread initialization code. |
| 224 id_ = PlatformThread::CurrentId(); |
| 225 DCHECK_NE(kInvalidThreadId, id_); |
| 226 id_event_.Signal(); |
| 227 |
214 // Complete the initialization of our Thread object. | 228 // Complete the initialization of our Thread object. |
215 PlatformThread::SetName(name_.c_str()); | 229 PlatformThread::SetName(name_.c_str()); |
216 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. | 230 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. |
217 | 231 |
218 // Lazily initialize the message_loop so that it can run on this thread. | 232 // Lazily initialize the message_loop so that it can run on this thread. |
219 DCHECK(message_loop_); | 233 DCHECK(message_loop_); |
220 scoped_ptr<MessageLoop> message_loop(message_loop_); | 234 scoped_ptr<MessageLoop> message_loop(message_loop_); |
221 message_loop_->BindToCurrentThread(); | 235 message_loop_->BindToCurrentThread(); |
222 message_loop_->set_thread_name(name_); | 236 message_loop_->set_thread_name(name_); |
223 message_loop_->SetTimerSlack(message_loop_timer_slack_); | 237 message_loop_->SetTimerSlack(message_loop_timer_slack_); |
224 | 238 |
225 #if defined(OS_WIN) | 239 #if defined(OS_WIN) |
226 scoped_ptr<win::ScopedCOMInitializer> com_initializer; | 240 scoped_ptr<win::ScopedCOMInitializer> com_initializer; |
227 if (com_status_ != NONE) { | 241 if (com_status_ != NONE) { |
228 com_initializer.reset((com_status_ == STA) ? | 242 com_initializer.reset((com_status_ == STA) ? |
229 new win::ScopedCOMInitializer() : | 243 new win::ScopedCOMInitializer() : |
230 new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA)); | 244 new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA)); |
231 } | 245 } |
232 #endif | 246 #endif |
233 | 247 |
234 // Make sure the thread_id() returns current thread. | |
235 // (This internally acquires lock against PlatformThread::Create) | |
236 DCHECK_EQ(thread_id(), PlatformThread::CurrentId()); | |
237 | |
238 // Let the thread do extra initialization. | 248 // Let the thread do extra initialization. |
239 Init(); | 249 Init(); |
240 | 250 |
241 { | 251 { |
242 AutoLock lock(running_lock_); | 252 AutoLock lock(running_lock_); |
243 running_ = true; | 253 running_ = true; |
244 } | 254 } |
245 | 255 |
246 start_event_->Signal(); | 256 start_event_->Signal(); |
247 | 257 |
(...skipping 13 matching lines...) Expand all Loading... |
261 | 271 |
262 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. | 272 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. |
263 DCHECK(GetThreadWasQuitProperly()); | 273 DCHECK(GetThreadWasQuitProperly()); |
264 | 274 |
265 // We can't receive messages anymore. | 275 // We can't receive messages anymore. |
266 // (The message loop is destructed at the end of this block) | 276 // (The message loop is destructed at the end of this block) |
267 message_loop_ = NULL; | 277 message_loop_ = NULL; |
268 } | 278 } |
269 | 279 |
270 } // namespace base | 280 } // namespace base |
OLD | NEW |