Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 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 | |
| 12 | bool RefCountedThreadSafeBase::HasOneRef() const { |
Luis Hector Chavez | e5b2c6f | 2017-07-26 17:33:47 +0000 | [diff] [blame] | 13 | return AtomicRefCountIsOne( |
| 14 | &const_cast<RefCountedThreadSafeBase*>(this)->ref_count_); |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 15 | } |
| 16 | |
Luis Hector Chavez | e5b2c6f | 2017-07-26 17:33:47 +0000 | [diff] [blame] | 17 | RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) { |
| 18 | #ifndef NDEBUG |
| 19 | in_dtor_ = false; |
| 20 | #endif |
| 21 | } |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 22 | |
| 23 | RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { |
Luis Hector Chavez | e5b2c6f | 2017-07-26 17:33:47 +0000 | [diff] [blame] | 24 | #ifndef NDEBUG |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 25 | DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " |
| 26 | "calling Release()"; |
| 27 | #endif |
| 28 | } |
| 29 | |
| 30 | void RefCountedThreadSafeBase::AddRef() const { |
Luis Hector Chavez | e5b2c6f | 2017-07-26 17:33:47 +0000 | [diff] [blame] | 31 | #ifndef NDEBUG |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 32 | DCHECK(!in_dtor_); |
| 33 | #endif |
| 34 | AtomicRefCountInc(&ref_count_); |
| 35 | } |
| 36 | |
| 37 | bool RefCountedThreadSafeBase::Release() const { |
Luis Hector Chavez | e5b2c6f | 2017-07-26 17:33:47 +0000 | [diff] [blame] | 38 | #ifndef NDEBUG |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 39 | DCHECK(!in_dtor_); |
| 40 | DCHECK(!AtomicRefCountIsZero(&ref_count_)); |
| 41 | #endif |
| 42 | if (!AtomicRefCountDec(&ref_count_)) { |
Luis Hector Chavez | e5b2c6f | 2017-07-26 17:33:47 +0000 | [diff] [blame] | 43 | #ifndef NDEBUG |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 44 | in_dtor_ = true; |
| 45 | #endif |
| 46 | return true; |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | } // namespace subtle |
| 52 | |
| 53 | } // namespace base |