| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
| 6 |
| 6 #include "base/threading/thread_collision_warner.h" | 7 #include "base/threading/thread_collision_warner.h" |
| 7 | 8 |
| 8 namespace base { | 9 namespace base { |
| 9 | 10 |
| 10 namespace subtle { | 11 namespace subtle { |
| 11 | 12 |
| 12 bool RefCountedThreadSafeBase::HasOneRef() const { | 13 bool RefCountedThreadSafeBase::HasOneRef() const { |
| 13 return AtomicRefCountIsOne(&ref_count_); | 14 return AtomicRefCountIsOne(&ref_count_); |
| 14 } | 15 } |
| 15 | 16 |
| 16 RefCountedThreadSafeBase::RefCountedThreadSafeBase() = default; | |
| 17 | |
| 18 RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { | 17 RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { |
| 19 #if DCHECK_IS_ON() | 18 #if DCHECK_IS_ON() |
| 20 DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " | 19 DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " |
| 21 "calling Release()"; | 20 "calling Release()"; |
| 22 #endif | 21 #endif |
| 23 } | 22 } |
| 24 | 23 |
| 25 void RefCountedThreadSafeBase::AddRef() const { | 24 void RefCountedThreadSafeBase::AddRef() const { |
| 26 #if DCHECK_IS_ON() | 25 #if DCHECK_IS_ON() |
| 27 DCHECK(!in_dtor_); | 26 DCHECK(!in_dtor_); |
| 27 DCHECK(!needs_adopt_ref_) |
| 28 << "This RefCounted object is created with non-zero reference count." |
| 29 << " The first reference to such a object has to be made by AdoptRef or" |
| 30 << " MakeShared."; |
| 28 #endif | 31 #endif |
| 29 AtomicRefCountInc(&ref_count_); | 32 AtomicRefCountInc(&ref_count_); |
| 30 } | 33 } |
| 31 | 34 |
| 32 bool RefCountedThreadSafeBase::Release() const { | 35 bool RefCountedThreadSafeBase::Release() const { |
| 33 #if DCHECK_IS_ON() | 36 #if DCHECK_IS_ON() |
| 34 DCHECK(!in_dtor_); | 37 DCHECK(!in_dtor_); |
| 35 DCHECK(!AtomicRefCountIsZero(&ref_count_)); | 38 DCHECK(!AtomicRefCountIsZero(&ref_count_)); |
| 36 #endif | 39 #endif |
| 37 if (!AtomicRefCountDec(&ref_count_)) { | 40 if (!AtomicRefCountDec(&ref_count_)) { |
| 38 #if DCHECK_IS_ON() | 41 #if DCHECK_IS_ON() |
| 39 in_dtor_ = true; | 42 in_dtor_ = true; |
| 40 #endif | 43 #endif |
| 41 return true; | 44 return true; |
| 42 } | 45 } |
| 43 return false; | 46 return false; |
| 44 } | 47 } |
| 45 | 48 |
| 46 } // namespace subtle | 49 } // namespace subtle |
| 47 | 50 |
| 48 } // namespace base | 51 } // namespace base |
| OLD | NEW |