ipc/chromium/src/base/ref_counted.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 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/ref_counted.h"
michael@0 6
michael@0 7 #include "base/logging.h"
michael@0 8 #include "base/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() : ref_count_(0) {
michael@0 15 #ifndef NDEBUG
michael@0 16 in_dtor_ = false;
michael@0 17 #endif
michael@0 18 }
michael@0 19
michael@0 20 RefCountedBase::~RefCountedBase() {
michael@0 21 #ifndef NDEBUG
michael@0 22 DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()";
michael@0 23 #endif
michael@0 24 }
michael@0 25
michael@0 26 void RefCountedBase::AddRef() {
michael@0 27 // TODO(maruel): Add back once it doesn't assert 500 times/sec.
michael@0 28 // Current thread books the critical section "AddRelease" without release it.
michael@0 29 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_);
michael@0 30 #ifndef NDEBUG
michael@0 31 DCHECK(!in_dtor_);
michael@0 32 #endif
michael@0 33 ++ref_count_;
michael@0 34 }
michael@0 35
michael@0 36 bool RefCountedBase::Release() {
michael@0 37 // TODO(maruel): Add back once it doesn't assert 500 times/sec.
michael@0 38 // Current thread books the critical section "AddRelease" without release it.
michael@0 39 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_);
michael@0 40 #ifndef NDEBUG
michael@0 41 DCHECK(!in_dtor_);
michael@0 42 #endif
michael@0 43 if (--ref_count_ == 0) {
michael@0 44 #ifndef NDEBUG
michael@0 45 in_dtor_ = true;
michael@0 46 #endif
michael@0 47 return true;
michael@0 48 }
michael@0 49 return false;
michael@0 50 }
michael@0 51
michael@0 52 RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) {
michael@0 53 #ifndef NDEBUG
michael@0 54 in_dtor_ = false;
michael@0 55 #endif
michael@0 56 }
michael@0 57
michael@0 58 RefCountedThreadSafeBase::~RefCountedThreadSafeBase() {
michael@0 59 #ifndef NDEBUG
michael@0 60 DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without "
michael@0 61 "calling Release()";
michael@0 62 #endif
michael@0 63 }
michael@0 64
michael@0 65 void RefCountedThreadSafeBase::AddRef() {
michael@0 66 #ifndef NDEBUG
michael@0 67 DCHECK(!in_dtor_);
michael@0 68 #endif
michael@0 69 AtomicRefCountInc(&ref_count_);
michael@0 70 }
michael@0 71
michael@0 72 bool RefCountedThreadSafeBase::Release() {
michael@0 73 #ifndef NDEBUG
michael@0 74 DCHECK(!in_dtor_);
michael@0 75 DCHECK(!AtomicRefCountIsZero(&ref_count_));
michael@0 76 #endif
michael@0 77 if (!AtomicRefCountDec(&ref_count_)) {
michael@0 78 #ifndef NDEBUG
michael@0 79 in_dtor_ = true;
michael@0 80 #endif
michael@0 81 return true;
michael@0 82 }
michael@0 83 return false;
michael@0 84 }
michael@0 85
michael@0 86 } // namespace subtle
michael@0 87
michael@0 88 } // namespace base

mercurial