tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

thread_collision_warner.cc (2159B)


      1 // Copyright 2010 The Chromium Authors
      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/threading/thread_collision_warner.h"
      6 
      7 #include <ostream>
      8 
      9 #include "base/notreached.h"
     10 #include "base/threading/platform_thread.h"
     11 
     12 namespace base {
     13 
     14 void DCheckAsserter::warn() {
     15  NOTREACHED() << "Thread Collision";
     16 }
     17 
     18 static subtle::Atomic32 CurrentThread() {
     19  const PlatformThreadId current_thread_id = PlatformThread::CurrentId();
     20  // We need to get the thread id into an atomic data type. This might be a
     21  // truncating conversion, but any loss-of-information just increases the
     22  // chance of a fault negative, not a false positive.
     23  const subtle::Atomic32 atomic_thread_id =
     24      static_cast<subtle::Atomic32>(current_thread_id);
     25 
     26  return atomic_thread_id;
     27 }
     28 
     29 void ThreadCollisionWarner::EnterSelf() {
     30  // If the active thread is 0 then I'll write the current thread ID
     31  // if two or more threads arrive here only one will succeed to
     32  // write on valid_thread_id_ the current thread ID.
     33  subtle::Atomic32 current_thread_id = CurrentThread();
     34 
     35  int previous_value = subtle::NoBarrier_CompareAndSwap(&valid_thread_id_,
     36                                                        0,
     37                                                        current_thread_id);
     38  if (previous_value != 0 && previous_value != current_thread_id) {
     39    // gotcha! a thread is trying to use the same class and that is
     40    // not current thread.
     41    asserter_->warn();
     42  }
     43 
     44  subtle::NoBarrier_AtomicIncrement(&counter_, 1);
     45 }
     46 
     47 void ThreadCollisionWarner::Enter() {
     48  subtle::Atomic32 current_thread_id = CurrentThread();
     49 
     50  if (subtle::NoBarrier_CompareAndSwap(&valid_thread_id_,
     51                                       0,
     52                                       current_thread_id) != 0) {
     53    // gotcha! another thread is trying to use the same class.
     54    asserter_->warn();
     55  }
     56 
     57  subtle::NoBarrier_AtomicIncrement(&counter_, 1);
     58 }
     59 
     60 void ThreadCollisionWarner::Leave() {
     61  if (subtle::Barrier_AtomicIncrement(&counter_, -1) == 0) {
     62    subtle::NoBarrier_Store(&valid_thread_id_, 0);
     63  }
     64 }
     65 
     66 }  // namespace base