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

Unified Diff: base/memory/ref_counted.cc

Issue 2666423002: Assert sequence validity on non-thread-safe RefCount manipulations (2) (Closed)
Patch Set: rebase on fonts_fix CL Created 3 years, 10 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
Index: base/memory/ref_counted.cc
diff --git a/base/memory/ref_counted.cc b/base/memory/ref_counted.cc
index cd6181b147349e46c4a86b7487e952d4725d1f91..2471238fd33eee4f5979f4b6dd60676d94e52bb7 100644
--- a/base/memory/ref_counted.cc
+++ b/base/memory/ref_counted.cc
@@ -3,9 +3,17 @@
// found in the LICENSE file.
#include "base/memory/ref_counted.h"
+
#include "base/threading/thread_collision_warner.h"
namespace base {
+namespace {
+
+#if DCHECK_IS_ON()
+AtomicRefCount g_cross_thread_ref_count_access_allow_count = 0;
+#endif
+
+} // namespace
namespace subtle {
@@ -48,6 +56,42 @@ bool RefCountedThreadSafeBase::Release() const {
return false;
}
+RefCountedBase::RefCountedBase() : ref_count_(0) {
+ DetachFromSequence();
+}
+
+RefCountedBase::~RefCountedBase() {
+#if DCHECK_IS_ON()
+ DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()";
+#endif
+}
gab 2017/02/16 21:38:46 Keep these inline (they're no-ops in non-dcheck bu
tzik 2017/02/17 11:36:08 Done.
+
+void RefCountedBase::DetachFromSequence() const {
+#if DCHECK_IS_ON()
+ sequence_checker_.DetachFromSequence();
+#endif
+}
+
+bool RefCountedBase::CalledOnValidSequence() const {
+#if DCHECK_IS_ON()
+ if (!AtomicRefCountIsZero(&g_cross_thread_ref_count_access_allow_count))
+ return true;
+ return sequence_checker_.CalledOnValidSequence();
gab 2017/02/16 21:38:46 Merge if into this call: return CalledOnValidSequ
tzik 2017/02/17 11:36:08 Done.
+#else
+ return true;
+#endif
+}
+
} // namespace subtle
+#if DCHECK_IS_ON()
+ScopedAllowCrossThreadRefCountAccess::ScopedAllowCrossThreadRefCountAccess() {
+ AtomicRefCountInc(&g_cross_thread_ref_count_access_allow_count);
+}
+
+ScopedAllowCrossThreadRefCountAccess::~ScopedAllowCrossThreadRefCountAccess() {
+ AtomicRefCountDec(&g_cross_thread_ref_count_access_allow_count);
+}
+#endif
+
} // namespace base

Powered by Google App Engine
This is Rietveld 408576698