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

Unified Diff: base/memory/weak_ptr.cc

Issue 7677028: Make WeakPtr thread-safe, i.e. allow cross-thread copying of WeakPtr (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 4 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
« base/memory/weak_ptr.h ('K') | « 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 30c777c00cf1b373e17b6c811e9aaf71fab86f11..bd2fb6497f04fe23d5440eaf432a672cee99a82d 100644
--- a/base/memory/weak_ptr.cc
+++ b/base/memory/weak_ptr.cc
@@ -7,28 +7,26 @@
namespace base {
namespace internal {
-WeakReference::Flag::Flag(Flag** handle) : handle_(handle) {
+WeakReference::Flag::Flag() : valid_(true) {
}
void WeakReference::Flag::Invalidate() {
DCHECK(thread_checker_.CalledOnValidThread());
- handle_ = NULL;
+ valid_ = false;
}
bool WeakReference::Flag::IsValid() const {
DCHECK(thread_checker_.CalledOnValidThread());
- return handle_ != NULL;
+ return valid_;
}
WeakReference::Flag::~Flag() {
- if (handle_)
- *handle_ = NULL;
}
WeakReference::WeakReference() {
}
-WeakReference::WeakReference(Flag* flag) : flag_(flag) {
+WeakReference::WeakReference(scoped_refptr<Flag> flag) : flag_(flag) {
}
WeakReference::~WeakReference() {
@@ -46,8 +44,10 @@ WeakReferenceOwner::~WeakReferenceOwner() {
}
WeakReference WeakReferenceOwner::GetRef() const {
- if (!flag_)
- flag_ = new WeakReference::Flag(&flag_);
+ // We also want to reattach to the current thread if all previous references
+ // have gone away.
+ if (!flag_ || flag_->HasOneRef())
darin (slow to review) 2011/08/18 18:29:06 perhaps it would be better to reuse the HasRefs()
willchan no longer on Chromium 2011/08/18 18:49:52 Sorry, it's not clear to me how this is safe. If
+ flag_ = new WeakReference::Flag();
return WeakReference(flag_);
}
« base/memory/weak_ptr.h ('K') | « base/memory/weak_ptr.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698