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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/memory/ref_counted.h" 5 #include "base/memory/ref_counted.h"
6
6 #include "base/threading/thread_collision_warner.h" 7 #include "base/threading/thread_collision_warner.h"
7 8
8 namespace base { 9 namespace base {
10 namespace {
11
12 #if DCHECK_IS_ON()
13 AtomicRefCount g_cross_thread_ref_count_access_allow_count = 0;
14 #endif
15
16 } // namespace
9 17
10 namespace subtle { 18 namespace subtle {
11 19
12 bool RefCountedThreadSafeBase::HasOneRef() const { 20 bool RefCountedThreadSafeBase::HasOneRef() const {
13 return AtomicRefCountIsOne( 21 return AtomicRefCountIsOne(
14 &const_cast<RefCountedThreadSafeBase*>(this)->ref_count_); 22 &const_cast<RefCountedThreadSafeBase*>(this)->ref_count_);
15 } 23 }
16 24
17 RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) { 25 RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) {
18 #if DCHECK_IS_ON() 26 #if DCHECK_IS_ON()
(...skipping 22 matching lines...) Expand all
41 #endif 49 #endif
42 if (!AtomicRefCountDec(&ref_count_)) { 50 if (!AtomicRefCountDec(&ref_count_)) {
43 #if DCHECK_IS_ON() 51 #if DCHECK_IS_ON()
44 in_dtor_ = true; 52 in_dtor_ = true;
45 #endif 53 #endif
46 return true; 54 return true;
47 } 55 }
48 return false; 56 return false;
49 } 57 }
50 58
59 RefCountedBase::RefCountedBase() : ref_count_(0) {
60 DetachFromSequence();
61 }
62
63 RefCountedBase::~RefCountedBase() {
64 #if DCHECK_IS_ON()
65 DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()";
66 #endif
67 }
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.
68
69 void RefCountedBase::DetachFromSequence() const {
70 #if DCHECK_IS_ON()
71 sequence_checker_.DetachFromSequence();
72 #endif
73 }
74
75 bool RefCountedBase::CalledOnValidSequence() const {
76 #if DCHECK_IS_ON()
77 if (!AtomicRefCountIsZero(&g_cross_thread_ref_count_access_allow_count))
78 return true;
79 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.
80 #else
81 return true;
82 #endif
83 }
84
51 } // namespace subtle 85 } // namespace subtle
52 86
87 #if DCHECK_IS_ON()
88 ScopedAllowCrossThreadRefCountAccess::ScopedAllowCrossThreadRefCountAccess() {
89 AtomicRefCountInc(&g_cross_thread_ref_count_access_allow_count);
90 }
91
92 ScopedAllowCrossThreadRefCountAccess::~ScopedAllowCrossThreadRefCountAccess() {
93 AtomicRefCountDec(&g_cross_thread_ref_count_access_allow_count);
94 }
95 #endif
96
53 } // namespace base 97 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698