asILockableSharedBool *asCScriptObject::GetWeakRefFlag() const { // If the object's refCount has already reached zero then the object is already // about to be destroyed so it's ok to return null if the weakRefFlag doesn't already // exist if( (extra && extra->weakRefFlag) || hasRefCountReachedZero ) return extra->weakRefFlag; // Lock globally so no other thread can attempt // to create a shared bool at the same time. // TODO: runtime optimize: Instead of locking globally, it would be possible to have // a critical section per object type. This would reduce the // chances of two threads lock on the same critical section. asAcquireExclusiveLock(); // Make sure another thread didn't create the // flag while we waited for the lock if( !extra ) extra = asNEW(SExtra); if( !extra->weakRefFlag ) extra->weakRefFlag = asNEW(asCLockableSharedBool); asReleaseExclusiveLock(); return extra->weakRefFlag; }
void *asCScriptObject::SetUserData(void *data, asPWORD type) { // Lock globally so no other thread can attempt // to manipulate the extra data at the same time. // TODO: runtime optimize: Instead of locking globally, it would be possible to have // a critical section per object type. This would reduce the // chances of two threads lock on the same critical section. asAcquireExclusiveLock(); // Make sure another thread didn't create the // flag while we waited for the lock if( !extra ) extra = asNEW(SExtra); // It is not intended to store a lot of different types of userdata, // so a more complex structure like a associative map would just have // more overhead than a simple array. for( asUINT n = 0; n < extra->userData.GetLength(); n += 2 ) { if( extra->userData[n] == type ) { void *oldData = reinterpret_cast<void*>(extra->userData[n+1]); extra->userData[n+1] = reinterpret_cast<asPWORD>(data); asReleaseExclusiveLock(); return oldData; } } extra->userData.PushLast(type); extra->userData.PushLast(reinterpret_cast<asPWORD>(data)); asReleaseExclusiveLock(); return 0; }
asILockableSharedBool *GetWeakRefFlag() { if( !weakRefFlag ) { // Lock globally so no other thread can attempt // to create a shared bool at the same time asAcquireExclusiveLock(); // Make sure another thread didn't create the // flag while we waited for the lock if( !weakRefFlag ) weakRefFlag = asCreateLockableSharedBool(); asReleaseExclusiveLock(); } return weakRefFlag; }
asILockableSharedBool *asCScriptObject::GetWeakRefFlag() const { if( weakRefFlag ) return weakRefFlag; // Lock globally so no other thread can attempt // to create a shared bool at the same time. // TODO: runtime optimize: Instead of locking globally, it would be possible to have // a critical section per object type. This would reduce the // chances of two threads lock on the same critical section. asAcquireExclusiveLock(); // Make sure another thread didn't create the // flag while we waited for the lock if( !weakRefFlag ) weakRefFlag = asNEW(asCLockableSharedBool); asReleaseExclusiveLock(); return weakRefFlag; }