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

Side by Side Diff: base/memory/ref_counted.h

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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #ifndef BASE_MEMORY_REF_COUNTED_H_ 5 #ifndef BASE_MEMORY_REF_COUNTED_H_
6 #define BASE_MEMORY_REF_COUNTED_H_ 6 #define BASE_MEMORY_REF_COUNTED_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <cassert> 10 #include <cassert>
11 #include <iosfwd> 11 #include <iosfwd>
12 #include <type_traits> 12 #include <type_traits>
13 13
14 #include "base/atomic_ref_count.h" 14 #include "base/atomic_ref_count.h"
15 #include "base/base_export.h" 15 #include "base/base_export.h"
16 #include "base/compiler_specific.h" 16 #include "base/compiler_specific.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/macros.h" 18 #include "base/macros.h"
19 #include "base/sequence_checker.h"
19 #include "base/threading/thread_collision_warner.h" 20 #include "base/threading/thread_collision_warner.h"
20 #include "build/build_config.h" 21 #include "build/build_config.h"
21 22
22 namespace base { 23 namespace base {
23
24 namespace subtle { 24 namespace subtle {
25 25
26 class BASE_EXPORT RefCountedBase { 26 class BASE_EXPORT RefCountedBase {
27 public: 27 public:
28 bool HasOneRef() const { return ref_count_ == 1; } 28 bool HasOneRef() const { return ref_count_ == 1; }
29 29
30 protected: 30 protected:
31 RefCountedBase() 31 RefCountedBase();
32 : ref_count_(0) 32 ~RefCountedBase();
33 #if DCHECK_IS_ON()
34 , in_dtor_(false)
35 #endif
36 {
37 }
38
39 ~RefCountedBase() {
40 #if DCHECK_IS_ON()
41 DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()";
42 #endif
43 }
44
45 33
46 void AddRef() const { 34 void AddRef() const {
35 ++ref_count_;
36
47 // TODO(maruel): Add back once it doesn't assert 500 times/sec. 37 // TODO(maruel): Add back once it doesn't assert 500 times/sec.
48 // Current thread books the critical section "AddRelease" 38 // Current thread books the critical section "AddRelease"
49 // without release it. 39 // without release it.
50 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); 40 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_);
51 #if DCHECK_IS_ON() 41 #if DCHECK_IS_ON()
52 DCHECK(!in_dtor_); 42 DCHECK(!in_dtor_);
43 if (ref_count_ >= 2)
44 DCHECK(CalledOnValidSequence());
gab 2017/02/16 21:38:47 I found this whole comparing with "2" confusing (t
tzik 2017/02/17 11:36:08 Done.
53 #endif 45 #endif
54 ++ref_count_;
55 } 46 }
56 47
57 // Returns true if the object should self-delete. 48 // Returns true if the object should self-delete.
58 bool Release() const { 49 bool Release() const {
59 // TODO(maruel): Add back once it doesn't assert 500 times/sec. 50 // TODO(maruel): Add back once it doesn't assert 500 times/sec.
60 // Current thread books the critical section "AddRelease" 51 // Current thread books the critical section "AddRelease"
61 // without release it. 52 // without release it.
62 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); 53 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_);
63 #if DCHECK_IS_ON() 54 #if DCHECK_IS_ON()
64 DCHECK(!in_dtor_); 55 DCHECK(!in_dtor_);
56 if (ref_count_ >= 2)
57 DCHECK(CalledOnValidSequence());
58 if (ref_count_ == 2)
59 DetachFromSequence();
65 #endif 60 #endif
66 if (--ref_count_ == 0) { 61 if (--ref_count_ == 0) {
67 #if DCHECK_IS_ON() 62 #if DCHECK_IS_ON()
68 in_dtor_ = true; 63 in_dtor_ = true;
69 #endif 64 #endif
70 return true; 65 return true;
71 } 66 }
72 return false; 67 return false;
73 } 68 }
74 69
75 private: 70 private:
71 void DetachFromSequence() const;
gab 2017/02/16 21:38:46 Invoked sequence_checker_.DetachFromSequence() dir
tzik 2017/02/17 11:36:08 Done.
72 bool CalledOnValidSequence() const;
gab 2017/02/16 21:38:47 Wrap method itself in DCHECK_IS_ON() since it's al
tzik 2017/02/17 11:36:08 Done.
73
76 mutable size_t ref_count_; 74 mutable size_t ref_count_;
gab 2017/02/16 21:38:47 Tangential cleanup while you're here = 0; here a
tzik 2017/02/17 11:36:09 Done.
77 #if DCHECK_IS_ON() 75 #if DCHECK_IS_ON()
78 mutable bool in_dtor_; 76 mutable bool in_dtor_ = false;
77 mutable SequenceChecker sequence_checker_;
79 #endif 78 #endif
80 79
81 DFAKE_MUTEX(add_release_); 80 DFAKE_MUTEX(add_release_);
82 81
83 DISALLOW_COPY_AND_ASSIGN(RefCountedBase); 82 DISALLOW_COPY_AND_ASSIGN(RefCountedBase);
84 }; 83 };
85 84
86 class BASE_EXPORT RefCountedThreadSafeBase { 85 class BASE_EXPORT RefCountedThreadSafeBase {
87 public: 86 public:
88 bool HasOneRef() const; 87 bool HasOneRef() const;
(...skipping 11 matching lines...) Expand all
100 mutable AtomicRefCount ref_count_; 99 mutable AtomicRefCount ref_count_;
101 #if DCHECK_IS_ON() 100 #if DCHECK_IS_ON()
102 mutable bool in_dtor_; 101 mutable bool in_dtor_;
103 #endif 102 #endif
104 103
105 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); 104 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase);
106 }; 105 };
107 106
108 } // namespace subtle 107 } // namespace subtle
109 108
109 class BASE_EXPORT ScopedAllowCrossThreadRefCountAccess final {
gab 2017/02/16 21:38:46 // See RefCounted class for documentation.
tzik 2017/02/17 11:36:08 Done.
110 public:
111 #if DCHECK_IS_ON()
112 ScopedAllowCrossThreadRefCountAccess();
113 ~ScopedAllowCrossThreadRefCountAccess();
114 #else
115 ScopedAllowCrossThreadRefCountAccess() {}
116 ~ScopedAllowCrossThreadRefCountAccess() {}
117 #endif
118 };
119
110 // 120 //
111 // A base class for reference counted classes. Otherwise, known as a cheap 121 // A base class for reference counted classes. Otherwise, known as a cheap
112 // knock-off of WebKit's RefCounted<T> class. To use this, just extend your 122 // knock-off of WebKit's RefCounted<T> class. To use this, just extend your
113 // class from it like so: 123 // class from it like so:
114 // 124 //
115 // class MyFoo : public base::RefCounted<MyFoo> { 125 // class MyFoo : public base::RefCounted<MyFoo> {
116 // ... 126 // ...
117 // private: 127 // private:
118 // friend class base::RefCounted<MyFoo>; 128 // friend class base::RefCounted<MyFoo>;
119 // ~MyFoo(); 129 // ~MyFoo();
120 // }; 130 // };
121 // 131 //
122 // You should always make your destructor non-public, to avoid any code deleting 132 // You should always make your destructor non-public, to avoid any code deleting
123 // the object accidently while there are references to it. 133 // the object accidently while there are references to it.
134 //
135 // The ref count manipulation to RefCounted is NOT thread safe, and has DCHECKs
gab 2017/02/16 21:38:47 rm comma
tzik 2017/02/17 11:36:08 Done.
136 // to trap unsafe cross thread usages. A subclass instance of RefCounted can be
gab 2017/02/16 21:38:47 s/usages/usage/
tzik 2017/02/17 11:36:08 Done.
137 // passed to the other thread or sequence only when its ref count is 1. If the
138 // ref count is more than 1, the ref count checks if it's on the same thread or
gab 2017/02/16 21:38:47 s/checks if it's on the same thread or sequence to
tzik 2017/02/17 11:36:09 Done.
139 // sequence to the previous ones.
gab 2017/02/16 21:38:47 s/thread or sequence/execution sequence/
tzik 2017/02/17 11:36:08 Done.
140 // If you want to temporarily opt-out from the check, use
141 // ScopedAllowCrossThreadRefCountAccess to disable the DCHECK.
gab 2017/02/16 21:38:47 For this last paragraph: // In rare cases where
tzik 2017/02/17 11:36:09 Done.
124 template <class T> 142 template <class T>
125 class RefCounted : public subtle::RefCountedBase { 143 class RefCounted : public subtle::RefCountedBase {
126 public: 144 public:
127 RefCounted() = default; 145 RefCounted() = default;
128 146
129 void AddRef() const { 147 void AddRef() const {
130 subtle::RefCountedBase::AddRef(); 148 subtle::RefCountedBase::AddRef();
131 } 149 }
132 150
133 void Release() const { 151 void Release() const {
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { 473 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) {
456 return !operator==(null, rhs); 474 return !operator==(null, rhs);
457 } 475 }
458 476
459 template <typename T> 477 template <typename T>
460 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { 478 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) {
461 return out << p.get(); 479 return out << p.get();
462 } 480 }
463 481
464 #endif // BASE_MEMORY_REF_COUNTED_H_ 482 #endif // BASE_MEMORY_REF_COUNTED_H_
OLDNEW
« no previous file with comments | « base/at_exit.cc ('k') | base/memory/ref_counted.cc » ('j') | base/memory/ref_counted.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698