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

Unified Diff: base/memory/ref_counted.h

Issue 1004593005: base::RefCounted now DCHECKs when referenced from multiple threads. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix lots of tests using UnsafeRefCounted. Created 5 years, 9 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
« no previous file with comments | « base/deferred_sequenced_task_runner_unittest.cc ('k') | components/nacl/loader/nacl_ipc_adapter.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
« no previous file with comments | « base/deferred_sequenced_task_runner_unittest.cc ('k') | components/nacl/loader/nacl_ipc_adapter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698