Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 18 | |
| 19 | #ifndef ANDROID_RS_BUILD_FOR_HOST |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 20 | #include "rsContext.h" |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 21 | #else |
| 22 | #include "rsContextHostStub.h" |
| 23 | #endif |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 24 | |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 25 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 26 | using namespace android; |
| 27 | using namespace android::renderscript; |
| 28 | |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 29 | pthread_mutex_t ObjectBase::gObjectInitMutex = PTHREAD_MUTEX_INITIALIZER; |
| 30 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 31 | ObjectBase::ObjectBase(Context *rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 32 | { |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 33 | mUserRefCount = 0; |
| 34 | mSysRefCount = 0; |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 35 | mRSC = rsc; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 36 | mNext = NULL; |
| 37 | mPrev = NULL; |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 38 | |
| 39 | #if RS_OBJECT_DEBUG |
| 40 | mStack.update(2); |
| 41 | #endif |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 42 | |
| 43 | rsAssert(rsc); |
| 44 | add(); |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 45 | //LOGV("ObjectBase %p con", this); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | ObjectBase::~ObjectBase() |
| 49 | { |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 50 | //LOGV("~ObjectBase %p ref %i,%i", this, mUserRefCount, mSysRefCount); |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 51 | #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 Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 65 | rsAssert(!mUserRefCount); |
| 66 | rsAssert(!mSysRefCount); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 69 | void ObjectBase::dumpLOGV(const char *op) const |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 70 | { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 71 | if (mName.size()) { |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 72 | LOGV("%s RSobj %p, name %s, refs %i,%i links %p,%p,%p", |
| 73 | op, this, mName.string(), mUserRefCount, mSysRefCount, mNext, mPrev, mRSC); |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 74 | } else { |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 75 | LOGV("%s RSobj %p, no-name, refs %i,%i links %p,%p,%p", |
| 76 | op, this, mUserRefCount, mSysRefCount, mNext, mPrev, mRSC); |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 80 | void ObjectBase::incUserRef() const |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 81 | { |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 82 | android_atomic_inc(&mUserRefCount); |
| 83 | //LOGV("ObjectBase %p incU ref %i, %i", this, mUserRefCount, mSysRefCount); |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 86 | void ObjectBase::incSysRef() const |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 87 | { |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 88 | android_atomic_inc(&mSysRefCount); |
| 89 | //LOGV("ObjectBase %p incS ref %i, %i", this, mUserRefCount, mSysRefCount); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 92 | void ObjectBase::preDestroy() const |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 93 | { |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 94 | } |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 95 | |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 96 | bool ObjectBase::checkDelete(const ObjectBase *ref) |
| 97 | { |
| 98 | if (!ref) { |
| 99 | return false; |
| 100 | } |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 101 | |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 102 | 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 | |
| 121 | bool ObjectBase::decUserRef() const |
| 122 | { |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 123 | rsAssert(mUserRefCount > 0); |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame^] | 124 | #if RS_OBJECT_DEBUG |
| 125 | LOGV("ObjectBase %p decU ref %i, %i", this, mUserRefCount, mSysRefCount); |
| 126 | if (mUserRefCount <= 0) { |
| 127 | mStack.dump(); |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 132 | if ((android_atomic_dec(&mUserRefCount) <= 1) && |
| 133 | (android_atomic_acquire_load(&mSysRefCount) <= 0)) { |
| 134 | return checkDelete(this); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 139 | bool ObjectBase::zeroUserRef() const |
| 140 | { |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 141 | //LOGV("ObjectBase %p zeroU ref %i, %i", this, mUserRefCount, mSysRefCount); |
| 142 | android_atomic_acquire_store(0, &mUserRefCount); |
| 143 | if (android_atomic_acquire_load(&mSysRefCount) <= 0) { |
| 144 | return checkDelete(this); |
| 145 | } |
| 146 | return false; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | bool ObjectBase::decSysRef() const |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 150 | { |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 151 | //LOGV("ObjectBase %p decS ref %i, %i", this, mUserRefCount, mSysRefCount); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 152 | rsAssert(mSysRefCount > 0); |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 153 | if ((android_atomic_dec(&mSysRefCount) <= 1) && |
| 154 | (android_atomic_acquire_load(&mUserRefCount) <= 0)) { |
| 155 | return checkDelete(this); |
| 156 | } |
| 157 | return false; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 160 | void ObjectBase::setName(const char *name) |
| 161 | { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 162 | mName.setTo(name); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 163 | } |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 164 | |
| 165 | void ObjectBase::setName(const char *name, uint32_t len) |
| 166 | { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 167 | mName.setTo(name, len); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 170 | void ObjectBase::asyncLock() |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 171 | { |
| 172 | pthread_mutex_lock(&gObjectInitMutex); |
| 173 | } |
| 174 | |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 175 | void ObjectBase::asyncUnlock() |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 176 | { |
| 177 | pthread_mutex_unlock(&gObjectInitMutex); |
| 178 | } |
| 179 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 180 | void ObjectBase::add() const |
| 181 | { |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 182 | asyncLock(); |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 183 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 184 | rsAssert(!mNext); |
| 185 | rsAssert(!mPrev); |
| 186 | //LOGV("calling add rsc %p", mRSC); |
| 187 | mNext = mRSC->mObjHead; |
| 188 | if (mRSC->mObjHead) { |
| 189 | mRSC->mObjHead->mPrev = this; |
| 190 | } |
| 191 | mRSC->mObjHead = this; |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 192 | |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 193 | asyncUnlock(); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void ObjectBase::remove() const |
| 197 | { |
| 198 | //LOGV("calling remove rsc %p", mRSC); |
| 199 | if (!mRSC) { |
| 200 | rsAssert(!mPrev); |
| 201 | rsAssert(!mNext); |
| 202 | return; |
| 203 | } |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 204 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 205 | if (mRSC->mObjHead == this) { |
| 206 | mRSC->mObjHead = mNext; |
| 207 | } |
| 208 | if (mPrev) { |
| 209 | mPrev->mNext = mNext; |
| 210 | } |
| 211 | if (mNext) { |
| 212 | mNext->mPrev = mPrev; |
| 213 | } |
| 214 | mPrev = NULL; |
| 215 | mNext = NULL; |
| 216 | } |
| 217 | |
| 218 | void ObjectBase::zeroAllUserRef(Context *rsc) |
| 219 | { |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 220 | if (rsc->props.mLogObjects) { |
| 221 | LOGV("Forcing release of all outstanding user refs."); |
| 222 | } |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 223 | |
| 224 | // This operation can be slow, only to be called during context cleanup. |
| 225 | const ObjectBase * o = rsc->mObjHead; |
| 226 | while (o) { |
| 227 | //LOGE("o %p", o); |
| 228 | if (o->zeroUserRef()) { |
| 229 | // deleted the object and possibly others, restart from head. |
| 230 | o = rsc->mObjHead; |
| 231 | //LOGE("o head %p", o); |
| 232 | } else { |
| 233 | o = o->mNext; |
| 234 | //LOGE("o next %p", o); |
| 235 | } |
| 236 | } |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 237 | |
| 238 | if (rsc->props.mLogObjects) { |
| 239 | LOGV("Objects remaining."); |
Jason Sams | ddac83a | 2009-11-19 13:08:17 -0800 | [diff] [blame] | 240 | dumpAll(rsc); |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 241 | } |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 244 | void ObjectBase::dumpAll(Context *rsc) |
| 245 | { |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 246 | asyncLock(); |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 247 | |
Jason Sams | ddac83a | 2009-11-19 13:08:17 -0800 | [diff] [blame] | 248 | LOGV("Dumping all objects"); |
| 249 | const ObjectBase * o = rsc->mObjHead; |
| 250 | while (o) { |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 251 | LOGV(" Object %p", o); |
Jason Sams | ddac83a | 2009-11-19 13:08:17 -0800 | [diff] [blame] | 252 | o->dumpLOGV(" "); |
| 253 | o = o->mNext; |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 254 | } |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 255 | |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 256 | asyncUnlock(); |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 259 | bool ObjectBase::isValid(const Context *rsc, const ObjectBase *obj) |
| 260 | { |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 261 | asyncLock(); |
Jason Sams | 3b9c52a | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 262 | |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 263 | const ObjectBase * o = rsc->mObjHead; |
| 264 | while (o) { |
| 265 | if (o == obj) { |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 266 | asyncUnlock(); |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 267 | return true; |
| 268 | } |
| 269 | o = o->mNext; |
| 270 | } |
Jason Sams | b38d534 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 271 | asyncUnlock(); |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 272 | return false; |
| 273 | } |
| 274 | |