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