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 |
(...skipping 30 matching lines...) Expand all Loading... |
41 // WeakPtr<Controller> controller_; | 41 // WeakPtr<Controller> controller_; |
42 // }; | 42 // }; |
43 // | 43 // |
44 // With this implementation a caller may use SpawnWorker() to dispatch multiple | 44 // With this implementation a caller may use SpawnWorker() to dispatch multiple |
45 // Workers and subsequently delete the Controller, without waiting for all | 45 // Workers and subsequently delete the Controller, without waiting for all |
46 // Workers to have completed. | 46 // Workers to have completed. |
47 | 47 |
48 // ------------------------- IMPORTANT: Thread-safety ------------------------- | 48 // ------------------------- IMPORTANT: Thread-safety ------------------------- |
49 | 49 |
50 // Weak pointers may be passed safely between threads, but must always be | 50 // Weak pointers may be passed safely between threads, but must always be |
51 // dereferenced and invalidated on the same thread otherwise checking the | 51 // dereferenced and invalidated on the same SequencedTaskRunner otherwise |
52 // pointer would be racey. | 52 // checking the pointer would be racey. |
53 // | 53 // |
54 // To ensure correct use, the first time a WeakPtr issued by a WeakPtrFactory | 54 // To ensure correct use, the first time a WeakPtr issued by a WeakPtrFactory |
55 // is dereferenced, the factory and its WeakPtrs become bound to the calling | 55 // is dereferenced, the factory and its WeakPtrs become bound to the calling |
56 // thread, and cannot be dereferenced or invalidated on any other thread. Bound | 56 // thread or current SequencedWorkerPool token, and cannot be dereferenced or |
57 // WeakPtrs can still be handed off to other threads, e.g. to use to post tasks | 57 // invalidated on any other task runner. Bound WeakPtrs can still be handed |
58 // back to object on the bound thread. | 58 // off to other task runners, e.g. to use to post tasks back to object on the |
| 59 // bound sequence. |
59 // | 60 // |
60 // Invalidating the factory's WeakPtrs un-binds it from the thread, allowing it | 61 // Invalidating the factory's WeakPtrs un-binds it from the sequence, allowing |
61 // to be passed for a different thread to use or delete it. | 62 // it to be passed for a different sequence to use or delete it. |
62 | 63 |
63 #ifndef BASE_MEMORY_WEAK_PTR_H_ | 64 #ifndef BASE_MEMORY_WEAK_PTR_H_ |
64 #define BASE_MEMORY_WEAK_PTR_H_ | 65 #define BASE_MEMORY_WEAK_PTR_H_ |
65 | 66 |
66 #include "base/basictypes.h" | 67 #include "base/basictypes.h" |
67 #include "base/base_export.h" | 68 #include "base/base_export.h" |
68 #include "base/logging.h" | 69 #include "base/logging.h" |
69 #include "base/memory/ref_counted.h" | 70 #include "base/memory/ref_counted.h" |
70 #include "base/sequence_checker.h" | 71 #include "base/sequence_checker.h" |
71 #include "base/template_util.h" | 72 #include "base/template_util.h" |
72 | 73 |
73 namespace base { | 74 namespace base { |
74 | 75 |
75 template <typename T> class SupportsWeakPtr; | 76 template <typename T> class SupportsWeakPtr; |
76 template <typename T> class WeakPtr; | 77 template <typename T> class WeakPtr; |
77 | 78 |
78 namespace internal { | 79 namespace internal { |
79 // These classes are part of the WeakPtr implementation. | 80 // These classes are part of the WeakPtr implementation. |
80 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. | 81 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. |
81 | 82 |
82 class BASE_EXPORT WeakReference { | 83 class BASE_EXPORT WeakReference { |
83 public: | 84 public: |
84 // Although Flag is bound to a specific thread, it may be deleted from another | 85 // Although Flag is bound to a specific SequencedTaskRunner, it may be |
85 // via base::WeakPtr::~WeakPtr(). | 86 // deleted from another via base::WeakPtr::~WeakPtr(). |
86 class BASE_EXPORT Flag : public RefCountedThreadSafe<Flag> { | 87 class BASE_EXPORT Flag : public RefCountedThreadSafe<Flag> { |
87 public: | 88 public: |
88 Flag(); | 89 Flag(); |
89 | 90 |
90 void Invalidate(); | 91 void Invalidate(); |
91 bool IsValid() const; | 92 bool IsValid() const; |
92 | 93 |
93 private: | 94 private: |
94 friend class base::RefCountedThreadSafe<Flag>; | 95 friend class base::RefCountedThreadSafe<Flag>; |
95 | 96 |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. | 330 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. |
330 | 331 |
331 template <typename Derived> | 332 template <typename Derived> |
332 WeakPtr<Derived> AsWeakPtr(Derived* t) { | 333 WeakPtr<Derived> AsWeakPtr(Derived* t) { |
333 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); | 334 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); |
334 } | 335 } |
335 | 336 |
336 } // namespace base | 337 } // namespace base |
337 | 338 |
338 #endif // BASE_MEMORY_WEAK_PTR_H_ | 339 #endif // BASE_MEMORY_WEAK_PTR_H_ |
OLD | NEW |