OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
6 #include "base/threading/thread_collision_warner.h" | 6 #include "base/threading/thread_collision_warner.h" |
7 | 7 |
8 namespace base { | 8 namespace base { |
9 | 9 |
10 namespace subtle { | 10 namespace subtle { |
11 | 11 |
12 bool RefCountedThreadSafeBase::HasOneRef() const { | 12 bool RefCountedThreadSafeBase::HasOneRef() const { |
13 return AtomicRefCountIsOne( | 13 return AtomicRefCountIsOne( |
14 &const_cast<RefCountedThreadSafeBase*>(this)->ref_count_); | 14 &const_cast<RefCountedThreadSafeBase*>(this)->ref_count_); |
15 } | 15 } |
16 | 16 |
17 RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) { | 17 RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) { |
18 #ifndef NDEBUG | 18 #if DCHECK_IS_ON() |
19 in_dtor_ = false; | 19 in_dtor_ = false; |
20 #endif | 20 #endif |
21 } | 21 } |
22 | 22 |
23 RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { | 23 RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { |
24 #ifndef NDEBUG | 24 #if DCHECK_IS_ON() |
25 DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " | 25 DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " |
26 "calling Release()"; | 26 "calling Release()"; |
27 #endif | 27 #endif |
28 } | 28 } |
29 | 29 |
30 void RefCountedThreadSafeBase::AddRef() const { | 30 void RefCountedThreadSafeBase::AddRef() const { |
31 #ifndef NDEBUG | 31 #if DCHECK_IS_ON() |
32 DCHECK(!in_dtor_); | 32 DCHECK(!in_dtor_); |
33 #endif | 33 #endif |
34 AtomicRefCountInc(&ref_count_); | 34 AtomicRefCountInc(&ref_count_); |
35 } | 35 } |
36 | 36 |
37 bool RefCountedThreadSafeBase::Release() const { | 37 bool RefCountedThreadSafeBase::Release() const { |
38 #ifndef NDEBUG | 38 #if DCHECK_IS_ON() |
39 DCHECK(!in_dtor_); | 39 DCHECK(!in_dtor_); |
40 DCHECK(!AtomicRefCountIsZero(&ref_count_)); | 40 DCHECK(!AtomicRefCountIsZero(&ref_count_)); |
41 #endif | 41 #endif |
42 if (!AtomicRefCountDec(&ref_count_)) { | 42 if (!AtomicRefCountDec(&ref_count_)) { |
43 #ifndef NDEBUG | 43 #if DCHECK_IS_ON() |
44 in_dtor_ = true; | 44 in_dtor_ = true; |
45 #endif | 45 #endif |
46 return true; | 46 return true; |
47 } | 47 } |
48 return false; | 48 return false; |
49 } | 49 } |
50 | 50 |
51 } // namespace subtle | 51 } // namespace subtle |
52 | 52 |
53 } // namespace base | 53 } // namespace base |
OLD | NEW |