blob: f5924d0fe76e44672ca24372f93b40d418f46b62 [file] [log] [blame]
Daniel Eratb8cf9492015-07-06 13:18:13 -06001// 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"
6#include "base/threading/thread_collision_warner.h"
7
8namespace base {
9
10namespace subtle {
11
12bool RefCountedThreadSafeBase::HasOneRef() const {
Luis Hector Chaveze5b2c6f2017-07-26 17:33:47 +000013 return AtomicRefCountIsOne(
14 &const_cast<RefCountedThreadSafeBase*>(this)->ref_count_);
Daniel Eratb8cf9492015-07-06 13:18:13 -060015}
16
Luis Hector Chaveze5b2c6f2017-07-26 17:33:47 +000017RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) {
18#ifndef NDEBUG
19 in_dtor_ = false;
20#endif
21}
Daniel Eratb8cf9492015-07-06 13:18:13 -060022
23RefCountedThreadSafeBase::~RefCountedThreadSafeBase() {
Luis Hector Chaveze5b2c6f2017-07-26 17:33:47 +000024#ifndef NDEBUG
Daniel Eratb8cf9492015-07-06 13:18:13 -060025 DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without "
26 "calling Release()";
27#endif
28}
29
30void RefCountedThreadSafeBase::AddRef() const {
Luis Hector Chaveze5b2c6f2017-07-26 17:33:47 +000031#ifndef NDEBUG
Daniel Eratb8cf9492015-07-06 13:18:13 -060032 DCHECK(!in_dtor_);
33#endif
34 AtomicRefCountInc(&ref_count_);
35}
36
37bool RefCountedThreadSafeBase::Release() const {
Luis Hector Chaveze5b2c6f2017-07-26 17:33:47 +000038#ifndef NDEBUG
Daniel Eratb8cf9492015-07-06 13:18:13 -060039 DCHECK(!in_dtor_);
40 DCHECK(!AtomicRefCountIsZero(&ref_count_));
41#endif
42 if (!AtomicRefCountDec(&ref_count_)) {
Luis Hector Chaveze5b2c6f2017-07-26 17:33:47 +000043#ifndef NDEBUG
Daniel Eratb8cf9492015-07-06 13:18:13 -060044 in_dtor_ = true;
45#endif
46 return true;
47 }
48 return false;
49}
50
51} // namespace subtle
52
53} // namespace base