diff options
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_; |