OLD | NEW |
---|---|
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 // Weak pointers are pointers to an object that do not affect its lifetime, | 5 // Weak pointers are pointers to an object that do not affect its lifetime, |
6 // and which may be invalidated (i.e. reset to NULL) by the object, or its | 6 // and which may be invalidated (i.e. reset to NULL) by the object, or its |
7 // owner, at any time, most commonly when the object is about to be deleted. | 7 // owner, at any time, most commonly when the object is about to be deleted. |
8 | 8 |
9 // Weak pointers are useful when an object needs to be accessed safely by one | 9 // Weak pointers are useful when an object needs to be accessed safely by one |
10 // or more objects other than its owner, and those callers can cope with the | 10 // or more objects other than its owner, and those callers can cope with the |
11 // object vanishing and e.g. tasks posted to it being silently dropped. | 11 // object vanishing and e.g. tasks posted to it being silently dropped. |
12 // Reference-counting such an object would complicate the ownership graph and | 12 // Reference-counting such an object would complicate the ownership graph and |
13 // make it harder to reason about the object's lifetime. | 13 // make it harder to reason about the object's lifetime. |
14 | 14 |
15 // EXAMPLE: | 15 // EXAMPLE: |
16 // | 16 // |
17 // class Controller { | 17 // class Controller { |
18 // public: | 18 // public: |
19 // void SpawnWorker() { Worker::StartNew(weak_factory_.GetWeakPtr()); } | 19 // void SpawnWorker() { Worker::StartNew(weak_factory_.GetWeakPtr()); } |
20 // void WorkComplete(const Result& result) { ... } | 20 // void WorkComplete(const Result& result) { ... } |
21 // private: | 21 // private: |
22 // // Member variables should appear before the WeakPtrFactory. | |
awong
2013/05/15 01:19:15
Explain why.
Wez
2013/05/15 22:51:50
Done.
| |
22 // WeakPtrFactory<Controller> weak_factory_; | 23 // WeakPtrFactory<Controller> weak_factory_; |
23 // }; | 24 // }; |
24 // | 25 // |
25 // class Worker { | 26 // class Worker { |
26 // public: | 27 // public: |
27 // static void StartNew(const WeakPtr<Controller>& controller) { | 28 // static void StartNew(const WeakPtr<Controller>& controller) { |
28 // Worker* worker = new Worker(controller); | 29 // Worker* worker = new Worker(controller); |
29 // // Kick off asynchronous processing... | 30 // // Kick off asynchronous processing... |
30 // } | 31 // } |
31 // private: | 32 // private: |
32 // Worker(const WeakPtr<Controller>& controller) | 33 // Worker(const WeakPtr<Controller>& controller) |
33 // : controller_(controller) {} | 34 // : controller_(controller) {} |
34 // void DidCompleteAsynchronousProcessing(const Result& result) { | 35 // void DidCompleteAsynchronousProcessing(const Result& result) { |
35 // if (controller_) | 36 // if (controller_) |
36 // controller_->WorkComplete(result); | 37 // controller_->WorkComplete(result); |
37 // } | 38 // } |
38 // WeakPtr<Controller> controller_; | 39 // WeakPtr<Controller> controller_; |
39 // }; | 40 // }; |
40 // | 41 // |
41 // With this implementation a caller may use SpawnWorker() to dispatch multiple | 42 // With this implementation a caller may use SpawnWorker() to dispatch multiple |
42 // Workers and subsequently delete the Controller, without waiting for all | 43 // Workers and subsequently delete the Controller, without waiting for all |
43 // Workers to have completed. | 44 // Workers to have completed. |
44 | 45 |
45 // ------------------------- IMPORTANT: Thread-safety ------------------------- | 46 // ------------------------- IMPORTANT: Thread-safety ------------------------- |
46 | 47 |
47 // Weak pointers must always be dereferenced and invalidated on the same thread | 48 // Weak pointers must always be dereferenced and invalidated on the same thread |
48 // otherwise checking the pointer would be racey. WeakPtrFactory enforces this | 49 // otherwise checking the pointer would be racey. |
49 // by binding itself to the current thread when a WeakPtr is first created | |
50 // and un-binding only when those pointers are invalidated. WeakPtrs may still | |
51 // be handed off to other threads, however, so long as they are only actually | |
52 // dereferenced on the originating thread. This includes posting tasks to the | |
53 // thread using base::Bind() to invoke a method on the object via the WeakPtr. | |
54 | 50 |
55 // Calling SupportsWeakPtr::DetachFromThread() can work around the limitations | 51 // To check this, a WeakPtrFactory and valid WeakPtrs it has issued become bound |
56 // above and cancel the thread binding of the object and all WeakPtrs pointing | 52 // to the calling thread, either when one of the pointers is dereferenced, or |
57 // to it, but it's not recommended and unsafe. See crbug.com/232143. | 53 // when they are invalidated. When a factory's pointers are invalidated, the |
54 // factory itself returns to being un-bound, and it and the object it dispenses | |
55 // pointers for may be moved to a new thread. | |
awong
2013/05/15 01:19:15
Would it be useful to list a reduction of the foll
Wez
2013/05/15 22:51:50
I've added that in. It feels to me a little too i
| |
56 | |
57 // This allows a WeakPtr to an object to be created on one thread, but used to | |
58 // post tasks to it on a second thread, so long as the WeakPtr is eventually | |
59 // invalidated (e.g. by the object being deleted) on the second thread. | |
60 | |
61 // It also allows a WeakPtr to be created on one thread, and passed to a second, | |
62 // for instance to use to post tasks back to the object on the first thread. | |
58 | 63 |
59 #ifndef BASE_MEMORY_WEAK_PTR_H_ | 64 #ifndef BASE_MEMORY_WEAK_PTR_H_ |
60 #define BASE_MEMORY_WEAK_PTR_H_ | 65 #define BASE_MEMORY_WEAK_PTR_H_ |
61 | 66 |
62 #include "base/basictypes.h" | 67 #include "base/basictypes.h" |
63 #include "base/base_export.h" | 68 #include "base/base_export.h" |
64 #include "base/logging.h" | 69 #include "base/logging.h" |
65 #include "base/memory/ref_counted.h" | 70 #include "base/memory/ref_counted.h" |
66 #include "base/template_util.h" | 71 #include "base/template_util.h" |
67 #include "base/threading/thread_checker.h" | 72 #include "base/threading/thread_checker.h" |
68 | 73 |
69 namespace base { | 74 namespace base { |
70 | 75 |
71 template <typename T> class SupportsWeakPtr; | 76 template <typename T> class SupportsWeakPtr; |
72 template <typename T> class WeakPtr; | 77 template <typename T> class WeakPtr; |
73 | 78 |
74 namespace internal { | 79 namespace internal { |
75 // These classes are part of the WeakPtr implementation. | 80 // These classes are part of the WeakPtr implementation. |
76 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. | 81 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. |
77 | 82 |
78 class BASE_EXPORT WeakReference { | 83 class BASE_EXPORT WeakReference { |
79 public: | 84 public: |
80 // While Flag is bound to a specific thread, it may be deleted from another | 85 // Although Flag is bound to a specific thread, it may be deleted from another |
81 // via base::WeakPtr::~WeakPtr(). | 86 // via base::WeakPtr::~WeakPtr(). |
82 class Flag : public RefCountedThreadSafe<Flag> { | 87 class Flag : public RefCountedThreadSafe<Flag> { |
83 public: | 88 public: |
84 Flag(); | 89 Flag(); |
85 | 90 |
86 void Invalidate(); | 91 void Invalidate(); |
87 bool IsValid() const; | 92 bool IsValid() const; |
88 | 93 |
89 void DetachFromThread() { thread_checker_.DetachFromThread(); } | 94 // Remove this when crbug.com/234964 is addressed. |
95 void DetachFromThreadHack() { thread_checker_.DetachFromThread(); } | |
90 | 96 |
91 private: | 97 private: |
92 friend class base::RefCountedThreadSafe<Flag>; | 98 friend class base::RefCountedThreadSafe<Flag>; |
93 | 99 |
94 ~Flag(); | 100 ~Flag(); |
95 | 101 |
96 ThreadChecker thread_checker_; | 102 ThreadChecker thread_checker_; |
97 bool is_valid_; | 103 bool is_valid_; |
98 }; | 104 }; |
99 | 105 |
(...skipping 13 matching lines...) Expand all Loading... | |
113 ~WeakReferenceOwner(); | 119 ~WeakReferenceOwner(); |
114 | 120 |
115 WeakReference GetRef() const; | 121 WeakReference GetRef() const; |
116 | 122 |
117 bool HasRefs() const { | 123 bool HasRefs() const { |
118 return flag_.get() && !flag_->HasOneRef(); | 124 return flag_.get() && !flag_->HasOneRef(); |
119 } | 125 } |
120 | 126 |
121 void Invalidate(); | 127 void Invalidate(); |
122 | 128 |
123 // Indicates that this object will be used on another thread from now on. | 129 // Remove this when crbug.com/234964 is addressed. |
124 // Do not use this in new code. See crbug.com/232143. | 130 void DetachFromThreadHack() { |
125 void DetachFromThread() { | 131 if (flag_) flag_->DetachFromThreadHack(); |
126 if (flag_) flag_->DetachFromThread(); | |
127 } | 132 } |
128 | 133 |
129 private: | 134 private: |
130 mutable scoped_refptr<WeakReference::Flag> flag_; | 135 mutable scoped_refptr<WeakReference::Flag> flag_; |
131 }; | 136 }; |
132 | 137 |
133 // This class simplifies the implementation of WeakPtr's type conversion | 138 // This class simplifies the implementation of WeakPtr's type conversion |
134 // constructor by avoiding the need for a public accessor for ref_. A | 139 // constructor by avoiding the need for a public accessor for ref_. A |
135 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this | 140 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this |
136 // base class gives us a way to access ref_ in a protected fashion. | 141 // base class gives us a way to access ref_ in a protected fashion. |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
262 DCHECK(ptr_); | 267 DCHECK(ptr_); |
263 weak_reference_owner_.Invalidate(); | 268 weak_reference_owner_.Invalidate(); |
264 } | 269 } |
265 | 270 |
266 // Call this method to determine if any weak pointers exist. | 271 // Call this method to determine if any weak pointers exist. |
267 bool HasWeakPtrs() const { | 272 bool HasWeakPtrs() const { |
268 DCHECK(ptr_); | 273 DCHECK(ptr_); |
269 return weak_reference_owner_.HasRefs(); | 274 return weak_reference_owner_.HasRefs(); |
270 } | 275 } |
271 | 276 |
272 // Indicates that this object will be used on another thread from now on. | |
273 // Do not use this in new code. See crbug.com/232143. | |
274 void DetachFromThread() { | |
275 DCHECK(ptr_); | |
276 weak_reference_owner_.DetachFromThread(); | |
277 } | |
278 | |
279 private: | 277 private: |
280 internal::WeakReferenceOwner weak_reference_owner_; | 278 internal::WeakReferenceOwner weak_reference_owner_; |
281 T* ptr_; | 279 T* ptr_; |
282 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); | 280 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); |
283 }; | 281 }; |
284 | 282 |
285 // A class may extend from SupportsWeakPtr to let others take weak pointers to | 283 // A class may extend from SupportsWeakPtr to let others take weak pointers to |
286 // it. This avoids the class itself implementing boilerplate to dispense weak | 284 // it. This avoids the class itself implementing boilerplate to dispense weak |
287 // pointers. However, since SupportsWeakPtr's destructor won't invalidate | 285 // pointers. However, since SupportsWeakPtr's destructor won't invalidate |
288 // weak pointers to the class until after the derived class' members have been | 286 // weak pointers to the class until after the derived class' members have been |
289 // destroyed, its use can lead to subtle use-after-destroy issues. | 287 // destroyed, its use can lead to subtle use-after-destroy issues. |
290 template <class T> | 288 template <class T> |
291 class SupportsWeakPtr : public internal::SupportsWeakPtrBase { | 289 class SupportsWeakPtr : public internal::SupportsWeakPtrBase { |
292 public: | 290 public: |
293 SupportsWeakPtr() {} | 291 SupportsWeakPtr() {} |
294 | 292 |
295 WeakPtr<T> AsWeakPtr() { | 293 WeakPtr<T> AsWeakPtr() { |
296 return WeakPtr<T>(weak_reference_owner_.GetRef(), static_cast<T*>(this)); | 294 return WeakPtr<T>(weak_reference_owner_.GetRef(), static_cast<T*>(this)); |
297 } | 295 } |
298 | 296 |
299 // Indicates that this object will be used on another thread from now on. | 297 // Removes the binding, if any, from this object to a particular thread. |
300 // Do not use this in new code. See crbug.com/232143. | 298 // This is used in WebGraphicsContext3DInProcessCommandBufferImpl to work- |
301 void DetachFromThread() { | 299 // around access to cmmand buffer objects by more than one thread. |
302 weak_reference_owner_.DetachFromThread(); | 300 // Remove this when crbug.com/234964 is addressed. |
301 void DetachFromThreadHack() { | |
302 weak_reference_owner_.DetachFromThreadHack(); | |
303 } | 303 } |
304 | 304 |
305 protected: | 305 protected: |
306 ~SupportsWeakPtr() {} | 306 ~SupportsWeakPtr() {} |
307 | 307 |
308 private: | 308 private: |
309 internal::WeakReferenceOwner weak_reference_owner_; | 309 internal::WeakReferenceOwner weak_reference_owner_; |
310 DISALLOW_COPY_AND_ASSIGN(SupportsWeakPtr); | 310 DISALLOW_COPY_AND_ASSIGN(SupportsWeakPtr); |
311 }; | 311 }; |
312 | 312 |
(...skipping 16 matching lines...) Expand all Loading... | |
329 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. | 329 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. |
330 | 330 |
331 template <typename Derived> | 331 template <typename Derived> |
332 WeakPtr<Derived> AsWeakPtr(Derived* t) { | 332 WeakPtr<Derived> AsWeakPtr(Derived* t) { |
333 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); | 333 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); |
334 } | 334 } |
335 | 335 |
336 } // namespace base | 336 } // namespace base |
337 | 337 |
338 #endif // BASE_MEMORY_WEAK_PTR_H_ | 338 #endif // BASE_MEMORY_WEAK_PTR_H_ |
OLD | NEW |