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:
Robert Osfield
2004-09-27 14:15:13 +00:00
parent 7e5c87de6a
commit de0e616433
2 changed files with 105 additions and 52 deletions

View File

@@ -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