Chromium Code Reviews| 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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 | 170 |
| 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 AutoLock lock(thread_lock_); | |
| 181 return thread_.id(); | |
| 182 } | |
| 183 | |
| 184 bool Thread::IsRunning() const { | 180 bool Thread::IsRunning() const { |
| 185 // If the thread's already started (i.e. message_loop_ is non-null) and | 181 // 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 | 182 // 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 | 183 // true. (Note that stopping_ is touched only on the same thread that |
| 188 // starts / started the new thread so we need no locking here.) | 184 // starts / started the new thread so we need no locking here.) |
| 189 if (message_loop_ && !stopping_) | 185 if (message_loop_ && !stopping_) |
| 190 return true; | 186 return true; |
| 191 // Otherwise check the running_ flag, which is set to true by the new thread | 187 // Otherwise check the running_ flag, which is set to true by the new thread |
| 192 // only while it is inside Run(). | 188 // only while it is inside Run(). |
| 193 AutoLock lock(running_lock_); | 189 AutoLock lock(running_lock_); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 205 bool Thread::GetThreadWasQuitProperly() { | 201 bool Thread::GetThreadWasQuitProperly() { |
| 206 bool quit_properly = true; | 202 bool quit_properly = true; |
| 207 #ifndef NDEBUG | 203 #ifndef NDEBUG |
| 208 quit_properly = lazy_tls_bool.Pointer()->Get(); | 204 quit_properly = lazy_tls_bool.Pointer()->Get(); |
| 209 #endif | 205 #endif |
| 210 return quit_properly; | 206 return quit_properly; |
| 211 } | 207 } |
| 212 | 208 |
| 213 void Thread::ThreadMain() { | 209 void Thread::ThreadMain() { |
| 214 // Complete the initialization of our Thread object. | 210 // Complete the initialization of our Thread object. |
| 211 id_ = PlatformThread::CurrentId(); | |
|
gab
2015/07/02 12:00:52
Need to lock to make sure this change is flushed t
Takashi Toyoshima
2015/07/03 04:22:56
Done.
| |
| 215 PlatformThread::SetName(name_.c_str()); | 212 PlatformThread::SetName(name_.c_str()); |
| 216 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. | 213 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. |
| 217 | 214 |
| 218 // Lazily initialize the message_loop so that it can run on this thread. | 215 // Lazily initialize the message_loop so that it can run on this thread. |
| 219 DCHECK(message_loop_); | 216 DCHECK(message_loop_); |
| 220 scoped_ptr<MessageLoop> message_loop(message_loop_); | 217 scoped_ptr<MessageLoop> message_loop(message_loop_); |
| 221 message_loop_->BindToCurrentThread(); | 218 message_loop_->BindToCurrentThread(); |
| 222 message_loop_->set_thread_name(name_); | 219 message_loop_->set_thread_name(name_); |
| 223 message_loop_->SetTimerSlack(message_loop_timer_slack_); | 220 message_loop_->SetTimerSlack(message_loop_timer_slack_); |
| 224 | 221 |
| 225 #if defined(OS_WIN) | 222 #if defined(OS_WIN) |
| 226 scoped_ptr<win::ScopedCOMInitializer> com_initializer; | 223 scoped_ptr<win::ScopedCOMInitializer> com_initializer; |
| 227 if (com_status_ != NONE) { | 224 if (com_status_ != NONE) { |
| 228 com_initializer.reset((com_status_ == STA) ? | 225 com_initializer.reset((com_status_ == STA) ? |
| 229 new win::ScopedCOMInitializer() : | 226 new win::ScopedCOMInitializer() : |
| 230 new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA)); | 227 new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA)); |
| 231 } | 228 } |
| 232 #endif | 229 #endif |
| 233 | 230 |
| 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. | 231 // Let the thread do extra initialization. |
| 239 Init(); | 232 Init(); |
| 240 | 233 |
| 241 { | 234 { |
| 242 AutoLock lock(running_lock_); | 235 AutoLock lock(running_lock_); |
| 243 running_ = true; | 236 running_ = true; |
| 244 } | 237 } |
| 245 | 238 |
| 246 start_event_->Signal(); | 239 start_event_->Signal(); |
| 247 | 240 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 261 | 254 |
| 262 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. | 255 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. |
| 263 DCHECK(GetThreadWasQuitProperly()); | 256 DCHECK(GetThreadWasQuitProperly()); |
| 264 | 257 |
| 265 // We can't receive messages anymore. | 258 // We can't receive messages anymore. |
| 266 // (The message loop is destructed at the end of this block) | 259 // (The message loop is destructed at the end of this block) |
| 267 message_loop_ = NULL; | 260 message_loop_ = NULL; |
| 268 } | 261 } |
| 269 | 262 |
| 270 } // namespace base | 263 } // namespace base |
| OLD | NEW |