blob: f428f9486d8d658014d24b0049a1f6d9f16806ce [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 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#include "rsObjectBase.h"
Jason Samse514b452009-09-25 14:51:22 -070018#include "rsContext.h"
Jason Sams225afd32010-10-21 14:06:55 -070019
Jason Sams326e0dd2009-05-22 14:03:28 -070020using namespace android;
21using namespace android::renderscript;
22
Jason Sams2353ae32010-10-14 17:48:46 -070023pthread_mutex_t ObjectBase::gObjectInitMutex = PTHREAD_MUTEX_INITIALIZER;
24
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080025ObjectBase::ObjectBase(Context *rsc) {
Jason Sams9397e302009-08-27 20:23:34 -070026 mUserRefCount = 0;
27 mSysRefCount = 0;
Jason Sams2353ae32010-10-14 17:48:46 -070028 mRSC = rsc;
Jason Samse514b452009-09-25 14:51:22 -070029 mNext = NULL;
30 mPrev = NULL;
Jason Sams225afd32010-10-21 14:06:55 -070031
32#if RS_OBJECT_DEBUG
33 mStack.update(2);
34#endif
Jason Sams2353ae32010-10-14 17:48:46 -070035
36 rsAssert(rsc);
37 add();
Jason Sams225afd32010-10-21 14:06:55 -070038 //LOGV("ObjectBase %p con", this);
Jason Sams326e0dd2009-05-22 14:03:28 -070039}
40
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080041ObjectBase::~ObjectBase() {
Jason Samse514b452009-09-25 14:51:22 -070042 //LOGV("~ObjectBase %p ref %i,%i", this, mUserRefCount, mSysRefCount);
Jason Sams225afd32010-10-21 14:06:55 -070043#if RS_OBJECT_DEBUG
44 mStack.dump();
45#endif
46
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080047 if (mPrev || mNext) {
Jason Sams225afd32010-10-21 14:06:55 -070048 // While the normal practice is to call remove before we call
49 // delete. Its possible for objects without a re-use list
50 // for avoiding duplication to be created on the stack. In those
51 // cases we need to remove ourself here.
52 asyncLock();
53 remove();
54 asyncUnlock();
55 }
56
Jason Sams9397e302009-08-27 20:23:34 -070057 rsAssert(!mUserRefCount);
58 rsAssert(!mSysRefCount);
Jason Samse514b452009-09-25 14:51:22 -070059}
60
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080061void ObjectBase::dumpLOGV(const char *op) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070062 if (mName.size()) {
Jason Sams225afd32010-10-21 14:06:55 -070063 LOGV("%s RSobj %p, name %s, refs %i,%i links %p,%p,%p",
64 op, this, mName.string(), mUserRefCount, mSysRefCount, mNext, mPrev, mRSC);
Jason Samsf2649a92009-09-25 16:37:33 -070065 } else {
Jason Sams225afd32010-10-21 14:06:55 -070066 LOGV("%s RSobj %p, no-name, refs %i,%i links %p,%p,%p",
67 op, this, mUserRefCount, mSysRefCount, mNext, mPrev, mRSC);
Jason Samsf2649a92009-09-25 16:37:33 -070068 }
69}
70
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080071void ObjectBase::incUserRef() const {
Jason Sams225afd32010-10-21 14:06:55 -070072 android_atomic_inc(&mUserRefCount);
73 //LOGV("ObjectBase %p incU ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams2353ae32010-10-14 17:48:46 -070074}
75
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080076void ObjectBase::incSysRef() const {
Jason Sams225afd32010-10-21 14:06:55 -070077 android_atomic_inc(&mSysRefCount);
78 //LOGV("ObjectBase %p incS ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams9397e302009-08-27 20:23:34 -070079}
80
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080081void ObjectBase::preDestroy() const {
Jason Sams225afd32010-10-21 14:06:55 -070082}
Jason Sams2353ae32010-10-14 17:48:46 -070083
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080084bool ObjectBase::checkDelete(const ObjectBase *ref) {
Jason Sams225afd32010-10-21 14:06:55 -070085 if (!ref) {
86 return false;
87 }
Jason Sams2353ae32010-10-14 17:48:46 -070088
Jason Sams225afd32010-10-21 14:06:55 -070089 asyncLock();
90 // This lock protects us against the non-RS threads changing
91 // the ref counts. At this point we should be the only thread
92 // working on them.
93 if (ref->mUserRefCount || ref->mSysRefCount) {
94 asyncUnlock();
95 return false;
96 }
97
98 ref->remove();
99 // At this point we can unlock because there should be no possible way
100 // for another thread to reference this object.
101 ref->preDestroy();
102 asyncUnlock();
103 delete ref;
104 return true;
105}
106
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800107bool ObjectBase::decUserRef() const {
Jason Sams225afd32010-10-21 14:06:55 -0700108 rsAssert(mUserRefCount > 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700109#if RS_OBJECT_DEBUG
110 LOGV("ObjectBase %p decU ref %i, %i", this, mUserRefCount, mSysRefCount);
111 if (mUserRefCount <= 0) {
112 mStack.dump();
113 }
114#endif
115
116
Jason Sams225afd32010-10-21 14:06:55 -0700117 if ((android_atomic_dec(&mUserRefCount) <= 1) &&
118 (android_atomic_acquire_load(&mSysRefCount) <= 0)) {
119 return checkDelete(this);
Jason Samse514b452009-09-25 14:51:22 -0700120 }
121 return false;
122}
123
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800124bool ObjectBase::zeroUserRef() const {
Jason Sams225afd32010-10-21 14:06:55 -0700125 //LOGV("ObjectBase %p zeroU ref %i, %i", this, mUserRefCount, mSysRefCount);
126 android_atomic_acquire_store(0, &mUserRefCount);
127 if (android_atomic_acquire_load(&mSysRefCount) <= 0) {
128 return checkDelete(this);
129 }
130 return false;
Jason Samse514b452009-09-25 14:51:22 -0700131}
132
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800133bool ObjectBase::decSysRef() const {
Jason Sams225afd32010-10-21 14:06:55 -0700134 //LOGV("ObjectBase %p decS ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams9397e302009-08-27 20:23:34 -0700135 rsAssert(mSysRefCount > 0);
Jason Sams225afd32010-10-21 14:06:55 -0700136 if ((android_atomic_dec(&mSysRefCount) <= 1) &&
137 (android_atomic_acquire_load(&mUserRefCount) <= 0)) {
138 return checkDelete(this);
139 }
140 return false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700141}
142
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800143void ObjectBase::setName(const char *name) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700144 mName.setTo(name);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700145}
Jason Samsa4a54e42009-06-10 18:39:40 -0700146
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800147void ObjectBase::setName(const char *name, uint32_t len) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700148 mName.setTo(name, len);
Jason Samsa4a54e42009-06-10 18:39:40 -0700149}
150
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800151void ObjectBase::asyncLock() {
Jason Sams2353ae32010-10-14 17:48:46 -0700152 pthread_mutex_lock(&gObjectInitMutex);
153}
154
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800155void ObjectBase::asyncUnlock() {
Jason Sams2353ae32010-10-14 17:48:46 -0700156 pthread_mutex_unlock(&gObjectInitMutex);
157}
158
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800159void ObjectBase::add() const {
Jason Sams225afd32010-10-21 14:06:55 -0700160 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700161
Jason Samse514b452009-09-25 14:51:22 -0700162 rsAssert(!mNext);
163 rsAssert(!mPrev);
164 //LOGV("calling add rsc %p", mRSC);
165 mNext = mRSC->mObjHead;
166 if (mRSC->mObjHead) {
167 mRSC->mObjHead->mPrev = this;
168 }
169 mRSC->mObjHead = this;
Jason Sams2353ae32010-10-14 17:48:46 -0700170
Jason Sams225afd32010-10-21 14:06:55 -0700171 asyncUnlock();
Jason Samse514b452009-09-25 14:51:22 -0700172}
173
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800174void ObjectBase::remove() const {
Jason Samse514b452009-09-25 14:51:22 -0700175 //LOGV("calling remove rsc %p", mRSC);
176 if (!mRSC) {
177 rsAssert(!mPrev);
178 rsAssert(!mNext);
179 return;
180 }
Jason Sams2353ae32010-10-14 17:48:46 -0700181
Jason Samse514b452009-09-25 14:51:22 -0700182 if (mRSC->mObjHead == this) {
183 mRSC->mObjHead = mNext;
184 }
185 if (mPrev) {
186 mPrev->mNext = mNext;
187 }
188 if (mNext) {
189 mNext->mPrev = mPrev;
190 }
191 mPrev = NULL;
192 mNext = NULL;
193}
194
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800195void ObjectBase::zeroAllUserRef(Context *rsc) {
Jason Sams1fddd902009-09-25 15:25:00 -0700196 if (rsc->props.mLogObjects) {
197 LOGV("Forcing release of all outstanding user refs.");
198 }
Jason Samse514b452009-09-25 14:51:22 -0700199
200 // This operation can be slow, only to be called during context cleanup.
201 const ObjectBase * o = rsc->mObjHead;
202 while (o) {
203 //LOGE("o %p", o);
204 if (o->zeroUserRef()) {
205 // deleted the object and possibly others, restart from head.
206 o = rsc->mObjHead;
207 //LOGE("o head %p", o);
208 } else {
209 o = o->mNext;
210 //LOGE("o next %p", o);
211 }
212 }
Jason Samsf2649a92009-09-25 16:37:33 -0700213
214 if (rsc->props.mLogObjects) {
215 LOGV("Objects remaining.");
Jason Sams25afc002009-11-19 13:08:17 -0800216 dumpAll(rsc);
Jason Samsf2649a92009-09-25 16:37:33 -0700217 }
Jason Samse514b452009-09-25 14:51:22 -0700218}
219
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800220void ObjectBase::dumpAll(Context *rsc) {
Jason Sams225afd32010-10-21 14:06:55 -0700221 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700222
Jason Sams25afc002009-11-19 13:08:17 -0800223 LOGV("Dumping all objects");
224 const ObjectBase * o = rsc->mObjHead;
225 while (o) {
Jason Sams81549542010-02-17 15:38:10 -0800226 LOGV(" Object %p", o);
Jason Sams25afc002009-11-19 13:08:17 -0800227 o->dumpLOGV(" ");
228 o = o->mNext;
Jason Samsc21cf402009-11-17 17:26:46 -0800229 }
Jason Sams2353ae32010-10-14 17:48:46 -0700230
Jason Sams225afd32010-10-21 14:06:55 -0700231 asyncUnlock();
Jason Samsc21cf402009-11-17 17:26:46 -0800232}
233
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800234bool ObjectBase::isValid(const Context *rsc, const ObjectBase *obj) {
Jason Sams225afd32010-10-21 14:06:55 -0700235 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700236
Jason Sams605048a2010-09-30 18:15:52 -0700237 const ObjectBase * o = rsc->mObjHead;
238 while (o) {
239 if (o == obj) {
Jason Sams225afd32010-10-21 14:06:55 -0700240 asyncUnlock();
Jason Sams605048a2010-09-30 18:15:52 -0700241 return true;
242 }
243 o = o->mNext;
244 }
Jason Sams225afd32010-10-21 14:06:55 -0700245 asyncUnlock();
Jason Sams605048a2010-09-30 18:15:52 -0700246 return false;
247}
248