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