// Copyright 2012 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Weak pointers are pointers to an object that do not affect its lifetime, // and which may be invalidated (i.e. reset to nullptr) by the object, or its // owner, at any time, most commonly when the object is about to be deleted. // Weak pointers are useful when an object needs to be accessed safely by one // or more objects other than its owner, and those callers can cope with the // object vanishing and e.g. tasks posted to it being silently dropped. // Reference-counting such an object would complicate the ownership graph and // make it harder to reason about the object's lifetime. // EXAMPLE: // // class Controller { // public: // void SpawnWorker() { Worker::StartNew(weak_factory_.GetWeakPtr()); } // void WorkComplete(const Result& result) { ... } // private: // // Member variables should appear before the WeakPtrFactory, to ensure // // that any WeakPtrs to Controller are invalidated before its members // // variable's destructors are executed, rendering them invalid. // WeakPtrFactory<Controller> weak_factory_{this}; // }; // // class Worker { // public: // static void StartNew(WeakPtr<Controller> controller) { // // Move WeakPtr when possible to avoid atomic refcounting churn on its // // internal state. // Worker* worker = new Worker(std::move(controller)); // // Kick off asynchronous processing... // } // private: // Worker(WeakPtr<Controller> controller) // : controller_(std::move(controller)) {} // void DidCompleteAsynchronousProcessing(const Result& result) { // if (controller_) // controller_->WorkComplete(result); // } // WeakPtr<Controller> controller_; // }; // // With this implementation a caller may use SpawnWorker() to dispatch multiple // Workers and subsequently delete the Controller, without waiting for all // Workers to have completed. // ------------------------- IMPORTANT: Thread-safety ------------------------- // Weak pointers may be passed safely between sequences, but must always be // dereferenced and invalidated on the same SequencedTaskRunner otherwise // checking the pointer would be racey. // // To ensure correct use, the first time a WeakPtr issued by a WeakPtrFactory // is dereferenced, the factory and its WeakPtrs become bound to the calling // sequence or current SequencedWorkerPool token, and cannot be dereferenced or // invalidated on any other task runner. Bound WeakPtrs can still be handed // off to other task runners, e.g. to use to post tasks back to object on the // bound sequence. // // If all WeakPtr objects are destroyed or invalidated then the factory is // unbound from the SequencedTaskRunner/Thread. The WeakPtrFactory may then be // destroyed, or new WeakPtr objects may be used, from a different sequence. // // Thus, at least one WeakPtr object must exist and have been dereferenced on // the correct sequence to enforce that other WeakPtr objects will enforce they // are used on the desired sequence. #ifndef BASE_MEMORY_WEAK_PTR_H_ #define BASE_MEMORY_WEAK_PTR_H_ #include <cstddef> #include <type_traits> #include <utility> #include "base/base_export.h" #include "base/check.h" #include "base/compiler_specific.h" #include "base/dcheck_is_on.h" #include "base/memory/raw_ptr.h" #include "base/memory/ref_counted.h" #include "base/memory/safe_ref_traits.h" #include "base/sequence_checker.h" #include "base/synchronization/atomic_flag.h" namespace performance_manager { class FrameNodeImpl; class PageNodeImpl; class ProcessNodeImpl; class WorkerNodeImpl; } // namespace performance_manager namespace base { namespace sequence_manager::internal { class TaskQueueImpl; } template <typename T> class WeakPtr; namespace internal { // These classes are part of the WeakPtr implementation. // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. class BASE_EXPORT TRIVIAL_ABI WeakReference { … }; class BASE_EXPORT WeakReferenceOwner { … }; // Forward declaration from safe_ptr.h. template <typename T> SafeRef<T> MakeSafeRefFromWeakPtrInternals(internal::WeakReference&& ref, T* ptr); } // namespace internal template <typename T> class WeakPtrFactory; // The WeakPtr class holds a weak reference to |T*|. // // This class is designed to be used like a normal pointer. You should always // null-test an object of this class before using it or invoking a method that // may result in the underlying object being destroyed. // // EXAMPLE: // // class Foo { ... }; // WeakPtr<Foo> foo; // if (foo) // foo->method(); // template <typename T> class TRIVIAL_ABI WeakPtr { … }; // Allow callers to compare WeakPtrs against nullptr to test validity. template <class T> bool operator!=(const WeakPtr<T>& weak_ptr, std::nullptr_t) { … } template <class T> bool operator!=(std::nullptr_t, const WeakPtr<T>& weak_ptr) { … } template <class T> bool operator==(const WeakPtr<T>& weak_ptr, std::nullptr_t) { … } template <class T> bool operator==(std::nullptr_t, const WeakPtr<T>& weak_ptr) { … } namespace internal { class BASE_EXPORT WeakPtrFactoryBase { … }; } // namespace internal namespace subtle { // Restricts access to WeakPtrFactory::BindToCurrentSequence() to authorized // callers. class BASE_EXPORT BindWeakPtrFactoryPassKey { … }; } // namespace subtle // A class may be composed of a WeakPtrFactory and thereby // control how it exposes weak pointers to itself. This is helpful if you only // need weak pointers within the implementation of a class. This class is also // useful when working with primitive types. For example, you could have a // WeakPtrFactory<bool> that is used to pass around a weak reference to a bool. template <class T> class WeakPtrFactory : public internal::WeakPtrFactoryBase { … }; } // namespace base #endif // BASE_MEMORY_WEAK_PTR_H_