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.

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

mercurial