| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkWeakRefCnt_DEFINED | 8 #ifndef SkWeakRefCnt_DEFINED |
| 9 #define SkWeakRefCnt_DEFINED | 9 #define SkWeakRefCnt_DEFINED |
| 10 | 10 |
| 11 #include "SkRefCnt.h" | 11 #include "SkRefCnt.h" |
| 12 #include "SkThread.h" | 12 #include "SkThread.h" |
| 13 | 13 |
| 14 /** \class SkWeakRefCnt | 14 /** \class SkWeakRefCnt |
| 15 | 15 |
| 16 SkWeakRefCnt is the base class for objects that may be shared by multiple | 16 SkWeakRefCnt is the base class for objects that may be shared by multiple |
| 17 objects. When an existing strong owner wants to share a reference, it calls | 17 objects. When an existing strong owner wants to share a reference, it calls |
| 18 ref(). When a strong owner wants to release its reference, it calls | 18 ref(). When a strong owner wants to release its reference, it calls |
| 19 unref(). When the shared object's strong reference count goes to zero as | 19 unref(). When the shared object's strong reference count goes to zero as |
| 20 the result of an unref() call, its (virtual) weak_dispose method is called. | 20 the result of an unref() call, its (virtual) weak_dispose method is called. |
| 21 It is an error for the destructor to be called explicitly (or via the | 21 It is an error for the destructor to be called explicitly (or via the |
| 22 object going out of scope on the stack or calling delete) if | 22 object going out of scope on the stack or calling delete) if |
| 23 getRefCnt() > 1. | 23 getRefCnt() > 1. |
| 24 | 24 |
| 25 In addition to strong ownership, an owner may instead obtain a weak | 25 In addition to strong ownership, an owner may instead obtain a weak |
| 26 reference by calling weak_ref(). A call to weak_ref() must be balanced my a | 26 reference by calling weak_ref(). A call to weak_ref() must be balanced by a |
| 27 call to weak_unref(). To obtain a strong reference from a weak reference, | 27 call to weak_unref(). To obtain a strong reference from a weak reference, |
| 28 call try_ref(). If try_ref() returns true, the owner's pointer is now also | 28 call try_ref(). If try_ref() returns true, the owner's pointer is now also |
| 29 a strong reference on which unref() must be called. Note that this does not | 29 a strong reference on which unref() must be called. Note that this does not |
| 30 affect the original weak reference, weak_unref() must still be called. When | 30 affect the original weak reference, weak_unref() must still be called. When |
| 31 the weak reference count goes to zero, the object is deleted. While the | 31 the weak reference count goes to zero, the object is deleted. While the |
| 32 weak reference count is positive and the strong reference count is zero the | 32 weak reference count is positive and the strong reference count is zero the |
| 33 object still exists, but will be in the disposed state. It is up to the | 33 object still exists, but will be in the disposed state. It is up to the |
| 34 object to define what this means. | 34 object to define what this means. |
| 35 | 35 |
| 36 Note that a strong reference implicitly implies a weak reference. As a | 36 Note that a strong reference implicitly implies a weak reference. As a |
| (...skipping 30 matching lines...) Expand all Loading... |
| 67 SkASSERT(fWeakCnt == 1); | 67 SkASSERT(fWeakCnt == 1); |
| 68 fWeakCnt = 0; | 68 fWeakCnt = 0; |
| 69 #endif | 69 #endif |
| 70 } | 70 } |
| 71 | 71 |
| 72 /** Return the weak reference count. | 72 /** Return the weak reference count. |
| 73 */ | 73 */ |
| 74 int32_t getWeakCnt() const { return fWeakCnt; } | 74 int32_t getWeakCnt() const { return fWeakCnt; } |
| 75 | 75 |
| 76 void validate() const { | 76 void validate() const { |
| 77 SkRefCnt::validate(); | 77 this->INHERITED::validate(); |
| 78 SkASSERT(fWeakCnt > 0); | 78 SkASSERT(fWeakCnt > 0); |
| 79 } | 79 } |
| 80 | 80 |
| 81 /** Creates a strong reference from a weak reference, if possible. The | 81 /** Creates a strong reference from a weak reference, if possible. The |
| 82 caller must already be an owner. If try_ref() returns true the owner | 82 caller must already be an owner. If try_ref() returns true the owner |
| 83 is in posession of an additional strong reference. Both the original | 83 is in posession of an additional strong reference. Both the original |
| 84 reference and new reference must be properly unreferenced. If try_ref() | 84 reference and new reference must be properly unreferenced. If try_ref() |
| 85 returns false, no strong reference could be created and the owner's | 85 returns false, no strong reference could be created and the owner's |
| 86 reference is in the same state as before the call. | 86 reference is in the same state as before the call. |
| 87 */ | 87 */ |
| (...skipping 25 matching lines...) Expand all Loading... |
| 113 SkASSERT(fWeakCnt > 0); | 113 SkASSERT(fWeakCnt > 0); |
| 114 // Release barrier (SL/S), if not provided below. | 114 // Release barrier (SL/S), if not provided below. |
| 115 if (sk_atomic_dec(&fWeakCnt) == 1) { | 115 if (sk_atomic_dec(&fWeakCnt) == 1) { |
| 116 // Aquire barrier (L/SL), if not provided above. | 116 // Aquire barrier (L/SL), if not provided above. |
| 117 // Prevents code in destructor from happening before the decrement. | 117 // Prevents code in destructor from happening before the decrement. |
| 118 sk_membar_aquire__after_atomic_dec(); | 118 sk_membar_aquire__after_atomic_dec(); |
| 119 #ifdef SK_DEBUG | 119 #ifdef SK_DEBUG |
| 120 // so our destructor won't complain | 120 // so our destructor won't complain |
| 121 fWeakCnt = 1; | 121 fWeakCnt = 1; |
| 122 #endif | 122 #endif |
| 123 SkRefCnt::internal_dispose(); | 123 this->INHERITED::internal_dispose(); |
| 124 } | 124 } |
| 125 } | 125 } |
| 126 | 126 |
| 127 /** Returns true if there are no strong references to the object. When this | 127 /** Returns true if there are no strong references to the object. When this |
| 128 is the case all future calls to try_ref() will return false. | 128 is the case all future calls to try_ref() will return false. |
| 129 */ | 129 */ |
| 130 bool weak_expired() const { | 130 bool weak_expired() const { |
| 131 return fRefCnt == 0; | 131 return fRefCnt == 0; |
| 132 } | 132 } |
| 133 | 133 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 150 weak_unref(); | 150 weak_unref(); |
| 151 } | 151 } |
| 152 | 152 |
| 153 /* Invariant: fWeakCnt = #weak + (fRefCnt > 0 ? 1 : 0) */ | 153 /* Invariant: fWeakCnt = #weak + (fRefCnt > 0 ? 1 : 0) */ |
| 154 mutable int32_t fWeakCnt; | 154 mutable int32_t fWeakCnt; |
| 155 | 155 |
| 156 typedef SkRefCnt INHERITED; | 156 typedef SkRefCnt INHERITED; |
| 157 }; | 157 }; |
| 158 | 158 |
| 159 #endif | 159 #endif |
| OLD | NEW |