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

Side by Side Diff: base/ios/weak_nsobject.h

Issue 853503002: Fix ThreadChecker bug in WeakNSObject (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added more thread checks Created 5 years, 11 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
« no previous file with comments | « no previous file | base/ios/weak_nsobject_unittest.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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_IOS_WEAK_NSOBJECT_H_ 5 #ifndef BASE_IOS_WEAK_NSOBJECT_H_
6 #define BASE_IOS_WEAK_NSOBJECT_H_ 6 #define BASE_IOS_WEAK_NSOBJECT_H_
7 7
8 #import <Foundation/Foundation.h> 8 #import <Foundation/Foundation.h>
9 #import <objc/runtime.h> 9 #import <objc/runtime.h>
10 10
(...skipping 19 matching lines...) Expand all
30 // foo.reset(); // The reference is released. 30 // foo.reset(); // The reference is released.
31 // [weak_foo description]; // Returns nil, as weak_foo is pointing to nil. 31 // [weak_foo description]; // Returns nil, as weak_foo is pointing to nil.
32 // 32 //
33 // 33 //
34 // Implementation wise a WeakNSObject keeps a reference to a refcounted 34 // Implementation wise a WeakNSObject keeps a reference to a refcounted
35 // WeakContainer. There is one unique instance of a WeakContainer per watched 35 // WeakContainer. There is one unique instance of a WeakContainer per watched
36 // NSObject, this relationship is maintained via the ObjectiveC associated 36 // NSObject, this relationship is maintained via the ObjectiveC associated
37 // object API, indirectly via an ObjectiveC CRBWeakNSProtocolSentinel class. 37 // object API, indirectly via an ObjectiveC CRBWeakNSProtocolSentinel class.
38 // 38 //
39 // The implementation assumes that the tracked object will be released on the 39 // The implementation assumes that the tracked object will be released on the
40 // same thread that the WeakNSObject is created on. 40 // same thread that the WeakNSObject is created on.
lliabraa 2015/01/14 15:25:00 how about expanding this section on threading with
41 // 41 //
42 namespace base { 42 namespace base {
43 43
44 // WeakContainer keeps a weak pointer to an object and clears it when it 44 // WeakContainer keeps a weak pointer to an object and clears it when it
45 // receives nullify() from the object's sentinel. 45 // receives nullify() from the object's sentinel.
46 class WeakContainer : public base::RefCountedThreadSafe<WeakContainer> { 46 class WeakContainer : public base::RefCountedThreadSafe<WeakContainer> {
47 public: 47 public:
48 WeakContainer(id object) : object_(object) {} 48 explicit WeakContainer(id object) : object_(object) {}
49 id object() { return object_; } 49
50 id object() {
51 DCHECK(checker_.CalledOnValidThread());
52 return object_;
53 }
54
50 void nullify() { 55 void nullify() {
51 DCHECK(checker_.CalledOnValidThread()); 56 DCHECK(checker_.CalledOnValidThread());
52 object_ = nil; 57 object_ = nil;
53 } 58 }
54 59
55 private: 60 private:
56 friend base::RefCountedThreadSafe<WeakContainer>; 61 friend base::RefCountedThreadSafe<WeakContainer>;
57 ~WeakContainer() {} 62 ~WeakContainer() {}
58 base::ThreadChecker checker_; 63 base::ThreadChecker checker_;
59 id object_; 64 id object_;
60 }; 65 };
61 66
62 } // namespace base 67 } // namespace base
63 68
64 // Sentinel for observing the object contained in the weak pointer. The object 69 // Sentinel for observing the object contained in the weak pointer. The object
65 // will be deleted when the weak object is deleted and will notify its 70 // will be deleted when the weak object is deleted and will notify its
66 // container. 71 // container.
67 @interface CRBWeakNSProtocolSentinel : NSObject 72 @interface CRBWeakNSProtocolSentinel : NSObject
68 // Return the only associated container for this object. There can be only one. 73 // Return the only associated container for this object. There can be only one.
69 // Will return null if object is nil . 74 // Will return null if object is nil .
70 + (scoped_refptr<base::WeakContainer>)containerForObject:(id)object; 75 + (scoped_refptr<base::WeakContainer>)containerForObject:(id)object;
71 @end 76 @end
72 77
73 namespace base { 78 namespace base {
74 79
75 // Base class for all WeakNSObject derivatives. 80 // Base class for all WeakNSObject derivatives.
76 template <typename NST> 81 template <typename NST>
77 class WeakNSProtocol : public base::NonThreadSafe { 82 class WeakNSProtocol {
78 public: 83 public:
79 explicit WeakNSProtocol(NST object = nil) { 84 explicit WeakNSProtocol(NST object = nil) {
85 // A WeakNSProtocol object can be allocated on one thread and used on
86 // another. This is not the case for the contained object.
87 checker_.DetachFromThread();
lliabraa 2015/01/14 15:25:00 won't calling DetachFromThread() here cause the We
80 container_ = [CRBWeakNSProtocolSentinel containerForObject:object]; 88 container_ = [CRBWeakNSProtocolSentinel containerForObject:object];
81 } 89 }
82 90
83 WeakNSProtocol(const WeakNSProtocol<NST>& that) { 91 WeakNSProtocol(const WeakNSProtocol<NST>& that) {
92 // A WeakNSProtocol object can be allocated on one thread and used on
93 // another. This is not the case for the contained object.
94 checker_.DetachFromThread();
84 container_ = that.container_; 95 container_ = that.container_;
85 } 96 }
86 97
87 ~WeakNSProtocol() { 98 ~WeakNSProtocol() {
88 // A WeakNSProtocol object can be allocated on one thread and released on 99 // A WeakNSProtocol object can be used on one thread and released on
89 // another. This is not the case for the contained object. 100 // another. This is not the case for the contained object.
90 DetachFromThread(); 101 checker_.DetachFromThread();
91 } 102 }
92 103
93 void reset(NST object = nil) { 104 void reset(NST object = nil) {
94 DCHECK(CalledOnValidThread()); 105 DCHECK(checker_.CalledOnValidThread());
95 container_ = [CRBWeakNSProtocolSentinel containerForObject:object]; 106 container_ = [CRBWeakNSProtocolSentinel containerForObject:object];
96 } 107 }
97 108
98 NST get() const { 109 NST get() const {
99 DCHECK(CalledOnValidThread()); 110 DCHECK(checker_.CalledOnValidThread());
100 if (!container_.get()) 111 if (!container_.get())
101 return nil; 112 return nil;
102 return container_->object(); 113 return container_->object();
103 } 114 }
104 115
105 WeakNSProtocol& operator=(const WeakNSProtocol<NST>& that) { 116 WeakNSProtocol& operator=(const WeakNSProtocol<NST>& that) {
106 DCHECK(CalledOnValidThread()); 117 DCHECK(checker_.CalledOnValidThread());
107 container_ = that.container_; 118 container_ = that.container_;
108 return *this; 119 return *this;
109 } 120 }
110 121
111 bool operator==(NST that) const { 122 bool operator==(NST that) const {
112 DCHECK(CalledOnValidThread()); 123 DCHECK(checker_.CalledOnValidThread());
113 return get() == that; 124 return get() == that;
114 } 125 }
115 126
116 bool operator!=(NST that) const { return get() != that; } 127 bool operator!=(NST that) const {
128 DCHECK(checker_.CalledOnValidThread());
129 return get() != that;
130 }
117 131
118 operator NST() const { return get(); } 132 operator NST() const {
133 DCHECK(checker_.CalledOnValidThread());
134 return get();
135 }
119 136
120 private: 137 private:
121 // Refecounted reference to the container tracking the ObjectiveC object this 138 // Refecounted reference to the container tracking the ObjectiveC object this
122 // class encapsulates. 139 // class encapsulates.
123 scoped_refptr<base::WeakContainer> container_; 140 scoped_refptr<base::WeakContainer> container_;
141 base::ThreadChecker checker_;
124 }; 142 };
125 143
126 // Free functions 144 // Free functions
127 template <class NST> 145 template <class NST>
128 bool operator==(NST p1, const WeakNSProtocol<NST>& p2) { 146 bool operator==(NST p1, const WeakNSProtocol<NST>& p2) {
129 return p1 == p2.get(); 147 return p1 == p2.get();
130 } 148 }
131 149
132 template <class NST> 150 template <class NST>
133 bool operator!=(NST p1, const WeakNSProtocol<NST>& p2) { 151 bool operator!=(NST p1, const WeakNSProtocol<NST>& p2) {
(...skipping 23 matching lines...) Expand all
157 175
158 WeakNSObject& operator=(const WeakNSObject<id>& that) { 176 WeakNSObject& operator=(const WeakNSObject<id>& that) {
159 WeakNSProtocol<id>::operator=(that); 177 WeakNSProtocol<id>::operator=(that);
160 return *this; 178 return *this;
161 } 179 }
162 }; 180 };
163 181
164 } // namespace base 182 } // namespace base
165 183
166 #endif // BASE_IOS_WEAK_NSOBJECT_H_ 184 #endif // BASE_IOS_WEAK_NSOBJECT_H_
OLDNEW
« no previous file with comments | « no previous file | base/ios/weak_nsobject_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698