Added an osgDB::ReadWriteMutex to help manage the ability to have serialize

write to objects but allow multiple threads to read at once in a read only way.
This commit is contained in:
Robert Osfield
2006-12-27 16:40:34 +00:00
parent cd133e5f2c
commit 30cb8735d3
8 changed files with 140 additions and 18 deletions

View File

@@ -12,7 +12,9 @@
*/
#include <osgDB/ReentrantMutex>
#include <OpenThreads/ScopedLock>
#include <osg/Notify>
using namespace osgDB;
using namespace OpenThreads;
@@ -48,6 +50,7 @@ int ReentrantMutex::lock()
int ReentrantMutex::unlock()
{
#if 0
if (_threadHoldingMutex==OpenThreads::Thread::CurrentThread() && _lockCount>0)
{
--_lockCount;
@@ -57,6 +60,21 @@ int ReentrantMutex::unlock()
return Mutex::unlock();
}
}
else
{
osg::notify(osg::NOTICE)<<"Error: ReentrantMutex::unlock() - unlocking from the wrong thread."<<std::endl;
}
#else
if (_lockCount>0)
{
--_lockCount;
if (_lockCount<=0)
{
_threadHoldingMutex = 0;
return Mutex::unlock();
}
}
#endif
return 0;
}
@@ -78,3 +96,49 @@ int ReentrantMutex::trylock()
return result;
}
}
ReadWriteMutex::ReadWriteMutex():
_readCount(0)
{
}
ReadWriteMutex::~ReadWriteMutex()
{
}
int ReadWriteMutex::readLock()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_readCountMutex);
int result = 0;
if (_readCount==0)
{
result = _readWriteMutex.lock();
}
++_readCount;
return result;
}
int ReadWriteMutex::readUnlock()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_readCountMutex);
int result = 0;
if (_readCount>0)
{
--_readCount;
if (_readCount==0)
{
result = _readWriteMutex.unlock();
}
}
return result;
}
int ReadWriteMutex::writeLock()
{
return _readWriteMutex.lock();
}
int ReadWriteMutex::writeUnlock()
{
return _readWriteMutex.unlock();
}