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 | 6 |
7 #include "base/threading/thread_collision_warner.h" | 7 #include "base/threading/thread_collision_warner.h" |
8 | 8 |
9 namespace base { | 9 namespace base { |
10 namespace { | 10 namespace { |
11 | 11 |
12 #if DCHECK_IS_ON() | 12 #if DCHECK_IS_ON() |
13 AtomicRefCount g_cross_thread_ref_count_access_allow_count = 0; | 13 AtomicRefCount g_cross_thread_ref_count_access_allow_count = 0; |
14 #endif | 14 #endif |
15 | 15 |
16 } // namespace | 16 } // namespace |
17 | 17 |
18 namespace subtle { | 18 namespace subtle { |
19 | 19 |
20 bool RefCountedThreadSafeBase::HasOneRef() const { | 20 bool RefCountedThreadSafeBase::HasOneRef() const { |
21 return AtomicRefCountIsOne(&ref_count_); | 21 return AtomicRefCountIsOne(&ref_count_); |
22 } | 22 } |
23 | 23 |
24 RefCountedThreadSafeBase::RefCountedThreadSafeBase() = default; | |
25 | |
26 RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { | 24 RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { |
27 #if DCHECK_IS_ON() | 25 #if DCHECK_IS_ON() |
28 DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " | 26 DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " |
29 "calling Release()"; | 27 "calling Release()"; |
30 #endif | 28 #endif |
31 } | 29 } |
32 | 30 |
33 void RefCountedThreadSafeBase::AddRef() const { | 31 void RefCountedThreadSafeBase::AddRef() const { |
34 #if DCHECK_IS_ON() | 32 #if DCHECK_IS_ON() |
35 DCHECK(!in_dtor_); | 33 DCHECK(!in_dtor_); |
| 34 DCHECK(!needs_adopt_ref_) |
| 35 << "This RefCounted object is created with non-zero reference count." |
| 36 << " The first reference to such a object has to be made by AdoptRef or" |
| 37 << " MakeShared."; |
36 #endif | 38 #endif |
37 AtomicRefCountInc(&ref_count_); | 39 AtomicRefCountInc(&ref_count_); |
38 } | 40 } |
39 | 41 |
40 bool RefCountedThreadSafeBase::Release() const { | 42 bool RefCountedThreadSafeBase::Release() const { |
41 #if DCHECK_IS_ON() | 43 #if DCHECK_IS_ON() |
42 DCHECK(!in_dtor_); | 44 DCHECK(!in_dtor_); |
43 DCHECK(!AtomicRefCountIsZero(&ref_count_)); | 45 DCHECK(!AtomicRefCountIsZero(&ref_count_)); |
44 #endif | 46 #endif |
45 if (!AtomicRefCountDec(&ref_count_)) { | 47 if (!AtomicRefCountDec(&ref_count_)) { |
(...skipping 18 matching lines...) Expand all Loading... |
64 ScopedAllowCrossThreadRefCountAccess::ScopedAllowCrossThreadRefCountAccess() { | 66 ScopedAllowCrossThreadRefCountAccess::ScopedAllowCrossThreadRefCountAccess() { |
65 AtomicRefCountInc(&g_cross_thread_ref_count_access_allow_count); | 67 AtomicRefCountInc(&g_cross_thread_ref_count_access_allow_count); |
66 } | 68 } |
67 | 69 |
68 ScopedAllowCrossThreadRefCountAccess::~ScopedAllowCrossThreadRefCountAccess() { | 70 ScopedAllowCrossThreadRefCountAccess::~ScopedAllowCrossThreadRefCountAccess() { |
69 AtomicRefCountDec(&g_cross_thread_ref_count_access_allow_count); | 71 AtomicRefCountDec(&g_cross_thread_ref_count_access_allow_count); |
70 } | 72 } |
71 #endif | 73 #endif |
72 | 74 |
73 } // namespace base | 75 } // namespace base |
OLD | NEW |