From af2750767440909def8c3373f8c9a6778d131829 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Mon, 1 Nov 2010 23:48:28 +0100 Subject: [PATCH] switched object reference counting to atomics --- include/mitsuba/core/object.h | 6 +++--- src/libcore/object.cpp | 31 +++++++------------------------ 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/include/mitsuba/core/object.h b/include/mitsuba/core/object.h index 9305942d..31f86620 100644 --- a/include/mitsuba/core/object.h +++ b/include/mitsuba/core/object.h @@ -73,10 +73,10 @@ protected: */ virtual ~Object(); private: - mutable int m_refCount; -#if defined(WIN32) - mutable CRITICAL_SECTION m_refLock; +#if defined(_WIN32) + mutable LONG volatile m_refCount; #else + mutable int m_refCount; mutable pthread_mutex_t m_refLock; #endif public: diff --git a/src/libcore/object.cpp b/src/libcore/object.cpp index 304611f6..7e29d752 100644 --- a/src/libcore/object.cpp +++ b/src/libcore/object.cpp @@ -24,40 +24,28 @@ Class *Object::m_theClass = new Class("Object", false, ""); Object::Object() : m_refCount(0) { -#if defined(WIN32) - InitializeCriticalSection(&m_refLock); -#else - pthread_mutex_init(&m_refLock, NULL); -#endif } void Object::incRef() const { -#if defined(WIN32) - EnterCriticalSection(&m_refLock); - m_refCount++; - LeaveCriticalSection(&m_refLock); +#if defined(_WIN32) + InterlockedIncrement(&m_refCount); #else - pthread_mutex_lock(&m_refLock); - m_refCount++; - pthread_mutex_unlock(&m_refLock); + __sync_fetch_and_add(&m_refCount, 1); #endif } void Object::decRef() const { -#if defined(WIN32) - EnterCriticalSection(&m_refLock); - int count = --m_refCount; - LeaveCriticalSection(&m_refLock); +#if defined(_WIN32) + int count = InterlockedDecrement(&m_refCount); #else - pthread_mutex_lock(&m_refLock); - int count = --m_refCount; - pthread_mutex_unlock(&m_refLock); + int count = __sync_sub_and_fetch(&m_refCount, 1); #endif AssertEx(count >= 0, "Reference count is below zero!"); if (count == 0) delete this; } + const Class *Object::getClass() const { return m_theClass; } @@ -74,11 +62,6 @@ Object::~Object() { Log(EWarn, "Deleting %s with reference count %i!", toString().c_str(), m_refCount); } -#if defined(WIN32) - DeleteCriticalSection(&m_refLock); -#else - pthread_mutex_destroy(&m_refLock); -#endif } MTS_NAMESPACE_END