ipc/chromium/src/base/thread_collision_warner.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial