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 15 matching lines...) Expand all Loading... | |
90 SetThreadWasQuitProperly(false); | 91 SetThreadWasQuitProperly(false); |
91 | 92 |
92 MessageLoop::Type type = options.message_loop_type; | 93 MessageLoop::Type type = options.message_loop_type; |
93 if (!options.message_pump_factory.is_null()) | 94 if (!options.message_pump_factory.is_null()) |
94 type = MessageLoop::TYPE_CUSTOM; | 95 type = MessageLoop::TYPE_CUSTOM; |
95 | 96 |
96 message_loop_timer_slack_ = options.timer_slack; | 97 message_loop_timer_slack_ = options.timer_slack; |
97 scoped_ptr<MessageLoop> message_loop = MessageLoop::CreateUnbound( | 98 scoped_ptr<MessageLoop> message_loop = MessageLoop::CreateUnbound( |
98 type, options.message_pump_factory); | 99 type, options.message_pump_factory); |
99 message_loop_ = message_loop.get(); | 100 message_loop_ = message_loop.get(); |
100 start_event_.reset(new WaitableEvent(false, false)); | 101 start_event_.reset(new WaitableEvent(true, false)); |
101 | 102 |
102 // Hold the thread_lock_ while starting a new thread, so that we can make sure | 103 if (!PlatformThread::CreateWithPriority(options.stack_size, this, |
103 // that thread_ is populated before the newly created thread accesses it. | 104 &thread_, options.priority)) { |
104 { | 105 DLOG(ERROR) << "failed to create thread"; |
105 AutoLock lock(thread_lock_); | 106 message_loop_ = nullptr; |
106 bool created; | 107 start_event_.reset(); |
107 if (options.priority == ThreadPriority::NORMAL) { | 108 return false; |
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 } | 109 } |
120 | 110 |
121 // The ownership of message_loop is managemed by the newly created thread | 111 // The ownership of message_loop is managemed by the newly created thread |
122 // within the ThreadMain. | 112 // within the ThreadMain. |
123 ignore_result(message_loop.release()); | 113 ignore_result(message_loop.release()); |
124 | 114 |
125 DCHECK(message_loop_); | 115 DCHECK(message_loop_); |
126 return true; | 116 return true; |
127 } | 117 } |
128 | 118 |
(...skipping 29 matching lines...) Expand all Loading... | |
158 // The thread should NULL message_loop_ on exit. | 148 // The thread should NULL message_loop_ on exit. |
159 DCHECK(!message_loop_); | 149 DCHECK(!message_loop_); |
160 | 150 |
161 // The thread no longer needs to be joined. | 151 // The thread no longer needs to be joined. |
162 start_event_.reset(); | 152 start_event_.reset(); |
163 | 153 |
164 stopping_ = false; | 154 stopping_ = false; |
165 } | 155 } |
166 | 156 |
167 void Thread::StopSoon() { | 157 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_) | 158 if (stopping_ || !message_loop_) |
173 return; | 159 return; |
174 | 160 |
161 // Wait for the thread starts running to call thread_id(). | |
162 WaitUntilThreadStarted(); | |
163 | |
164 // We should only be called on the same thread that started us. | |
165 DCHECK_NE(thread_id(), PlatformThread::CurrentId()); | |
166 | |
175 stopping_ = true; | 167 stopping_ = true; |
176 task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); | 168 task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); |
177 } | 169 } |
178 | 170 |
179 PlatformThreadId Thread::thread_id() const { | 171 PlatformThreadId Thread::thread_id() const { |
180 AutoLock lock(thread_lock_); | 172 AutoLock lock(lock_); |
181 return thread_.id(); | 173 DCHECK_NE(kInvalidThreadId, id_); |
kinuko
2015/07/16 00:50:36
Assume that we could fix all the failing tests, ho
Takashi Toyoshima
2015/07/16 05:07:31
Thanks. I'm doing trial and error here https://cod
| |
174 return id_; | |
182 } | 175 } |
183 | 176 |
184 bool Thread::IsRunning() const { | 177 bool Thread::IsRunning() const { |
185 // If the thread's already started (i.e. message_loop_ is non-null) and | 178 // 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 | 179 // 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 | 180 // true. (Note that stopping_ is touched only on the same thread that |
188 // starts / started the new thread so we need no locking here.) | 181 // starts / started the new thread so we need no locking here.) |
189 if (message_loop_ && !stopping_) | 182 if (message_loop_ && !stopping_) |
190 return true; | 183 return true; |
191 // Otherwise check the running_ flag, which is set to true by the new thread | 184 // Otherwise check the running_ flag, which is set to true by the new thread |
192 // only while it is inside Run(). | 185 // only while it is inside Run(). |
193 AutoLock lock(running_lock_); | 186 AutoLock lock(lock_); |
194 return running_; | 187 return running_; |
195 } | 188 } |
196 | 189 |
197 void Thread::Run(MessageLoop* message_loop) { | 190 void Thread::Run(MessageLoop* message_loop) { |
198 message_loop->Run(); | 191 message_loop->Run(); |
199 } | 192 } |
200 | 193 |
201 void Thread::SetThreadWasQuitProperly(bool flag) { | 194 void Thread::SetThreadWasQuitProperly(bool flag) { |
202 lazy_tls_bool.Pointer()->Set(flag); | 195 lazy_tls_bool.Pointer()->Set(flag); |
203 } | 196 } |
204 | 197 |
205 bool Thread::GetThreadWasQuitProperly() { | 198 bool Thread::GetThreadWasQuitProperly() { |
206 bool quit_properly = true; | 199 bool quit_properly = true; |
207 #ifndef NDEBUG | 200 #ifndef NDEBUG |
208 quit_properly = lazy_tls_bool.Pointer()->Get(); | 201 quit_properly = lazy_tls_bool.Pointer()->Get(); |
209 #endif | 202 #endif |
210 return quit_properly; | 203 return quit_properly; |
211 } | 204 } |
212 | 205 |
213 void Thread::ThreadMain() { | 206 void Thread::ThreadMain() { |
214 // Complete the initialization of our Thread object. | 207 // Complete the initialization of our Thread object. |
208 { | |
209 AutoLock lock(lock_); | |
210 id_ = PlatformThread::CurrentId(); | |
211 } | |
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(lock_); |
243 running_ = true; | 236 running_ = true; |
244 } | 237 } |
245 | 238 |
246 start_event_->Signal(); | 239 start_event_->Signal(); |
247 | 240 |
248 Run(message_loop_); | 241 Run(message_loop_); |
249 | 242 |
250 { | 243 { |
251 AutoLock lock(running_lock_); | 244 AutoLock lock(lock_); |
252 running_ = false; | 245 running_ = false; |
253 } | 246 } |
254 | 247 |
255 // Let the thread do extra cleanup. | 248 // Let the thread do extra cleanup. |
256 CleanUp(); | 249 CleanUp(); |
257 | 250 |
258 #if defined(OS_WIN) | 251 #if defined(OS_WIN) |
259 com_initializer.reset(); | 252 com_initializer.reset(); |
260 #endif | 253 #endif |
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 |