diff --git a/include/OpenThreads/Mutex b/include/OpenThreads/Mutex index df8c4abf1..e13ad189f 100644 --- a/include/OpenThreads/Mutex +++ b/include/OpenThreads/Mutex @@ -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; }; diff --git a/include/OpenThreads/ReentrantMutex b/include/OpenThreads/ReentrantMutex index dc2f173aa..fdfefb32d 100644 --- a/include/OpenThreads/ReentrantMutex +++ b/include/OpenThreads/ReentrantMutex @@ -25,98 +25,7 @@ class ReentrantMutex : public OpenThreads::Mutex public: ReentrantMutex(): - _threadHoldingMutex(0), - _lockCount(0) {} - - virtual ~ReentrantMutex() {} - - virtual int lock() - { - { - OpenThreads::ScopedLock lock(_lockCountMutex); - if (_threadHoldingMutex==OpenThreads::Thread::CurrentThread() && _lockCount>0) - { - ++_lockCount; - return 0; - } - } - - int result = Mutex::lock(); - if (result==0) - { - OpenThreads::ScopedLock lock(_lockCountMutex); - - _threadHoldingMutex = OpenThreads::Thread::CurrentThread(); - _lockCount = 1; - } - return result; - } - - - virtual int unlock() - { - OpenThreads::ScopedLock 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."<0) - { - --_lockCount; - if (_lockCount<=0) - { - _threadHoldingMutex = 0; - return Mutex::unlock(); - } - } - #endif - return 0; - } - - - virtual int trylock() - { - { - OpenThreads::ScopedLock lock(_lockCountMutex); - if (_threadHoldingMutex==OpenThreads::Thread::CurrentThread() && _lockCount>0) - { - ++_lockCount; - return 0; - } - } - - int result = Mutex::trylock(); - if (result==0) - { - OpenThreads::ScopedLock 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) {} }; diff --git a/src/OpenThreads/pthreads/PThreadMutex.c++ b/src/OpenThreads/pthreads/PThreadMutex.c++ index 733c9fcb5..b9dfbfb0a 100644 --- a/src/OpenThreads/pthreads/PThreadMutex.c++ +++ b/src/OpenThreads/pthreads/PThreadMutex.c++ @@ -30,17 +30,25 @@ using namespace OpenThreads; // // Use: public. // -Mutex::Mutex() { +Mutex::Mutex(MutexType type): + _mutexType(type) +{ pthread_mutexattr_t mutex_attr; pthread_mutexattr_init( &mutex_attr ); PThreadMutexPrivateData *pd = new PThreadMutexPrivateData(); - -#ifndef __linux__ // (not available until NPTL) [ - pthread_mutexattr_settype( &mutex_attr, PTHREAD_MUTEX_ERRORCHECK ); -#endif // ] __linux__ + if (type==MUTEX_RECURSIVE) + { + pthread_mutexattr_settype( &mutex_attr, PTHREAD_MUTEX_RECURSIVE ); + } + else + { + #ifndef __linux__ // (not available until NPTL) [ + pthread_mutexattr_settype( &mutex_attr, PTHREAD_MUTEX_ERRORCHECK ); + #endif // ] __linux__ + } #ifdef ALLOW_PRIORITY_SCHEDULING // [ #ifdef __sun // [ diff --git a/src/OpenThreads/sproc/SprocMutex.c++ b/src/OpenThreads/sproc/SprocMutex.c++ index 700927c1d..4ae939d43 100755 --- a/src/OpenThreads/sproc/SprocMutex.c++ +++ b/src/OpenThreads/sproc/SprocMutex.c++ @@ -34,7 +34,9 @@ using namespace OpenThreads; // // Use: public. // -Mutex::Mutex() { +Mutex::Mutex(MutexType type): + _mutexType(type) +{ SprocMutexPrivateData *pd = new SprocMutexPrivateData(); diff --git a/src/OpenThreads/win32/Win32Mutex.cpp b/src/OpenThreads/win32/Win32Mutex.cpp index 81af208bf..9ddd4058c 100644 --- a/src/OpenThreads/win32/Win32Mutex.cpp +++ b/src/OpenThreads/win32/Win32Mutex.cpp @@ -81,7 +81,10 @@ static void _S_nsec_sleep(int __log_nsec) { // // Use: public. // -Mutex::Mutex() { +Mutex::Mutex(MutexType type): + _mutexType(type) +) +{ Win32MutexPrivateData *pd = new Win32MutexPrivateData(); _prvData = static_cast(pd); }