| 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 id_event_.Reset(); |
| 93 id_ = kInvalidThreadId; |
| 94 |
| 90 SetThreadWasQuitProperly(false); | 95 SetThreadWasQuitProperly(false); |
| 91 | 96 |
| 92 MessageLoop::Type type = options.message_loop_type; | 97 MessageLoop::Type type = options.message_loop_type; |
| 93 if (!options.message_pump_factory.is_null()) | 98 if (!options.message_pump_factory.is_null()) |
| 94 type = MessageLoop::TYPE_CUSTOM; | 99 type = MessageLoop::TYPE_CUSTOM; |
| 95 | 100 |
| 96 message_loop_timer_slack_ = options.timer_slack; | 101 message_loop_timer_slack_ = options.timer_slack; |
| 97 scoped_ptr<MessageLoop> message_loop = MessageLoop::CreateUnbound( | 102 scoped_ptr<MessageLoop> message_loop = MessageLoop::CreateUnbound( |
| 98 type, options.message_pump_factory); | 103 type, options.message_pump_factory); |
| 99 message_loop_ = message_loop.get(); | 104 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()); | 175 DCHECK_NE(thread_id(), PlatformThread::CurrentId()); |
| 171 | 176 |
| 172 if (stopping_ || !message_loop_) | 177 if (stopping_ || !message_loop_) |
| 173 return; | 178 return; |
| 174 | 179 |
| 175 stopping_ = true; | 180 stopping_ = true; |
| 176 task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); | 181 task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); |
| 177 } | 182 } |
| 178 | 183 |
| 179 PlatformThreadId Thread::thread_id() const { | 184 PlatformThreadId Thread::thread_id() const { |
| 180 AutoLock lock(thread_lock_); | 185 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 181 return thread_.id(); | 186 id_event_.Wait(); |
| 187 return id_; |
| 182 } | 188 } |
| 183 | 189 |
| 184 bool Thread::IsRunning() const { | 190 bool Thread::IsRunning() const { |
| 185 // If the thread's already started (i.e. message_loop_ is non-null) and | 191 // 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 | 192 // 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 | 193 // true. (Note that stopping_ is touched only on the same thread that |
| 188 // starts / started the new thread so we need no locking here.) | 194 // starts / started the new thread so we need no locking here.) |
| 189 if (message_loop_ && !stopping_) | 195 if (message_loop_ && !stopping_) |
| 190 return true; | 196 return true; |
| 191 // Otherwise check the running_ flag, which is set to true by the new thread | 197 // Otherwise check the running_ flag, which is set to true by the new thread |
| (...skipping 12 matching lines...) Expand all Loading... |
| 204 | 210 |
| 205 bool Thread::GetThreadWasQuitProperly() { | 211 bool Thread::GetThreadWasQuitProperly() { |
| 206 bool quit_properly = true; | 212 bool quit_properly = true; |
| 207 #ifndef NDEBUG | 213 #ifndef NDEBUG |
| 208 quit_properly = lazy_tls_bool.Pointer()->Get(); | 214 quit_properly = lazy_tls_bool.Pointer()->Get(); |
| 209 #endif | 215 #endif |
| 210 return quit_properly; | 216 return quit_properly; |
| 211 } | 217 } |
| 212 | 218 |
| 213 void Thread::ThreadMain() { | 219 void Thread::ThreadMain() { |
| 220 // Make thread_id() available first since it could be called any place. |
| 221 id_ = PlatformThread::CurrentId(); |
| 222 DCHECK_NE(kInvalidThreadId, id_); |
| 223 id_event_.Signal(); |
| 224 |
| 214 // Complete the initialization of our Thread object. | 225 // Complete the initialization of our Thread object. |
| 215 PlatformThread::SetName(name_.c_str()); | 226 PlatformThread::SetName(name_.c_str()); |
| 216 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. | 227 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. |
| 217 | 228 |
| 218 // Lazily initialize the message_loop so that it can run on this thread. | 229 // Lazily initialize the message_loop so that it can run on this thread. |
| 219 DCHECK(message_loop_); | 230 DCHECK(message_loop_); |
| 220 scoped_ptr<MessageLoop> message_loop(message_loop_); | 231 scoped_ptr<MessageLoop> message_loop(message_loop_); |
| 221 message_loop_->BindToCurrentThread(); | 232 message_loop_->BindToCurrentThread(); |
| 222 message_loop_->set_thread_name(name_); | 233 message_loop_->set_thread_name(name_); |
| 223 message_loop_->SetTimerSlack(message_loop_timer_slack_); | 234 message_loop_->SetTimerSlack(message_loop_timer_slack_); |
| 224 | 235 |
| 225 #if defined(OS_WIN) | 236 #if defined(OS_WIN) |
| 226 scoped_ptr<win::ScopedCOMInitializer> com_initializer; | 237 scoped_ptr<win::ScopedCOMInitializer> com_initializer; |
| 227 if (com_status_ != NONE) { | 238 if (com_status_ != NONE) { |
| 228 com_initializer.reset((com_status_ == STA) ? | 239 com_initializer.reset((com_status_ == STA) ? |
| 229 new win::ScopedCOMInitializer() : | 240 new win::ScopedCOMInitializer() : |
| 230 new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA)); | 241 new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA)); |
| 231 } | 242 } |
| 232 #endif | 243 #endif |
| 233 | 244 |
| 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. | 245 // Let the thread do extra initialization. |
| 239 Init(); | 246 Init(); |
| 240 | 247 |
| 241 { | 248 { |
| 242 AutoLock lock(running_lock_); | 249 AutoLock lock(running_lock_); |
| 243 running_ = true; | 250 running_ = true; |
| 244 } | 251 } |
| 245 | 252 |
| 246 start_event_->Signal(); | 253 start_event_->Signal(); |
| 247 | 254 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 261 | 268 |
| 262 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. | 269 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. |
| 263 DCHECK(GetThreadWasQuitProperly()); | 270 DCHECK(GetThreadWasQuitProperly()); |
| 264 | 271 |
| 265 // We can't receive messages anymore. | 272 // We can't receive messages anymore. |
| 266 // (The message loop is destructed at the end of this block) | 273 // (The message loop is destructed at the end of this block) |
| 267 message_loop_ = NULL; | 274 message_loop_ = NULL; |
| 268 } | 275 } |
| 269 | 276 |
| 270 } // namespace base | 277 } // namespace base |
| OLD | NEW |