Index: base/memory/ref_counted.h |
diff --git a/base/memory/ref_counted.h b/base/memory/ref_counted.h |
index 219437ed8d82b8b35835a6c3297d5f719f14c277..4be15840371e3da4ee65193ee069d8909426e1fe 100644 |
--- a/base/memory/ref_counted.h |
+++ b/base/memory/ref_counted.h |
@@ -14,6 +14,7 @@ |
#ifndef NDEBUG |
#include "base/logging.h" |
#endif |
+#include "base/threading/thread_checker.h" |
#include "base/threading/thread_collision_warner.h" |
#include "build/build_config.h" |
@@ -119,16 +120,21 @@ class BASE_EXPORT RefCountedThreadSafeBase { |
// |
// You should always make your destructor private, to avoid any code deleting |
// the object accidently while there are references to it. |
+// |
+// RefCounted objects are not thread-safe. To use a ref-counted object from more |
+// than one thead, use a RefCountedThreadSafe. |
template <class T> |
class RefCounted : public subtle::RefCountedBase { |
public: |
RefCounted() {} |
void AddRef() const { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
subtle::RefCountedBase::AddRef(); |
} |
void Release() const { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
if (subtle::RefCountedBase::Release()) { |
delete static_cast<const T*>(this); |
} |
@@ -138,9 +144,34 @@ class RefCounted : public subtle::RefCountedBase { |
~RefCounted() {} |
private: |
+ base::ThreadChecker thread_checker_; |
+ |
DISALLOW_COPY_AND_ASSIGN(RefCounted<T>); |
}; |
+// Same as RefCounted, but does not enforce thread safety. Subclasses of |
+// UnsafeRefCounted probably have race conditions and should be fixed up, either |
+// by ensuring they are only referenced from one thread (and moving to |
+// RefCounted), or by moving to RefCountedThreadSafe. |
+template <class T> |
+class UnsafeRefCounted : public subtle::RefCountedBase { |
+ public: |
+ UnsafeRefCounted() {} |
+ |
+ void AddRef() const { subtle::RefCountedBase::AddRef(); } |
+ |
+ void Release() const { |
+ if (subtle::RefCountedBase::Release()) |
+ delete static_cast<const T*>(this); |
+ } |
+ |
+ protected: |
+ ~UnsafeRefCounted() {} |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(UnsafeRefCounted<T>); |
+}; |
+ |
// Forward declaration. |
template <class T, typename Traits> class RefCountedThreadSafe; |