diff options
author | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-11 20:47:25 +0000 |
---|---|---|
committer | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-11 20:47:25 +0000 |
commit | 05b1cd610660a4481bcf0ee0cdee740ed4ee76ee (patch) | |
tree | e00a73f40db0c77648a6976e4d91c6c6f908213b /base | |
parent | b2f9016277dd867f7d2cd1c7b0be3e3d6ba2bfa3 (diff) | |
download | chromium_src-05b1cd610660a4481bcf0ee0cdee740ed4ee76ee.zip chromium_src-05b1cd610660a4481bcf0ee0cdee740ed4ee76ee.tar.gz chromium_src-05b1cd610660a4481bcf0ee0cdee740ed4ee76ee.tar.bz2 |
Fix base::internal::WeakReference::Flag to be RefCountedThreadSafe.
This allows a base::WeakPtr to be deleted on a different thread, which will modify the refcount on Flag.
BUG=none
TEST=none
Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=81142
Review URL: http://codereview.chromium.org/6826034
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@81148 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/memory/weak_ptr.cc | 24 | ||||
-rw-r--r-- | base/memory/weak_ptr.h | 23 |
2 files changed, 23 insertions, 24 deletions
diff --git a/base/memory/weak_ptr.cc b/base/memory/weak_ptr.cc index fb09219..30c777c 100644 --- a/base/memory/weak_ptr.cc +++ b/base/memory/weak_ptr.cc @@ -10,31 +10,21 @@ namespace internal { WeakReference::Flag::Flag(Flag** handle) : handle_(handle) { } -WeakReference::Flag::~Flag() { - if (handle_) - *handle_ = NULL; -} - -void WeakReference::Flag::AddRef() const { - DCHECK(CalledOnValidThread()); - RefCounted<Flag>::AddRef(); -} - -void WeakReference::Flag::Release() const { - DCHECK(CalledOnValidThread()); - RefCounted<Flag>::Release(); -} - void WeakReference::Flag::Invalidate() { - DCHECK(CalledOnValidThread()); + DCHECK(thread_checker_.CalledOnValidThread()); handle_ = NULL; } bool WeakReference::Flag::IsValid() const { - DCHECK(CalledOnValidThread()); + DCHECK(thread_checker_.CalledOnValidThread()); return handle_ != NULL; } +WeakReference::Flag::~Flag() { + if (handle_) + *handle_ = NULL; +} + WeakReference::WeakReference() { } diff --git a/base/memory/weak_ptr.h b/base/memory/weak_ptr.h index edb9d93..0c6b4a7 100644 --- a/base/memory/weak_ptr.h +++ b/base/memory/weak_ptr.h @@ -55,7 +55,7 @@ #include "base/base_api.h" #include "base/logging.h" #include "base/memory/ref_counted.h" -#include "base/threading/non_thread_safe.h" +#include "base/threading/thread_checker.h" namespace base { @@ -65,19 +65,23 @@ namespace internal { class BASE_API WeakReference { public: - class Flag : public RefCounted<Flag>, public base::NonThreadSafe { + // While Flag is bound to a specific thread, it may be deleted from another + // via base::WeakPtr::~WeakPtr(). + class Flag : public RefCountedThreadSafe<Flag> { public: - Flag(Flag** handle); - ~Flag(); + explicit Flag(Flag** handle); - void AddRef() const; - void Release() const; void Invalidate(); bool IsValid() const; - void DetachFromThread() { base::NonThreadSafe::DetachFromThread(); } + void DetachFromThread() { thread_checker_.DetachFromThread(); } private: + friend class base::RefCountedThreadSafe<Flag>; + + ~Flag(); + + ThreadChecker thread_checker_; Flag** handle_; }; @@ -235,6 +239,11 @@ class WeakPtrFactory { return weak_reference_owner_.HasRefs(); } + // Indicates that this object will be used on another thread from now on. + void DetachFromThread() { + weak_reference_owner_.DetachFromThread(); + } + private: internal::WeakReferenceOwner weak_reference_owner_; T* ptr_; |