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), |
65 message_loop_(nullptr), | 66 message_loop_(nullptr), |
66 message_loop_timer_slack_(TIMER_SLACK_NONE), | 67 message_loop_timer_slack_(TIMER_SLACK_NONE), |
67 name_(name) { | 68 name_(name) { |
68 } | 69 } |
69 | 70 |
70 Thread::~Thread() { | 71 Thread::~Thread() { |
71 Stop(); | 72 Stop(); |
72 } | 73 } |
73 | 74 |
74 bool Thread::Start() { | 75 bool Thread::Start() { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 | 166 |
166 DCHECK_NE(thread_id(), PlatformThread::CurrentId()); | 167 DCHECK_NE(thread_id(), PlatformThread::CurrentId()); |
167 | 168 |
168 if (stopping_ || !message_loop_) | 169 if (stopping_ || !message_loop_) |
169 return; | 170 return; |
170 | 171 |
171 stopping_ = true; | 172 stopping_ = true; |
172 task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); | 173 task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); |
173 } | 174 } |
174 | 175 |
175 PlatformThreadId Thread::thread_id() const { | |
176 AutoLock lock(thread_lock_); | |
177 return thread_.id(); | |
178 } | |
179 | |
180 bool Thread::IsRunning() const { | 176 bool Thread::IsRunning() const { |
181 // If the thread's already started (i.e. message_loop_ is non-null) and | 177 // If the thread's already started (i.e. message_loop_ is non-null) and |
182 // not yet requested to stop (i.e. stopping_ is false) we can just return | 178 // not yet requested to stop (i.e. stopping_ is false) we can just return |
183 // true. (Note that stopping_ is touched only on the same thread that | 179 // true. (Note that stopping_ is touched only on the same thread that |
184 // starts / started the new thread so we need no locking here.) | 180 // starts / started the new thread so we need no locking here.) |
185 if (message_loop_ && !stopping_) | 181 if (message_loop_ && !stopping_) |
186 return true; | 182 return true; |
187 // Otherwise check the running_ flag, which is set to true by the new thread | 183 // Otherwise check the running_ flag, which is set to true by the new thread |
188 // only while it is inside Run(). | 184 // only while it is inside Run(). |
189 AutoLock lock(running_lock_); | 185 AutoLock lock(running_lock_); |
(...skipping 11 matching lines...) Expand all Loading... |
201 bool Thread::GetThreadWasQuitProperly() { | 197 bool Thread::GetThreadWasQuitProperly() { |
202 bool quit_properly = true; | 198 bool quit_properly = true; |
203 #ifndef NDEBUG | 199 #ifndef NDEBUG |
204 quit_properly = lazy_tls_bool.Pointer()->Get(); | 200 quit_properly = lazy_tls_bool.Pointer()->Get(); |
205 #endif | 201 #endif |
206 return quit_properly; | 202 return quit_properly; |
207 } | 203 } |
208 | 204 |
209 void Thread::ThreadMain() { | 205 void Thread::ThreadMain() { |
210 // Complete the initialization of our Thread object. | 206 // Complete the initialization of our Thread object. |
| 207 id_ = PlatformThread::CurrentId(); |
211 PlatformThread::SetName(name_.c_str()); | 208 PlatformThread::SetName(name_.c_str()); |
212 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. | 209 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. |
213 | 210 |
214 // Lazily initialize the message_loop so that it can run on this thread. | 211 // Lazily initialize the message_loop so that it can run on this thread. |
215 DCHECK(message_loop_); | 212 DCHECK(message_loop_); |
216 scoped_ptr<MessageLoop> message_loop(message_loop_); | 213 scoped_ptr<MessageLoop> message_loop(message_loop_); |
217 message_loop_->BindToCurrentThread(); | 214 message_loop_->BindToCurrentThread(); |
218 message_loop_->set_thread_name(name_); | 215 message_loop_->set_thread_name(name_); |
219 message_loop_->SetTimerSlack(message_loop_timer_slack_); | 216 message_loop_->SetTimerSlack(message_loop_timer_slack_); |
220 | 217 |
221 #if defined(OS_WIN) | 218 #if defined(OS_WIN) |
222 scoped_ptr<win::ScopedCOMInitializer> com_initializer; | 219 scoped_ptr<win::ScopedCOMInitializer> com_initializer; |
223 if (com_status_ != NONE) { | 220 if (com_status_ != NONE) { |
224 com_initializer.reset((com_status_ == STA) ? | 221 com_initializer.reset((com_status_ == STA) ? |
225 new win::ScopedCOMInitializer() : | 222 new win::ScopedCOMInitializer() : |
226 new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA)); | 223 new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA)); |
227 } | 224 } |
228 #endif | 225 #endif |
229 | 226 |
230 // Make sure the thread_id() returns current thread. | |
231 // (This internally acquires lock against PlatformThread::Create) | |
232 DCHECK_EQ(thread_id(), PlatformThread::CurrentId()); | |
233 | |
234 // Let the thread do extra initialization. | 227 // Let the thread do extra initialization. |
235 Init(); | 228 Init(); |
236 | 229 |
237 { | 230 { |
238 AutoLock lock(running_lock_); | 231 AutoLock lock(running_lock_); |
239 running_ = true; | 232 running_ = true; |
240 } | 233 } |
241 | 234 |
242 start_event_->Signal(); | 235 start_event_->Signal(); |
243 | 236 |
(...skipping 13 matching lines...) Expand all Loading... |
257 | 250 |
258 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. | 251 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. |
259 DCHECK(GetThreadWasQuitProperly()); | 252 DCHECK(GetThreadWasQuitProperly()); |
260 | 253 |
261 // We can't receive messages anymore. | 254 // We can't receive messages anymore. |
262 // (The message loop is destructed at the end of this block) | 255 // (The message loop is destructed at the end of this block) |
263 message_loop_ = NULL; | 256 message_loop_ = NULL; |
264 } | 257 } |
265 | 258 |
266 } // namespace base | 259 } // namespace base |
OLD | NEW |