Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(209)

Side by Side Diff: base/threading/thread.cc

Issue 2303943003: Ensure that |Thread::id_| is only accessed when an happens-after relationship has been established … (Closed)
Patch Set: Only AssertWaitAllowed() in WaitableEvent::TimedWait if time is non-zero Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/threading/thread.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/location.h" 10 #include "base/location.h"
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 if (message_loop_ && !stopping_) 216 if (message_loop_ && !stopping_)
217 return true; 217 return true;
218 // Otherwise check the |running_| flag, which is set to true by the new thread 218 // Otherwise check the |running_| flag, which is set to true by the new thread
219 // only while it is inside Run(). 219 // only while it is inside Run().
220 AutoLock lock(running_lock_); 220 AutoLock lock(running_lock_);
221 return running_; 221 return running_;
222 } 222 }
223 223
224 void Thread::Run(RunLoop* run_loop) { 224 void Thread::Run(RunLoop* run_loop) {
225 // Overridable protected method to be called from our |thread_| only. 225 // Overridable protected method to be called from our |thread_| only.
226 DCHECK(id_event_.IsSignaled());
226 DCHECK_EQ(id_, PlatformThread::CurrentId()); 227 DCHECK_EQ(id_, PlatformThread::CurrentId());
227 228
228 run_loop->Run(); 229 run_loop->Run();
229 } 230 }
230 231
231 // static 232 // static
232 void Thread::SetThreadWasQuitProperly(bool flag) { 233 void Thread::SetThreadWasQuitProperly(bool flag) {
233 lazy_tls_bool.Pointer()->Set(flag); 234 lazy_tls_bool.Pointer()->Set(flag);
234 } 235 }
235 236
(...skipping 20 matching lines...) Expand all
256 DCHECK(!IsRunning()); 257 DCHECK(!IsRunning());
257 message_loop_ = message_loop; 258 message_loop_ = message_loop;
258 DCHECK(IsRunning()); 259 DCHECK(IsRunning());
259 260
260 using_external_message_loop_ = true; 261 using_external_message_loop_ = true;
261 } 262 }
262 263
263 void Thread::ThreadMain() { 264 void Thread::ThreadMain() {
264 // First, make GetThreadId() available to avoid deadlocks. It could be called 265 // First, make GetThreadId() available to avoid deadlocks. It could be called
265 // any place in the following thread initialization code. 266 // any place in the following thread initialization code.
267 DCHECK(!id_event_.IsSignaled());
268 // Note: this read of |id_| while |id_event_| isn't signaled is exceptionally
269 // okay because ThreadMain has an happens-after relationship with the other
270 // write in StartWithOptions().
271 DCHECK_EQ(kInvalidThreadId, id_);
266 id_ = PlatformThread::CurrentId(); 272 id_ = PlatformThread::CurrentId();
267 DCHECK_NE(kInvalidThreadId, id_); 273 DCHECK_NE(kInvalidThreadId, id_);
268 id_event_.Signal(); 274 id_event_.Signal();
269 275
270 // Complete the initialization of our Thread object. 276 // Complete the initialization of our Thread object.
271 PlatformThread::SetName(name_.c_str()); 277 PlatformThread::SetName(name_.c_str());
272 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. 278 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector.
273 279
274 // Lazily initialize the |message_loop| so that it can run on this thread. 280 // Lazily initialize the |message_loop| so that it can run on this thread.
275 DCHECK(message_loop_); 281 DCHECK(message_loop_);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 run_loop_ = nullptr; 331 run_loop_ = nullptr;
326 } 332 }
327 333
328 void Thread::ThreadQuitHelper() { 334 void Thread::ThreadQuitHelper() {
329 DCHECK(run_loop_); 335 DCHECK(run_loop_);
330 run_loop_->QuitWhenIdle(); 336 run_loop_->QuitWhenIdle();
331 SetThreadWasQuitProperly(true); 337 SetThreadWasQuitProperly(true);
332 } 338 }
333 339
334 } // namespace base 340 } // namespace base
OLDNEW
« no previous file with comments | « base/threading/thread.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698