Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [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" |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 6 | |
| 7 | namespace base { |
| 8 | |
| 9 | namespace subtle { |
| 10 | |
| 11 | bool RefCountedThreadSafeBase::HasOneRef() const { |
Vitaly Buka | b1fcfc0 | 2015-09-14 10:24:39 -0700 | [diff] [blame] | 12 | return ref_count_ == 1; |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) { |
| 16 | #ifndef NDEBUG |
| 17 | in_dtor_ = false; |
| 18 | #endif |
| 19 | } |
| 20 | |
| 21 | RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { |
| 22 | #ifndef NDEBUG |
| 23 | DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " |
| 24 | "calling Release()"; |
| 25 | #endif |
| 26 | } |
| 27 | |
| 28 | void RefCountedThreadSafeBase::AddRef() const { |
| 29 | #ifndef NDEBUG |
| 30 | DCHECK(!in_dtor_); |
| 31 | #endif |
Vitaly Buka | b1fcfc0 | 2015-09-14 10:24:39 -0700 | [diff] [blame] | 32 | ++ref_count_; |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | bool RefCountedThreadSafeBase::Release() const { |
| 36 | #ifndef NDEBUG |
| 37 | DCHECK(!in_dtor_); |
Vitaly Buka | b1fcfc0 | 2015-09-14 10:24:39 -0700 | [diff] [blame] | 38 | DCHECK(ref_count_ != 0); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 39 | #endif |
Vitaly Buka | b1fcfc0 | 2015-09-14 10:24:39 -0700 | [diff] [blame] | 40 | if (--ref_count_ == 0) { |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 41 | #ifndef NDEBUG |
| 42 | in_dtor_ = true; |
| 43 | #endif |
| 44 | return true; |
| 45 | } |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | } // namespace subtle |
| 50 | |
| 51 | } // namespace base |