blob: 42e777c2aeb160d848295f8c2b2616b0050200eb [file] [log] [blame]
Vitaly Bukacbed2062015-08-17 12:54:05 -07001// 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.
4
5#include "base/memory/ref_counted.h"
Vitaly Bukacbed2062015-08-17 12:54:05 -07006
7namespace base {
8
9namespace subtle {
10
11bool RefCountedThreadSafeBase::HasOneRef() const {
Vitaly Bukab1fcfc02015-09-14 10:24:39 -070012 return ref_count_ == 1;
Vitaly Bukacbed2062015-08-17 12:54:05 -070013}
14
15RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) {
16#ifndef NDEBUG
17 in_dtor_ = false;
18#endif
19}
20
21RefCountedThreadSafeBase::~RefCountedThreadSafeBase() {
22#ifndef NDEBUG
23 DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without "
24 "calling Release()";
25#endif
26}
27
28void RefCountedThreadSafeBase::AddRef() const {
29#ifndef NDEBUG
30 DCHECK(!in_dtor_);
31#endif
Vitaly Bukab1fcfc02015-09-14 10:24:39 -070032 ++ref_count_;
Vitaly Bukacbed2062015-08-17 12:54:05 -070033}
34
35bool RefCountedThreadSafeBase::Release() const {
36#ifndef NDEBUG
37 DCHECK(!in_dtor_);
Vitaly Bukab1fcfc02015-09-14 10:24:39 -070038 DCHECK(ref_count_ != 0);
Vitaly Bukacbed2062015-08-17 12:54:05 -070039#endif
Vitaly Bukab1fcfc02015-09-14 10:24:39 -070040 if (--ref_count_ == 0) {
Vitaly Bukacbed2062015-08-17 12:54:05 -070041#ifndef NDEBUG
42 in_dtor_ = true;
43#endif
44 return true;
45 }
46 return false;
47}
48
49} // namespace subtle
50
51} // namespace base