blob: 02907ad670716fd33832da1634804774c7bdf50e [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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 Agopianda8ec4b2013-03-19 17:36:57 -070018// #define LOG_NDEBUG 0
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080019
Mark Salyzyn5bed8032014-04-30 11:10:46 -070020#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 Projectcbb10112009-03-03 19:31:44 -080028#include <utils/RefBase.h>
29
30#include <utils/Atomic.h>
31#include <utils/CallStack.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080032#include <utils/Log.h>
33#include <utils/threads.h>
34
Mark Salyzyn5bed8032014-04-30 11:10:46 -070035#ifndef __unused
36#define __unused __attribute__((__unused__))
37#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080038
39// compile with refcounting debugging enabled
40#define DEBUG_REFS 0
Mathias Agopian6d4419d2013-03-18 20:31:18 -070041
42// whether ref-tracking is enabled by default, if not, trackMe(true, false)
43// needs to be called explicitly
44#define DEBUG_REFS_ENABLED_BY_DEFAULT 0
45
46// whether callstack are collected (significantly slows things down)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080047#define DEBUG_REFS_CALLSTACK_ENABLED 1
48
Mathias Agopian6d4419d2013-03-18 20:31:18 -070049// folder where stack traces are saved when DEBUG_REFS is enabled
50// this folder needs to exist and be writable
51#define DEBUG_REFS_CALLSTACK_PATH "/data/debug"
52
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080053// log all reference counting operations
54#define PRINT_REFS 0
55
56// ---------------------------------------------------------------------------
57
58namespace android {
59
60#define INITIAL_STRONG_VALUE (1<<28)
61
62// ---------------------------------------------------------------------------
63
64class RefBase::weakref_impl : public RefBase::weakref_type
65{
66public:
67 volatile int32_t mStrong;
68 volatile int32_t mWeak;
69 RefBase* const mBase;
70 volatile int32_t mFlags;
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -070071
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080072#if !DEBUG_REFS
73
74 weakref_impl(RefBase* base)
75 : mStrong(INITIAL_STRONG_VALUE)
76 , mWeak(0)
77 , mBase(base)
78 , mFlags(0)
79 {
80 }
81
82 void addStrongRef(const void* /*id*/) { }
83 void removeStrongRef(const void* /*id*/) { }
Mathias Agopianad099652011-08-10 21:07:02 -070084 void renameStrongRefId(const void* /*old_id*/, const void* /*new_id*/) { }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080085 void addWeakRef(const void* /*id*/) { }
86 void removeWeakRef(const void* /*id*/) { }
Mathias Agopianad099652011-08-10 21:07:02 -070087 void renameWeakRefId(const void* /*old_id*/, const void* /*new_id*/) { }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080088 void printRefs() const { }
89 void trackMe(bool, bool) { }
90
91#else
92
93 weakref_impl(RefBase* base)
94 : mStrong(INITIAL_STRONG_VALUE)
95 , mWeak(0)
96 , mBase(base)
97 , mFlags(0)
98 , mStrongRefs(NULL)
99 , mWeakRefs(NULL)
100 , mTrackEnabled(!!DEBUG_REFS_ENABLED_BY_DEFAULT)
101 , mRetain(false)
102 {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800103 }
104
105 ~weakref_impl()
106 {
Mathias Agopianad099652011-08-10 21:07:02 -0700107 bool dumpStack = false;
108 if (!mRetain && mStrongRefs != NULL) {
109 dumpStack = true;
Steve Block1b781ab2012-01-06 19:20:56 +0000110 ALOGE("Strong references remain:");
Mathias Agopianad099652011-08-10 21:07:02 -0700111 ref_entry* refs = mStrongRefs;
112 while (refs) {
113 char inc = refs->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000114 ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
Mathias Agopianad099652011-08-10 21:07:02 -0700115#if DEBUG_REFS_CALLSTACK_ENABLED
Ian McKellar55e0f1c2014-03-31 15:59:31 -0700116 refs->stack.log(LOG_TAG);
Mathias Agopianad099652011-08-10 21:07:02 -0700117#endif
118 refs = refs->next;
119 }
120 }
121
122 if (!mRetain && mWeakRefs != NULL) {
123 dumpStack = true;
Steve Block1b781ab2012-01-06 19:20:56 +0000124 ALOGE("Weak references remain!");
Mathias Agopianad099652011-08-10 21:07:02 -0700125 ref_entry* refs = mWeakRefs;
126 while (refs) {
127 char inc = refs->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000128 ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
Mathias Agopianad099652011-08-10 21:07:02 -0700129#if DEBUG_REFS_CALLSTACK_ENABLED
Ian McKellar55e0f1c2014-03-31 15:59:31 -0700130 refs->stack.log(LOG_TAG);
Mathias Agopianad099652011-08-10 21:07:02 -0700131#endif
132 refs = refs->next;
133 }
134 }
135 if (dumpStack) {
Steve Block1b781ab2012-01-06 19:20:56 +0000136 ALOGE("above errors at:");
Mathias Agopiand34a8ca2013-03-21 17:12:40 -0700137 CallStack stack(LOG_TAG);
Mathias Agopianad099652011-08-10 21:07:02 -0700138 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800139 }
140
Mathias Agopianad099652011-08-10 21:07:02 -0700141 void addStrongRef(const void* id) {
Steve Blockeb095332011-12-20 16:23:08 +0000142 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700143 // "addStrongRef: RefBase=%p, id=%p", mBase, id);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800144 addRef(&mStrongRefs, id, mStrong);
145 }
146
Mathias Agopianad099652011-08-10 21:07:02 -0700147 void removeStrongRef(const void* id) {
Steve Blockeb095332011-12-20 16:23:08 +0000148 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700149 // "removeStrongRef: RefBase=%p, id=%p", mBase, id);
150 if (!mRetain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800151 removeRef(&mStrongRefs, id);
Mathias Agopianad099652011-08-10 21:07:02 -0700152 } else {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800153 addRef(&mStrongRefs, id, -mStrong);
Mathias Agopianad099652011-08-10 21:07:02 -0700154 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800155 }
156
Mathias Agopianad099652011-08-10 21:07:02 -0700157 void renameStrongRefId(const void* old_id, const void* new_id) {
Steve Blockeb095332011-12-20 16:23:08 +0000158 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700159 // "renameStrongRefId: RefBase=%p, oid=%p, nid=%p",
160 // mBase, old_id, new_id);
161 renameRefsId(mStrongRefs, old_id, new_id);
162 }
163
164 void addWeakRef(const void* id) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800165 addRef(&mWeakRefs, id, mWeak);
166 }
167
Mathias Agopianad099652011-08-10 21:07:02 -0700168 void removeWeakRef(const void* id) {
169 if (!mRetain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800170 removeRef(&mWeakRefs, id);
Mathias Agopianad099652011-08-10 21:07:02 -0700171 } else {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800172 addRef(&mWeakRefs, id, -mWeak);
Mathias Agopianad099652011-08-10 21:07:02 -0700173 }
174 }
175
176 void renameWeakRefId(const void* old_id, const void* new_id) {
177 renameRefsId(mWeakRefs, old_id, new_id);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800178 }
179
180 void trackMe(bool track, bool retain)
181 {
182 mTrackEnabled = track;
183 mRetain = retain;
184 }
185
186 void printRefs() const
187 {
188 String8 text;
189
190 {
Mathias Agopianad099652011-08-10 21:07:02 -0700191 Mutex::Autolock _l(mMutex);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800192 char buf[128];
193 sprintf(buf, "Strong references on RefBase %p (weakref_type %p):\n", mBase, this);
194 text.append(buf);
195 printRefsLocked(&text, mStrongRefs);
196 sprintf(buf, "Weak references on RefBase %p (weakref_type %p):\n", mBase, this);
197 text.append(buf);
198 printRefsLocked(&text, mWeakRefs);
199 }
200
201 {
202 char name[100];
Mathias Agopian6d4419d2013-03-18 20:31:18 -0700203 snprintf(name, 100, DEBUG_REFS_CALLSTACK_PATH "/%p.stack", this);
Mathias Agopian769828d2013-03-06 17:51:15 -0800204 int rc = open(name, O_RDWR | O_CREAT | O_APPEND, 644);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800205 if (rc >= 0) {
206 write(rc, text.string(), text.length());
207 close(rc);
Steve Blockeb095332011-12-20 16:23:08 +0000208 ALOGD("STACK TRACE for %p saved in %s", this, name);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800209 }
Steve Block1b781ab2012-01-06 19:20:56 +0000210 else ALOGE("FAILED TO PRINT STACK TRACE for %p in %s: %s", this,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800211 name, strerror(errno));
212 }
213 }
214
215private:
216 struct ref_entry
217 {
218 ref_entry* next;
219 const void* id;
220#if DEBUG_REFS_CALLSTACK_ENABLED
221 CallStack stack;
222#endif
223 int32_t ref;
224 };
225
226 void addRef(ref_entry** refs, const void* id, int32_t mRef)
227 {
228 if (mTrackEnabled) {
229 AutoMutex _l(mMutex);
Mathias Agopianad099652011-08-10 21:07:02 -0700230
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800231 ref_entry* ref = new ref_entry;
232 // Reference count at the time of the snapshot, but before the
233 // update. Positive value means we increment, negative--we
234 // decrement the reference count.
235 ref->ref = mRef;
236 ref->id = id;
237#if DEBUG_REFS_CALLSTACK_ENABLED
238 ref->stack.update(2);
239#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800240 ref->next = *refs;
241 *refs = ref;
242 }
243 }
244
245 void removeRef(ref_entry** refs, const void* id)
246 {
247 if (mTrackEnabled) {
248 AutoMutex _l(mMutex);
249
Mathias Agopianad099652011-08-10 21:07:02 -0700250 ref_entry* const head = *refs;
251 ref_entry* ref = head;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800252 while (ref != NULL) {
253 if (ref->id == id) {
254 *refs = ref->next;
255 delete ref;
256 return;
257 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800258 refs = &ref->next;
259 ref = *refs;
260 }
Mathias Agopianad099652011-08-10 21:07:02 -0700261
Steve Block1b781ab2012-01-06 19:20:56 +0000262 ALOGE("RefBase: removing id %p on RefBase %p"
Mathias Agopianad099652011-08-10 21:07:02 -0700263 "(weakref_type %p) that doesn't exist!",
264 id, mBase, this);
265
266 ref = head;
267 while (ref) {
268 char inc = ref->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000269 ALOGD("\t%c ID %p (ref %d):", inc, ref->id, ref->ref);
Mathias Agopianad099652011-08-10 21:07:02 -0700270 ref = ref->next;
271 }
272
Mathias Agopiand34a8ca2013-03-21 17:12:40 -0700273 CallStack stack(LOG_TAG);
Mathias Agopianad099652011-08-10 21:07:02 -0700274 }
275 }
276
277 void renameRefsId(ref_entry* r, const void* old_id, const void* new_id)
278 {
279 if (mTrackEnabled) {
280 AutoMutex _l(mMutex);
281 ref_entry* ref = r;
282 while (ref != NULL) {
283 if (ref->id == old_id) {
284 ref->id = new_id;
285 }
286 ref = ref->next;
287 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800288 }
289 }
290
291 void printRefsLocked(String8* out, const ref_entry* refs) const
292 {
293 char buf[128];
294 while (refs) {
295 char inc = refs->ref >= 0 ? '+' : '-';
296 sprintf(buf, "\t%c ID %p (ref %d):\n",
297 inc, refs->id, refs->ref);
298 out->append(buf);
299#if DEBUG_REFS_CALLSTACK_ENABLED
300 out->append(refs->stack.toString("\t\t"));
301#else
302 out->append("\t\t(call stacks disabled)");
303#endif
304 refs = refs->next;
305 }
306 }
307
Mathias Agopianad099652011-08-10 21:07:02 -0700308 mutable Mutex mMutex;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800309 ref_entry* mStrongRefs;
310 ref_entry* mWeakRefs;
311
312 bool mTrackEnabled;
313 // Collect stack traces on addref and removeref, instead of deleting the stack references
314 // on removeref that match the address ones.
315 bool mRetain;
316
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800317#endif
318};
319
320// ---------------------------------------------------------------------------
321
322void RefBase::incStrong(const void* id) const
323{
324 weakref_impl* const refs = mRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800325 refs->incWeak(id);
326
327 refs->addStrongRef(id);
328 const int32_t c = android_atomic_inc(&refs->mStrong);
Steve Blockae074452012-01-09 18:35:44 +0000329 ALOG_ASSERT(c > 0, "incStrong() called on %p after last strong ref", refs);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800330#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000331 ALOGD("incStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800332#endif
333 if (c != INITIAL_STRONG_VALUE) {
334 return;
335 }
336
337 android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);
Mathias Agopianad099652011-08-10 21:07:02 -0700338 refs->mBase->onFirstRef();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800339}
340
341void RefBase::decStrong(const void* id) const
342{
343 weakref_impl* const refs = mRefs;
344 refs->removeStrongRef(id);
345 const int32_t c = android_atomic_dec(&refs->mStrong);
346#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000347 ALOGD("decStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800348#endif
Steve Blockae074452012-01-09 18:35:44 +0000349 ALOG_ASSERT(c >= 1, "decStrong() called on %p too many times", refs);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800350 if (c == 1) {
Mathias Agopianad099652011-08-10 21:07:02 -0700351 refs->mBase->onLastStrongRef(id);
352 if ((refs->mFlags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700353 delete this;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800354 }
355 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800356 refs->decWeak(id);
357}
358
359void RefBase::forceIncStrong(const void* id) const
360{
361 weakref_impl* const refs = mRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800362 refs->incWeak(id);
363
364 refs->addStrongRef(id);
365 const int32_t c = android_atomic_inc(&refs->mStrong);
Steve Blockae074452012-01-09 18:35:44 +0000366 ALOG_ASSERT(c >= 0, "forceIncStrong called on %p after ref count underflow",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800367 refs);
368#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000369 ALOGD("forceIncStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800370#endif
371
372 switch (c) {
373 case INITIAL_STRONG_VALUE:
374 android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);
375 // fall through...
376 case 0:
Mathias Agopianad099652011-08-10 21:07:02 -0700377 refs->mBase->onFirstRef();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800378 }
379}
380
381int32_t RefBase::getStrongCount() const
382{
383 return mRefs->mStrong;
384}
385
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800386RefBase* RefBase::weakref_type::refBase() const
387{
388 return static_cast<const weakref_impl*>(this)->mBase;
389}
390
391void RefBase::weakref_type::incWeak(const void* id)
392{
393 weakref_impl* const impl = static_cast<weakref_impl*>(this);
394 impl->addWeakRef(id);
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700395 const int32_t c __unused = android_atomic_inc(&impl->mWeak);
Steve Blockae074452012-01-09 18:35:44 +0000396 ALOG_ASSERT(c >= 0, "incWeak called on %p after last weak ref", this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800397}
398
Mathias Agopianad099652011-08-10 21:07:02 -0700399
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800400void RefBase::weakref_type::decWeak(const void* id)
401{
402 weakref_impl* const impl = static_cast<weakref_impl*>(this);
403 impl->removeWeakRef(id);
404 const int32_t c = android_atomic_dec(&impl->mWeak);
Steve Blockae074452012-01-09 18:35:44 +0000405 ALOG_ASSERT(c >= 1, "decWeak called on %p too many times", this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800406 if (c != 1) return;
Mathias Agopianad099652011-08-10 21:07:02 -0700407
408 if ((impl->mFlags&OBJECT_LIFETIME_WEAK) == OBJECT_LIFETIME_STRONG) {
409 // This is the regular lifetime case. The object is destroyed
410 // when the last strong reference goes away. Since weakref_impl
411 // outlive the object, it is not destroyed in the dtor, and
412 // we'll have to do it here.
413 if (impl->mStrong == INITIAL_STRONG_VALUE) {
414 // Special case: we never had a strong reference, so we need to
415 // destroy the object now.
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700416 delete impl->mBase;
Mathias Agopianad099652011-08-10 21:07:02 -0700417 } else {
Steve Blockb37fbe92011-10-20 11:56:00 +0100418 // ALOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800419 delete impl;
420 }
421 } else {
Mathias Agopianad099652011-08-10 21:07:02 -0700422 // less common case: lifetime is OBJECT_LIFETIME_{WEAK|FOREVER}
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800423 impl->mBase->onLastWeakRef(id);
Mathias Agopianad099652011-08-10 21:07:02 -0700424 if ((impl->mFlags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_WEAK) {
425 // this is the OBJECT_LIFETIME_WEAK case. The last weak-reference
426 // is gone, we can destroy the object.
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700427 delete impl->mBase;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800428 }
429 }
430}
431
432bool RefBase::weakref_type::attemptIncStrong(const void* id)
433{
434 incWeak(id);
435
436 weakref_impl* const impl = static_cast<weakref_impl*>(this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800437 int32_t curCount = impl->mStrong;
Dianne Hackborna729ab12013-03-14 15:26:30 -0700438
439 ALOG_ASSERT(curCount >= 0,
440 "attemptIncStrong called on %p after underflow", this);
441
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800442 while (curCount > 0 && curCount != INITIAL_STRONG_VALUE) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700443 // we're in the easy/common case of promoting a weak-reference
444 // from an existing strong reference.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800445 if (android_atomic_cmpxchg(curCount, curCount+1, &impl->mStrong) == 0) {
446 break;
447 }
Dianne Hackborna729ab12013-03-14 15:26:30 -0700448 // the strong count has changed on us, we need to re-assert our
449 // situation.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800450 curCount = impl->mStrong;
451 }
452
453 if (curCount <= 0 || curCount == INITIAL_STRONG_VALUE) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700454 // we're now in the harder case of either:
455 // - there never was a strong reference on us
456 // - or, all strong references have been released
457 if ((impl->mFlags&OBJECT_LIFETIME_WEAK) == OBJECT_LIFETIME_STRONG) {
458 // this object has a "normal" life-time, i.e.: it gets destroyed
459 // when the last strong reference goes away
460 if (curCount <= 0) {
461 // the last strong-reference got released, the object cannot
462 // be revived.
463 decWeak(id);
464 return false;
465 }
466
467 // here, curCount == INITIAL_STRONG_VALUE, which means
468 // there never was a strong-reference, so we can try to
469 // promote this object; we need to do that atomically.
470 while (curCount > 0) {
471 if (android_atomic_cmpxchg(curCount, curCount + 1,
472 &impl->mStrong) == 0) {
473 break;
474 }
475 // the strong count has changed on us, we need to re-assert our
476 // situation (e.g.: another thread has inc/decStrong'ed us)
477 curCount = impl->mStrong;
478 }
479
480 if (curCount <= 0) {
481 // promote() failed, some other thread destroyed us in the
482 // meantime (i.e.: strong count reached zero).
483 decWeak(id);
484 return false;
485 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800486 } else {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700487 // this object has an "extended" life-time, i.e.: it can be
488 // revived from a weak-reference only.
489 // Ask the object's implementation if it agrees to be revived
490 if (!impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id)) {
491 // it didn't so give-up.
492 decWeak(id);
493 return false;
494 }
495 // grab a strong-reference, which is always safe due to the
496 // extended life-time.
497 curCount = android_atomic_inc(&impl->mStrong);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800498 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800499
500 // If the strong reference count has already been incremented by
501 // someone else, the implementor of onIncStrongAttempted() is holding
502 // an unneeded reference. So call onLastStrongRef() here to remove it.
503 // (No, this is not pretty.) Note that we MUST NOT do this if we
504 // are in fact acquiring the first reference.
505 if (curCount > 0 && curCount < INITIAL_STRONG_VALUE) {
506 impl->mBase->onLastStrongRef(id);
507 }
508 }
509
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800510 impl->addStrongRef(id);
511
512#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000513 ALOGD("attemptIncStrong of %p from %p: cnt=%d\n", this, id, curCount);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800514#endif
515
Dianne Hackborna729ab12013-03-14 15:26:30 -0700516 // now we need to fix-up the count if it was INITIAL_STRONG_VALUE
517 // this must be done safely, i.e.: handle the case where several threads
518 // were here in attemptIncStrong().
519 curCount = impl->mStrong;
520 while (curCount >= INITIAL_STRONG_VALUE) {
521 ALOG_ASSERT(curCount > INITIAL_STRONG_VALUE,
522 "attemptIncStrong in %p underflowed to INITIAL_STRONG_VALUE",
523 this);
524 if (android_atomic_cmpxchg(curCount, curCount-INITIAL_STRONG_VALUE,
525 &impl->mStrong) == 0) {
526 break;
527 }
528 // the strong-count changed on us, we need to re-assert the situation,
529 // for e.g.: it's possible the fix-up happened in another thread.
530 curCount = impl->mStrong;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800531 }
Dianne Hackborna729ab12013-03-14 15:26:30 -0700532
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800533 return true;
534}
535
536bool RefBase::weakref_type::attemptIncWeak(const void* id)
537{
538 weakref_impl* const impl = static_cast<weakref_impl*>(this);
Mathias Agopianad099652011-08-10 21:07:02 -0700539
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800540 int32_t curCount = impl->mWeak;
Steve Blockae074452012-01-09 18:35:44 +0000541 ALOG_ASSERT(curCount >= 0, "attemptIncWeak called on %p after underflow",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800542 this);
543 while (curCount > 0) {
544 if (android_atomic_cmpxchg(curCount, curCount+1, &impl->mWeak) == 0) {
545 break;
546 }
547 curCount = impl->mWeak;
548 }
549
550 if (curCount > 0) {
551 impl->addWeakRef(id);
552 }
553
554 return curCount > 0;
555}
556
557int32_t RefBase::weakref_type::getWeakCount() const
558{
559 return static_cast<const weakref_impl*>(this)->mWeak;
560}
561
562void RefBase::weakref_type::printRefs() const
563{
564 static_cast<const weakref_impl*>(this)->printRefs();
565}
566
567void RefBase::weakref_type::trackMe(bool enable, bool retain)
568{
Mathias Agopianad099652011-08-10 21:07:02 -0700569 static_cast<weakref_impl*>(this)->trackMe(enable, retain);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800570}
571
572RefBase::weakref_type* RefBase::createWeak(const void* id) const
573{
574 mRefs->incWeak(id);
575 return mRefs;
576}
577
578RefBase::weakref_type* RefBase::getWeakRefs() const
579{
580 return mRefs;
581}
582
583RefBase::RefBase()
584 : mRefs(new weakref_impl(this))
585{
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800586}
587
588RefBase::~RefBase()
589{
Mathias Agopianad099652011-08-10 21:07:02 -0700590 if (mRefs->mStrong == INITIAL_STRONG_VALUE) {
591 // we never acquired a strong (and/or weak) reference on this object.
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700592 delete mRefs;
Mathias Agopianad099652011-08-10 21:07:02 -0700593 } else {
594 // life-time of this object is extended to WEAK or FOREVER, in
595 // which case weakref_impl doesn't out-live the object and we
596 // can free it now.
597 if ((mRefs->mFlags & OBJECT_LIFETIME_MASK) != OBJECT_LIFETIME_STRONG) {
598 // It's possible that the weak count is not 0 if the object
599 // re-acquired a weak reference in its destructor
600 if (mRefs->mWeak == 0) {
601 delete mRefs;
602 }
603 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800604 }
Mathias Agopianad099652011-08-10 21:07:02 -0700605 // for debugging purposes, clear this.
606 const_cast<weakref_impl*&>(mRefs) = NULL;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800607}
608
609void RefBase::extendObjectLifetime(int32_t mode)
610{
611 android_atomic_or(mode, &mRefs->mFlags);
612}
613
614void RefBase::onFirstRef()
615{
616}
617
618void RefBase::onLastStrongRef(const void* /*id*/)
619{
620}
621
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700622bool RefBase::onIncStrongAttempted(uint32_t flags, const void* /*id*/)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800623{
624 return (flags&FIRST_INC_STRONG) ? true : false;
625}
626
627void RefBase::onLastWeakRef(const void* /*id*/)
628{
629}
Mathias Agopianad099652011-08-10 21:07:02 -0700630
631// ---------------------------------------------------------------------------
632
Mathias Agopianad099652011-08-10 21:07:02 -0700633#if DEBUG_REFS
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700634void RefBase::renameRefs(size_t n, const ReferenceRenamer& renamer) {
Mathias Agopianad099652011-08-10 21:07:02 -0700635 for (size_t i=0 ; i<n ; i++) {
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700636 renamer(i);
Mathias Agopianad099652011-08-10 21:07:02 -0700637 }
Mathias Agopianad099652011-08-10 21:07:02 -0700638}
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700639#else
640void RefBase::renameRefs(size_t /*n*/, const ReferenceRenamer& /*renamer*/) { }
641#endif
Mathias Agopianad099652011-08-10 21:07:02 -0700642
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700643void RefBase::renameRefId(weakref_type* ref,
644 const void* old_id, const void* new_id) {
645 weakref_impl* const impl = static_cast<weakref_impl*>(ref);
646 impl->renameStrongRefId(old_id, new_id);
647 impl->renameWeakRefId(old_id, new_id);
648}
649
650void RefBase::renameRefId(RefBase* ref,
651 const void* old_id, const void* new_id) {
652 ref->mRefs->renameStrongRefId(old_id, new_id);
653 ref->mRefs->renameWeakRefId(old_id, new_id);
654}
655
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800656}; // namespace android