Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(449)

Side by Side Diff: base/memory/weak_ptr.h

Issue 2963623002: Make base::WeakPtr::Get() fast (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/memory/weak_ptr.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // Weak pointers are pointers to an object that do not affect its lifetime, 5 // Weak pointers are pointers to an object that do not affect its lifetime,
6 // and which may be invalidated (i.e. reset to nullptr) by the object, or its 6 // and which may be invalidated (i.e. reset to nullptr) by the object, or its
7 // owner, at any time, most commonly when the object is about to be deleted. 7 // owner, at any time, most commonly when the object is about to be deleted.
8 8
9 // Weak pointers are useful when an object needs to be accessed safely by one 9 // Weak pointers are useful when an object needs to be accessed safely by one
10 // or more objects other than its owner, and those callers can cope with the 10 // or more objects other than its owner, and those callers can cope with the
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 83
84 template <typename T> class SupportsWeakPtr; 84 template <typename T> class SupportsWeakPtr;
85 template <typename T> class WeakPtr; 85 template <typename T> class WeakPtr;
86 86
87 namespace internal { 87 namespace internal {
88 // These classes are part of the WeakPtr implementation. 88 // These classes are part of the WeakPtr implementation.
89 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. 89 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF.
90 90
91 class BASE_EXPORT WeakReference { 91 class BASE_EXPORT WeakReference {
92 public: 92 public:
93 93 // Although Flag is bound to a specific SequencedTaskRunner, it may be
94 class BASE_EXPORT NewFlag { 94 // deleted from another via base::WeakPtr::~WeakPtr().
95 class BASE_EXPORT Flag : public RefCountedThreadSafe<Flag> {
95 public: 96 public:
96 constexpr static uintptr_t kTrueMask = ~static_cast<uintptr_t>(0); 97 Flag();
97 98 static Flag* nullFlag();
98 static NewFlag *NullFlag() { return &g_null_flag; }
99 NewFlag() : is_valid_(kTrueMask), ref_count_(0) {
100 #if DCHECK_IS_ON()
101 // Flags only become bound when checked for validity, or invalidated,
102 // so that we can check that later validity/invalidation operations on
103 // the same Flag take place on the same sequenced thread.
104 sequence_checker_.DetachFromSequence();
105 #endif
106 }
107 99
108 void Invalidate() { 100 void Invalidate() {
109 #if DCHECK_IS_ON() 101 #if DCHECK_IS_ON()
110 if (this == NullFlag()) { 102 if (this == nullFlag())
111 DCHECK(!is_valid_);
112 return; 103 return;
113 }
114 // The flag being invalidated with a single ref implies that there are no 104 // The flag being invalidated with a single ref implies that there are no
115 // weak pointers in existence. Allow deletion on other thread in this case . 105 // weak pointers in existence. Allow deletion on other thread in this case .
116 DCHECK(sequence_checker_.CalledOnValidSequence() || HasOneRef()) 106 DCHECK(sequence_checker_.CalledOnValidSequence() || HasOneRef())
117 << "WeakPtrs must be invalidated on the same sequenced thread."; 107 << "WeakPtrs must be invalidated on the same sequenced thread.";
118 #endif 108 #endif
119 is_valid_ = 0; 109 is_valid_ = 0;
120 } 110 }
121
122 uintptr_t IsValid() const { 111 uintptr_t IsValid() const {
123 #if DCHECK_IS_ON() 112 #if DCHECK_IS_ON()
124 if (this == NullFlag()) { 113 if (this == nullFlag()) {
125 DCHECK(!is_valid_); 114 DCHECK(!is_valid_);
126 return false; 115 return 0;
127 } 116 }
128 DCHECK(sequence_checker_.CalledOnValidSequence()) 117 DCHECK(sequence_checker_.CalledOnValidSequence())
129 << "WeakPtrs must be checked on the same sequenced thread."; 118 << "WeakPtrs must be checked on the same sequenced thread.";
130 #endif 119 #endif
131 return is_valid_; 120 return is_valid_;
132 } 121 }
133 122
134 uintptr_t NumRefs() const { return ref_count_; } 123 private:
124 friend class base::RefCountedThreadSafe<Flag>;
135 125
136 void AddRef() const { AtomicRefCountInc(&ref_count_); } 126 enum NullFlagTag { kNullFlagTag };
137 void Release() const { 127 Flag(NullFlagTag);
138 if (!AtomicRefCountDec(&ref_count_))
139 delete this;
140 }
141 128
142 private: 129 ~Flag();
143 ~NewFlag() = default;
144 enum NullFlagTag { kNullFlagTag };
145 #if DCHECK_IS_ON()
146 NewFlag(NullFlagTag);
147 #else
148 constexpr NewFlag(NullFlagTag);
149 #endif
150
151 static NewFlag g_null_flag;
152 130
153 uintptr_t is_valid_; 131 uintptr_t is_valid_;
154 mutable AtomicRefCount ref_count_;
155 #if DCHECK_IS_ON() 132 #if DCHECK_IS_ON()
133 // Even if SequenceChecker is an empty class in non-dcheck builds, it still
134 // takes up space in the class.
156 SequenceChecker sequence_checker_; 135 SequenceChecker sequence_checker_;
157 #endif 136 #endif
158 }; 137 };
159 138
160 // Although Flag is bound to a specific SequencedTaskRunner, it may be
161 // deleted from another via base::WeakPtr::~WeakPtr().
162 class BASE_EXPORT Flag : public RefCountedThreadSafe<Flag> {
163 public:
164 Flag();
165
166 void Invalidate();
167 bool IsValid() const;
168
169 private:
170 friend class base::RefCountedThreadSafe<Flag>;
171
172 ~Flag();
173
174 SequenceChecker sequence_checker_;
175 bool is_valid_;
176 };
177
178 WeakReference(); 139 WeakReference();
179 explicit WeakReference(/*const Flag* flag,*/ const NewFlag* new_flag); 140 explicit WeakReference(const Flag* flag);
180 ~WeakReference(); 141 ~WeakReference();
181 142
182 WeakReference(WeakReference&& other); 143 WeakReference(WeakReference&& other);
183 WeakReference(const WeakReference& other); 144 WeakReference(const WeakReference& other);
184 WeakReference& operator=(WeakReference&& other) = default; 145 WeakReference& operator=(WeakReference&& other) = default;
185 WeakReference& operator=(const WeakReference& other) = default; 146 WeakReference& operator=(const WeakReference& other) = default;
186 147
187 uintptr_t is_valid() const { return new_flag_->IsValid(); } 148 uintptr_t is_valid() const { return flag_->IsValid(); }
188 149
189 private: 150 private:
190 //scoped_refptr<const Flag> flag_; 151 // Note: To avoid null-checks, flag_ always points to either Flag::nullFlag()
191 scoped_refptr<const NewFlag> new_flag_; 152 // or some other object.
153 scoped_refptr<const Flag> flag_;
192 }; 154 };
193 155
194 class BASE_EXPORT WeakReferenceOwner { 156 class BASE_EXPORT WeakReferenceOwner {
195 public: 157 public:
196 WeakReferenceOwner(); 158 WeakReferenceOwner();
197 ~WeakReferenceOwner(); 159 ~WeakReferenceOwner();
198 160
199 WeakReference GetRef() const; 161 WeakReference GetRef() const;
200 162
201 bool HasRefs() const { 163 bool HasRefs() const { return flag_->IsValid() && !flag_->HasOneRef(); }
202 return (new_flag_->IsValid() & new_flag_->NumRefs()) > 1;
203
204 #if 0
Nico 2017/06/27 22:05:41 This looks like your diffbase might be off?
hans 2017/06/27 22:09:50 Yeah, something is not right here :-(
205 return new_flag_ != WeakReference::NewFlag::NullFlag() &&
206 !new_flag_->HasOneRef();
207 #endif
208 //return flag_.get() && !flag_->HasOneRef();
209 }
210 164
211 void Invalidate(); 165 void Invalidate();
212 166
213 private: 167 private:
214 //mutable scoped_refptr<WeakReference::Flag> flag_; 168 mutable scoped_refptr<WeakReference::Flag> flag_;
215 mutable scoped_refptr<WeakReference::NewFlag> new_flag_;
216 }; 169 };
217 170
218 // This class simplifies the implementation of WeakPtr's type conversion 171 // This class simplifies the implementation of WeakPtr's type conversion
219 // constructor by avoiding the need for a public accessor for ref_. A 172 // constructor by avoiding the need for a public accessor for ref_. A
220 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this 173 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this
221 // base class gives us a way to access ref_ in a protected fashion. 174 // base class gives us a way to access ref_ in a protected fashion.
222 class BASE_EXPORT WeakPtrBase { 175 class BASE_EXPORT WeakPtrBase {
223 public: 176 public:
224 WeakPtrBase(); 177 WeakPtrBase();
225 ~WeakPtrBase(); 178 ~WeakPtrBase();
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 } 298 }
346 template <class T> 299 template <class T>
347 bool operator==(const WeakPtr<T>& weak_ptr, std::nullptr_t) { 300 bool operator==(const WeakPtr<T>& weak_ptr, std::nullptr_t) {
348 return weak_ptr.get() == nullptr; 301 return weak_ptr.get() == nullptr;
349 } 302 }
350 template <class T> 303 template <class T>
351 bool operator==(std::nullptr_t, const WeakPtr<T>& weak_ptr) { 304 bool operator==(std::nullptr_t, const WeakPtr<T>& weak_ptr) {
352 return weak_ptr == nullptr; 305 return weak_ptr == nullptr;
353 } 306 }
354 307
355 // A class may be composed of a WeakPtrFactory and thereby 308 namespace internal {
356 // control how it exposes weak pointers to itself. This is helpful if you only
357 // need weak pointers within the implementation of a class. This class is also
358 // useful when working with primitive types. For example, you could have a
359 // WeakPtrFactory<bool> that is used to pass around a weak reference to a bool.
360 class BASE_EXPORT WeakPtrFactoryBase { 309 class BASE_EXPORT WeakPtrFactoryBase {
361 protected: 310 protected:
362 WeakPtrFactoryBase(uintptr_t ptr); 311 WeakPtrFactoryBase(uintptr_t ptr);
363 ~WeakPtrFactoryBase(); 312 ~WeakPtrFactoryBase();
364 internal::WeakReferenceOwner weak_reference_owner_; 313 internal::WeakReferenceOwner weak_reference_owner_;
365 uintptr_t ptr_; 314 uintptr_t ptr_;
366 }; 315 };
316 } // namespace internal
367 317
318 // A class may be composed of a WeakPtrFactory and thereby
319 // control how it exposes weak pointers to itself. This is helpful if you only
320 // need weak pointers within the implementation of a class. This class is also
321 // useful when working with primitive types. For example, you could have a
322 // WeakPtrFactory<bool> that is used to pass around a weak reference to a bool.
368 template <class T> 323 template <class T>
369 class WeakPtrFactory : public WeakPtrFactoryBase { 324 class WeakPtrFactory : public internal::WeakPtrFactoryBase {
370 public: 325 public:
371 explicit WeakPtrFactory(T* ptr) : WeakPtrFactoryBase(reinterpret_cast<uintptr_ t>(ptr)) { } 326 explicit WeakPtrFactory(T* ptr)
327 : WeakPtrFactoryBase(reinterpret_cast<uintptr_t>(ptr)) {}
372 328
373 ~WeakPtrFactory() {} 329 ~WeakPtrFactory() {}
374 330
375 WeakPtr<T> GetWeakPtr() { 331 WeakPtr<T> GetWeakPtr() {
376 DCHECK(ptr_); 332 DCHECK(ptr_);
377 return WeakPtr<T>(weak_reference_owner_.GetRef(), reinterpret_cast<T*>(ptr_) ); 333 return WeakPtr<T>(weak_reference_owner_.GetRef(),
334 reinterpret_cast<T*>(ptr_));
378 } 335 }
379 336
380 // Call this method to invalidate all existing weak pointers. 337 // Call this method to invalidate all existing weak pointers.
381 void InvalidateWeakPtrs() { 338 void InvalidateWeakPtrs() {
382 DCHECK(ptr_); 339 DCHECK(ptr_);
383 weak_reference_owner_.Invalidate(); 340 weak_reference_owner_.Invalidate();
384 } 341 }
385 342
386 // Call this method to determine if any weak pointers exist. 343 // Call this method to determine if any weak pointers exist.
387 bool HasWeakPtrs() const { 344 bool HasWeakPtrs() const {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. 391 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails.
435 392
436 template <typename Derived> 393 template <typename Derived>
437 WeakPtr<Derived> AsWeakPtr(Derived* t) { 394 WeakPtr<Derived> AsWeakPtr(Derived* t) {
438 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); 395 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t);
439 } 396 }
440 397
441 } // namespace base 398 } // namespace base
442 399
443 #endif // BASE_MEMORY_WEAK_PTR_H_ 400 #endif // BASE_MEMORY_WEAK_PTR_H_
OLDNEW
« no previous file with comments | « no previous file | base/memory/weak_ptr.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698