blob: aec2f6794de5024f9d61559f0c2701abac0ed74f [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"
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070018
19#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samse514b452009-09-25 14:51:22 -070020#include "rsContext.h"
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070021#else
22#include "rsContextHostStub.h"
23#endif
Jason Sams326e0dd2009-05-22 14:03:28 -070024
Jason Sams225afd32010-10-21 14:06:55 -070025
Jason Sams326e0dd2009-05-22 14:03:28 -070026using namespace android;
27using namespace android::renderscript;
28
Jason Sams2353ae32010-10-14 17:48:46 -070029pthread_mutex_t ObjectBase::gObjectInitMutex = PTHREAD_MUTEX_INITIALIZER;
30
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080031ObjectBase::ObjectBase(Context *rsc) {
Jason Sams9397e302009-08-27 20:23:34 -070032 mUserRefCount = 0;
33 mSysRefCount = 0;
Jason Sams2353ae32010-10-14 17:48:46 -070034 mRSC = rsc;
Jason Samse514b452009-09-25 14:51:22 -070035 mNext = NULL;
36 mPrev = NULL;
Jason Sams225afd32010-10-21 14:06:55 -070037
38#if RS_OBJECT_DEBUG
39 mStack.update(2);
40#endif
Jason Sams2353ae32010-10-14 17:48:46 -070041
42 rsAssert(rsc);
43 add();
Jason Sams225afd32010-10-21 14:06:55 -070044 //LOGV("ObjectBase %p con", this);
Jason Sams326e0dd2009-05-22 14:03:28 -070045}
46
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080047ObjectBase::~ObjectBase() {
Jason Samse514b452009-09-25 14:51:22 -070048 //LOGV("~ObjectBase %p ref %i,%i", this, mUserRefCount, mSysRefCount);
Jason Sams225afd32010-10-21 14:06:55 -070049#if RS_OBJECT_DEBUG
50 mStack.dump();
51#endif
52
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080053 if (mPrev || mNext) {
Jason Sams225afd32010-10-21 14:06:55 -070054 // While the normal practice is to call remove before we call
55 // delete. Its possible for objects without a re-use list
56 // for avoiding duplication to be created on the stack. In those
57 // cases we need to remove ourself here.
58 asyncLock();
59 remove();
60 asyncUnlock();
61 }
62
Jason Sams9397e302009-08-27 20:23:34 -070063 rsAssert(!mUserRefCount);
64 rsAssert(!mSysRefCount);
Jason Samse514b452009-09-25 14:51:22 -070065}
66
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080067void ObjectBase::dumpLOGV(const char *op) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070068 if (mName.size()) {
Jason Sams225afd32010-10-21 14:06:55 -070069 LOGV("%s RSobj %p, name %s, refs %i,%i links %p,%p,%p",
70 op, this, mName.string(), mUserRefCount, mSysRefCount, mNext, mPrev, mRSC);
Jason Samsf2649a92009-09-25 16:37:33 -070071 } else {
Jason Sams225afd32010-10-21 14:06:55 -070072 LOGV("%s RSobj %p, no-name, refs %i,%i links %p,%p,%p",
73 op, this, mUserRefCount, mSysRefCount, mNext, mPrev, mRSC);
Jason Samsf2649a92009-09-25 16:37:33 -070074 }
75}
76
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080077void ObjectBase::incUserRef() const {
Jason Sams225afd32010-10-21 14:06:55 -070078 android_atomic_inc(&mUserRefCount);
79 //LOGV("ObjectBase %p incU ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams2353ae32010-10-14 17:48:46 -070080}
81
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080082void ObjectBase::incSysRef() const {
Jason Sams225afd32010-10-21 14:06:55 -070083 android_atomic_inc(&mSysRefCount);
84 //LOGV("ObjectBase %p incS ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams9397e302009-08-27 20:23:34 -070085}
86
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080087void ObjectBase::preDestroy() const {
Jason Sams225afd32010-10-21 14:06:55 -070088}
Jason Sams2353ae32010-10-14 17:48:46 -070089
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080090bool ObjectBase::checkDelete(const ObjectBase *ref) {
Jason Sams225afd32010-10-21 14:06:55 -070091 if (!ref) {
92 return false;
93 }
Jason Sams2353ae32010-10-14 17:48:46 -070094
Jason Sams225afd32010-10-21 14:06:55 -070095 asyncLock();
96 // This lock protects us against the non-RS threads changing
97 // the ref counts. At this point we should be the only thread
98 // working on them.
99 if (ref->mUserRefCount || ref->mSysRefCount) {
100 asyncUnlock();
101 return false;
102 }
103
104 ref->remove();
105 // At this point we can unlock because there should be no possible way
106 // for another thread to reference this object.
107 ref->preDestroy();
108 asyncUnlock();
109 delete ref;
110 return true;
111}
112
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800113bool ObjectBase::decUserRef() const {
Jason Sams225afd32010-10-21 14:06:55 -0700114 rsAssert(mUserRefCount > 0);
Jason Samsf0c1df42010-10-26 13:09:17 -0700115#if RS_OBJECT_DEBUG
116 LOGV("ObjectBase %p decU ref %i, %i", this, mUserRefCount, mSysRefCount);
117 if (mUserRefCount <= 0) {
118 mStack.dump();
119 }
120#endif
121
122
Jason Sams225afd32010-10-21 14:06:55 -0700123 if ((android_atomic_dec(&mUserRefCount) <= 1) &&
124 (android_atomic_acquire_load(&mSysRefCount) <= 0)) {
125 return checkDelete(this);
Jason Samse514b452009-09-25 14:51:22 -0700126 }
127 return false;
128}
129
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800130bool ObjectBase::zeroUserRef() const {
Jason Sams225afd32010-10-21 14:06:55 -0700131 //LOGV("ObjectBase %p zeroU ref %i, %i", this, mUserRefCount, mSysRefCount);
132 android_atomic_acquire_store(0, &mUserRefCount);
133 if (android_atomic_acquire_load(&mSysRefCount) <= 0) {
134 return checkDelete(this);
135 }
136 return false;
Jason Samse514b452009-09-25 14:51:22 -0700137}
138
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800139bool ObjectBase::decSysRef() const {
Jason Sams225afd32010-10-21 14:06:55 -0700140 //LOGV("ObjectBase %p decS ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams9397e302009-08-27 20:23:34 -0700141 rsAssert(mSysRefCount > 0);
Jason Sams225afd32010-10-21 14:06:55 -0700142 if ((android_atomic_dec(&mSysRefCount) <= 1) &&
143 (android_atomic_acquire_load(&mUserRefCount) <= 0)) {
144 return checkDelete(this);
145 }
146 return false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700147}
148
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800149void ObjectBase::setName(const char *name) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700150 mName.setTo(name);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700151}
Jason Samsa4a54e42009-06-10 18:39:40 -0700152
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800153void ObjectBase::setName(const char *name, uint32_t len) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700154 mName.setTo(name, len);
Jason Samsa4a54e42009-06-10 18:39:40 -0700155}
156
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800157void ObjectBase::asyncLock() {
Jason Sams2353ae32010-10-14 17:48:46 -0700158 pthread_mutex_lock(&gObjectInitMutex);
159}
160
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800161void ObjectBase::asyncUnlock() {
Jason Sams2353ae32010-10-14 17:48:46 -0700162 pthread_mutex_unlock(&gObjectInitMutex);
163}
164
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800165void ObjectBase::add() const {
Jason Sams225afd32010-10-21 14:06:55 -0700166 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700167
Jason Samse514b452009-09-25 14:51:22 -0700168 rsAssert(!mNext);
169 rsAssert(!mPrev);
170 //LOGV("calling add rsc %p", mRSC);
171 mNext = mRSC->mObjHead;
172 if (mRSC->mObjHead) {
173 mRSC->mObjHead->mPrev = this;
174 }
175 mRSC->mObjHead = this;
Jason Sams2353ae32010-10-14 17:48:46 -0700176
Jason Sams225afd32010-10-21 14:06:55 -0700177 asyncUnlock();
Jason Samse514b452009-09-25 14:51:22 -0700178}
179
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800180void ObjectBase::remove() const {
Jason Samse514b452009-09-25 14:51:22 -0700181 //LOGV("calling remove rsc %p", mRSC);
182 if (!mRSC) {
183 rsAssert(!mPrev);
184 rsAssert(!mNext);
185 return;
186 }
Jason Sams2353ae32010-10-14 17:48:46 -0700187
Jason Samse514b452009-09-25 14:51:22 -0700188 if (mRSC->mObjHead == this) {
189 mRSC->mObjHead = mNext;
190 }
191 if (mPrev) {
192 mPrev->mNext = mNext;
193 }
194 if (mNext) {
195 mNext->mPrev = mPrev;
196 }
197 mPrev = NULL;
198 mNext = NULL;
199}
200
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800201void ObjectBase::zeroAllUserRef(Context *rsc) {
Jason Sams1fddd902009-09-25 15:25:00 -0700202 if (rsc->props.mLogObjects) {
203 LOGV("Forcing release of all outstanding user refs.");
204 }
Jason Samse514b452009-09-25 14:51:22 -0700205
206 // This operation can be slow, only to be called during context cleanup.
207 const ObjectBase * o = rsc->mObjHead;
208 while (o) {
209 //LOGE("o %p", o);
210 if (o->zeroUserRef()) {
211 // deleted the object and possibly others, restart from head.
212 o = rsc->mObjHead;
213 //LOGE("o head %p", o);
214 } else {
215 o = o->mNext;
216 //LOGE("o next %p", o);
217 }
218 }
Jason Samsf2649a92009-09-25 16:37:33 -0700219
220 if (rsc->props.mLogObjects) {
221 LOGV("Objects remaining.");
Jason Sams25afc002009-11-19 13:08:17 -0800222 dumpAll(rsc);
Jason Samsf2649a92009-09-25 16:37:33 -0700223 }
Jason Samse514b452009-09-25 14:51:22 -0700224}
225
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800226void ObjectBase::dumpAll(Context *rsc) {
Jason Sams225afd32010-10-21 14:06:55 -0700227 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700228
Jason Sams25afc002009-11-19 13:08:17 -0800229 LOGV("Dumping all objects");
230 const ObjectBase * o = rsc->mObjHead;
231 while (o) {
Jason Sams81549542010-02-17 15:38:10 -0800232 LOGV(" Object %p", o);
Jason Sams25afc002009-11-19 13:08:17 -0800233 o->dumpLOGV(" ");
234 o = o->mNext;
Jason Samsc21cf402009-11-17 17:26:46 -0800235 }
Jason Sams2353ae32010-10-14 17:48:46 -0700236
Jason Sams225afd32010-10-21 14:06:55 -0700237 asyncUnlock();
Jason Samsc21cf402009-11-17 17:26:46 -0800238}
239
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800240bool ObjectBase::isValid(const Context *rsc, const ObjectBase *obj) {
Jason Sams225afd32010-10-21 14:06:55 -0700241 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700242
Jason Sams605048a2010-09-30 18:15:52 -0700243 const ObjectBase * o = rsc->mObjHead;
244 while (o) {
245 if (o == obj) {
Jason Sams225afd32010-10-21 14:06:55 -0700246 asyncUnlock();
Jason Sams605048a2010-09-30 18:15:52 -0700247 return true;
248 }
249 o = o->mNext;
250 }
Jason Sams225afd32010-10-21 14:06:55 -0700251 asyncUnlock();
Jason Sams605048a2010-09-30 18:15:52 -0700252 return false;
253}
254