ipc/chromium/src/base/ref_counted.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/ref_counted.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,88 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include "base/ref_counted.h"
     1.9 +
    1.10 +#include "base/logging.h"
    1.11 +#include "base/thread_collision_warner.h"
    1.12 +
    1.13 +namespace base {
    1.14 +
    1.15 +namespace subtle {
    1.16 +
    1.17 +RefCountedBase::RefCountedBase() : ref_count_(0) {
    1.18 +#ifndef NDEBUG
    1.19 +  in_dtor_ = false;
    1.20 +#endif
    1.21 +}
    1.22 +
    1.23 +RefCountedBase::~RefCountedBase() {
    1.24 +#ifndef NDEBUG
    1.25 +  DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()";
    1.26 +#endif
    1.27 +}
    1.28 +
    1.29 +void RefCountedBase::AddRef() {
    1.30 +  // TODO(maruel): Add back once it doesn't assert 500 times/sec.
    1.31 +  // Current thread books the critical section "AddRelease" without release it.
    1.32 +  // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_);
    1.33 +#ifndef NDEBUG
    1.34 +  DCHECK(!in_dtor_);
    1.35 +#endif
    1.36 +  ++ref_count_;
    1.37 +}
    1.38 +
    1.39 +bool RefCountedBase::Release() {
    1.40 +  // TODO(maruel): Add back once it doesn't assert 500 times/sec.
    1.41 +  // Current thread books the critical section "AddRelease" without release it.
    1.42 +  // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_);
    1.43 +#ifndef NDEBUG
    1.44 +  DCHECK(!in_dtor_);
    1.45 +#endif
    1.46 +  if (--ref_count_ == 0) {
    1.47 +#ifndef NDEBUG
    1.48 +    in_dtor_ = true;
    1.49 +#endif
    1.50 +    return true;
    1.51 +  }
    1.52 +  return false;
    1.53 +}
    1.54 +
    1.55 +RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) {
    1.56 +#ifndef NDEBUG
    1.57 +  in_dtor_ = false;
    1.58 +#endif
    1.59 +}
    1.60 +
    1.61 +RefCountedThreadSafeBase::~RefCountedThreadSafeBase() {
    1.62 +#ifndef NDEBUG
    1.63 +  DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without "
    1.64 +                      "calling Release()";
    1.65 +#endif
    1.66 +}
    1.67 +
    1.68 +void RefCountedThreadSafeBase::AddRef() {
    1.69 +#ifndef NDEBUG
    1.70 +  DCHECK(!in_dtor_);
    1.71 +#endif
    1.72 +  AtomicRefCountInc(&ref_count_);
    1.73 +}
    1.74 +
    1.75 +bool RefCountedThreadSafeBase::Release() {
    1.76 +#ifndef NDEBUG
    1.77 +  DCHECK(!in_dtor_);
    1.78 +  DCHECK(!AtomicRefCountIsZero(&ref_count_));
    1.79 +#endif
    1.80 +  if (!AtomicRefCountDec(&ref_count_)) {
    1.81 +#ifndef NDEBUG
    1.82 +    in_dtor_ = true;
    1.83 +#endif
    1.84 +    return true;
    1.85 +  }
    1.86 +  return false;
    1.87 +}
    1.88 +
    1.89 +}  // namespace subtle
    1.90 +
    1.91 +}  // namespace base

mercurial