| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 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/thread_collision_warner.h" | 5 #include "base/thread_collision_warner.h" |
| 6 | 6 |
| 7 #include "base/atomicops.h" | 7 #include "base/atomicops.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 | 9 |
| 10 namespace base { | 10 namespace base { |
| 11 | 11 |
| 12 void DCheckAsserter::warn() { | 12 void DCheckAsserter::warn() { |
| 13 NOTREACHED() << "Thread Collision"; | 13 NOTREACHED() << "Thread Collision"; |
| 14 } | 14 } |
| 15 | 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 |
| 16 void ThreadCollisionWarner::EnterSelf() { | 27 void ThreadCollisionWarner::EnterSelf() { |
| 17 // If the active thread is 0 then I'll write the current thread ID | 28 // If the active thread is 0 then I'll write the current thread ID |
| 18 // if two or more threads arrive here only one will succeed to | 29 // if two or more threads arrive here only one will succeed to |
| 19 // write on valid_thread_id_ the current thread ID. | 30 // write on valid_thread_id_ the current thread ID. |
| 20 const int current_thread_id = PlatformThread::CurrentId(); | 31 subtle::Atomic32 current_thread_id = CurrentThread(); |
| 21 | 32 |
| 22 int previous_value = subtle::NoBarrier_CompareAndSwap(&valid_thread_id_, | 33 int previous_value = subtle::NoBarrier_CompareAndSwap(&valid_thread_id_, |
| 23 0, | 34 0, |
| 24 current_thread_id); | 35 current_thread_id); |
| 25 if (previous_value != 0 && previous_value != current_thread_id) { | 36 if (previous_value != 0 && previous_value != current_thread_id) { |
| 26 // gotcha! a thread is trying to use the same class and that is | 37 // gotcha! a thread is trying to use the same class and that is |
| 27 // not current thread. | 38 // not current thread. |
| 28 asserter_->warn(); | 39 asserter_->warn(); |
| 29 } | 40 } |
| 30 | 41 |
| 31 subtle::NoBarrier_AtomicIncrement(&counter_, 1); | 42 subtle::NoBarrier_AtomicIncrement(&counter_, 1); |
| 32 } | 43 } |
| 33 | 44 |
| 34 void ThreadCollisionWarner::Enter() { | 45 void ThreadCollisionWarner::Enter() { |
| 35 const int current_thread_id = PlatformThread::CurrentId(); | 46 subtle::Atomic32 current_thread_id = CurrentThread(); |
| 36 | 47 |
| 37 if (subtle::NoBarrier_CompareAndSwap(&valid_thread_id_, | 48 if (subtle::NoBarrier_CompareAndSwap(&valid_thread_id_, |
| 38 0, | 49 0, |
| 39 current_thread_id) != 0) { | 50 current_thread_id) != 0) { |
| 40 // gotcha! another thread is trying to use the same class. | 51 // gotcha! another thread is trying to use the same class. |
| 41 asserter_->warn(); | 52 asserter_->warn(); |
| 42 } | 53 } |
| 43 | 54 |
| 44 subtle::NoBarrier_AtomicIncrement(&counter_, 1); | 55 subtle::NoBarrier_AtomicIncrement(&counter_, 1); |
| 45 } | 56 } |
| 46 | 57 |
| 47 void ThreadCollisionWarner::Leave() { | 58 void ThreadCollisionWarner::Leave() { |
| 48 if (subtle::Barrier_AtomicIncrement(&counter_, -1) == 0) { | 59 if (subtle::Barrier_AtomicIncrement(&counter_, -1) == 0) { |
| 49 subtle::NoBarrier_Store(&valid_thread_id_, 0); | 60 subtle::NoBarrier_Store(&valid_thread_id_, 0); |
| 50 } | 61 } |
| 51 } | 62 } |
| 52 | 63 |
| 53 } // namespace base | 64 } // namespace base |
| OLD | NEW |