[CleanUp] Move PowerApiManager to power_api and Rename
[chromium-blink-merge.git] / base / memory / ref_counted.cc
blobf5924d0fe76e44672ca24372f93b40d418f46b62
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.
5 #include "base/memory/ref_counted.h"
6 #include "base/threading/thread_collision_warner.h"
8 namespace base {
10 namespace subtle {
12 bool RefCountedThreadSafeBase::HasOneRef() const {
13 return AtomicRefCountIsOne(
14 &const_cast<RefCountedThreadSafeBase*>(this)->ref_count_);
17 RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) {
18 #ifndef NDEBUG
19 in_dtor_ = false;
20 #endif
23 RefCountedThreadSafeBase::~RefCountedThreadSafeBase() {
24 #ifndef NDEBUG
25 DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without "
26 "calling Release()";
27 #endif
30 void RefCountedThreadSafeBase::AddRef() const {
31 #ifndef NDEBUG
32 DCHECK(!in_dtor_);
33 #endif
34 AtomicRefCountInc(&ref_count_);
37 bool RefCountedThreadSafeBase::Release() const {
38 #ifndef NDEBUG
39 DCHECK(!in_dtor_);
40 DCHECK(!AtomicRefCountIsZero(&ref_count_));
41 #endif
42 if (!AtomicRefCountDec(&ref_count_)) {
43 #ifndef NDEBUG
44 in_dtor_ = true;
45 #endif
46 return true;
48 return false;
51 } // namespace subtle
53 } // namespace base