diff --git a/Foundation/include/Poco/Mutex.h b/Foundation/include/Poco/Mutex.h index c4f849e7d..74c092025 100644 --- a/Foundation/include/Poco/Mutex.h +++ b/Foundation/include/Poco/Mutex.h @@ -49,9 +49,16 @@ class Foundation_API Mutex: private MutexImpl /// lock and unlock a mutex. { public: + enum MutexType + /// The type of a mutex. + { + MUTEX_RECURSIVE = MUTEX_RECURSIVE_IMPL, /// A recursive mutex + MUTEX_NONRECURSIVE = MUTEX_NONRECURSIVE_IMPL, /// A non-recursive mutex + }; + typedef Poco::ScopedLock ScopedLock; - explicit Mutex(bool recursive = true); + explicit Mutex(MutexType type = MUTEX_RECURSIVE); /// creates the Mutex. ~Mutex(); diff --git a/Foundation/include/Poco/Mutex_POSIX.h b/Foundation/include/Poco/Mutex_POSIX.h index db586b745..5dfb83928 100644 --- a/Foundation/include/Poco/Mutex_POSIX.h +++ b/Foundation/include/Poco/Mutex_POSIX.h @@ -31,8 +31,15 @@ namespace Poco { class Foundation_API MutexImpl { +public: + enum MutexTypeImpl + { + MUTEX_RECURSIVE_IMPL, + MUTEX_NONRECURSIVE_IMPL, + }; + protected: - MutexImpl(bool recursive); + explicit MutexImpl(MutexTypeImpl type); ~MutexImpl(); void lockImpl(); bool tryLockImpl(); diff --git a/Foundation/include/Poco/Mutex_VX.h b/Foundation/include/Poco/Mutex_VX.h index 27457d5ed..3fbb37834 100644 --- a/Foundation/include/Poco/Mutex_VX.h +++ b/Foundation/include/Poco/Mutex_VX.h @@ -31,8 +31,15 @@ namespace Poco { class Foundation_API MutexImpl { +public: + enum MutexTypeImpl + { + MUTEX_RECURSIVE_IMPL, + MUTEX_NONRECURSIVE_IMPL, + }; + protected: - MutexImpl(bool recursive); + explicit MutexImpl(MutexTypeImpl type); ~MutexImpl(); void lockImpl(); bool tryLockImpl(); diff --git a/Foundation/include/Poco/Mutex_WIN32.h b/Foundation/include/Poco/Mutex_WIN32.h index 6ae48d005..e4fc2c55b 100644 --- a/Foundation/include/Poco/Mutex_WIN32.h +++ b/Foundation/include/Poco/Mutex_WIN32.h @@ -30,8 +30,15 @@ namespace Poco { class Foundation_API MutexImpl { +public: + enum MutexTypeImpl + { + MUTEX_RECURSIVE_IMPL, + MUTEX_NONRECURSIVE_IMPL, + }; + protected: - MutexImpl(bool recursive); + explicit MutexImpl(MutexTypeImpl type); ~MutexImpl(); void lockImpl(); bool tryLockImpl(); diff --git a/Foundation/include/Poco/Mutex_WINCE.h b/Foundation/include/Poco/Mutex_WINCE.h index 8f672d46d..21fca3f03 100644 --- a/Foundation/include/Poco/Mutex_WINCE.h +++ b/Foundation/include/Poco/Mutex_WINCE.h @@ -30,8 +30,15 @@ namespace Poco { class Foundation_API MutexImpl { +public: + enum MutexTypeImpl + { + MUTEX_RECURSIVE_IMPL, + MUTEX_NONRECURSIVE_IMPL, + }; + protected: - MutexImpl(bool recursive); + explicit MutexImpl(MutexTypeImpl type); ~MutexImpl(); void lockImpl(); bool tryLockImpl(); diff --git a/Foundation/samples/Benchmark/src/Benchmark.cpp b/Foundation/samples/Benchmark/src/Benchmark.cpp index dac13c848..02b843c5b 100644 --- a/Foundation/samples/Benchmark/src/Benchmark.cpp +++ b/Foundation/samples/Benchmark/src/Benchmark.cpp @@ -49,12 +49,12 @@ int main(int argc, char** argv) } { - Poco::Mutex mtx(true); + Poco::Mutex mtx(Poco::Mutex::MUTEX_RECURSIVE); Benchmark(mtx, "Mutex(true)"); } { - Poco::Mutex mtx(false); + Poco::Mutex mtx(Poco::Mutex::MUTEX_NONRECURSIVE); Benchmark(mtx, "Mutex(false)"); } diff --git a/Foundation/src/Mutex.cpp b/Foundation/src/Mutex.cpp index 5ad648f3d..fa28e7aab 100644 --- a/Foundation/src/Mutex.cpp +++ b/Foundation/src/Mutex.cpp @@ -33,8 +33,8 @@ namespace Poco { -Mutex::Mutex(bool recursive) - : MutexImpl(recursive) +Mutex::Mutex(MutexType type) + : MutexImpl((MutexTypeImpl) type) { } diff --git a/Foundation/src/Mutex_POSIX.cpp b/Foundation/src/Mutex_POSIX.cpp index 7d242ed48..c3d63c875 100644 --- a/Foundation/src/Mutex_POSIX.cpp +++ b/Foundation/src/Mutex_POSIX.cpp @@ -38,7 +38,7 @@ namespace Poco { -MutexImpl::MutexImpl(bool recursive) +MutexImpl::MutexImpl(MutexTypeImpl type) { #if defined(POCO_VXWORKS) // This workaround is for VxWorks 5.x where @@ -50,9 +50,9 @@ MutexImpl::MutexImpl(bool recursive) pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); #if defined(PTHREAD_MUTEX_RECURSIVE_NP) - pthread_mutexattr_settype_np(&attr, recursive ? PTHREAD_MUTEX_RECURSIVE_NP : PTHREAD_MUTEX_NORMAL_NP); + pthread_mutexattr_settype_np(&attr, type == MUTEX_RECURSIVE_IMPL ? PTHREAD_MUTEX_RECURSIVE_NP : PTHREAD_MUTEX_NORMAL_NP); #elif !defined(POCO_VXWORKS) - pthread_mutexattr_settype(&attr, recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL); + pthread_mutexattr_settype(&attr, type == MUTEX_RECURSIVE_IMPL ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL); #endif if (pthread_mutex_init(&_mutex, &attr)) { diff --git a/Foundation/src/Mutex_VX.cpp b/Foundation/src/Mutex_VX.cpp index 1ebf73043..352ba826c 100644 --- a/Foundation/src/Mutex_VX.cpp +++ b/Foundation/src/Mutex_VX.cpp @@ -21,15 +21,16 @@ namespace Poco { -MutexImpl::MutexImpl(bool recursive) +MutexImpl::MutexImpl(MutexTypeImpl type) { - if (recursive) + switch (type) { + case MUTEX_RECURSIVE_IMPL: _sem = semMCreate(SEM_INVERSION_SAFE | SEM_Q_PRIORITY); - } - else - { + break; + case MUTEX_NONRECURSIVE_IMPL: _sem = semBCreate(SEM_Q_PRIORITY, SEM_FULL); + break; } if (_sem == 0) throw Poco::SystemException("cannot create mutex"); diff --git a/Foundation/src/Mutex_WIN32.cpp b/Foundation/src/Mutex_WIN32.cpp index f784df9f0..63d3ae161 100644 --- a/Foundation/src/Mutex_WIN32.cpp +++ b/Foundation/src/Mutex_WIN32.cpp @@ -22,9 +22,9 @@ namespace Poco { -MutexImpl::MutexImpl(bool recursive) +MutexImpl::MutexImpl(MutexTypeImpl type) : _lockCount(0) - , _recursive(recursive) + , _recursive(type == MUTEX_RECURSIVE_IMPL) { // the fct has a boolean return value under WInnNt/2000/XP but not on Win98 // the return only checks if the input address of &_cs was valid, so it is safe to omit it diff --git a/Foundation/src/Mutex_WINCE.cpp b/Foundation/src/Mutex_WINCE.cpp index e37ea2a68..6d9f544ca 100644 --- a/Foundation/src/Mutex_WINCE.cpp +++ b/Foundation/src/Mutex_WINCE.cpp @@ -22,9 +22,9 @@ namespace Poco { -MutexImpl::MutexImpl(bool recursive) +MutexImpl::MutexImpl(MutexTypeImpl type) : _lockCount(0) - , _recursive(recursive) + , _recursive(type == MUTEX_RECURSIVE_IMPL) { _mutex = CreateMutexW(NULL, FALSE, NULL); if (!_mutex) throw SystemException("cannot create mutex"); diff --git a/Foundation/testsuite/src/MutexTest.cpp b/Foundation/testsuite/src/MutexTest.cpp index 49019eeaa..9b3f70f24 100644 --- a/Foundation/testsuite/src/MutexTest.cpp +++ b/Foundation/testsuite/src/MutexTest.cpp @@ -40,7 +40,7 @@ namespace { try { - Mutex mtx(false); + Mutex mtx(Mutex::MUTEX_NONRECURSIVE); mtx.lock(); mtx.lock(); } @@ -74,7 +74,7 @@ MutexTest::~MutexTest() void MutexTest::testMutexRecursion() { - Mutex mtx(false); + Mutex mtx(Mutex::MUTEX_NONRECURSIVE); mtx.lock(); bool success = mtx.tryLock();