Refactored the ReentrantMutex support so that it utilises the underling thread implementation for recusive mutex support.
This commit is contained in:
@@ -34,16 +34,26 @@ class OPENTHREAD_EXPORT_DIRECTIVE Mutex {
|
||||
|
||||
public:
|
||||
|
||||
enum MutexType
|
||||
{
|
||||
MUTEX_NORMAL,
|
||||
MUTEX_RECURSIVE
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
Mutex();
|
||||
Mutex(MutexType type=MUTEX_NORMAL);
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
virtual ~Mutex();
|
||||
|
||||
|
||||
MutexType getMutexType() const { return _mutexType; }
|
||||
|
||||
|
||||
/**
|
||||
* Lock the mutex
|
||||
*
|
||||
@@ -81,6 +91,7 @@ private:
|
||||
* Implementation-specific private data.
|
||||
*/
|
||||
void *_prvData;
|
||||
MutexType _mutexType;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -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) {}
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user