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.
4 :
5 : #include "base/ref_counted.h"
6 :
7 : #include "base/logging.h"
8 : #include "base/thread_collision_warner.h"
9 :
10 : namespace base {
11 :
12 : namespace subtle {
13 :
14 2 : RefCountedBase::RefCountedBase() : ref_count_(0) {
15 : #ifndef NDEBUG
16 1 : in_dtor_ = false;
17 : #endif
18 1 : }
19 :
20 0 : RefCountedBase::~RefCountedBase() {
21 : #ifndef NDEBUG
22 0 : DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()";
23 : #endif
24 0 : }
25 :
26 1 : 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 1 : DCHECK(!in_dtor_);
32 : #endif
33 1 : ++ref_count_;
34 1 : }
35 :
36 0 : 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 0 : DCHECK(!in_dtor_);
42 : #endif
43 0 : if (--ref_count_ == 0) {
44 : #ifndef NDEBUG
45 0 : in_dtor_ = true;
46 : #endif
47 0 : return true;
48 : }
49 0 : return false;
50 : }
51 :
52 7145 : RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) {
53 : #ifndef NDEBUG
54 7145 : in_dtor_ = false;
55 : #endif
56 7145 : }
57 :
58 5677 : RefCountedThreadSafeBase::~RefCountedThreadSafeBase() {
59 : #ifndef NDEBUG
60 5677 : DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without "
61 0 : "calling Release()";
62 : #endif
63 5677 : }
64 :
65 8564 : void RefCountedThreadSafeBase::AddRef() {
66 : #ifndef NDEBUG
67 8564 : DCHECK(!in_dtor_);
68 : #endif
69 8564 : AtomicRefCountInc(&ref_count_);
70 8564 : }
71 :
72 7096 : bool RefCountedThreadSafeBase::Release() {
73 : #ifndef NDEBUG
74 7096 : DCHECK(!in_dtor_);
75 7096 : DCHECK(!AtomicRefCountIsZero(&ref_count_));
76 : #endif
77 7096 : if (!AtomicRefCountDec(&ref_count_)) {
78 : #ifndef NDEBUG
79 5677 : in_dtor_ = true;
80 : #endif
81 5677 : return true;
82 : }
83 1419 : return false;
84 : }
85 :
86 : } // namespace subtle
87 :
88 : } // namespace base
|