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 id_ = kInvalidThreadId; |
102 start_event_.reset(new WaitableEvent(true, false)); | |
101 | 103 |
102 // Hold the thread_lock_ while starting a new thread, so that we can make sure | 104 if (!PlatformThread::CreateWithPriority(options.stack_size, this, &thread_, |
103 // that thread_ is populated before the newly created thread accesses it. | 105 options.priority)) { |
104 { | 106 DLOG(ERROR) << "failed to create thread"; |
105 AutoLock lock(thread_lock_); | 107 message_loop_ = nullptr; |
106 bool created; | 108 start_event_.reset(); |
107 if (options.priority == ThreadPriority::NORMAL) { | 109 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 } | 110 } |
120 | 111 |
121 // The ownership of message_loop is managemed by the newly created thread | 112 // The ownership of message_loop is managemed by the newly created thread |
122 // within the ThreadMain. | 113 // within the ThreadMain. |
123 ignore_result(message_loop.release()); | 114 ignore_result(message_loop.release()); |
124 | 115 |
125 DCHECK(message_loop_); | 116 DCHECK(message_loop_); |
126 return true; | 117 return true; |
127 } | 118 } |
128 | 119 |
129 bool Thread::StartAndWaitForTesting() { | 120 bool Thread::StartAndWaitForTesting() { |
130 bool result = Start(); | 121 bool result = Start(); |
131 if (!result) | 122 if (!result) |
132 return false; | 123 return false; |
133 WaitUntilThreadStarted(); | 124 WaitUntilThreadStarted(); |
134 return true; | 125 return true; |
135 } | 126 } |
136 | 127 |
137 bool Thread::WaitUntilThreadStarted() { | 128 bool Thread::WaitUntilThreadStarted() const { |
138 if (!start_event_) | 129 if (!start_event_) |
139 return false; | 130 return false; |
140 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 131 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
141 start_event_->Wait(); | 132 start_event_->Wait(); |
142 return true; | 133 return true; |
143 } | 134 } |
144 | 135 |
145 void Thread::Stop() { | 136 void Thread::Stop() { |
146 if (!start_event_) | 137 if (!start_event_) |
147 return; | 138 return; |
(...skipping 10 matching lines...) Expand all Loading... | |
158 // The thread should NULL message_loop_ on exit. | 149 // The thread should NULL message_loop_ on exit. |
159 DCHECK(!message_loop_); | 150 DCHECK(!message_loop_); |
160 | 151 |
161 // The thread no longer needs to be joined. | 152 // The thread no longer needs to be joined. |
162 start_event_.reset(); | 153 start_event_.reset(); |
163 | 154 |
164 stopping_ = false; | 155 stopping_ = false; |
165 } | 156 } |
166 | 157 |
167 void Thread::StopSoon() { | 158 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_) | 159 if (stopping_ || !message_loop_) |
173 return; | 160 return; |
174 | 161 |
162 // We should only be called on the same thread that started us. | |
163 DCHECK_NE(thread_id(), PlatformThread::CurrentId()); | |
gab
2015/07/16 17:19:33
Why move this? DCHECKs enforcing the method's cont
Takashi Toyoshima
2015/07/16 19:51:11
Because without this fix, this assertion fires in
| |
164 | |
175 stopping_ = true; | 165 stopping_ = true; |
176 task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); | 166 task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); |
177 } | 167 } |
178 | 168 |
179 PlatformThreadId Thread::thread_id() const { | 169 PlatformThreadId Thread::thread_id() const { |
180 AutoLock lock(thread_lock_); | 170 WaitUntilThreadStarted(); |
181 return thread_.id(); | 171 // Could fire if the method is called before starting the thread. |
172 DCHECK_NE(kInvalidThreadId, id_); | |
173 return id_; | |
182 } | 174 } |
183 | 175 |
184 bool Thread::IsRunning() const { | 176 bool Thread::IsRunning() const { |
185 // If the thread's already started (i.e. message_loop_ is non-null) and | 177 // 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 | 178 // 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 | 179 // true. (Note that stopping_ is touched only on the same thread that |
188 // starts / started the new thread so we need no locking here.) | 180 // starts / started the new thread so we need no locking here.) |
189 if (message_loop_ && !stopping_) | 181 if (message_loop_ && !stopping_) |
190 return true; | 182 return true; |
191 // Otherwise check the running_ flag, which is set to true by the new thread | 183 // Otherwise check the running_ flag, which is set to true by the new thread |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
224 | 216 |
225 #if defined(OS_WIN) | 217 #if defined(OS_WIN) |
226 scoped_ptr<win::ScopedCOMInitializer> com_initializer; | 218 scoped_ptr<win::ScopedCOMInitializer> com_initializer; |
227 if (com_status_ != NONE) { | 219 if (com_status_ != NONE) { |
228 com_initializer.reset((com_status_ == STA) ? | 220 com_initializer.reset((com_status_ == STA) ? |
229 new win::ScopedCOMInitializer() : | 221 new win::ScopedCOMInitializer() : |
230 new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA)); | 222 new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA)); |
231 } | 223 } |
232 #endif | 224 #endif |
233 | 225 |
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. | 226 // Let the thread do extra initialization. |
239 Init(); | 227 Init(); |
240 | 228 |
241 { | 229 { |
242 AutoLock lock(running_lock_); | 230 AutoLock lock(running_lock_); |
243 running_ = true; | 231 running_ = true; |
244 } | 232 } |
245 | 233 |
234 id_ = PlatformThread::CurrentId(); | |
gab
2015/07/16 17:19:33
You still need a lock to protect this even if all
Takashi Toyoshima
2015/07/16 19:51:10
Yeah, I understand memory consistency. As I commen
| |
235 DCHECK_NE(kInvalidThreadId, id_); | |
236 | |
246 start_event_->Signal(); | 237 start_event_->Signal(); |
247 | 238 |
248 Run(message_loop_); | 239 Run(message_loop_); |
249 | 240 |
250 { | 241 { |
251 AutoLock lock(running_lock_); | 242 AutoLock lock(running_lock_); |
252 running_ = false; | 243 running_ = false; |
253 } | 244 } |
254 | 245 |
255 // Let the thread do extra cleanup. | 246 // Let the thread do extra cleanup. |
256 CleanUp(); | 247 CleanUp(); |
257 | 248 |
258 #if defined(OS_WIN) | 249 #if defined(OS_WIN) |
259 com_initializer.reset(); | 250 com_initializer.reset(); |
260 #endif | 251 #endif |
261 | 252 |
262 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. | 253 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. |
263 DCHECK(GetThreadWasQuitProperly()); | 254 DCHECK(GetThreadWasQuitProperly()); |
264 | 255 |
265 // We can't receive messages anymore. | 256 // We can't receive messages anymore. |
266 // (The message loop is destructed at the end of this block) | 257 // (The message loop is destructed at the end of this block) |
267 message_loop_ = NULL; | 258 message_loop_ = nullptr; |
268 } | 259 } |
269 | 260 |
270 } // namespace base | 261 } // namespace base |
OLD | NEW |