diff options
Diffstat (limited to 'base/memory/weak_ptr.h')
-rw-r--r-- | base/memory/weak_ptr.h | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/base/memory/weak_ptr.h b/base/memory/weak_ptr.h index abda1af..bdfc308 100644 --- a/base/memory/weak_ptr.h +++ b/base/memory/weak_ptr.h @@ -7,6 +7,15 @@ // bound to the lifetime of the referrers. In other words, this is useful when // reference counting is not a good fit. // +// Thread-safety notes: +// When you get a WeakPtr (from a WeakPtrFactory or SupportsWeakPtr), +// the WeakPtr becomes bound to the current thread. You may only +// dereference the WeakPtr on that thread. However, it is safe to +// destroy the WeakPtr object on another thread. +// Since a WeakPtr object may be destroyed on a background thread, +// querying WeakPtrFactory's HasWeakPtrs() method can be racy. +// +// // A common alternative to weak pointers is to have the shared object hold a // list of all referrers, and then when the shared object is destroyed, it // calls a method on the referrers to tell them to drop their references. This @@ -45,8 +54,6 @@ // dereferencing the Controller back pointer after the Controller has been // destroyed. // -// WARNING: weak pointers are not threadsafe!!! You must only use a WeakPtr -// instance on thread where it was created. #ifndef BASE_MEMORY_WEAK_PTR_H_ #define BASE_MEMORY_WEAK_PTR_H_ @@ -69,7 +76,7 @@ class BASE_EXPORT WeakReference { // via base::WeakPtr::~WeakPtr(). class Flag : public RefCountedThreadSafe<Flag> { public: - explicit Flag(Flag** handle); + Flag(); void Invalidate(); bool IsValid() const; @@ -82,17 +89,17 @@ class BASE_EXPORT WeakReference { ~Flag(); ThreadChecker thread_checker_; - Flag** handle_; + bool is_valid_; }; WeakReference(); - WeakReference(Flag* flag); + explicit WeakReference(const Flag* flag); ~WeakReference(); bool is_valid() const; private: - scoped_refptr<Flag> flag_; + scoped_refptr<const Flag> flag_; }; class BASE_EXPORT WeakReferenceOwner { @@ -103,7 +110,7 @@ class BASE_EXPORT WeakReferenceOwner { WeakReference GetRef() const; bool HasRefs() const { - return flag_ != NULL; + return flag_.get() && !flag_->HasOneRef(); } void Invalidate(); @@ -114,7 +121,7 @@ class BASE_EXPORT WeakReferenceOwner { } private: - mutable WeakReference::Flag* flag_; + mutable scoped_refptr<WeakReference::Flag> flag_; }; // This class simplifies the implementation of WeakPtr's type conversion |