From Mathias Froehlich, added support for using OpenThreads::Atomic for thread safe ref/unref.

This commit is contained in:
Robert Osfield
2008-06-19 17:30:38 +00:00
parent 2ba5f002d2
commit 936edacc92
2 changed files with 106 additions and 4 deletions

View File

@@ -30,6 +30,10 @@
#include <OpenThreads/Mutex>
#endif
#include <OpenThreads/Atomic>
#if !defined(_OPENTHREADS_ATOMIC_USE_MUTEX)
# define _OSG_REFERENCED_USE_ATOMIC_OPERATIONS
#endif
namespace osg {
@@ -57,10 +61,19 @@ class OSG_EXPORT Referenced
virtual void setThreadSafeRefUnref(bool threadSafe);
/** Get whether a mutex is used to ensure ref() and unref() are thread safe.*/
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
bool getThreadSafeRefUnref() const { return true; }
#else
bool getThreadSafeRefUnref() const { return _refMutex!=0; }
#endif
/** Get the mutex used to ensure thread safety of ref()/unref(). */
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
OpenThreads::Mutex* getRefMutex() const { return 0; }
#else
OpenThreads::Mutex* getRefMutex() const { return _refMutex; }
#endif
/** Increment the reference count by one, indicating that
this object has another pointer which is referencing it.*/
@@ -112,17 +125,28 @@ class OSG_EXPORT Referenced
virtual ~Referenced();
void deleteUsingDeleteHandler() const;
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
struct ObserverSetData;
OpenThreads::AtomicPtr<ObserverSetData> _observerSetDataPtr;
mutable OpenThreads::Atomic _refCount;
#else
mutable OpenThreads::Mutex* _refMutex;
mutable int _refCount;
void* _observers;
void* _observers;
#endif
};
inline void Referenced::ref() const
{
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
++_refCount;
#else
if (_refMutex)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*_refMutex);
@@ -132,10 +156,14 @@ inline void Referenced::ref() const
{
++_refCount;
}
#endif
}
inline void Referenced::unref() const
{
#if defined(_OSG_REFERENCED_USE_ATOMIC_OPERATIONS)
bool needDelete = (--_refCount == 0);
#else
bool needDelete = false;
if (_refMutex)
{
@@ -148,6 +176,7 @@ inline void Referenced::unref() const
--_refCount;
needDelete = _refCount<=0;
}
#endif
if (needDelete)
{