| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/threading/thread.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/lazy_instance.h" | |
| 9 #include "base/location.h" | |
| 10 #include "base/synchronization/waitable_event.h" | |
| 11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | |
| 12 #include "base/threading/thread_id_name_manager.h" | |
| 13 #include "base/threading/thread_local.h" | |
| 14 #include "base/threading/thread_restrictions.h" | |
| 15 | |
| 16 #if defined(OS_WIN) | |
| 17 #include "base/win/scoped_com_initializer.h" | |
| 18 #endif | |
| 19 | |
| 20 namespace base { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // We use this thread-local variable to record whether or not a thread exited | |
| 25 // because its Stop method was called. This allows us to catch cases where | |
| 26 // MessageLoop::QuitWhenIdle() is called directly, which is unexpected when | |
| 27 // using a Thread to setup and run a MessageLoop. | |
| 28 base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool = | |
| 29 LAZY_INSTANCE_INITIALIZER; | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 // This is used to trigger the message loop to exit. | |
| 34 void ThreadQuitHelper() { | |
| 35 MessageLoop::current()->QuitWhenIdle(); | |
| 36 Thread::SetThreadWasQuitProperly(true); | |
| 37 } | |
| 38 | |
| 39 Thread::Options::Options() | |
| 40 : message_loop_type(MessageLoop::TYPE_DEFAULT), | |
| 41 timer_slack(TIMER_SLACK_NONE), | |
| 42 stack_size(0), | |
| 43 priority(ThreadPriority::NORMAL) { | |
| 44 } | |
| 45 | |
| 46 Thread::Options::Options(MessageLoop::Type type, | |
| 47 size_t size) | |
| 48 : message_loop_type(type), | |
| 49 timer_slack(TIMER_SLACK_NONE), | |
| 50 stack_size(size), | |
| 51 priority(ThreadPriority::NORMAL) { | |
| 52 } | |
| 53 | |
| 54 Thread::Options::~Options() { | |
| 55 } | |
| 56 | |
| 57 Thread::Thread(const std::string& name) | |
| 58 : | |
| 59 #if defined(OS_WIN) | |
| 60 com_status_(NONE), | |
| 61 #endif | |
| 62 stopping_(false), | |
| 63 running_(false), | |
| 64 thread_(0), | |
| 65 message_loop_(nullptr), | |
| 66 message_loop_timer_slack_(TIMER_SLACK_NONE), | |
| 67 name_(name) { | |
| 68 } | |
| 69 | |
| 70 Thread::~Thread() { | |
| 71 Stop(); | |
| 72 } | |
| 73 | |
| 74 bool Thread::Start() { | |
| 75 Options options; | |
| 76 #if defined(OS_WIN) | |
| 77 if (com_status_ == STA) | |
| 78 options.message_loop_type = MessageLoop::TYPE_UI; | |
| 79 #endif | |
| 80 return StartWithOptions(options); | |
| 81 } | |
| 82 | |
| 83 bool Thread::StartWithOptions(const Options& options) { | |
| 84 DCHECK(!message_loop_); | |
| 85 #if defined(OS_WIN) | |
| 86 DCHECK((com_status_ != STA) || | |
| 87 (options.message_loop_type == MessageLoop::TYPE_UI)); | |
| 88 #endif | |
| 89 | |
| 90 SetThreadWasQuitProperly(false); | |
| 91 | |
| 92 MessageLoop::Type type = options.message_loop_type; | |
| 93 if (!options.message_pump_factory.is_null()) | |
| 94 type = MessageLoop::TYPE_CUSTOM; | |
| 95 | |
| 96 message_loop_timer_slack_ = options.timer_slack; | |
| 97 scoped_ptr<MessageLoop> message_loop = | |
| 98 MessageLoop::CreateUnbound(type, options.message_pump_factory); | |
| 99 message_loop_ = message_loop.get(); | |
| 100 start_event_.reset(new WaitableEvent(false, false)); | |
| 101 | |
| 102 // Hold the thread_lock_ while starting a new thread, so that we can make sure | |
| 103 // that thread_ is populated before the newly created thread accesses it. | |
| 104 { | |
| 105 AutoLock lock(thread_lock_); | |
| 106 bool created; | |
| 107 if (options.priority == ThreadPriority::NORMAL) { | |
| 108 created = PlatformThread::Create(options.stack_size, this, &thread_); | |
| 109 } else { | |
| 110 created = PlatformThread::CreateWithPriority(options.stack_size, this, | |
| 111 &thread_, options.priority); | |
| 112 } | |
| 113 if (!created) { | |
| 114 DLOG(ERROR) << "failed to create thread"; | |
| 115 message_loop_ = nullptr; | |
| 116 start_event_.reset(); | |
| 117 return false; | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 // The ownership of message_loop is managemed by the newly created thread | |
| 122 // within the ThreadMain. | |
| 123 ignore_result(message_loop.release()); | |
| 124 | |
| 125 DCHECK(message_loop_); | |
| 126 return true; | |
| 127 } | |
| 128 | |
| 129 bool Thread::StartAndWaitForTesting() { | |
| 130 bool result = Start(); | |
| 131 if (!result) | |
| 132 return false; | |
| 133 WaitUntilThreadStarted(); | |
| 134 return true; | |
| 135 } | |
| 136 | |
| 137 bool Thread::WaitUntilThreadStarted() { | |
| 138 if (!start_event_) | |
| 139 return false; | |
| 140 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 141 start_event_->Wait(); | |
| 142 return true; | |
| 143 } | |
| 144 | |
| 145 void Thread::Stop() { | |
| 146 if (!start_event_) | |
| 147 return; | |
| 148 | |
| 149 StopSoon(); | |
| 150 | |
| 151 // Wait for the thread to exit. | |
| 152 // | |
| 153 // TODO(darin): Unfortunately, we need to keep message_loop_ around until | |
| 154 // the thread exits. Some consumers are abusing the API. Make them stop. | |
| 155 // | |
| 156 PlatformThread::Join(thread_); | |
| 157 | |
| 158 // The thread should NULL message_loop_ on exit. | |
| 159 DCHECK(!message_loop_); | |
| 160 | |
| 161 // The thread no longer needs to be joined. | |
| 162 start_event_.reset(); | |
| 163 | |
| 164 stopping_ = false; | |
| 165 } | |
| 166 | |
| 167 void Thread::StopSoon() { | |
| 168 // We should only be called on the same thread that started us. | |
| 169 | |
| 170 DCHECK_NE(thread_id(), PlatformThread::CurrentId()); | |
| 171 | |
| 172 if (stopping_ || !message_loop_) | |
| 173 return; | |
| 174 | |
| 175 stopping_ = true; | |
| 176 task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); | |
| 177 } | |
| 178 | |
| 179 PlatformThreadId Thread::thread_id() const { | |
| 180 AutoLock lock(thread_lock_); | |
| 181 return thread_.id(); | |
| 182 } | |
| 183 | |
| 184 bool Thread::IsRunning() const { | |
| 185 // 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 | |
| 187 // true. (Note that stopping_ is touched only on the same thread that | |
| 188 // starts / started the new thread so we need no locking here.) | |
| 189 if (message_loop_ && !stopping_) | |
| 190 return true; | |
| 191 // Otherwise check the running_ flag, which is set to true by the new thread | |
| 192 // only while it is inside Run(). | |
| 193 AutoLock lock(running_lock_); | |
| 194 return running_; | |
| 195 } | |
| 196 | |
| 197 void Thread::Run(MessageLoop* message_loop) { | |
| 198 message_loop->Run(); | |
| 199 } | |
| 200 | |
| 201 void Thread::SetThreadWasQuitProperly(bool flag) { | |
| 202 lazy_tls_bool.Pointer()->Set(flag); | |
| 203 } | |
| 204 | |
| 205 bool Thread::GetThreadWasQuitProperly() { | |
| 206 bool quit_properly = true; | |
| 207 #ifndef NDEBUG | |
| 208 quit_properly = lazy_tls_bool.Pointer()->Get(); | |
| 209 #endif | |
| 210 return quit_properly; | |
| 211 } | |
| 212 | |
| 213 void Thread::ThreadMain() { | |
| 214 // Complete the initialization of our Thread object. | |
| 215 PlatformThread::SetName(name_.c_str()); | |
| 216 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. | |
| 217 | |
| 218 // Lazily initialize the message_loop so that it can run on this thread. | |
| 219 DCHECK(message_loop_); | |
| 220 scoped_ptr<MessageLoop> message_loop(message_loop_); | |
| 221 message_loop_->BindToCurrentThread(); | |
| 222 message_loop_->set_thread_name(name_); | |
| 223 message_loop_->SetTimerSlack(message_loop_timer_slack_); | |
| 224 | |
| 225 #if defined(OS_WIN) | |
| 226 scoped_ptr<win::ScopedCOMInitializer> com_initializer; | |
| 227 if (com_status_ != NONE) { | |
| 228 com_initializer.reset((com_status_ == STA) ? | |
| 229 new win::ScopedCOMInitializer() : | |
| 230 new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA)); | |
| 231 } | |
| 232 #endif | |
| 233 | |
| 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. | |
| 239 Init(); | |
| 240 | |
| 241 { | |
| 242 AutoLock lock(running_lock_); | |
| 243 running_ = true; | |
| 244 } | |
| 245 | |
| 246 start_event_->Signal(); | |
| 247 | |
| 248 Run(message_loop_); | |
| 249 | |
| 250 { | |
| 251 AutoLock lock(running_lock_); | |
| 252 running_ = false; | |
| 253 } | |
| 254 | |
| 255 // Let the thread do extra cleanup. | |
| 256 CleanUp(); | |
| 257 | |
| 258 #if defined(OS_WIN) | |
| 259 com_initializer.reset(); | |
| 260 #endif | |
| 261 | |
| 262 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. | |
| 263 DCHECK(GetThreadWasQuitProperly()); | |
| 264 | |
| 265 // We can't receive messages anymore. | |
| 266 // (The message loop is destructed at the end of this block) | |
| 267 message_loop_ = NULL; | |
| 268 } | |
| 269 | |
| 270 } // namespace base | |
| OLD | NEW |