Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 2c69139 | 2013-04-25 20:47:18 | [diff] [blame] | 5 | // Weak pointers are pointers to an object that do not affect its lifetime, |
dcheng | efeb19b | 2016-04-05 20:07:40 | [diff] [blame] | 6 | // and which may be invalidated (i.e. reset to nullptr) by the object, or its |
[email protected] | 2c69139 | 2013-04-25 20:47:18 | [diff] [blame] | 7 | // owner, at any time, most commonly when the object is about to be deleted. |
| 8 | |
| 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 |
| 11 | // object vanishing and e.g. tasks posted to it being silently dropped. |
| 12 | // Reference-counting such an object would complicate the ownership graph and |
| 13 | // make it harder to reason about the object's lifetime. |
| 14 | |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 15 | // EXAMPLE: |
| 16 | // |
[email protected] | 2c69139 | 2013-04-25 20:47:18 | [diff] [blame] | 17 | // class Controller { |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 18 | // public: |
[email protected] | 2c69139 | 2013-04-25 20:47:18 | [diff] [blame] | 19 | // void SpawnWorker() { Worker::StartNew(weak_factory_.GetWeakPtr()); } |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 20 | // void WorkComplete(const Result& result) { ... } |
[email protected] | 2c69139 | 2013-04-25 20:47:18 | [diff] [blame] | 21 | // private: |
[email protected] | ed3f6734 | 2013-05-29 08:04:32 | [diff] [blame] | 22 | // // Member variables should appear before the WeakPtrFactory, to ensure |
| 23 | // // that any WeakPtrs to Controller are invalidated before its members |
| 24 | // // variable's destructors are executed, rendering them invalid. |
Jeremy Roman | 0dd0b2f | 2019-07-16 21:00:43 | [diff] [blame] | 25 | // WeakPtrFactory<Controller> weak_factory_{this}; |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 26 | // }; |
| 27 | // |
| 28 | // class Worker { |
| 29 | // public: |
Daniel Hosseinian | fff4da5 | 2021-01-27 22:13:08 | [diff] [blame] | 30 | // static void StartNew(WeakPtr<Controller> controller) { |
danakj | b57ac1d | 2021-12-03 21:52:49 | [diff] [blame] | 31 | // // Move WeakPtr when possible to avoid atomic refcounting churn on its |
| 32 | // // internal state. |
Daniel Hosseinian | fff4da5 | 2021-01-27 22:13:08 | [diff] [blame] | 33 | // Worker* worker = new Worker(std::move(controller)); |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 34 | // // Kick off asynchronous processing... |
| 35 | // } |
| 36 | // private: |
Daniel Hosseinian | fff4da5 | 2021-01-27 22:13:08 | [diff] [blame] | 37 | // Worker(WeakPtr<Controller> controller) |
| 38 | // : controller_(std::move(controller)) {} |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 39 | // void DidCompleteAsynchronousProcessing(const Result& result) { |
| 40 | // if (controller_) |
| 41 | // controller_->WorkComplete(result); |
| 42 | // } |
| 43 | // WeakPtr<Controller> controller_; |
| 44 | // }; |
| 45 | // |
[email protected] | 2c69139 | 2013-04-25 20:47:18 | [diff] [blame] | 46 | // With this implementation a caller may use SpawnWorker() to dispatch multiple |
| 47 | // Workers and subsequently delete the Controller, without waiting for all |
| 48 | // Workers to have completed. |
| 49 | |
| 50 | // ------------------------- IMPORTANT: Thread-safety ------------------------- |
| 51 | |
Nicolas Ouellet-payeur | ff1eab6 | 2018-07-19 18:41:50 | [diff] [blame] | 52 | // Weak pointers may be passed safely between sequences, but must always be |
davidben | e58877e | 2014-10-22 18:37:11 | [diff] [blame] | 53 | // dereferenced and invalidated on the same SequencedTaskRunner otherwise |
| 54 | // checking the pointer would be racey. |
[email protected] | ed3f6734 | 2013-05-29 08:04:32 | [diff] [blame] | 55 | // |
| 56 | // To ensure correct use, the first time a WeakPtr issued by a WeakPtrFactory |
| 57 | // is dereferenced, the factory and its WeakPtrs become bound to the calling |
Nicolas Ouellet-payeur | ff1eab6 | 2018-07-19 18:41:50 | [diff] [blame] | 58 | // sequence or current SequencedWorkerPool token, and cannot be dereferenced or |
davidben | e58877e | 2014-10-22 18:37:11 | [diff] [blame] | 59 | // invalidated on any other task runner. Bound WeakPtrs can still be handed |
| 60 | // off to other task runners, e.g. to use to post tasks back to object on the |
| 61 | // bound sequence. |
[email protected] | ed3f6734 | 2013-05-29 08:04:32 | [diff] [blame] | 62 | // |
scheib | 3ad0372 | 2015-10-05 22:27:41 | [diff] [blame] | 63 | // If all WeakPtr objects are destroyed or invalidated then the factory is |
| 64 | // unbound from the SequencedTaskRunner/Thread. The WeakPtrFactory may then be |
| 65 | // destroyed, or new WeakPtr objects may be used, from a different sequence. |
| 66 | // |
| 67 | // Thus, at least one WeakPtr object must exist and have been dereferenced on |
Nicolas Ouellet-payeur | ff1eab6 | 2018-07-19 18:41:50 | [diff] [blame] | 68 | // the correct sequence to enforce that other WeakPtr objects will enforce they |
| 69 | // are used on the desired sequence. |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 70 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 71 | #ifndef BASE_MEMORY_WEAK_PTR_H_ |
| 72 | #define BASE_MEMORY_WEAK_PTR_H_ |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 73 | |
dcheng | efeb19b | 2016-04-05 20:07:40 | [diff] [blame] | 74 | #include <cstddef> |
tzik | 403cb6c | 2016-03-10 07:17:25 | [diff] [blame] | 75 | #include <type_traits> |
Sumaid Syed | 22f60eeb | 2021-08-26 05:16:26 | [diff] [blame] | 76 | #include <utility> |
tzik | 403cb6c | 2016-03-10 07:17:25 | [diff] [blame] | 77 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 78 | #include "base/base_export.h" |
Hans Wennborg | 7b53371 | 2020-06-22 20:52:27 | [diff] [blame] | 79 | #include "base/check.h" |
Lukasz Anforowicz | 7623e19 | 2021-12-02 23:19:36 | [diff] [blame] | 80 | #include "base/compiler_specific.h" |
Igor Bukanov | 933bad99 | 2021-09-10 19:47:25 | [diff] [blame] | 81 | #include "base/dcheck_is_on.h" |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 82 | #include "base/memory/raw_ptr.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 83 | #include "base/memory/ref_counted.h" |
[email protected] | d52426c | 2013-07-30 19:26:40 | [diff] [blame] | 84 | #include "base/sequence_checker.h" |
Nicolas Ouellet-payeur | ff1eab6 | 2018-07-19 18:41:50 | [diff] [blame] | 85 | #include "base/synchronization/atomic_flag.h" |
Scott Haseley | da86547 | 2023-05-10 19:03:11 | [diff] [blame^] | 86 | #include "base/types/pass_key.h" |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 87 | |
| 88 | namespace base { |
| 89 | |
Scott Haseley | da86547 | 2023-05-10 19:03:11 | [diff] [blame^] | 90 | namespace sequence_manager::internal { |
| 91 | class TaskQueueImpl; |
| 92 | } |
| 93 | |
danakj | 45c9178 | 2021-09-29 18:23:57 | [diff] [blame] | 94 | template <typename T> |
| 95 | class SafeRef; |
[email protected] | 4c44b844 | 2012-06-15 16:36:12 | [diff] [blame] | 96 | template <typename T> class SupportsWeakPtr; |
| 97 | template <typename T> class WeakPtr; |
| 98 | |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 99 | namespace internal { |
| 100 | // These classes are part of the WeakPtr implementation. |
| 101 | // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. |
| 102 | |
Lukasz Anforowicz | 7623e19 | 2021-12-02 23:19:36 | [diff] [blame] | 103 | class BASE_EXPORT TRIVIAL_ABI WeakReference { |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 104 | public: |
Wez | fb4a528 | 2018-05-01 09:02:11 | [diff] [blame] | 105 | // Although Flag is bound to a specific SequencedTaskRunner, it may be |
| 106 | // deleted from another via base::WeakPtr::~WeakPtr(). |
[email protected] | a827a56 | 2014-06-11 00:37:42 | [diff] [blame] | 107 | class BASE_EXPORT Flag : public RefCountedThreadSafe<Flag> { |
[email protected] | d52e9709 | 2009-09-17 00:31:09 | [diff] [blame] | 108 | public: |
[email protected] | 1edefc4 | 2011-08-26 17:32:29 | [diff] [blame] | 109 | Flag(); |
[email protected] | 59326aac | 2009-09-25 23:34:34 | [diff] [blame] | 110 | |
hans | 24046fd | 2017-06-29 22:07:42 | [diff] [blame] | 111 | void Invalidate(); |
Hans Wennborg | 4cfa301 | 2017-08-08 03:33:38 | [diff] [blame] | 112 | bool IsValid() const; |
[email protected] | 59326aac | 2009-09-25 23:34:34 | [diff] [blame] | 113 | |
Nicolas Ouellet-payeur | ff1eab6 | 2018-07-19 18:41:50 | [diff] [blame] | 114 | bool MaybeValid() const; |
| 115 | |
Igor Bukanov | 933bad99 | 2021-09-10 19:47:25 | [diff] [blame] | 116 | #if DCHECK_IS_ON() |
Wez | c5acb1ae | 2019-02-14 03:09:32 | [diff] [blame] | 117 | void DetachFromSequence(); |
Scott Haseley | da86547 | 2023-05-10 19:03:11 | [diff] [blame^] | 118 | void BindToCurrentSequence(); |
Igor Bukanov | 933bad99 | 2021-09-10 19:47:25 | [diff] [blame] | 119 | #endif |
Wez | c5acb1ae | 2019-02-14 03:09:32 | [diff] [blame] | 120 | |
[email protected] | 59326aac | 2009-09-25 23:34:34 | [diff] [blame] | 121 | private: |
[email protected] | 05b1cd61 | 2011-04-11 20:47:25 | [diff] [blame] | 122 | friend class base::RefCountedThreadSafe<Flag>; |
| 123 | |
| 124 | ~Flag(); |
| 125 | |
Nicolas Ouellet-payeur | ff1eab6 | 2018-07-19 18:41:50 | [diff] [blame] | 126 | SEQUENCE_CHECKER(sequence_checker_); |
| 127 | AtomicFlag invalidated_; |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 128 | }; |
| 129 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 130 | WeakReference(); |
Wez | 7eedd9e | 2018-01-23 00:27:05 | [diff] [blame] | 131 | explicit WeakReference(const scoped_refptr<Flag>& flag); |
[email protected] | 20136647 | 2010-07-16 17:22:49 | [diff] [blame] | 132 | ~WeakReference(); |
[email protected] | 59326aac | 2009-09-25 23:34:34 | [diff] [blame] | 133 | |
mek | 7901681 | 2016-06-24 21:29:04 | [diff] [blame] | 134 | WeakReference(const WeakReference& other); |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 135 | WeakReference& operator=(const WeakReference& other); |
mek | 7901681 | 2016-06-24 21:29:04 | [diff] [blame] | 136 | |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 137 | WeakReference(WeakReference&& other) noexcept; |
| 138 | WeakReference& operator=(WeakReference&& other) noexcept; |
| 139 | |
| 140 | void Reset(); |
| 141 | // Returns whether the WeakReference is valid, meaning the WeakPtrFactory has |
| 142 | // not invalidated the pointer. Unlike, RefIsMaybeValid(), this may only be |
| 143 | // called from the same sequence as where the WeakPtr was created. |
Nicolas Ouellet-payeur | ff1eab6 | 2018-07-19 18:41:50 | [diff] [blame] | 144 | bool IsValid() const; |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 145 | // Returns false if the WeakReference is confirmed to be invalid. This call is |
| 146 | // safe to make from any thread, e.g. to optimize away unnecessary work, but |
| 147 | // RefIsValid() must always be called, on the correct sequence, before |
| 148 | // actually using the pointer. |
| 149 | // |
| 150 | // Warning: as with any object, this call is only thread-safe if the WeakPtr |
| 151 | // instance isn't being re-assigned or reset() racily with this call. |
Nicolas Ouellet-payeur | ff1eab6 | 2018-07-19 18:41:50 | [diff] [blame] | 152 | bool MaybeValid() const; |
[email protected] | 59326aac | 2009-09-25 23:34:34 | [diff] [blame] | 153 | |
| 154 | private: |
[email protected] | 1edefc4 | 2011-08-26 17:32:29 | [diff] [blame] | 155 | scoped_refptr<const Flag> flag_; |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 156 | }; |
| 157 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 158 | class BASE_EXPORT WeakReferenceOwner { |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 159 | public: |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 160 | WeakReferenceOwner(); |
| 161 | ~WeakReferenceOwner(); |
[email protected] | 59326aac | 2009-09-25 23:34:34 | [diff] [blame] | 162 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 163 | WeakReference GetRef() const; |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 164 | |
Wez | c5acb1ae | 2019-02-14 03:09:32 | [diff] [blame] | 165 | bool HasRefs() const { return !flag_->HasOneRef(); } |
[email protected] | 59326aac | 2009-09-25 23:34:34 | [diff] [blame] | 166 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 167 | void Invalidate(); |
Scott Haseley | da86547 | 2023-05-10 19:03:11 | [diff] [blame^] | 168 | void BindToCurrentSequence(); |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 169 | |
| 170 | private: |
Wez | 85ed948 | 2019-11-13 02:23:31 | [diff] [blame] | 171 | scoped_refptr<WeakReference::Flag> flag_; |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 172 | }; |
| 173 | |
[email protected] | 4c44b844 | 2012-06-15 16:36:12 | [diff] [blame] | 174 | // This class provides a common implementation of common functions that would |
| 175 | // otherwise get instantiated separately for each distinct instantiation of |
| 176 | // SupportsWeakPtr<>. |
| 177 | class SupportsWeakPtrBase { |
| 178 | public: |
| 179 | // A safe static downcast of a WeakPtr<Base> to WeakPtr<Derived>. This |
| 180 | // conversion will only compile if there is exists a Base which inherits |
| 181 | // from SupportsWeakPtr<Base>. See base::AsWeakPtr() below for a helper |
| 182 | // function that makes calling this easier. |
François Degros | 75d898c | 2017-11-17 04:03:33 | [diff] [blame] | 183 | // |
| 184 | // Precondition: t != nullptr |
[email protected] | 4c44b844 | 2012-06-15 16:36:12 | [diff] [blame] | 185 | template<typename Derived> |
| 186 | static WeakPtr<Derived> StaticAsWeakPtr(Derived* t) { |
tzik | 8df3dd0 | 2016-03-16 07:20:12 | [diff] [blame] | 187 | static_assert( |
| 188 | std::is_base_of<internal::SupportsWeakPtrBase, Derived>::value, |
| 189 | "AsWeakPtr argument must inherit from SupportsWeakPtr"); |
François Degros | 75d898c | 2017-11-17 04:03:33 | [diff] [blame] | 190 | return AsWeakPtrImpl<Derived>(t); |
[email protected] | 4c44b844 | 2012-06-15 16:36:12 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | private: |
| 194 | // This template function uses type inference to find a Base of Derived |
| 195 | // which is an instance of SupportsWeakPtr<Base>. We can then safely |
| 196 | // static_cast the Base* to a Derived*. |
| 197 | template <typename Derived, typename Base> |
François Degros | 75d898c | 2017-11-17 04:03:33 | [diff] [blame] | 198 | static WeakPtr<Derived> AsWeakPtrImpl(SupportsWeakPtr<Base>* t) { |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 199 | WeakPtr<Base> weak = t->AsWeakPtr(); |
| 200 | return WeakPtr<Derived>(weak.CloneWeakReference(), |
| 201 | static_cast<Derived*>(weak.ptr_)); |
[email protected] | 4c44b844 | 2012-06-15 16:36:12 | [diff] [blame] | 202 | } |
| 203 | }; |
| 204 | |
danakj | 45c9178 | 2021-09-29 18:23:57 | [diff] [blame] | 205 | // Forward declaration from safe_ptr.h. |
| 206 | template <typename T> |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 207 | SafeRef<T> MakeSafeRefFromWeakPtrInternals(internal::WeakReference&& ref, |
danakj | 45c9178 | 2021-09-29 18:23:57 | [diff] [blame] | 208 | T* ptr); |
| 209 | |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 210 | } // namespace internal |
| 211 | |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 212 | template <typename T> class WeakPtrFactory; |
| 213 | |
| 214 | // The WeakPtr class holds a weak reference to |T*|. |
| 215 | // |
| 216 | // This class is designed to be used like a normal pointer. You should always |
| 217 | // null-test an object of this class before using it or invoking a method that |
| 218 | // may result in the underlying object being destroyed. |
| 219 | // |
| 220 | // EXAMPLE: |
| 221 | // |
| 222 | // class Foo { ... }; |
| 223 | // WeakPtr<Foo> foo; |
| 224 | // if (foo) |
| 225 | // foo->method(); |
| 226 | // |
| 227 | template <typename T> |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 228 | class TRIVIAL_ABI WeakPtr { |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 229 | public: |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 230 | WeakPtr() = default; |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 231 | // NOLINTNEXTLINE(google-explicit-constructor) |
hans | cc65b299 | 2017-06-29 20:35:28 | [diff] [blame] | 232 | WeakPtr(std::nullptr_t) {} |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 233 | |
[email protected] | c33acdb | 2013-03-02 02:31:45 | [diff] [blame] | 234 | // Allow conversion from U to T provided U "is a" T. Note that this |
mek | 7901681 | 2016-06-24 21:29:04 | [diff] [blame] | 235 | // is separate from the (implicit) copy and move constructors. |
Alan Cutter | 6e972c8 | 2022-10-07 05:03:45 | [diff] [blame] | 236 | template <typename U, |
| 237 | typename = std::enable_if_t<std::is_convertible_v<U*, T*>>> |
| 238 | // NOLINTNEXTLINE(google-explicit-constructor) |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 239 | WeakPtr(const WeakPtr<U>& other) : ref_(other.ref_), ptr_(other.ptr_) {} |
Alan Cutter | 6e972c8 | 2022-10-07 05:03:45 | [diff] [blame] | 240 | template <typename U, |
| 241 | typename = std::enable_if_t<std::is_convertible_v<U*, T*>>> |
| 242 | // NOLINTNEXTLINE(google-explicit-constructor) |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 243 | WeakPtr& operator=(const WeakPtr<U>& other) { |
| 244 | ref_ = other.ref_; |
| 245 | ptr_ = other.ptr_; |
| 246 | return *this; |
hans | cc65b299 | 2017-06-29 20:35:28 | [diff] [blame] | 247 | } |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 248 | |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 249 | template <typename U, |
| 250 | typename = std::enable_if_t<std::is_convertible_v<U*, T*>>> |
| 251 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 252 | WeakPtr(WeakPtr<U>&& other) |
| 253 | : ref_(std::move(other.ref_)), ptr_(std::move(other.ptr_)) {} |
| 254 | template <typename U, |
| 255 | typename = std::enable_if_t<std::is_convertible_v<U*, T*>>> |
| 256 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 257 | WeakPtr& operator=(WeakPtr<U>&& other) { |
| 258 | ref_ = std::move(other.ref_); |
| 259 | ptr_ = std::move(other.ptr_); |
| 260 | return *this; |
hans | 526f714c | 2017-06-29 18:22:36 | [diff] [blame] | 261 | } |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 262 | |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 263 | T* get() const { return ref_.IsValid() ? ptr_ : nullptr; } |
| 264 | |
| 265 | // Provide access to the underlying T as a reference. Will CHECK() if the T |
| 266 | // pointee is no longer alive. |
[email protected] | f1836f4 | 2012-02-29 06:59:03 | [diff] [blame] | 267 | T& operator*() const { |
Daniel Cheng | 0b308a0 | 2020-10-13 19:17:03 | [diff] [blame] | 268 | CHECK(ref_.IsValid()); |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 269 | return *ptr_; |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 270 | } |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 271 | |
| 272 | // Used to call methods on the underlying T. Will CHECK() if the T pointee is |
| 273 | // no longer alive. |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 274 | T* operator->() const { |
Daniel Cheng | 0b308a0 | 2020-10-13 19:17:03 | [diff] [blame] | 275 | CHECK(ref_.IsValid()); |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 276 | return ptr_; |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 277 | } |
| 278 | |
wez | b9820d4 | 2016-06-22 23:41:04 | [diff] [blame] | 279 | // Allow conditionals to test validity, e.g. if (weak_ptr) {...}; |
Wez | caf863b99 | 2018-05-01 11:03:29 | [diff] [blame] | 280 | explicit operator bool() const { return get() != nullptr; } |
[email protected] | 380b139 | 2013-06-06 05:32:37 | [diff] [blame] | 281 | |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 282 | // Resets the WeakPtr to hold nothing. |
| 283 | // |
| 284 | // The `get()` method will return `nullptr` thereafter, and `MaybeValid()` |
| 285 | // will be `false`. |
| 286 | void reset() { |
| 287 | ref_.Reset(); |
| 288 | ptr_ = nullptr; |
| 289 | } |
| 290 | |
Nicolas Ouellet-payeur | ff1eab6 | 2018-07-19 18:41:50 | [diff] [blame] | 291 | // Returns false if the WeakPtr is confirmed to be invalid. This call is safe |
| 292 | // to make from any thread, e.g. to optimize away unnecessary work, but |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 293 | // RefIsValid() must always be called, on the correct sequence, before |
Nicolas Ouellet-payeur | ff1eab6 | 2018-07-19 18:41:50 | [diff] [blame] | 294 | // actually using the pointer. |
| 295 | // |
| 296 | // Warning: as with any object, this call is only thread-safe if the WeakPtr |
| 297 | // instance isn't being re-assigned or reset() racily with this call. |
| 298 | bool MaybeValid() const { return ref_.MaybeValid(); } |
| 299 | |
Trent Apted | 30f97fd | 2018-08-21 09:03:47 | [diff] [blame] | 300 | // Returns whether the object |this| points to has been invalidated. This can |
| 301 | // be used to distinguish a WeakPtr to a destroyed object from one that has |
Trent Apted | 453d0b5b | 2018-08-23 00:13:22 | [diff] [blame] | 302 | // been explicitly set to null. |
Trent Apted | 30f97fd | 2018-08-21 09:03:47 | [diff] [blame] | 303 | bool WasInvalidated() const { return ptr_ && !ref_.IsValid(); } |
| 304 | |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 305 | private: |
[email protected] | 4c44b844 | 2012-06-15 16:36:12 | [diff] [blame] | 306 | friend class internal::SupportsWeakPtrBase; |
[email protected] | c33acdb | 2013-03-02 02:31:45 | [diff] [blame] | 307 | template <typename U> friend class WeakPtr; |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 308 | friend class SupportsWeakPtr<T>; |
| 309 | friend class WeakPtrFactory<T>; |
Alan Cutter | c2d2c47 | 2022-10-20 02:14:43 | [diff] [blame] | 310 | friend class WeakPtrFactory<std::remove_const_t<T>>; |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 311 | |
danakj | cfcdfb7a | 2023-02-23 01:05:35 | [diff] [blame] | 312 | WeakPtr(internal::WeakReference&& ref, T* ptr) |
| 313 | : ref_(std::move(ref)), ptr_(ptr) { |
| 314 | DCHECK(ptr); |
| 315 | } |
| 316 | |
| 317 | internal::WeakReference CloneWeakReference() const { return ref_; } |
| 318 | |
| 319 | internal::WeakReference ref_; |
| 320 | |
| 321 | // This pointer is only valid when ref_.is_valid() is true. Otherwise, its |
| 322 | // value is undefined (as opposed to nullptr). The pointer is allowed to |
| 323 | // dangle as we verify its liveness through `ref_` before allowing access to |
| 324 | // the pointee. We don't use raw_ptr<T> here to prevent WeakPtr from keeping |
| 325 | // the memory allocation in quarantine, as it can't be accessed through the |
| 326 | // WeakPtr. |
| 327 | RAW_PTR_EXCLUSION T* ptr_ = nullptr; |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 328 | }; |
| 329 | |
wez | b9820d4 | 2016-06-22 23:41:04 | [diff] [blame] | 330 | // Allow callers to compare WeakPtrs against nullptr to test validity. |
| 331 | template <class T> |
| 332 | bool operator!=(const WeakPtr<T>& weak_ptr, std::nullptr_t) { |
| 333 | return !(weak_ptr == nullptr); |
| 334 | } |
| 335 | template <class T> |
| 336 | bool operator!=(std::nullptr_t, const WeakPtr<T>& weak_ptr) { |
| 337 | return weak_ptr != nullptr; |
| 338 | } |
| 339 | template <class T> |
| 340 | bool operator==(const WeakPtr<T>& weak_ptr, std::nullptr_t) { |
| 341 | return weak_ptr.get() == nullptr; |
| 342 | } |
| 343 | template <class T> |
| 344 | bool operator==(std::nullptr_t, const WeakPtr<T>& weak_ptr) { |
| 345 | return weak_ptr == nullptr; |
| 346 | } |
| 347 | |
hans | d2a722f | 2017-06-28 21:12:54 | [diff] [blame] | 348 | namespace internal { |
| 349 | class BASE_EXPORT WeakPtrFactoryBase { |
| 350 | protected: |
| 351 | WeakPtrFactoryBase(uintptr_t ptr); |
| 352 | ~WeakPtrFactoryBase(); |
| 353 | internal::WeakReferenceOwner weak_reference_owner_; |
| 354 | uintptr_t ptr_; |
| 355 | }; |
| 356 | } // namespace internal |
| 357 | |
[email protected] | 2c69139 | 2013-04-25 20:47:18 | [diff] [blame] | 358 | // A class may be composed of a WeakPtrFactory and thereby |
| 359 | // control how it exposes weak pointers to itself. This is helpful if you only |
| 360 | // need weak pointers within the implementation of a class. This class is also |
| 361 | // useful when working with primitive types. For example, you could have a |
| 362 | // WeakPtrFactory<bool> that is used to pass around a weak reference to a bool. |
| 363 | template <class T> |
hans | d2a722f | 2017-06-28 21:12:54 | [diff] [blame] | 364 | class WeakPtrFactory : public internal::WeakPtrFactoryBase { |
[email protected] | 2c69139 | 2013-04-25 20:47:18 | [diff] [blame] | 365 | public: |
Peter Boström | 511258be | 2021-11-03 01:18:46 | [diff] [blame] | 366 | WeakPtrFactory() = delete; |
| 367 | |
hans | d2a722f | 2017-06-28 21:12:54 | [diff] [blame] | 368 | explicit WeakPtrFactory(T* ptr) |
| 369 | : WeakPtrFactoryBase(reinterpret_cast<uintptr_t>(ptr)) {} |
[email protected] | 2c69139 | 2013-04-25 20:47:18 | [diff] [blame] | 370 | |
Peter Boström | 511258be | 2021-11-03 01:18:46 | [diff] [blame] | 371 | WeakPtrFactory(const WeakPtrFactory&) = delete; |
| 372 | WeakPtrFactory& operator=(const WeakPtrFactory&) = delete; |
| 373 | |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 374 | ~WeakPtrFactory() = default; |
[email protected] | 2c69139 | 2013-04-25 20:47:18 | [diff] [blame] | 375 | |
Alan Cutter | c2d2c47 | 2022-10-20 02:14:43 | [diff] [blame] | 376 | WeakPtr<const T> GetWeakPtr() const { |
| 377 | return WeakPtr<const T>(weak_reference_owner_.GetRef(), |
| 378 | reinterpret_cast<const T*>(ptr_)); |
| 379 | } |
Alan Cutter | 4252ed2 | 2022-10-05 09:04:19 | [diff] [blame] | 380 | |
Alan Cutter | c2d2c47 | 2022-10-20 02:14:43 | [diff] [blame] | 381 | template <int&... ExplicitArgumentBarrier, |
| 382 | typename U = T, |
| 383 | typename = std::enable_if_t<!std::is_const_v<U>>> |
Alan Cutter | 4252ed2 | 2022-10-05 09:04:19 | [diff] [blame] | 384 | WeakPtr<T> GetWeakPtr() { |
| 385 | return WeakPtr<T>(weak_reference_owner_.GetRef(), |
| 386 | reinterpret_cast<T*>(ptr_)); |
| 387 | } |
| 388 | |
Alan Cutter | c2d2c47 | 2022-10-20 02:14:43 | [diff] [blame] | 389 | template <int&... ExplicitArgumentBarrier, |
| 390 | typename U = T, |
| 391 | typename = std::enable_if_t<!std::is_const_v<U>>> |
Alan Cutter | 4252ed2 | 2022-10-05 09:04:19 | [diff] [blame] | 392 | WeakPtr<T> GetMutableWeakPtr() const { |
hans | d2a722f | 2017-06-28 21:12:54 | [diff] [blame] | 393 | return WeakPtr<T>(weak_reference_owner_.GetRef(), |
| 394 | reinterpret_cast<T*>(ptr_)); |
[email protected] | 2c69139 | 2013-04-25 20:47:18 | [diff] [blame] | 395 | } |
| 396 | |
danakj | 45c9178 | 2021-09-29 18:23:57 | [diff] [blame] | 397 | // Returns a smart pointer that is valid until the WeakPtrFactory is |
| 398 | // invalidated. Unlike WeakPtr, this smart pointer cannot be null, and cannot |
| 399 | // be checked to see if the WeakPtrFactory is invalidated. It's intended to |
| 400 | // express that the pointer will not (intentionally) outlive the `T` object it |
| 401 | // points to, and to crash safely in the case of a bug instead of causing a |
| 402 | // use-after-free. This type provides an alternative to WeakPtr to prevent |
| 403 | // use-after-free bugs without also introducing "fuzzy lifetimes" that can be |
| 404 | // checked for at runtime. |
| 405 | SafeRef<T> GetSafeRef() const { |
| 406 | return internal::MakeSafeRefFromWeakPtrInternals( |
| 407 | weak_reference_owner_.GetRef(), reinterpret_cast<T*>(ptr_)); |
| 408 | } |
| 409 | |
[email protected] | 2c69139 | 2013-04-25 20:47:18 | [diff] [blame] | 410 | // Call this method to invalidate all existing weak pointers. |
| 411 | void InvalidateWeakPtrs() { |
| 412 | DCHECK(ptr_); |
| 413 | weak_reference_owner_.Invalidate(); |
| 414 | } |
| 415 | |
| 416 | // Call this method to determine if any weak pointers exist. |
| 417 | bool HasWeakPtrs() const { |
| 418 | DCHECK(ptr_); |
| 419 | return weak_reference_owner_.HasRefs(); |
| 420 | } |
Scott Haseley | da86547 | 2023-05-10 19:03:11 | [diff] [blame^] | 421 | |
| 422 | // Rebind the factory to the current sequence. This allows creating a task |
| 423 | // queue and associated weak pointers on a different thread from the one they |
| 424 | // are used on. |
| 425 | void BindToCurrentSequence( |
| 426 | PassKey<sequence_manager::internal::TaskQueueImpl>) { |
| 427 | weak_reference_owner_.BindToCurrentSequence(); |
| 428 | } |
[email protected] | 2c69139 | 2013-04-25 20:47:18 | [diff] [blame] | 429 | }; |
| 430 | |
| 431 | // A class may extend from SupportsWeakPtr to let others take weak pointers to |
| 432 | // it. This avoids the class itself implementing boilerplate to dispense weak |
| 433 | // pointers. However, since SupportsWeakPtr's destructor won't invalidate |
| 434 | // weak pointers to the class until after the derived class' members have been |
| 435 | // destroyed, its use can lead to subtle use-after-destroy issues. |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 436 | template <class T> |
[email protected] | 4c44b844 | 2012-06-15 16:36:12 | [diff] [blame] | 437 | class SupportsWeakPtr : public internal::SupportsWeakPtrBase { |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 438 | public: |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 439 | SupportsWeakPtr() = default; |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 440 | |
Peter Boström | 75cd3c0 | 2021-09-28 15:23:18 | [diff] [blame] | 441 | SupportsWeakPtr(const SupportsWeakPtr&) = delete; |
| 442 | SupportsWeakPtr& operator=(const SupportsWeakPtr&) = delete; |
| 443 | |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 444 | WeakPtr<T> AsWeakPtr() { |
| 445 | return WeakPtr<T>(weak_reference_owner_.GetRef(), static_cast<T*>(this)); |
| 446 | } |
| 447 | |
[email protected] | cb93248 | 2012-06-26 06:23:00 | [diff] [blame] | 448 | protected: |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 449 | ~SupportsWeakPtr() = default; |
[email protected] | cb93248 | 2012-06-26 06:23:00 | [diff] [blame] | 450 | |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 451 | private: |
| 452 | internal::WeakReferenceOwner weak_reference_owner_; |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 453 | }; |
| 454 | |
[email protected] | 4c44b844 | 2012-06-15 16:36:12 | [diff] [blame] | 455 | // Helper function that uses type deduction to safely return a WeakPtr<Derived> |
| 456 | // when Derived doesn't directly extend SupportsWeakPtr<Derived>, instead it |
| 457 | // extends a Base that extends SupportsWeakPtr<Base>. |
| 458 | // |
| 459 | // EXAMPLE: |
| 460 | // class Base : public base::SupportsWeakPtr<Producer> {}; |
| 461 | // class Derived : public Base {}; |
| 462 | // |
| 463 | // Derived derived; |
| 464 | // base::WeakPtr<Derived> ptr = base::AsWeakPtr(&derived); |
| 465 | // |
| 466 | // Note that the following doesn't work (invalid type conversion) since |
| 467 | // Derived::AsWeakPtr() is WeakPtr<Base> SupportsWeakPtr<Base>::AsWeakPtr(), |
| 468 | // and there's no way to safely cast WeakPtr<Base> to WeakPtr<Derived> at |
| 469 | // the caller. |
| 470 | // |
| 471 | // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. |
| 472 | |
| 473 | template <typename Derived> |
| 474 | WeakPtr<Derived> AsWeakPtr(Derived* t) { |
| 475 | return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); |
| 476 | } |
| 477 | |
[email protected] | 3125d646 | 2009-09-01 20:50:17 | [diff] [blame] | 478 | } // namespace base |
| 479 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 480 | #endif // BASE_MEMORY_WEAK_PTR_H_ |