Chromium Code Reviews| Index: base/memory/weak_ptr.h |
| diff --git a/base/memory/weak_ptr.h b/base/memory/weak_ptr.h |
| index abda1af2eaa7e0abf54a9a79bd0d9f7a4a526538..87fe29fb19b5c6763257c9bc5a070aa53a8f55df 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); |
| + explicit Flag(); |
|
awong
2011/08/18 23:58:19
nit: no need for explicit on 0-arity constructors.
|
| 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); |
| + WeakReference(const Flag* flag); |
|
awong
2011/08/18 23:58:19
this should have explicit on it though I think.
|
| ~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 |