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