blob: 724172ee203913a006f8607132ad467a1737e86e [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
Jason Samse514b452009-09-25 14:51:22 -070031ObjectBase::ObjectBase(Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070032{
Jason Sams9397e302009-08-27 20:23:34 -070033 mUserRefCount = 0;
34 mSysRefCount = 0;
Jason Sams2353ae32010-10-14 17:48:46 -070035 mRSC = rsc;
Jason Samse514b452009-09-25 14:51:22 -070036 mNext = NULL;
37 mPrev = NULL;
Jason Sams225afd32010-10-21 14:06:55 -070038
39#if RS_OBJECT_DEBUG
40 mStack.update(2);
41#endif
Jason Sams2353ae32010-10-14 17:48:46 -070042
43 rsAssert(rsc);
44 add();
Jason Sams225afd32010-10-21 14:06:55 -070045 //LOGV("ObjectBase %p con", this);
Jason Sams326e0dd2009-05-22 14:03:28 -070046}
47
48ObjectBase::~ObjectBase()
49{
Jason Samse514b452009-09-25 14:51:22 -070050 //LOGV("~ObjectBase %p ref %i,%i", this, mUserRefCount, mSysRefCount);
Jason Sams225afd32010-10-21 14:06:55 -070051#if RS_OBJECT_DEBUG
52 mStack.dump();
53#endif
54
55 if(mPrev || mNext) {
56 // While the normal practice is to call remove before we call
57 // delete. Its possible for objects without a re-use list
58 // for avoiding duplication to be created on the stack. In those
59 // cases we need to remove ourself here.
60 asyncLock();
61 remove();
62 asyncUnlock();
63 }
64
Jason Sams9397e302009-08-27 20:23:34 -070065 rsAssert(!mUserRefCount);
66 rsAssert(!mSysRefCount);
Jason Samse514b452009-09-25 14:51:22 -070067}
68
Jason Samse12c1c52009-09-27 17:50:38 -070069void ObjectBase::dumpLOGV(const char *op) const
Jason Samsf2649a92009-09-25 16:37:33 -070070{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070071 if (mName.size()) {
Jason Sams225afd32010-10-21 14:06:55 -070072 LOGV("%s RSobj %p, name %s, refs %i,%i links %p,%p,%p",
73 op, this, mName.string(), mUserRefCount, mSysRefCount, mNext, mPrev, mRSC);
Jason Samsf2649a92009-09-25 16:37:33 -070074 } else {
Jason Sams225afd32010-10-21 14:06:55 -070075 LOGV("%s RSobj %p, no-name, refs %i,%i links %p,%p,%p",
76 op, this, mUserRefCount, mSysRefCount, mNext, mPrev, mRSC);
Jason Samsf2649a92009-09-25 16:37:33 -070077 }
78}
79
Jason Sams9397e302009-08-27 20:23:34 -070080void ObjectBase::incUserRef() const
Jason Sams326e0dd2009-05-22 14:03:28 -070081{
Jason Sams225afd32010-10-21 14:06:55 -070082 android_atomic_inc(&mUserRefCount);
83 //LOGV("ObjectBase %p incU ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams2353ae32010-10-14 17:48:46 -070084}
85
Jason Sams9397e302009-08-27 20:23:34 -070086void ObjectBase::incSysRef() const
Jason Sams326e0dd2009-05-22 14:03:28 -070087{
Jason Sams225afd32010-10-21 14:06:55 -070088 android_atomic_inc(&mSysRefCount);
89 //LOGV("ObjectBase %p incS ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams9397e302009-08-27 20:23:34 -070090}
91
Jason Sams225afd32010-10-21 14:06:55 -070092void ObjectBase::preDestroy() const
Jason Samse514b452009-09-25 14:51:22 -070093{
Jason Sams225afd32010-10-21 14:06:55 -070094}
Jason Sams2353ae32010-10-14 17:48:46 -070095
Jason Sams225afd32010-10-21 14:06:55 -070096bool ObjectBase::checkDelete(const ObjectBase *ref)
97{
98 if (!ref) {
99 return false;
100 }
Jason Sams2353ae32010-10-14 17:48:46 -0700101
Jason Sams225afd32010-10-21 14:06:55 -0700102 asyncLock();
103 // This lock protects us against the non-RS threads changing
104 // the ref counts. At this point we should be the only thread
105 // working on them.
106 if (ref->mUserRefCount || ref->mSysRefCount) {
107 asyncUnlock();
108 return false;
109 }
110
111 ref->remove();
112 // At this point we can unlock because there should be no possible way
113 // for another thread to reference this object.
114 ref->preDestroy();
115 asyncUnlock();
116 delete ref;
117 return true;
118}
119
120
121bool ObjectBase::decUserRef() const
122{
123 //LOGV("ObjectBase %p decU ref %i, %i", this, mUserRefCount, mSysRefCount);
124 rsAssert(mUserRefCount > 0);
125 if ((android_atomic_dec(&mUserRefCount) <= 1) &&
126 (android_atomic_acquire_load(&mSysRefCount) <= 0)) {
127 return checkDelete(this);
Jason Samse514b452009-09-25 14:51:22 -0700128 }
129 return false;
130}
131
Jason Samse514b452009-09-25 14:51:22 -0700132bool ObjectBase::zeroUserRef() const
133{
Jason Sams225afd32010-10-21 14:06:55 -0700134 //LOGV("ObjectBase %p zeroU ref %i, %i", this, mUserRefCount, mSysRefCount);
135 android_atomic_acquire_store(0, &mUserRefCount);
136 if (android_atomic_acquire_load(&mSysRefCount) <= 0) {
137 return checkDelete(this);
138 }
139 return false;
Jason Samse514b452009-09-25 14:51:22 -0700140}
141
142bool ObjectBase::decSysRef() const
Jason Sams9397e302009-08-27 20:23:34 -0700143{
Jason Sams225afd32010-10-21 14:06:55 -0700144 //LOGV("ObjectBase %p decS ref %i, %i", this, mUserRefCount, mSysRefCount);
Jason Sams9397e302009-08-27 20:23:34 -0700145 rsAssert(mSysRefCount > 0);
Jason Sams225afd32010-10-21 14:06:55 -0700146 if ((android_atomic_dec(&mSysRefCount) <= 1) &&
147 (android_atomic_acquire_load(&mUserRefCount) <= 0)) {
148 return checkDelete(this);
149 }
150 return false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700151}
152
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700153void ObjectBase::setName(const char *name)
154{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700155 mName.setTo(name);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700156}
Jason Samsa4a54e42009-06-10 18:39:40 -0700157
158void ObjectBase::setName(const char *name, uint32_t len)
159{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700160 mName.setTo(name, len);
Jason Samsa4a54e42009-06-10 18:39:40 -0700161}
162
Jason Sams225afd32010-10-21 14:06:55 -0700163void ObjectBase::asyncLock()
Jason Sams2353ae32010-10-14 17:48:46 -0700164{
165 pthread_mutex_lock(&gObjectInitMutex);
166}
167
Jason Sams225afd32010-10-21 14:06:55 -0700168void ObjectBase::asyncUnlock()
Jason Sams2353ae32010-10-14 17:48:46 -0700169{
170 pthread_mutex_unlock(&gObjectInitMutex);
171}
172
Jason Samse514b452009-09-25 14:51:22 -0700173void ObjectBase::add() const
174{
Jason Sams225afd32010-10-21 14:06:55 -0700175 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700176
Jason Samse514b452009-09-25 14:51:22 -0700177 rsAssert(!mNext);
178 rsAssert(!mPrev);
179 //LOGV("calling add rsc %p", mRSC);
180 mNext = mRSC->mObjHead;
181 if (mRSC->mObjHead) {
182 mRSC->mObjHead->mPrev = this;
183 }
184 mRSC->mObjHead = this;
Jason Sams2353ae32010-10-14 17:48:46 -0700185
Jason Sams225afd32010-10-21 14:06:55 -0700186 asyncUnlock();
Jason Samse514b452009-09-25 14:51:22 -0700187}
188
189void ObjectBase::remove() const
190{
191 //LOGV("calling remove rsc %p", mRSC);
192 if (!mRSC) {
193 rsAssert(!mPrev);
194 rsAssert(!mNext);
195 return;
196 }
Jason Sams2353ae32010-10-14 17:48:46 -0700197
Jason Samse514b452009-09-25 14:51:22 -0700198 if (mRSC->mObjHead == this) {
199 mRSC->mObjHead = mNext;
200 }
201 if (mPrev) {
202 mPrev->mNext = mNext;
203 }
204 if (mNext) {
205 mNext->mPrev = mPrev;
206 }
207 mPrev = NULL;
208 mNext = NULL;
209}
210
211void ObjectBase::zeroAllUserRef(Context *rsc)
212{
Jason Sams1fddd902009-09-25 15:25:00 -0700213 if (rsc->props.mLogObjects) {
214 LOGV("Forcing release of all outstanding user refs.");
215 }
Jason Samse514b452009-09-25 14:51:22 -0700216
217 // This operation can be slow, only to be called during context cleanup.
218 const ObjectBase * o = rsc->mObjHead;
219 while (o) {
220 //LOGE("o %p", o);
221 if (o->zeroUserRef()) {
222 // deleted the object and possibly others, restart from head.
223 o = rsc->mObjHead;
224 //LOGE("o head %p", o);
225 } else {
226 o = o->mNext;
227 //LOGE("o next %p", o);
228 }
229 }
Jason Samsf2649a92009-09-25 16:37:33 -0700230
231 if (rsc->props.mLogObjects) {
232 LOGV("Objects remaining.");
Jason Sams25afc002009-11-19 13:08:17 -0800233 dumpAll(rsc);
Jason Samsf2649a92009-09-25 16:37:33 -0700234 }
Jason Samse514b452009-09-25 14:51:22 -0700235}
236
Jason Samsc21cf402009-11-17 17:26:46 -0800237void ObjectBase::dumpAll(Context *rsc)
238{
Jason Sams225afd32010-10-21 14:06:55 -0700239 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700240
Jason Sams25afc002009-11-19 13:08:17 -0800241 LOGV("Dumping all objects");
242 const ObjectBase * o = rsc->mObjHead;
243 while (o) {
Jason Sams81549542010-02-17 15:38:10 -0800244 LOGV(" Object %p", o);
Jason Sams25afc002009-11-19 13:08:17 -0800245 o->dumpLOGV(" ");
246 o = o->mNext;
Jason Samsc21cf402009-11-17 17:26:46 -0800247 }
Jason Sams2353ae32010-10-14 17:48:46 -0700248
Jason Sams225afd32010-10-21 14:06:55 -0700249 asyncUnlock();
Jason Samsc21cf402009-11-17 17:26:46 -0800250}
251
Jason Sams605048a2010-09-30 18:15:52 -0700252bool ObjectBase::isValid(const Context *rsc, const ObjectBase *obj)
253{
Jason Sams225afd32010-10-21 14:06:55 -0700254 asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700255
Jason Sams605048a2010-09-30 18:15:52 -0700256 const ObjectBase * o = rsc->mObjHead;
257 while (o) {
258 if (o == obj) {
Jason Sams225afd32010-10-21 14:06:55 -0700259 asyncUnlock();
Jason Sams605048a2010-09-30 18:15:52 -0700260 return true;
261 }
262 o = o->mNext;
263 }
Jason Sams225afd32010-10-21 14:06:55 -0700264 asyncUnlock();
Jason Sams605048a2010-09-30 18:15:52 -0700265 return false;
266}
267