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

Unified Diff: base/memory/weak_ptr.cc

Issue 692233003: Base: Bind SupportsWeakPtr's references to the constructor's thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month 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
« no previous file with comments | « base/memory/weak_ptr.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/weak_ptr.cc
diff --git a/base/memory/weak_ptr.cc b/base/memory/weak_ptr.cc
index d9ce86ad18901a0719255c031fae1a1357ad1f2f..2eaa6ea4cfc6f7ee97eb426a9ee0650a5ccfbdc3 100644
--- a/base/memory/weak_ptr.cc
+++ b/base/memory/weak_ptr.cc
@@ -7,11 +7,13 @@
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 sequenced thread.
- sequence_checker_.DetachFromSequence();
+WeakReference::Flag::Flag(bool bound_to_current_sequence) : is_valid_(true) {
+ if (!bound_to_current_sequence) {
+ // 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();
+ }
}
void WeakReference::Flag::Invalidate() {
@@ -42,7 +44,10 @@ WeakReference::~WeakReference() {
bool WeakReference::is_valid() const { return flag_.get() && flag_->IsValid(); }
-WeakReferenceOwner::WeakReferenceOwner() {
+WeakReferenceOwner::WeakReferenceOwner(bool bound_to_current_sequence)
+ : bound_to_constructor_sequence_(bound_to_current_sequence) {
+ if (bound_to_current_sequence)
+ flag_ = new WeakReference::Flag(bound_to_current_sequence);
}
WeakReferenceOwner::~WeakReferenceOwner() {
@@ -50,9 +55,10 @@ WeakReferenceOwner::~WeakReferenceOwner() {
}
WeakReference WeakReferenceOwner::GetRef() const {
- // If we hold the last reference to the Flag then create a new one.
- if (!HasRefs())
- flag_ = new WeakReference::Flag();
+ // If not bound to the constructor sequence, and there are no outstanding weak
+ // pointers, recreate flag to allow re-binding to a different sequence.
+ if (!bound_to_constructor_sequence_ && !HasRefs())
+ flag_ = new WeakReference::Flag(false /* bound_to_constructor_sequence_ */);
return WeakReference(flag_.get());
}
« no previous file with comments | « base/memory/weak_ptr.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698