Index: base/memory/weak_ptr.cc |
diff --git a/base/memory/weak_ptr.cc b/base/memory/weak_ptr.cc |
index c179b80097cc674e1ad2e484b99909166dda860a..53c63f70ca38d0855b83b6cceed9c4d3f4aed0e5 100644 |
--- a/base/memory/weak_ptr.cc |
+++ b/base/memory/weak_ptr.cc |
@@ -7,47 +7,49 @@ |
namespace base { |
namespace internal { |
-WeakReference::Flag::Flag() : is_valid_(true) { |
+static constexpr uintptr_t kTrueMask = ~static_cast<uintptr_t>(0); |
+ |
+WeakReference::Flag::Flag() : is_valid_(kTrueMask) { |
+#if DCHECK_IS_ON() |
// Flags only become bound when checked for validity, or invalidated, |
// so that we can check that later validity/invalidation operations on |
// the same Flag take place on the same sequenced thread. |
sequence_checker_.DetachFromSequence(); |
+#endif |
} |
-void WeakReference::Flag::Invalidate() { |
- // The flag being invalidated with a single ref implies that there are no |
- // weak pointers in existence. Allow deletion on other thread in this case. |
- DCHECK(sequence_checker_.CalledOnValidSequence() || HasOneRef()) |
- << "WeakPtrs must be invalidated on the same sequenced thread."; |
- is_valid_ = false; |
-} |
+WeakReference::Flag::Flag(WeakReference::Flag::NullFlagTag) : is_valid_(false) { |
+ // There is no need for sequence_checker_.DetachFromSequence() because the |
+ // null flag doesn't participate in the sequence checks. See DCHECK in |
+ // Invalidate() and IsValid(). |
-bool WeakReference::Flag::IsValid() const { |
- DCHECK(sequence_checker_.CalledOnValidSequence()) |
- << "WeakPtrs must be checked on the same sequenced thread."; |
- return is_valid_; |
+ // Keep the object alive perpetually, even when there are no references to it. |
+ AddRef(); |
} |
-WeakReference::Flag::~Flag() { |
+WeakReference::Flag* WeakReference::Flag::NullFlag() { |
+ static Flag* g_null_flag = new Flag(kNullFlagTag); |
+ return g_null_flag; |
} |
-WeakReference::WeakReference() { |
-} |
+WeakReference::Flag::~Flag() {} |
-WeakReference::WeakReference(const Flag* flag) : flag_(flag) { |
-} |
+WeakReference::WeakReference() : flag_(Flag::NullFlag()) {} |
WeakReference::~WeakReference() { |
} |
-WeakReference::WeakReference(WeakReference&& other) = default; |
+WeakReference::WeakReference(const Flag* flag) : flag_(flag) {} |
-WeakReference::WeakReference(const WeakReference& other) = default; |
+WeakReference::WeakReference(WeakReference&& other) |
+ : flag_(std::move(other.flag_)) { |
+ other.flag_ = Flag::NullFlag(); |
+} |
-bool WeakReference::is_valid() const { return flag_.get() && flag_->IsValid(); } |
+WeakReference::WeakReference(const WeakReference& other) = default; |
-WeakReferenceOwner::WeakReferenceOwner() { |
-} |
+WeakReferenceOwner::WeakReferenceOwner() |
+ : flag_(WeakReference::Flag::NullFlag()) {} |
WeakReferenceOwner::~WeakReferenceOwner() { |
Invalidate(); |
@@ -62,10 +64,8 @@ WeakReference WeakReferenceOwner::GetRef() const { |
} |
void WeakReferenceOwner::Invalidate() { |
- if (flag_.get()) { |
- flag_->Invalidate(); |
- flag_ = NULL; |
- } |
+ flag_->Invalidate(); |
+ flag_ = WeakReference::Flag::NullFlag(); |
} |
WeakPtrBase::WeakPtrBase() { |