Commit c5acb1ae authored by Wez's avatar Wez Committed by Commit Bot

[WeakPtr] Always retain a Flag in WeakReferenceOwner.

Rather than nulling the WeakReferenceOwner's |flag_| field when it is
invalidated, recreate the Flag so that there is always a valid one.

This is a precursor to requiring manual re-creation of the Flag in order
to create new WeakPtrs after having called InvalidateWeakPtrs().

Bug: 927987
Change-Id: Idbaa89c288a1b61cd22d4a7039aa60a5ad916a24
Reviewed-on: https://chromium-review.googlesource.com/c/1460491
Commit-Queue: Wez <wez@chromium.org>
Reviewed-by: default avatarAlbert J. Wong <ajwong@chromium.org>
Cr-Commit-Position: refs/heads/master@{#632060}
parent eeca74b9
...@@ -34,6 +34,10 @@ bool WeakReference::Flag::MaybeValid() const { ...@@ -34,6 +34,10 @@ bool WeakReference::Flag::MaybeValid() const {
return !invalidated_.IsSet(); return !invalidated_.IsSet();
} }
void WeakReference::Flag::DetachFromSequence() {
DETACH_FROM_SEQUENCE(sequence_checker_);
}
WeakReference::Flag::~Flag() = default; WeakReference::Flag::~Flag() = default;
WeakReference::WeakReference() = default; WeakReference::WeakReference() = default;
...@@ -54,25 +58,24 @@ bool WeakReference::MaybeValid() const { ...@@ -54,25 +58,24 @@ bool WeakReference::MaybeValid() const {
return flag_ && flag_->MaybeValid(); return flag_ && flag_->MaybeValid();
} }
WeakReferenceOwner::WeakReferenceOwner() = default; WeakReferenceOwner::WeakReferenceOwner()
: flag_(MakeRefCounted<WeakReference::Flag>()) {}
WeakReferenceOwner::~WeakReferenceOwner() { WeakReferenceOwner::~WeakReferenceOwner() {
Invalidate(); flag_->Invalidate();
} }
WeakReference WeakReferenceOwner::GetRef() const { WeakReference WeakReferenceOwner::GetRef() const {
// If we hold the last reference to the Flag then create a new one. // If we hold the last reference to the Flag then detach the SequenceChecker.
if (!HasRefs()) if (!HasRefs())
flag_ = new WeakReference::Flag(); flag_->DetachFromSequence();
return WeakReference(flag_); return WeakReference(flag_);
} }
void WeakReferenceOwner::Invalidate() { void WeakReferenceOwner::Invalidate() {
if (flag_) {
flag_->Invalidate(); flag_->Invalidate();
flag_ = nullptr; flag_ = MakeRefCounted<WeakReference::Flag>();
}
} }
WeakPtrBase::WeakPtrBase() : ptr_(0) {} WeakPtrBase::WeakPtrBase() : ptr_(0) {}
......
...@@ -102,6 +102,8 @@ class BASE_EXPORT WeakReference { ...@@ -102,6 +102,8 @@ class BASE_EXPORT WeakReference {
bool MaybeValid() const; bool MaybeValid() const;
void DetachFromSequence();
private: private:
friend class base::RefCountedThreadSafe<Flag>; friend class base::RefCountedThreadSafe<Flag>;
...@@ -134,7 +136,7 @@ class BASE_EXPORT WeakReferenceOwner { ...@@ -134,7 +136,7 @@ class BASE_EXPORT WeakReferenceOwner {
WeakReference GetRef() const; WeakReference GetRef() const;
bool HasRefs() const { return flag_ && !flag_->HasOneRef(); } bool HasRefs() const { return !flag_->HasOneRef(); }
void Invalidate(); void Invalidate();
...@@ -223,7 +225,6 @@ template <typename T> ...@@ -223,7 +225,6 @@ template <typename T>
class WeakPtr : public internal::WeakPtrBase { class WeakPtr : public internal::WeakPtrBase {
public: public:
WeakPtr() = default; WeakPtr() = default;
WeakPtr(std::nullptr_t) {} WeakPtr(std::nullptr_t) {}
// Allow conversion from U to T provided U "is a" T. Note that this // Allow conversion from U to T provided U "is a" T. Note that this
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment