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 #include "base/threading/thread_collision_warner.h" 7 8 namespace base { 9 10 namespace subtle { 11 HasOneRef() const12bool RefCountedThreadSafeBase::HasOneRef() const { 13 return AtomicRefCountIsOne( 14 &const_cast<RefCountedThreadSafeBase*>(this)->ref_count_); 15 } 16 RefCountedThreadSafeBase()17RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) { 18 #ifndef NDEBUG 19 in_dtor_ = false; 20 #endif 21 } 22 ~RefCountedThreadSafeBase()23RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { 24 #ifndef NDEBUG 25 DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " 26 "calling Release()"; 27 #endif 28 } 29 AddRef() const30void RefCountedThreadSafeBase::AddRef() const { 31 #ifndef NDEBUG 32 DCHECK(!in_dtor_); 33 #endif 34 AtomicRefCountInc(&ref_count_); 35 } 36 Release() const37bool RefCountedThreadSafeBase::Release() const { 38 #ifndef NDEBUG 39 DCHECK(!in_dtor_); 40 DCHECK(!AtomicRefCountIsZero(&ref_count_)); 41 #endif 42 if (!AtomicRefCountDec(&ref_count_)) { 43 #ifndef NDEBUG 44 in_dtor_ = true; 45 #endif 46 return true; 47 } 48 return false; 49 } 50 51 } // namespace subtle 52 53 } // namespace base 54