Replaced boolean with enum in c'tor.

Implemented in a similar style to FPEnvironment.
This commit is contained in:
martin-osborne
2015-01-14 10:48:22 +00:00
parent f9942829d3
commit 00b568193c
12 changed files with 59 additions and 23 deletions

View File

@@ -49,9 +49,16 @@ class Foundation_API Mutex: private MutexImpl
/// lock and unlock a mutex. /// lock and unlock a mutex.
{ {
public: 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<Mutex> ScopedLock; typedef Poco::ScopedLock<Mutex> ScopedLock;
explicit Mutex(bool recursive = true); explicit Mutex(MutexType type = MUTEX_RECURSIVE);
/// creates the Mutex. /// creates the Mutex.
~Mutex(); ~Mutex();

View File

@@ -31,8 +31,15 @@ namespace Poco {
class Foundation_API MutexImpl class Foundation_API MutexImpl
{ {
public:
enum MutexTypeImpl
{
MUTEX_RECURSIVE_IMPL,
MUTEX_NONRECURSIVE_IMPL,
};
protected: protected:
MutexImpl(bool recursive); explicit MutexImpl(MutexTypeImpl type);
~MutexImpl(); ~MutexImpl();
void lockImpl(); void lockImpl();
bool tryLockImpl(); bool tryLockImpl();

View File

@@ -31,8 +31,15 @@ namespace Poco {
class Foundation_API MutexImpl class Foundation_API MutexImpl
{ {
public:
enum MutexTypeImpl
{
MUTEX_RECURSIVE_IMPL,
MUTEX_NONRECURSIVE_IMPL,
};
protected: protected:
MutexImpl(bool recursive); explicit MutexImpl(MutexTypeImpl type);
~MutexImpl(); ~MutexImpl();
void lockImpl(); void lockImpl();
bool tryLockImpl(); bool tryLockImpl();

View File

@@ -30,8 +30,15 @@ namespace Poco {
class Foundation_API MutexImpl class Foundation_API MutexImpl
{ {
public:
enum MutexTypeImpl
{
MUTEX_RECURSIVE_IMPL,
MUTEX_NONRECURSIVE_IMPL,
};
protected: protected:
MutexImpl(bool recursive); explicit MutexImpl(MutexTypeImpl type);
~MutexImpl(); ~MutexImpl();
void lockImpl(); void lockImpl();
bool tryLockImpl(); bool tryLockImpl();

View File

@@ -30,8 +30,15 @@ namespace Poco {
class Foundation_API MutexImpl class Foundation_API MutexImpl
{ {
public:
enum MutexTypeImpl
{
MUTEX_RECURSIVE_IMPL,
MUTEX_NONRECURSIVE_IMPL,
};
protected: protected:
MutexImpl(bool recursive); explicit MutexImpl(MutexTypeImpl type);
~MutexImpl(); ~MutexImpl();
void lockImpl(); void lockImpl();
bool tryLockImpl(); bool tryLockImpl();

View File

@@ -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)"); Benchmark(mtx, "Mutex(true)");
} }
{ {
Poco::Mutex mtx(false); Poco::Mutex mtx(Poco::Mutex::MUTEX_NONRECURSIVE);
Benchmark(mtx, "Mutex(false)"); Benchmark(mtx, "Mutex(false)");
} }

View File

@@ -33,8 +33,8 @@
namespace Poco { namespace Poco {
Mutex::Mutex(bool recursive) Mutex::Mutex(MutexType type)
: MutexImpl(recursive) : MutexImpl((MutexTypeImpl) type)
{ {
} }

View File

@@ -38,7 +38,7 @@
namespace Poco { namespace Poco {
MutexImpl::MutexImpl(bool recursive) MutexImpl::MutexImpl(MutexTypeImpl type)
{ {
#if defined(POCO_VXWORKS) #if defined(POCO_VXWORKS)
// This workaround is for VxWorks 5.x where // This workaround is for VxWorks 5.x where
@@ -50,9 +50,9 @@ MutexImpl::MutexImpl(bool recursive)
pthread_mutexattr_t attr; pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr); pthread_mutexattr_init(&attr);
#if defined(PTHREAD_MUTEX_RECURSIVE_NP) #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) #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 #endif
if (pthread_mutex_init(&_mutex, &attr)) if (pthread_mutex_init(&_mutex, &attr))
{ {

View File

@@ -21,15 +21,16 @@
namespace Poco { 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); _sem = semMCreate(SEM_INVERSION_SAFE | SEM_Q_PRIORITY);
} break;
else case MUTEX_NONRECURSIVE_IMPL:
{
_sem = semBCreate(SEM_Q_PRIORITY, SEM_FULL); _sem = semBCreate(SEM_Q_PRIORITY, SEM_FULL);
break;
} }
if (_sem == 0) if (_sem == 0)
throw Poco::SystemException("cannot create mutex"); throw Poco::SystemException("cannot create mutex");

View File

@@ -22,9 +22,9 @@
namespace Poco { namespace Poco {
MutexImpl::MutexImpl(bool recursive) MutexImpl::MutexImpl(MutexTypeImpl type)
: _lockCount(0) : _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 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 // the return only checks if the input address of &_cs was valid, so it is safe to omit it

View File

@@ -22,9 +22,9 @@
namespace Poco { namespace Poco {
MutexImpl::MutexImpl(bool recursive) MutexImpl::MutexImpl(MutexTypeImpl type)
: _lockCount(0) : _lockCount(0)
, _recursive(recursive) , _recursive(type == MUTEX_RECURSIVE_IMPL)
{ {
_mutex = CreateMutexW(NULL, FALSE, NULL); _mutex = CreateMutexW(NULL, FALSE, NULL);
if (!_mutex) throw SystemException("cannot create mutex"); if (!_mutex) throw SystemException("cannot create mutex");

View File

@@ -40,7 +40,7 @@ namespace
{ {
try try
{ {
Mutex mtx(false); Mutex mtx(Mutex::MUTEX_NONRECURSIVE);
mtx.lock(); mtx.lock();
mtx.lock(); mtx.lock();
} }
@@ -74,7 +74,7 @@ MutexTest::~MutexTest()
void MutexTest::testMutexRecursion() void MutexTest::testMutexRecursion()
{ {
Mutex mtx(false); Mutex mtx(Mutex::MUTEX_NONRECURSIVE);
mtx.lock(); mtx.lock();
bool success = mtx.tryLock(); bool success = mtx.tryLock();