The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "RefBase" |
Mathias Agopian | da8ec4b | 2013-03-19 17:36:57 -0700 | [diff] [blame] | 18 | // #define LOG_NDEBUG 0 |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | |
| 20 | #include <utils/RefBase.h> |
| 21 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <utils/CallStack.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 24 | #ifndef __unused |
| 25 | #define __unused __attribute__((__unused__)) |
| 26 | #endif |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| 28 | // compile with refcounting debugging enabled |
| 29 | #define DEBUG_REFS 0 |
Mathias Agopian | 6d4419d | 2013-03-18 20:31:18 -0700 | [diff] [blame] | 30 | |
| 31 | // whether ref-tracking is enabled by default, if not, trackMe(true, false) |
| 32 | // needs to be called explicitly |
| 33 | #define DEBUG_REFS_ENABLED_BY_DEFAULT 0 |
| 34 | |
| 35 | // whether callstack are collected (significantly slows things down) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | #define DEBUG_REFS_CALLSTACK_ENABLED 1 |
| 37 | |
Mathias Agopian | 6d4419d | 2013-03-18 20:31:18 -0700 | [diff] [blame] | 38 | // folder where stack traces are saved when DEBUG_REFS is enabled |
| 39 | // this folder needs to exist and be writable |
| 40 | #define DEBUG_REFS_CALLSTACK_PATH "/data/debug" |
| 41 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | // log all reference counting operations |
| 43 | #define PRINT_REFS 0 |
| 44 | |
| 45 | // --------------------------------------------------------------------------- |
| 46 | |
| 47 | namespace android { |
| 48 | |
Hans Boehm | 9ba7192 | 2016-07-21 18:56:55 -0700 | [diff] [blame] | 49 | // Observations, invariants, etc: |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 50 | |
Hans Boehm | 9ba7192 | 2016-07-21 18:56:55 -0700 | [diff] [blame] | 51 | // By default, obects are destroyed when the last strong reference disappears |
| 52 | // or, if the object never had a strong reference, when the last weak reference |
| 53 | // disappears. |
| 54 | // |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 55 | // OBJECT_LIFETIME_WEAK changes this behavior to retain the object |
| 56 | // unconditionally until the last reference of either kind disappears. The |
| 57 | // client ensures that the extendObjectLifetime call happens before the dec |
| 58 | // call that would otherwise have deallocated the object, or before an |
| 59 | // attemptIncStrong call that might rely on it. We do not worry about |
| 60 | // concurrent changes to the object lifetime. |
Hans Boehm | 9ba7192 | 2016-07-21 18:56:55 -0700 | [diff] [blame] | 61 | // |
| 62 | // AttemptIncStrong will succeed if the object has a strong reference, or if it |
| 63 | // has a weak reference and has never had a strong reference. |
| 64 | // AttemptIncWeak really does succeed only if there is already a WEAK |
| 65 | // reference, and thus may fail when attemptIncStrong would succeed. |
| 66 | // |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 67 | // mStrong is the strong reference count. mWeak is the weak reference count. |
| 68 | // Between calls, and ignoring memory ordering effects, mWeak includes strong |
| 69 | // references, and is thus >= mStrong. |
| 70 | // |
Hans Boehm | 9ba7192 | 2016-07-21 18:56:55 -0700 | [diff] [blame] | 71 | // A weakref_impl holds all the information, including both reference counts, |
| 72 | // required to perform wp<> operations. Thus these can continue to be performed |
| 73 | // after the RefBase object has been destroyed. |
| 74 | // |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 75 | // A weakref_impl is allocated as the value of mRefs in a RefBase object on |
| 76 | // construction. |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 77 | // In the OBJECT_LIFETIME_STRONG case, it is normally deallocated in decWeak, |
| 78 | // and hence lives as long as the last weak reference. (It can also be |
| 79 | // deallocated in the RefBase destructor iff the strong reference count was |
| 80 | // never incremented and the weak count is zero, e.g. if the RefBase object is |
| 81 | // explicitly destroyed without decrementing the strong count. This should be |
| 82 | // avoided.) In this case, the RefBase destructor should be invoked from |
| 83 | // decStrong. |
| 84 | // In the OBJECT_LIFETIME_WEAK case, the weakref_impl is always deallocated in |
| 85 | // the RefBase destructor, which is always invoked by decWeak. DecStrong |
| 86 | // explicitly avoids the deletion in this case. |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 87 | // |
| 88 | // Memory ordering: |
| 89 | // The client must ensure that every inc() call, together with all other |
| 90 | // accesses to the object, happens before the corresponding dec() call. |
| 91 | // |
| 92 | // We try to keep memory ordering constraints on atomics as weak as possible, |
| 93 | // since memory fences or ordered memory accesses are likely to be a major |
| 94 | // performance cost for this code. All accesses to mStrong, mWeak, and mFlags |
| 95 | // explicitly relax memory ordering in some way. |
| 96 | // |
| 97 | // The only operations that are not memory_order_relaxed are reference count |
| 98 | // decrements. All reference count decrements are release operations. In |
| 99 | // addition, the final decrement leading the deallocation is followed by an |
| 100 | // acquire fence, which we can view informally as also turning it into an |
| 101 | // acquire operation. (See 29.8p4 [atomics.fences] for details. We could |
| 102 | // alternatively use acq_rel operations for all decrements. This is probably |
| 103 | // slower on most current (2016) hardware, especially on ARMv7, but that may |
| 104 | // not be true indefinitely.) |
| 105 | // |
| 106 | // This convention ensures that the second-to-last decrement synchronizes with |
| 107 | // (in the language of 1.10 in the C++ standard) the final decrement of a |
| 108 | // reference count. Since reference counts are only updated using atomic |
| 109 | // read-modify-write operations, this also extends to any earlier decrements. |
| 110 | // (See "release sequence" in 1.10.) |
| 111 | // |
| 112 | // Since all operations on an object happen before the corresponding reference |
| 113 | // count decrement, and all reference count decrements happen before the final |
| 114 | // one, we are guaranteed that all other object accesses happen before the |
| 115 | // object is destroyed. |
| 116 | |
| 117 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | #define INITIAL_STRONG_VALUE (1<<28) |
| 119 | |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 120 | #define MAX_COUNT 0xfffff |
| 121 | |
| 122 | // Test whether the argument is a clearly invalid strong reference count. |
| 123 | // Used only for error checking on the value before an atomic decrement. |
| 124 | // Intended to be very cheap. |
| 125 | // Note that we cannot just check for excess decrements by comparing to zero |
| 126 | // since the object would be deallocated before that. |
| 127 | #define BAD_STRONG(c) \ |
| 128 | ((c) == 0 || ((c) & (~(MAX_COUNT | INITIAL_STRONG_VALUE))) != 0) |
| 129 | |
| 130 | // Same for weak counts. |
| 131 | #define BAD_WEAK(c) ((c) == 0 || ((c) & (~MAX_COUNT)) != 0) |
| 132 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | // --------------------------------------------------------------------------- |
| 134 | |
| 135 | class RefBase::weakref_impl : public RefBase::weakref_type |
| 136 | { |
| 137 | public: |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 138 | std::atomic<int32_t> mStrong; |
| 139 | std::atomic<int32_t> mWeak; |
| 140 | RefBase* const mBase; |
| 141 | std::atomic<int32_t> mFlags; |
Mathias Agopian | 9c8fa9e | 2011-06-15 20:42:47 -0700 | [diff] [blame] | 142 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | #if !DEBUG_REFS |
| 144 | |
Chih-Hung Hsieh | 1c563d9 | 2016-04-29 15:44:04 -0700 | [diff] [blame] | 145 | explicit weakref_impl(RefBase* base) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | : mStrong(INITIAL_STRONG_VALUE) |
| 147 | , mWeak(0) |
| 148 | , mBase(base) |
| 149 | , mFlags(0) |
| 150 | { |
| 151 | } |
| 152 | |
| 153 | void addStrongRef(const void* /*id*/) { } |
| 154 | void removeStrongRef(const void* /*id*/) { } |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 155 | void renameStrongRefId(const void* /*old_id*/, const void* /*new_id*/) { } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | void addWeakRef(const void* /*id*/) { } |
| 157 | void removeWeakRef(const void* /*id*/) { } |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 158 | void renameWeakRefId(const void* /*old_id*/, const void* /*new_id*/) { } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | void printRefs() const { } |
| 160 | void trackMe(bool, bool) { } |
| 161 | |
| 162 | #else |
| 163 | |
| 164 | weakref_impl(RefBase* base) |
| 165 | : mStrong(INITIAL_STRONG_VALUE) |
| 166 | , mWeak(0) |
| 167 | , mBase(base) |
| 168 | , mFlags(0) |
| 169 | , mStrongRefs(NULL) |
| 170 | , mWeakRefs(NULL) |
| 171 | , mTrackEnabled(!!DEBUG_REFS_ENABLED_BY_DEFAULT) |
| 172 | , mRetain(false) |
| 173 | { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | ~weakref_impl() |
| 177 | { |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 178 | bool dumpStack = false; |
| 179 | if (!mRetain && mStrongRefs != NULL) { |
| 180 | dumpStack = true; |
Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 181 | ALOGE("Strong references remain:"); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 182 | ref_entry* refs = mStrongRefs; |
| 183 | while (refs) { |
| 184 | char inc = refs->ref >= 0 ? '+' : '-'; |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 185 | ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 186 | #if DEBUG_REFS_CALLSTACK_ENABLED |
Ian McKellar | 55e0f1c | 2014-03-31 15:59:31 -0700 | [diff] [blame] | 187 | refs->stack.log(LOG_TAG); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 188 | #endif |
| 189 | refs = refs->next; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if (!mRetain && mWeakRefs != NULL) { |
| 194 | dumpStack = true; |
Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 195 | ALOGE("Weak references remain!"); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 196 | ref_entry* refs = mWeakRefs; |
| 197 | while (refs) { |
| 198 | char inc = refs->ref >= 0 ? '+' : '-'; |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 199 | ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 200 | #if DEBUG_REFS_CALLSTACK_ENABLED |
Ian McKellar | 55e0f1c | 2014-03-31 15:59:31 -0700 | [diff] [blame] | 201 | refs->stack.log(LOG_TAG); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 202 | #endif |
| 203 | refs = refs->next; |
| 204 | } |
| 205 | } |
| 206 | if (dumpStack) { |
Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 207 | ALOGE("above errors at:"); |
Mathias Agopian | d34a8ca | 2013-03-21 17:12:40 -0700 | [diff] [blame] | 208 | CallStack stack(LOG_TAG); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 209 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 212 | void addStrongRef(const void* id) { |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 213 | //ALOGD_IF(mTrackEnabled, |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 214 | // "addStrongRef: RefBase=%p, id=%p", mBase, id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 215 | addRef(&mStrongRefs, id, mStrong.load(std::memory_order_relaxed)); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 218 | void removeStrongRef(const void* id) { |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 219 | //ALOGD_IF(mTrackEnabled, |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 220 | // "removeStrongRef: RefBase=%p, id=%p", mBase, id); |
| 221 | if (!mRetain) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | removeRef(&mStrongRefs, id); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 223 | } else { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 224 | addRef(&mStrongRefs, id, -mStrong.load(std::memory_order_relaxed)); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 225 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 228 | void renameStrongRefId(const void* old_id, const void* new_id) { |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 229 | //ALOGD_IF(mTrackEnabled, |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 230 | // "renameStrongRefId: RefBase=%p, oid=%p, nid=%p", |
| 231 | // mBase, old_id, new_id); |
| 232 | renameRefsId(mStrongRefs, old_id, new_id); |
| 233 | } |
| 234 | |
| 235 | void addWeakRef(const void* id) { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 236 | addRef(&mWeakRefs, id, mWeak.load(std::memory_order_relaxed)); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 239 | void removeWeakRef(const void* id) { |
| 240 | if (!mRetain) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | removeRef(&mWeakRefs, id); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 242 | } else { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 243 | addRef(&mWeakRefs, id, -mWeak.load(std::memory_order_relaxed)); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | void renameWeakRefId(const void* old_id, const void* new_id) { |
| 248 | renameRefsId(mWeakRefs, old_id, new_id); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void trackMe(bool track, bool retain) |
| 252 | { |
| 253 | mTrackEnabled = track; |
| 254 | mRetain = retain; |
| 255 | } |
| 256 | |
| 257 | void printRefs() const |
| 258 | { |
| 259 | String8 text; |
| 260 | |
| 261 | { |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 262 | Mutex::Autolock _l(mMutex); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | char buf[128]; |
George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 264 | snprintf(buf, sizeof(buf), |
| 265 | "Strong references on RefBase %p (weakref_type %p):\n", |
| 266 | mBase, this); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | text.append(buf); |
| 268 | printRefsLocked(&text, mStrongRefs); |
George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 269 | snprintf(buf, sizeof(buf), |
| 270 | "Weak references on RefBase %p (weakref_type %p):\n", |
| 271 | mBase, this); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | text.append(buf); |
| 273 | printRefsLocked(&text, mWeakRefs); |
| 274 | } |
| 275 | |
| 276 | { |
| 277 | char name[100]; |
George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 278 | snprintf(name, sizeof(name), DEBUG_REFS_CALLSTACK_PATH "/%p.stack", |
| 279 | this); |
Mathias Agopian | 769828d | 2013-03-06 17:51:15 -0800 | [diff] [blame] | 280 | int rc = open(name, O_RDWR | O_CREAT | O_APPEND, 644); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | if (rc >= 0) { |
| 282 | write(rc, text.string(), text.length()); |
| 283 | close(rc); |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 284 | ALOGD("STACK TRACE for %p saved in %s", this, name); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | } |
Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 286 | else ALOGE("FAILED TO PRINT STACK TRACE for %p in %s: %s", this, |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | name, strerror(errno)); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | private: |
| 292 | struct ref_entry |
| 293 | { |
| 294 | ref_entry* next; |
| 295 | const void* id; |
| 296 | #if DEBUG_REFS_CALLSTACK_ENABLED |
| 297 | CallStack stack; |
| 298 | #endif |
| 299 | int32_t ref; |
| 300 | }; |
| 301 | |
| 302 | void addRef(ref_entry** refs, const void* id, int32_t mRef) |
| 303 | { |
| 304 | if (mTrackEnabled) { |
| 305 | AutoMutex _l(mMutex); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 306 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | ref_entry* ref = new ref_entry; |
| 308 | // Reference count at the time of the snapshot, but before the |
| 309 | // update. Positive value means we increment, negative--we |
| 310 | // decrement the reference count. |
| 311 | ref->ref = mRef; |
| 312 | ref->id = id; |
| 313 | #if DEBUG_REFS_CALLSTACK_ENABLED |
| 314 | ref->stack.update(2); |
| 315 | #endif |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | ref->next = *refs; |
| 317 | *refs = ref; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | void removeRef(ref_entry** refs, const void* id) |
| 322 | { |
| 323 | if (mTrackEnabled) { |
| 324 | AutoMutex _l(mMutex); |
| 325 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 326 | ref_entry* const head = *refs; |
| 327 | ref_entry* ref = head; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | while (ref != NULL) { |
| 329 | if (ref->id == id) { |
| 330 | *refs = ref->next; |
| 331 | delete ref; |
| 332 | return; |
| 333 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | refs = &ref->next; |
| 335 | ref = *refs; |
| 336 | } |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 337 | |
Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 338 | ALOGE("RefBase: removing id %p on RefBase %p" |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 339 | "(weakref_type %p) that doesn't exist!", |
| 340 | id, mBase, this); |
| 341 | |
| 342 | ref = head; |
| 343 | while (ref) { |
| 344 | char inc = ref->ref >= 0 ? '+' : '-'; |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 345 | ALOGD("\t%c ID %p (ref %d):", inc, ref->id, ref->ref); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 346 | ref = ref->next; |
| 347 | } |
| 348 | |
Mathias Agopian | d34a8ca | 2013-03-21 17:12:40 -0700 | [diff] [blame] | 349 | CallStack stack(LOG_TAG); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | void renameRefsId(ref_entry* r, const void* old_id, const void* new_id) |
| 354 | { |
| 355 | if (mTrackEnabled) { |
| 356 | AutoMutex _l(mMutex); |
| 357 | ref_entry* ref = r; |
| 358 | while (ref != NULL) { |
| 359 | if (ref->id == old_id) { |
| 360 | ref->id = new_id; |
| 361 | } |
| 362 | ref = ref->next; |
| 363 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
| 367 | void printRefsLocked(String8* out, const ref_entry* refs) const |
| 368 | { |
| 369 | char buf[128]; |
| 370 | while (refs) { |
| 371 | char inc = refs->ref >= 0 ? '+' : '-'; |
George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 372 | snprintf(buf, sizeof(buf), "\t%c ID %p (ref %d):\n", |
| 373 | inc, refs->id, refs->ref); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | out->append(buf); |
| 375 | #if DEBUG_REFS_CALLSTACK_ENABLED |
| 376 | out->append(refs->stack.toString("\t\t")); |
| 377 | #else |
| 378 | out->append("\t\t(call stacks disabled)"); |
| 379 | #endif |
| 380 | refs = refs->next; |
| 381 | } |
| 382 | } |
| 383 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 384 | mutable Mutex mMutex; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | ref_entry* mStrongRefs; |
| 386 | ref_entry* mWeakRefs; |
| 387 | |
| 388 | bool mTrackEnabled; |
| 389 | // Collect stack traces on addref and removeref, instead of deleting the stack references |
| 390 | // on removeref that match the address ones. |
| 391 | bool mRetain; |
| 392 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | #endif |
| 394 | }; |
| 395 | |
| 396 | // --------------------------------------------------------------------------- |
| 397 | |
| 398 | void RefBase::incStrong(const void* id) const |
| 399 | { |
| 400 | weakref_impl* const refs = mRefs; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | refs->incWeak(id); |
| 402 | |
| 403 | refs->addStrongRef(id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 404 | const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 405 | ALOG_ASSERT(c > 0, "incStrong() called on %p after last strong ref", refs); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | #if PRINT_REFS |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 407 | ALOGD("incStrong of %p from %p: cnt=%d\n", this, id, c); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | #endif |
| 409 | if (c != INITIAL_STRONG_VALUE) { |
| 410 | return; |
| 411 | } |
| 412 | |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 413 | int32_t old = refs->mStrong.fetch_sub(INITIAL_STRONG_VALUE, |
| 414 | std::memory_order_relaxed); |
| 415 | // A decStrong() must still happen after us. |
| 416 | ALOG_ASSERT(old > INITIAL_STRONG_VALUE, "0x%x too small", old); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 417 | refs->mBase->onFirstRef(); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | void RefBase::decStrong(const void* id) const |
| 421 | { |
| 422 | weakref_impl* const refs = mRefs; |
| 423 | refs->removeStrongRef(id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 424 | const int32_t c = refs->mStrong.fetch_sub(1, std::memory_order_release); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | #if PRINT_REFS |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 426 | ALOGD("decStrong of %p from %p: cnt=%d\n", this, id, c); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | #endif |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 428 | LOG_ALWAYS_FATAL_IF(BAD_STRONG(c), "decStrong() called on %p too many times", |
| 429 | refs); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | if (c == 1) { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 431 | std::atomic_thread_fence(std::memory_order_acquire); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 432 | refs->mBase->onLastStrongRef(id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 433 | int32_t flags = refs->mFlags.load(std::memory_order_relaxed); |
| 434 | if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) { |
Mathias Agopian | 9c8fa9e | 2011-06-15 20:42:47 -0700 | [diff] [blame] | 435 | delete this; |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 436 | // The destructor does not delete refs in this case. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | } |
| 438 | } |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 439 | // Note that even with only strong reference operations, the thread |
| 440 | // deallocating this may not be the same as the thread deallocating refs. |
| 441 | // That's OK: all accesses to this happen before its deletion here, |
| 442 | // and all accesses to refs happen before its deletion in the final decWeak. |
| 443 | // The destructor can safely access mRefs because either it's deleting |
| 444 | // mRefs itself, or it's running entirely before the final mWeak decrement. |
George Burgess IV | 6753bc4 | 2017-10-01 12:38:44 -0700 | [diff] [blame] | 445 | // |
| 446 | // Since we're doing atomic loads of `flags`, the static analyzer assumes |
| 447 | // they can change between `delete this;` and `refs->decWeak(id);`. This is |
| 448 | // not the case. The analyzer may become more okay with this patten when |
| 449 | // https://bugs.llvm.org/show_bug.cgi?id=34365 gets resolved. NOLINTNEXTLINE |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | refs->decWeak(id); |
| 451 | } |
| 452 | |
| 453 | void RefBase::forceIncStrong(const void* id) const |
| 454 | { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 455 | // Allows initial mStrong of 0 in addition to INITIAL_STRONG_VALUE. |
| 456 | // TODO: Better document assumptions. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | weakref_impl* const refs = mRefs; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | refs->incWeak(id); |
| 459 | |
| 460 | refs->addStrongRef(id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 461 | const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 462 | ALOG_ASSERT(c >= 0, "forceIncStrong called on %p after ref count underflow", |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | refs); |
| 464 | #if PRINT_REFS |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 465 | ALOGD("forceIncStrong of %p from %p: cnt=%d\n", this, id, c); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | #endif |
| 467 | |
| 468 | switch (c) { |
| 469 | case INITIAL_STRONG_VALUE: |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 470 | refs->mStrong.fetch_sub(INITIAL_STRONG_VALUE, |
| 471 | std::memory_order_relaxed); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | // fall through... |
| 473 | case 0: |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 474 | refs->mBase->onFirstRef(); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
| 478 | int32_t RefBase::getStrongCount() const |
| 479 | { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 480 | // Debugging only; No memory ordering guarantees. |
| 481 | return mRefs->mStrong.load(std::memory_order_relaxed); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | } |
| 483 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | RefBase* RefBase::weakref_type::refBase() const |
| 485 | { |
| 486 | return static_cast<const weakref_impl*>(this)->mBase; |
| 487 | } |
| 488 | |
| 489 | void RefBase::weakref_type::incWeak(const void* id) |
| 490 | { |
| 491 | weakref_impl* const impl = static_cast<weakref_impl*>(this); |
| 492 | impl->addWeakRef(id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 493 | const int32_t c __unused = impl->mWeak.fetch_add(1, |
| 494 | std::memory_order_relaxed); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 495 | ALOG_ASSERT(c >= 0, "incWeak called on %p after last weak ref", this); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 498 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | void RefBase::weakref_type::decWeak(const void* id) |
| 500 | { |
| 501 | weakref_impl* const impl = static_cast<weakref_impl*>(this); |
| 502 | impl->removeWeakRef(id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 503 | const int32_t c = impl->mWeak.fetch_sub(1, std::memory_order_release); |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 504 | LOG_ALWAYS_FATAL_IF(BAD_WEAK(c), "decWeak called on %p too many times", |
| 505 | this); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | if (c != 1) return; |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 507 | atomic_thread_fence(std::memory_order_acquire); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 508 | |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 509 | int32_t flags = impl->mFlags.load(std::memory_order_relaxed); |
| 510 | if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) { |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 511 | // This is the regular lifetime case. The object is destroyed |
| 512 | // when the last strong reference goes away. Since weakref_impl |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 513 | // outlives the object, it is not destroyed in the dtor, and |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 514 | // we'll have to do it here. |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 515 | if (impl->mStrong.load(std::memory_order_relaxed) |
| 516 | == INITIAL_STRONG_VALUE) { |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 517 | // Decrementing a weak count to zero when object never had a strong |
| 518 | // reference. We assume it acquired a weak reference early, e.g. |
| 519 | // in the constructor, and will eventually be properly destroyed, |
| 520 | // usually via incrementing and decrementing the strong count. |
| 521 | // Thus we no longer do anything here. We log this case, since it |
| 522 | // seems to be extremely rare, and should not normally occur. We |
| 523 | // used to deallocate mBase here, so this may now indicate a leak. |
| 524 | ALOGW("RefBase: Object at %p lost last weak reference " |
| 525 | "before it had a strong reference", impl->mBase); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 526 | } else { |
Steve Block | b37fbe9 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 527 | // ALOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | delete impl; |
| 529 | } |
| 530 | } else { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 531 | // This is the OBJECT_LIFETIME_WEAK case. The last weak-reference |
| 532 | // is gone, we can destroy the object. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | impl->mBase->onLastWeakRef(id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 534 | delete impl->mBase; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
| 538 | bool RefBase::weakref_type::attemptIncStrong(const void* id) |
| 539 | { |
| 540 | incWeak(id); |
| 541 | |
| 542 | weakref_impl* const impl = static_cast<weakref_impl*>(this); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 543 | int32_t curCount = impl->mStrong.load(std::memory_order_relaxed); |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 544 | |
| 545 | ALOG_ASSERT(curCount >= 0, |
| 546 | "attemptIncStrong called on %p after underflow", this); |
| 547 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | while (curCount > 0 && curCount != INITIAL_STRONG_VALUE) { |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 549 | // we're in the easy/common case of promoting a weak-reference |
| 550 | // from an existing strong reference. |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 551 | if (impl->mStrong.compare_exchange_weak(curCount, curCount+1, |
| 552 | std::memory_order_relaxed)) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | break; |
| 554 | } |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 555 | // the strong count has changed on us, we need to re-assert our |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 556 | // situation. curCount was updated by compare_exchange_weak. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | if (curCount <= 0 || curCount == INITIAL_STRONG_VALUE) { |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 560 | // we're now in the harder case of either: |
| 561 | // - there never was a strong reference on us |
| 562 | // - or, all strong references have been released |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 563 | int32_t flags = impl->mFlags.load(std::memory_order_relaxed); |
| 564 | if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) { |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 565 | // this object has a "normal" life-time, i.e.: it gets destroyed |
| 566 | // when the last strong reference goes away |
| 567 | if (curCount <= 0) { |
| 568 | // the last strong-reference got released, the object cannot |
| 569 | // be revived. |
| 570 | decWeak(id); |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | // here, curCount == INITIAL_STRONG_VALUE, which means |
| 575 | // there never was a strong-reference, so we can try to |
| 576 | // promote this object; we need to do that atomically. |
| 577 | while (curCount > 0) { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 578 | if (impl->mStrong.compare_exchange_weak(curCount, curCount+1, |
| 579 | std::memory_order_relaxed)) { |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 580 | break; |
| 581 | } |
| 582 | // the strong count has changed on us, we need to re-assert our |
| 583 | // situation (e.g.: another thread has inc/decStrong'ed us) |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 584 | // curCount has been updated. |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | if (curCount <= 0) { |
| 588 | // promote() failed, some other thread destroyed us in the |
| 589 | // meantime (i.e.: strong count reached zero). |
| 590 | decWeak(id); |
| 591 | return false; |
| 592 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | } else { |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 594 | // this object has an "extended" life-time, i.e.: it can be |
| 595 | // revived from a weak-reference only. |
| 596 | // Ask the object's implementation if it agrees to be revived |
| 597 | if (!impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id)) { |
| 598 | // it didn't so give-up. |
| 599 | decWeak(id); |
| 600 | return false; |
| 601 | } |
| 602 | // grab a strong-reference, which is always safe due to the |
| 603 | // extended life-time. |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 604 | curCount = impl->mStrong.fetch_add(1, std::memory_order_relaxed); |
Hans Boehm | 7f27cbc | 2016-07-29 14:39:10 -0700 | [diff] [blame] | 605 | // If the strong reference count has already been incremented by |
| 606 | // someone else, the implementor of onIncStrongAttempted() is holding |
| 607 | // an unneeded reference. So call onLastStrongRef() here to remove it. |
| 608 | // (No, this is not pretty.) Note that we MUST NOT do this if we |
| 609 | // are in fact acquiring the first reference. |
| 610 | if (curCount != 0 && curCount != INITIAL_STRONG_VALUE) { |
| 611 | impl->mBase->onLastStrongRef(id); |
| 612 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | impl->addStrongRef(id); |
| 617 | |
| 618 | #if PRINT_REFS |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 619 | ALOGD("attemptIncStrong of %p from %p: cnt=%d\n", this, id, curCount); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 620 | #endif |
| 621 | |
Hans Boehm | 7f27cbc | 2016-07-29 14:39:10 -0700 | [diff] [blame] | 622 | // curCount is the value of mStrong before we incremented it. |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 623 | // Now we need to fix-up the count if it was INITIAL_STRONG_VALUE. |
| 624 | // This must be done safely, i.e.: handle the case where several threads |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 625 | // were here in attemptIncStrong(). |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 626 | // curCount > INITIAL_STRONG_VALUE is OK, and can happen if we're doing |
| 627 | // this in the middle of another incStrong. The subtraction is handled |
| 628 | // by the thread that started with INITIAL_STRONG_VALUE. |
| 629 | if (curCount == INITIAL_STRONG_VALUE) { |
| 630 | impl->mStrong.fetch_sub(INITIAL_STRONG_VALUE, |
| 631 | std::memory_order_relaxed); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | } |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 633 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | return true; |
| 635 | } |
| 636 | |
| 637 | bool RefBase::weakref_type::attemptIncWeak(const void* id) |
| 638 | { |
| 639 | weakref_impl* const impl = static_cast<weakref_impl*>(this); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 640 | |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 641 | int32_t curCount = impl->mWeak.load(std::memory_order_relaxed); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 642 | ALOG_ASSERT(curCount >= 0, "attemptIncWeak called on %p after underflow", |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | this); |
| 644 | while (curCount > 0) { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 645 | if (impl->mWeak.compare_exchange_weak(curCount, curCount+1, |
| 646 | std::memory_order_relaxed)) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 647 | break; |
| 648 | } |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 649 | // curCount has been updated. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | if (curCount > 0) { |
| 653 | impl->addWeakRef(id); |
| 654 | } |
| 655 | |
| 656 | return curCount > 0; |
| 657 | } |
| 658 | |
| 659 | int32_t RefBase::weakref_type::getWeakCount() const |
| 660 | { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 661 | // Debug only! |
| 662 | return static_cast<const weakref_impl*>(this)->mWeak |
| 663 | .load(std::memory_order_relaxed); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | void RefBase::weakref_type::printRefs() const |
| 667 | { |
| 668 | static_cast<const weakref_impl*>(this)->printRefs(); |
| 669 | } |
| 670 | |
| 671 | void RefBase::weakref_type::trackMe(bool enable, bool retain) |
| 672 | { |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 673 | static_cast<weakref_impl*>(this)->trackMe(enable, retain); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | RefBase::weakref_type* RefBase::createWeak(const void* id) const |
| 677 | { |
| 678 | mRefs->incWeak(id); |
| 679 | return mRefs; |
| 680 | } |
| 681 | |
| 682 | RefBase::weakref_type* RefBase::getWeakRefs() const |
| 683 | { |
| 684 | return mRefs; |
| 685 | } |
| 686 | |
| 687 | RefBase::RefBase() |
| 688 | : mRefs(new weakref_impl(this)) |
| 689 | { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | RefBase::~RefBase() |
| 693 | { |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 694 | int32_t flags = mRefs->mFlags.load(std::memory_order_relaxed); |
| 695 | // Life-time of this object is extended to WEAK, in |
| 696 | // which case weakref_impl doesn't out-live the object and we |
| 697 | // can free it now. |
| 698 | if ((flags & OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_WEAK) { |
| 699 | // It's possible that the weak count is not 0 if the object |
| 700 | // re-acquired a weak reference in its destructor |
| 701 | if (mRefs->mWeak.load(std::memory_order_relaxed) == 0) { |
| 702 | delete mRefs; |
| 703 | } |
| 704 | } else if (mRefs->mStrong.load(std::memory_order_relaxed) |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 705 | == INITIAL_STRONG_VALUE) { |
Hans Boehm | 9ba7192 | 2016-07-21 18:56:55 -0700 | [diff] [blame] | 706 | // We never acquired a strong reference on this object. |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 707 | LOG_ALWAYS_FATAL_IF(mRefs->mWeak.load() != 0, |
| 708 | "RefBase: Explicit destruction with non-zero weak " |
| 709 | "reference count"); |
| 710 | // TODO: Always report if we get here. Currently MediaMetadataRetriever |
| 711 | // C++ objects are inconsistently managed and sometimes get here. |
| 712 | // There may be other cases, but we believe they should all be fixed. |
Mathias Agopian | 9c8fa9e | 2011-06-15 20:42:47 -0700 | [diff] [blame] | 713 | delete mRefs; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | } |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 715 | // For debugging purposes, clear mRefs. Ineffective against outstanding wp's. |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 716 | const_cast<weakref_impl*&>(mRefs) = NULL; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | void RefBase::extendObjectLifetime(int32_t mode) |
| 720 | { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 721 | // Must be happens-before ordered with respect to construction or any |
| 722 | // operation that could destroy the object. |
| 723 | mRefs->mFlags.fetch_or(mode, std::memory_order_relaxed); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | void RefBase::onFirstRef() |
| 727 | { |
| 728 | } |
| 729 | |
| 730 | void RefBase::onLastStrongRef(const void* /*id*/) |
| 731 | { |
| 732 | } |
| 733 | |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 734 | bool RefBase::onIncStrongAttempted(uint32_t flags, const void* /*id*/) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | { |
| 736 | return (flags&FIRST_INC_STRONG) ? true : false; |
| 737 | } |
| 738 | |
| 739 | void RefBase::onLastWeakRef(const void* /*id*/) |
| 740 | { |
| 741 | } |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 742 | |
| 743 | // --------------------------------------------------------------------------- |
| 744 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 745 | #if DEBUG_REFS |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 746 | void RefBase::renameRefs(size_t n, const ReferenceRenamer& renamer) { |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 747 | for (size_t i=0 ; i<n ; i++) { |
Mathias Agopian | 6cd548c | 2013-03-18 22:27:41 -0700 | [diff] [blame] | 748 | renamer(i); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 749 | } |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 750 | } |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 751 | #else |
| 752 | void RefBase::renameRefs(size_t /*n*/, const ReferenceRenamer& /*renamer*/) { } |
| 753 | #endif |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 754 | |
Mathias Agopian | 6cd548c | 2013-03-18 22:27:41 -0700 | [diff] [blame] | 755 | void RefBase::renameRefId(weakref_type* ref, |
| 756 | const void* old_id, const void* new_id) { |
| 757 | weakref_impl* const impl = static_cast<weakref_impl*>(ref); |
| 758 | impl->renameStrongRefId(old_id, new_id); |
| 759 | impl->renameWeakRefId(old_id, new_id); |
| 760 | } |
| 761 | |
| 762 | void RefBase::renameRefId(RefBase* ref, |
| 763 | const void* old_id, const void* new_id) { |
| 764 | ref->mRefs->renameStrongRefId(old_id, new_id); |
| 765 | ref->mRefs->renameWeakRefId(old_id, new_id); |
| 766 | } |
| 767 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 768 | }; // namespace android |