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

Side by Side Diff: base/thread_collision_warner.cc

Issue 6028009: Move base/thread.h to base/threading, fix up callers to use the new location.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 9 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « base/thread_collision_warner.h ('k') | base/thread_collision_warner_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2006-2008 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/thread_collision_warner.h"
6
7 #include "base/logging.h"
8 #include "base/threading/platform_thread.h"
9
10 namespace base {
11
12 void DCheckAsserter::warn() {
13 NOTREACHED() << "Thread Collision";
14 }
15
16 static subtle::Atomic32 CurrentThread() {
17 const PlatformThreadId current_thread_id = PlatformThread::CurrentId();
18 // We need to get the thread id into an atomic data type. This might be a
19 // truncating conversion, but any loss-of-information just increases the
20 // chance of a fault negative, not a false positive.
21 const subtle::Atomic32 atomic_thread_id =
22 static_cast<subtle::Atomic32>(current_thread_id);
23
24 return atomic_thread_id;
25 }
26
27 void ThreadCollisionWarner::EnterSelf() {
28 // If the active thread is 0 then I'll write the current thread ID
29 // if two or more threads arrive here only one will succeed to
30 // write on valid_thread_id_ the current thread ID.
31 subtle::Atomic32 current_thread_id = CurrentThread();
32
33 int previous_value = subtle::NoBarrier_CompareAndSwap(&valid_thread_id_,
34 0,
35 current_thread_id);
36 if (previous_value != 0 && previous_value != current_thread_id) {
37 // gotcha! a thread is trying to use the same class and that is
38 // not current thread.
39 asserter_->warn();
40 }
41
42 subtle::NoBarrier_AtomicIncrement(&counter_, 1);
43 }
44
45 void ThreadCollisionWarner::Enter() {
46 subtle::Atomic32 current_thread_id = CurrentThread();
47
48 if (subtle::NoBarrier_CompareAndSwap(&valid_thread_id_,
49 0,
50 current_thread_id) != 0) {
51 // gotcha! another thread is trying to use the same class.
52 asserter_->warn();
53 }
54
55 subtle::NoBarrier_AtomicIncrement(&counter_, 1);
56 }
57
58 void ThreadCollisionWarner::Leave() {
59 if (subtle::Barrier_AtomicIncrement(&counter_, -1) == 0) {
60 subtle::NoBarrier_Store(&valid_thread_id_, 0);
61 }
62 }
63
64 } // namespace base
OLDNEW
« no previous file with comments | « base/thread_collision_warner.h ('k') | base/thread_collision_warner_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698