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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 DCHECK_NE(thread_id(), PlatformThread::CurrentId()); | 171 DCHECK_NE(thread_id(), PlatformThread::CurrentId()); |
171 | 172 |
172 if (stopping_ || !message_loop_) | 173 if (stopping_ || !message_loop_) |
173 return; | 174 return; |
174 | 175 |
175 stopping_ = true; | 176 stopping_ = true; |
176 task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); | 177 task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); |
177 } | 178 } |
178 | 179 |
179 PlatformThreadId Thread::thread_id() const { | 180 PlatformThreadId Thread::thread_id() const { |
180 AutoLock lock(thread_lock_); | 181 CHECK_NE(kInvalidThreadId, id_); |
gab
2015/07/09 18:19:00
Is this really a security issue? i.e. is it worth
Takashi Toyoshima
2015/07/14 10:47:28
I see, DCHECK_NE looks better here.
| |
181 return thread_.id(); | 182 AutoLock lock(id_lock_); |
183 return id_; | |
182 } | 184 } |
183 | 185 |
184 bool Thread::IsRunning() const { | 186 bool Thread::IsRunning() const { |
185 // If the thread's already started (i.e. message_loop_ is non-null) and | 187 // 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 | 188 // 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 | 189 // true. (Note that stopping_ is touched only on the same thread that |
188 // starts / started the new thread so we need no locking here.) | 190 // starts / started the new thread so we need no locking here.) |
189 if (message_loop_ && !stopping_) | 191 if (message_loop_ && !stopping_) |
190 return true; | 192 return true; |
191 // Otherwise check the running_ flag, which is set to true by the new thread | 193 // Otherwise check the running_ flag, which is set to true by the new thread |
(...skipping 13 matching lines...) Expand all Loading... | |
205 bool Thread::GetThreadWasQuitProperly() { | 207 bool Thread::GetThreadWasQuitProperly() { |
206 bool quit_properly = true; | 208 bool quit_properly = true; |
207 #ifndef NDEBUG | 209 #ifndef NDEBUG |
208 quit_properly = lazy_tls_bool.Pointer()->Get(); | 210 quit_properly = lazy_tls_bool.Pointer()->Get(); |
209 #endif | 211 #endif |
210 return quit_properly; | 212 return quit_properly; |
211 } | 213 } |
212 | 214 |
213 void Thread::ThreadMain() { | 215 void Thread::ThreadMain() { |
214 // Complete the initialization of our Thread object. | 216 // Complete the initialization of our Thread object. |
217 { | |
218 AutoLock lock(id_lock_); | |
219 id_ = PlatformThread::CurrentId(); | |
220 } | |
215 PlatformThread::SetName(name_.c_str()); | 221 PlatformThread::SetName(name_.c_str()); |
216 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. | 222 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. |
217 | 223 |
218 // Lazily initialize the message_loop so that it can run on this thread. | 224 // Lazily initialize the message_loop so that it can run on this thread. |
219 DCHECK(message_loop_); | 225 DCHECK(message_loop_); |
220 scoped_ptr<MessageLoop> message_loop(message_loop_); | 226 scoped_ptr<MessageLoop> message_loop(message_loop_); |
221 message_loop_->BindToCurrentThread(); | 227 message_loop_->BindToCurrentThread(); |
222 message_loop_->set_thread_name(name_); | 228 message_loop_->set_thread_name(name_); |
223 message_loop_->SetTimerSlack(message_loop_timer_slack_); | 229 message_loop_->SetTimerSlack(message_loop_timer_slack_); |
224 | 230 |
225 #if defined(OS_WIN) | 231 #if defined(OS_WIN) |
226 scoped_ptr<win::ScopedCOMInitializer> com_initializer; | 232 scoped_ptr<win::ScopedCOMInitializer> com_initializer; |
227 if (com_status_ != NONE) { | 233 if (com_status_ != NONE) { |
228 com_initializer.reset((com_status_ == STA) ? | 234 com_initializer.reset((com_status_ == STA) ? |
229 new win::ScopedCOMInitializer() : | 235 new win::ScopedCOMInitializer() : |
230 new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA)); | 236 new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA)); |
231 } | 237 } |
232 #endif | 238 #endif |
233 | 239 |
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. | 240 // Let the thread do extra initialization. |
239 Init(); | 241 Init(); |
240 | 242 |
241 { | 243 { |
242 AutoLock lock(running_lock_); | 244 AutoLock lock(running_lock_); |
243 running_ = true; | 245 running_ = true; |
244 } | 246 } |
245 | 247 |
246 start_event_->Signal(); | 248 start_event_->Signal(); |
247 | 249 |
(...skipping 13 matching lines...) Expand all Loading... | |
261 | 263 |
262 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. | 264 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. |
263 DCHECK(GetThreadWasQuitProperly()); | 265 DCHECK(GetThreadWasQuitProperly()); |
264 | 266 |
265 // We can't receive messages anymore. | 267 // We can't receive messages anymore. |
266 // (The message loop is destructed at the end of this block) | 268 // (The message loop is destructed at the end of this block) |
267 message_loop_ = NULL; | 269 message_loop_ = NULL; |
268 } | 270 } |
269 | 271 |
270 } // namespace base | 272 } // namespace base |
OLD | NEW |