Added new methods into osg::Referenced for controlling the use of thread safe
ref/unref:
/** Set whether reference counting should be use a mutex to create thread reference counting.*/
static void setThreadSafeReferenceCounting(bool enableThreadSafeReferenceCounting);
/** Get whether reference counting is active.*/
static bool getThreadSafeReferenceCounting();
This commit is contained in:
@@ -16,9 +16,27 @@
|
||||
#include <typeinfo>
|
||||
#include <memory>
|
||||
|
||||
#include <OpenThreads/ScopedLock>
|
||||
#include <OpenThreads/Mutex>
|
||||
|
||||
|
||||
namespace osg
|
||||
{
|
||||
|
||||
static bool s_useThreadSafeReferenceCounting = getenv("OSG_THREAD_SAFE_REF_UNREF")!=0;
|
||||
|
||||
void Referenced::setThreadSafeReferenceCounting(bool enableThreadSafeReferenceCounting)
|
||||
{
|
||||
s_useThreadSafeReferenceCounting = enableThreadSafeReferenceCounting;
|
||||
}
|
||||
|
||||
bool Referenced::getThreadSafeReferenceCounting()
|
||||
{
|
||||
return s_useThreadSafeReferenceCounting;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static std::auto_ptr<DeleteHandler> s_deleteHandler(0);
|
||||
|
||||
void Referenced::setDeleteHandler(DeleteHandler* handler)
|
||||
@@ -31,6 +49,7 @@ DeleteHandler* Referenced::getDeleteHandler()
|
||||
return s_deleteHandler.get();
|
||||
}
|
||||
|
||||
|
||||
Referenced::~Referenced()
|
||||
{
|
||||
if (_refCount>0)
|
||||
@@ -40,4 +59,54 @@ Referenced::~Referenced()
|
||||
}
|
||||
}
|
||||
|
||||
void Referenced::ref() const
|
||||
{
|
||||
if (s_useThreadSafeReferenceCounting)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_refMutex);
|
||||
++_refCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
++_refCount;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Referenced::unref() const
|
||||
{
|
||||
bool needDelete = false;
|
||||
if (s_useThreadSafeReferenceCounting)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_refMutex);
|
||||
--_refCount;
|
||||
needDelete = _refCount<=0;
|
||||
}
|
||||
else
|
||||
{
|
||||
--_refCount;
|
||||
needDelete = _refCount<=0;
|
||||
}
|
||||
|
||||
if (needDelete)
|
||||
{
|
||||
if (getDeleteHandler()) getDeleteHandler()->requestDelete(this);
|
||||
else delete this;
|
||||
}
|
||||
}
|
||||
|
||||
void Referenced::unref_nodelete() const
|
||||
{
|
||||
if (s_useThreadSafeReferenceCounting)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_refMutex);
|
||||
--_refCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
--_refCount;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}; // end of namespace osg
|
||||
|
||||
Reference in New Issue
Block a user