Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "base/memory/ref_counted.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "base/logging.h" |
michael@0 | 8 | #include "base/threading/thread_collision_warner.h" |
michael@0 | 9 | |
michael@0 | 10 | namespace base { |
michael@0 | 11 | |
michael@0 | 12 | namespace subtle { |
michael@0 | 13 | |
michael@0 | 14 | RefCountedBase::RefCountedBase() |
michael@0 | 15 | : ref_count_(0) |
michael@0 | 16 | #ifndef NDEBUG |
michael@0 | 17 | , in_dtor_(false) |
michael@0 | 18 | #endif |
michael@0 | 19 | { |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | RefCountedBase::~RefCountedBase() { |
michael@0 | 23 | #ifndef NDEBUG |
michael@0 | 24 | DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()"; |
michael@0 | 25 | #endif |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | void RefCountedBase::AddRef() const { |
michael@0 | 29 | // TODO(maruel): Add back once it doesn't assert 500 times/sec. |
michael@0 | 30 | // Current thread books the critical section "AddRelease" without release it. |
michael@0 | 31 | // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); |
michael@0 | 32 | #ifndef NDEBUG |
michael@0 | 33 | DCHECK(!in_dtor_); |
michael@0 | 34 | #endif |
michael@0 | 35 | ++ref_count_; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | bool RefCountedBase::Release() const { |
michael@0 | 39 | // TODO(maruel): Add back once it doesn't assert 500 times/sec. |
michael@0 | 40 | // Current thread books the critical section "AddRelease" without release it. |
michael@0 | 41 | // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); |
michael@0 | 42 | #ifndef NDEBUG |
michael@0 | 43 | DCHECK(!in_dtor_); |
michael@0 | 44 | #endif |
michael@0 | 45 | if (--ref_count_ == 0) { |
michael@0 | 46 | #ifndef NDEBUG |
michael@0 | 47 | in_dtor_ = true; |
michael@0 | 48 | #endif |
michael@0 | 49 | return true; |
michael@0 | 50 | } |
michael@0 | 51 | return false; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | bool RefCountedThreadSafeBase::HasOneRef() const { |
michael@0 | 55 | return AtomicRefCountIsOne( |
michael@0 | 56 | &const_cast<RefCountedThreadSafeBase*>(this)->ref_count_); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) { |
michael@0 | 60 | #ifndef NDEBUG |
michael@0 | 61 | in_dtor_ = false; |
michael@0 | 62 | #endif |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { |
michael@0 | 66 | #ifndef NDEBUG |
michael@0 | 67 | DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " |
michael@0 | 68 | "calling Release()"; |
michael@0 | 69 | #endif |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | void RefCountedThreadSafeBase::AddRef() const { |
michael@0 | 73 | #ifndef NDEBUG |
michael@0 | 74 | DCHECK(!in_dtor_); |
michael@0 | 75 | #endif |
michael@0 | 76 | AtomicRefCountInc(&ref_count_); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | bool RefCountedThreadSafeBase::Release() const { |
michael@0 | 80 | #ifndef NDEBUG |
michael@0 | 81 | DCHECK(!in_dtor_); |
michael@0 | 82 | DCHECK(!AtomicRefCountIsZero(&ref_count_)); |
michael@0 | 83 | #endif |
michael@0 | 84 | if (!AtomicRefCountDec(&ref_count_)) { |
michael@0 | 85 | #ifndef NDEBUG |
michael@0 | 86 | in_dtor_ = true; |
michael@0 | 87 | #endif |
michael@0 | 88 | return true; |
michael@0 | 89 | } |
michael@0 | 90 | return false; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | } // namespace subtle |
michael@0 | 94 | |
michael@0 | 95 | } // namespace base |