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

Side by Side Diff: base/thread_collision_warner.cc

Issue 6258: This CL is due the thread I have made on chromium-dev:... (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 12 years, 2 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/atomicops.h"
8 #include "base/logging.h"
9
10 namespace base {
11
12 void DCheckAsserter::warn() { DCHECK(false) << "Thread Collision"; }
13
14 void ThreadCollisionWarner::EnterSelf() {
15 // If the active thread is 0 then I'll write the current thread ID
16 // if two or more threads arrive here only one will success to
17 // write on valid_thread_id_ the current thread ID.
18
19 const int current_thread_id = PlatformThread::CurrentId();
20
21 if (subtle::NoBarrier_CompareAndSwap(&valid_thread_id_,
22 0,
23 current_thread_id) != 0) {
24
25 // Last chance! may be is the thread itself calling from a
26 // critical section another critical section, need to check if
27 // valid_thread_id == current_thread_id.
28
29 if (subtle::NoBarrier_CompareAndSwap(&valid_thread_id_,
30 current_thread_id,
31 valid_thread_id_) !=
32 current_thread_id ) {
33 // gotcha! a thread is trying to use the same class and that is
34 // not current thread.
35 asserter_->warn();
36 }
37 }
38
39 subtle::NoBarrier_AtomicIncrement(&counter_, 1);
40 }
41
42 void ThreadCollisionWarner::Enter() {
43
44 const int current_thread_id = PlatformThread::CurrentId();
45
46 if (subtle::NoBarrier_CompareAndSwap(&valid_thread_id_,
47 0,
48 current_thread_id) != 0) {
49 // gotcha! another thread is trying to use the same class.
50 asserter_->warn();
51 }
52
53 subtle::NoBarrier_AtomicIncrement(&counter_, 1);
54 }
55
56 void ThreadCollisionWarner::Leave() {
57 if (subtle::Barrier_AtomicIncrement(&counter_, -1) == 0 ) {
58 subtle::NoBarrier_Store(&valid_thread_id_, 0);
59 }
60 }
61
62 } // 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