switched object reference counting to atomics
parent
44f7624f6e
commit
af27507674
|
@ -73,10 +73,10 @@ protected:
|
||||||
*/
|
*/
|
||||||
virtual ~Object();
|
virtual ~Object();
|
||||||
private:
|
private:
|
||||||
mutable int m_refCount;
|
#if defined(_WIN32)
|
||||||
#if defined(WIN32)
|
mutable LONG volatile m_refCount;
|
||||||
mutable CRITICAL_SECTION m_refLock;
|
|
||||||
#else
|
#else
|
||||||
|
mutable int m_refCount;
|
||||||
mutable pthread_mutex_t m_refLock;
|
mutable pthread_mutex_t m_refLock;
|
||||||
#endif
|
#endif
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -24,40 +24,28 @@ Class *Object::m_theClass = new Class("Object", false, "");
|
||||||
|
|
||||||
Object::Object()
|
Object::Object()
|
||||||
: m_refCount(0) {
|
: m_refCount(0) {
|
||||||
#if defined(WIN32)
|
|
||||||
InitializeCriticalSection(&m_refLock);
|
|
||||||
#else
|
|
||||||
pthread_mutex_init(&m_refLock, NULL);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Object::incRef() const {
|
void Object::incRef() const {
|
||||||
#if defined(WIN32)
|
#if defined(_WIN32)
|
||||||
EnterCriticalSection(&m_refLock);
|
InterlockedIncrement(&m_refCount);
|
||||||
m_refCount++;
|
|
||||||
LeaveCriticalSection(&m_refLock);
|
|
||||||
#else
|
#else
|
||||||
pthread_mutex_lock(&m_refLock);
|
__sync_fetch_and_add(&m_refCount, 1);
|
||||||
m_refCount++;
|
|
||||||
pthread_mutex_unlock(&m_refLock);
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void Object::decRef() const {
|
void Object::decRef() const {
|
||||||
#if defined(WIN32)
|
#if defined(_WIN32)
|
||||||
EnterCriticalSection(&m_refLock);
|
int count = InterlockedDecrement(&m_refCount);
|
||||||
int count = --m_refCount;
|
|
||||||
LeaveCriticalSection(&m_refLock);
|
|
||||||
#else
|
#else
|
||||||
pthread_mutex_lock(&m_refLock);
|
int count = __sync_sub_and_fetch(&m_refCount, 1);
|
||||||
int count = --m_refCount;
|
|
||||||
pthread_mutex_unlock(&m_refLock);
|
|
||||||
#endif
|
#endif
|
||||||
AssertEx(count >= 0, "Reference count is below zero!");
|
AssertEx(count >= 0, "Reference count is below zero!");
|
||||||
if (count == 0)
|
if (count == 0)
|
||||||
delete this;
|
delete this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const Class *Object::getClass() const {
|
const Class *Object::getClass() const {
|
||||||
return m_theClass;
|
return m_theClass;
|
||||||
}
|
}
|
||||||
|
@ -74,11 +62,6 @@ Object::~Object() {
|
||||||
Log(EWarn, "Deleting %s with reference count %i!",
|
Log(EWarn, "Deleting %s with reference count %i!",
|
||||||
toString().c_str(), m_refCount);
|
toString().c_str(), m_refCount);
|
||||||
}
|
}
|
||||||
#if defined(WIN32)
|
|
||||||
DeleteCriticalSection(&m_refLock);
|
|
||||||
#else
|
|
||||||
pthread_mutex_destroy(&m_refLock);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MTS_NAMESPACE_END
|
MTS_NAMESPACE_END
|
||||||
|
|
Loading…
Reference in New Issue