Refactored the ReentrantMutex support so that it utilises the underling thread implementation for recusive mutex support.

This commit is contained in:
Robert Osfield
2010-02-18 20:14:41 +00:00
parent ab66740fb0
commit 787daeeb93
5 changed files with 33 additions and 100 deletions

View File

@@ -25,98 +25,7 @@ class ReentrantMutex : public OpenThreads::Mutex
public:
ReentrantMutex():
_threadHoldingMutex(0),
_lockCount(0) {}
virtual ~ReentrantMutex() {}
virtual int lock()
{
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_lockCountMutex);
if (_threadHoldingMutex==OpenThreads::Thread::CurrentThread() && _lockCount>0)
{
++_lockCount;
return 0;
}
}
int result = Mutex::lock();
if (result==0)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_lockCountMutex);
_threadHoldingMutex = OpenThreads::Thread::CurrentThread();
_lockCount = 1;
}
return result;
}
virtual int unlock()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_lockCountMutex);
#if 0
if (_threadHoldingMutex==OpenThreads::Thread::CurrentThread() && _lockCount>0)
{
--_lockCount;
if (_lockCount<=0)
{
_threadHoldingMutex = 0;
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;
}
virtual int trylock()
{
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_lockCountMutex);
if (_threadHoldingMutex==OpenThreads::Thread::CurrentThread() && _lockCount>0)
{
++_lockCount;
return 0;
}
}
int result = Mutex::trylock();
if (result==0)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_lockCountMutex);
_threadHoldingMutex = OpenThreads::Thread::CurrentThread();
_lockCount = 1;
}
return result;
}
private:
ReentrantMutex(const ReentrantMutex&):OpenThreads::Mutex() {}
ReentrantMutex& operator =(const ReentrantMutex&) { return *(this); }
OpenThreads::Thread* _threadHoldingMutex;
OpenThreads::Mutex _lockCountMutex;
unsigned int _lockCount;
Mutex(MUTEX_RECURSIVE) {}
};