Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2931)

Unified Diff: base/memory/weak_ptr.cc

Issue 14299011: Remove all but one use of WeakPtrFactory::DetachFromThread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Simplify change to IOThread. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/memory/weak_ptr.cc
diff --git a/base/memory/weak_ptr.cc b/base/memory/weak_ptr.cc
index 9dec8fd6db9c29956673a79326efa4b22af2f3ad..321b0576b2555ed995ac8a39c74bd40d944f155f 100644
--- a/base/memory/weak_ptr.cc
+++ b/base/memory/weak_ptr.cc
@@ -8,17 +8,23 @@ namespace base {
namespace internal {
WeakReference::Flag::Flag() : is_valid_(true) {
+ // 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 thread.
+ thread_checker_.DetachFromThread();
}
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(thread_checker_.CalledOnValidThread() || HasOneRef());
+ DCHECK(thread_checker_.CalledOnValidThread() || HasOneRef())
+ << "WeakPtrs must be checked and invalidated on the same thread.";
is_valid_ = false;
}
bool WeakReference::Flag::IsValid() const {
- DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(thread_checker_.CalledOnValidThread())
+ << "WeakPtrs must be checked and invalidated on the same thread.";
return is_valid_;
}
@@ -46,10 +52,10 @@ WeakReferenceOwner::~WeakReferenceOwner() {
}
WeakReference WeakReferenceOwner::GetRef() const {
- // We also want to reattach to the current thread if all previous references
- // have gone away.
+ // If we hold the last reference to the Flag then create a new one.
if (!HasRefs())
flag_ = new WeakReference::Flag();
+
return WeakReference(flag_);
}

Powered by Google App Engine
This is Rietveld 408576698