1 // Copyright (c) 2011 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/memory/ref_counted.h" 6 7 #include "base/threading/thread_collision_warner.h" 8 9 namespace base { 10 namespace { 11 12 #if DCHECK_IS_ON() 13 std::atomic_int g_cross_thread_ref_count_access_allow_count(0); 14 #endif 15 16 } // namespace 17 18 namespace subtle { 19 HasOneRef() const20bool RefCountedThreadSafeBase::HasOneRef() const { 21 return ref_count_.IsOne(); 22 } 23 24 #if DCHECK_IS_ON() ~RefCountedThreadSafeBase()25RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { 26 DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " 27 "calling Release()"; 28 } 29 #endif 30 31 #if defined(ARCH_CPU_64_BIT) AddRefImpl() const32void RefCountedBase::AddRefImpl() const { 33 // Check if |ref_count_| overflow only on 64 bit archs since the number of 34 // objects may exceed 2^32. 35 // To avoid the binary size bloat, use non-inline function here. 36 CHECK(++ref_count_ > 0); 37 } 38 #endif 39 40 #if !defined(ARCH_CPU_X86_FAMILY) Release() const41bool RefCountedThreadSafeBase::Release() const { 42 return ReleaseImpl(); 43 } AddRef() const44void RefCountedThreadSafeBase::AddRef() const { 45 AddRefImpl(); 46 } 47 #endif 48 49 #if DCHECK_IS_ON() CalledOnValidSequence() const50bool RefCountedBase::CalledOnValidSequence() const { 51 return sequence_checker_.CalledOnValidSequence() || 52 g_cross_thread_ref_count_access_allow_count.load() != 0; 53 } 54 #endif 55 56 } // namespace subtle 57 58 #if DCHECK_IS_ON() ScopedAllowCrossThreadRefCountAccess()59ScopedAllowCrossThreadRefCountAccess::ScopedAllowCrossThreadRefCountAccess() { 60 ++g_cross_thread_ref_count_access_allow_count; 61 } 62 ~ScopedAllowCrossThreadRefCountAccess()63ScopedAllowCrossThreadRefCountAccess::~ScopedAllowCrossThreadRefCountAccess() { 64 --g_cross_thread_ref_count_access_allow_count; 65 } 66 #endif 67 68 } // namespace base 69