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