OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef BASE_MEMORY_REF_COUNTED_H_ | 5 #ifndef BASE_MEMORY_REF_COUNTED_H_ |
6 #define BASE_MEMORY_REF_COUNTED_H_ | 6 #define BASE_MEMORY_REF_COUNTED_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <cassert> | 10 #include <cassert> |
11 #include <iosfwd> | 11 #include <iosfwd> |
12 #include <type_traits> | 12 #include <type_traits> |
13 | 13 |
14 #include "base/atomic_ref_count.h" | 14 #include "base/atomic_ref_count.h" |
15 #include "base/base_export.h" | 15 #include "base/base_export.h" |
16 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "base/sequence_checker.h" | |
19 #include "base/threading/thread_collision_warner.h" | 20 #include "base/threading/thread_collision_warner.h" |
20 #include "build/build_config.h" | 21 #include "build/build_config.h" |
21 | 22 |
22 namespace base { | 23 namespace base { |
23 | |
24 namespace subtle { | 24 namespace subtle { |
25 | 25 |
26 class BASE_EXPORT RefCountedBase { | 26 class BASE_EXPORT RefCountedBase { |
27 public: | 27 public: |
28 bool HasOneRef() const { return ref_count_ == 1; } | 28 bool HasOneRef() const { return ref_count_ == 1; } |
29 | 29 |
30 protected: | 30 protected: |
31 RefCountedBase() | 31 RefCountedBase() { |
32 : ref_count_(0) | |
33 #if DCHECK_IS_ON() | 32 #if DCHECK_IS_ON() |
34 , in_dtor_(false) | 33 sequence_checker_.DetachFromSequence(); |
35 #endif | 34 #endif |
36 { | |
37 } | 35 } |
38 | |
39 ~RefCountedBase() { | 36 ~RefCountedBase() { |
40 #if DCHECK_IS_ON() | 37 #if DCHECK_IS_ON() |
41 DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()"; | 38 DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()"; |
42 #endif | 39 #endif |
43 } | 40 } |
44 | 41 |
45 | |
46 void AddRef() const { | 42 void AddRef() const { |
47 // TODO(maruel): Add back once it doesn't assert 500 times/sec. | 43 // TODO(maruel): Add back once it doesn't assert 500 times/sec. |
48 // Current thread books the critical section "AddRelease" | 44 // Current thread books the critical section "AddRelease" |
49 // without release it. | 45 // without release it. |
50 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); | 46 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); |
51 #if DCHECK_IS_ON() | 47 #if DCHECK_IS_ON() |
52 DCHECK(!in_dtor_); | 48 DCHECK(!in_dtor_); |
49 if (ref_count_ >= 1) | |
50 DCHECK(CalledOnValidSequence()); | |
53 #endif | 51 #endif |
52 | |
54 ++ref_count_; | 53 ++ref_count_; |
55 } | 54 } |
56 | 55 |
57 // Returns true if the object should self-delete. | 56 // Returns true if the object should self-delete. |
58 bool Release() const { | 57 bool Release() const { |
58 --ref_count_; | |
59 | |
59 // TODO(maruel): Add back once it doesn't assert 500 times/sec. | 60 // TODO(maruel): Add back once it doesn't assert 500 times/sec. |
60 // Current thread books the critical section "AddRelease" | 61 // Current thread books the critical section "AddRelease" |
61 // without release it. | 62 // without release it. |
62 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); | 63 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); |
64 | |
63 #if DCHECK_IS_ON() | 65 #if DCHECK_IS_ON() |
64 DCHECK(!in_dtor_); | 66 DCHECK(!in_dtor_); |
67 if (ref_count_ == 0) | |
68 in_dtor_ = true; | |
69 | |
70 if (ref_count_ >= 1) | |
71 DCHECK(CalledOnValidSequence()); | |
72 if (ref_count_ == 1) | |
73 sequence_checker_.DetachFromSequence(); | |
65 #endif | 74 #endif |
66 if (--ref_count_ == 0) { | 75 |
67 #if DCHECK_IS_ON() | 76 return ref_count_ == 0; |
68 in_dtor_ = true; | |
69 #endif | |
70 return true; | |
71 } | |
72 return false; | |
73 } | 77 } |
74 | 78 |
75 private: | 79 private: |
76 mutable size_t ref_count_; | |
77 #if DCHECK_IS_ON() | 80 #if DCHECK_IS_ON() |
78 mutable bool in_dtor_; | 81 bool CalledOnValidSequence() const; |
82 #endif | |
83 | |
84 mutable size_t ref_count_ = 0; | |
85 | |
86 #if DCHECK_IS_ON() | |
87 mutable bool in_dtor_ = false; | |
88 mutable SequenceChecker sequence_checker_; | |
79 #endif | 89 #endif |
80 | 90 |
81 DFAKE_MUTEX(add_release_); | 91 DFAKE_MUTEX(add_release_); |
82 | 92 |
83 DISALLOW_COPY_AND_ASSIGN(RefCountedBase); | 93 DISALLOW_COPY_AND_ASSIGN(RefCountedBase); |
84 }; | 94 }; |
85 | 95 |
86 class BASE_EXPORT RefCountedThreadSafeBase { | 96 class BASE_EXPORT RefCountedThreadSafeBase { |
87 public: | 97 public: |
88 bool HasOneRef() const; | 98 bool HasOneRef() const; |
(...skipping 11 matching lines...) Expand all Loading... | |
100 mutable AtomicRefCount ref_count_; | 110 mutable AtomicRefCount ref_count_; |
101 #if DCHECK_IS_ON() | 111 #if DCHECK_IS_ON() |
102 mutable bool in_dtor_; | 112 mutable bool in_dtor_; |
103 #endif | 113 #endif |
104 | 114 |
105 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); | 115 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); |
106 }; | 116 }; |
107 | 117 |
108 } // namespace subtle | 118 } // namespace subtle |
109 | 119 |
120 // See RefCounted class for documentation. | |
121 class BASE_EXPORT ScopedAllowCrossThreadRefCountAccess final { | |
122 public: | |
123 #if DCHECK_IS_ON() | |
124 ScopedAllowCrossThreadRefCountAccess(); | |
125 ~ScopedAllowCrossThreadRefCountAccess(); | |
126 #else | |
127 ScopedAllowCrossThreadRefCountAccess() {} | |
128 ~ScopedAllowCrossThreadRefCountAccess() {} | |
129 #endif | |
130 }; | |
131 | |
110 // | 132 // |
111 // A base class for reference counted classes. Otherwise, known as a cheap | 133 // A base class for reference counted classes. Otherwise, known as a cheap |
112 // knock-off of WebKit's RefCounted<T> class. To use this, just extend your | 134 // knock-off of WebKit's RefCounted<T> class. To use this, just extend your |
113 // class from it like so: | 135 // class from it like so: |
114 // | 136 // |
115 // class MyFoo : public base::RefCounted<MyFoo> { | 137 // class MyFoo : public base::RefCounted<MyFoo> { |
116 // ... | 138 // ... |
117 // private: | 139 // private: |
118 // friend class base::RefCounted<MyFoo>; | 140 // friend class base::RefCounted<MyFoo>; |
119 // ~MyFoo(); | 141 // ~MyFoo(); |
120 // }; | 142 // }; |
121 // | 143 // |
122 // You should always make your destructor non-public, to avoid any code deleting | 144 // You should always make your destructor non-public, to avoid any code deleting |
123 // the object accidently while there are references to it. | 145 // the object accidently while there are references to it. |
146 // | |
147 // The ref count manipulation to RefCounted is NOT thread safe and has DCHECKs | |
148 // to trap unsafe cross thread usage. A subclass instance of RefCounted can be | |
149 // passed to the other thread or sequence only when its ref count is 1. If the | |
gab
2017/02/17 21:10:41
s/the other/another/
tzik
2017/02/21 06:15:02
Done.
| |
150 // ref count is more than 1, the ref count verifies the ref updates are made on | |
gab
2017/02/17 21:10:40
s/the ref count/the RefCounted class/
tzik
2017/02/21 06:15:02
Done.
| |
151 // the same execution sequence as the previous ones. | |
152 // In rare cases where thread-safety is guaranteed through other means (e.g. | |
153 // locks or explicit sequencing of calls across execution sequences): | |
154 // ScopedAllowCrossThreadRefCountAccess can be used to explicitly indicate this | |
155 // and disable the aforementioned check. | |
124 template <class T> | 156 template <class T> |
125 class RefCounted : public subtle::RefCountedBase { | 157 class RefCounted : public subtle::RefCountedBase { |
126 public: | 158 public: |
127 RefCounted() = default; | 159 RefCounted() = default; |
128 | 160 |
129 void AddRef() const { | 161 void AddRef() const { |
130 subtle::RefCountedBase::AddRef(); | 162 subtle::RefCountedBase::AddRef(); |
131 } | 163 } |
132 | 164 |
133 void Release() const { | 165 void Release() const { |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
455 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { | 487 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { |
456 return !operator==(null, rhs); | 488 return !operator==(null, rhs); |
457 } | 489 } |
458 | 490 |
459 template <typename T> | 491 template <typename T> |
460 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 492 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
461 return out << p.get(); | 493 return out << p.get(); |
462 } | 494 } |
463 | 495 |
464 #endif // BASE_MEMORY_REF_COUNTED_H_ | 496 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |