From 1b94865540f78a04f70f30c4984fae1c20658f51 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Fri, 5 Jul 2013 19:38:08 +0200 Subject: [PATCH] Small Mutex refactor --- include/Mutex.hpp | 34 ++++++++++++++++------------------ src/Mutex.cpp | 36 ++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 38 deletions(-) diff --git a/include/Mutex.hpp b/include/Mutex.hpp index 2931b87..85a5e51 100644 --- a/include/Mutex.hpp +++ b/include/Mutex.hpp @@ -3,43 +3,41 @@ #include -class Mutex +class Mutex { public: - Mutex(int kind = PTHREAD_MUTEX_DEFAULT); + enum MutexType { + Normal = PTHREAD_MUTEX_NORMAL, // no deadlock check, unlock a non-locked mutex results undefined behaviour + ErrorCheck = PTHREAD_MUTEX_ERRORCHECK, // error returned when relock or unlocking a non-locked mutex + Recursive = PTHREAD_MUTEX_RECURSIVE, // Lock counting with error handling. + Default = PTHREAD_MUTEX_DEFAULT // equals normal + }; + + Mutex(MutexType type = MutexType::Default); ~Mutex(); - /** - * Locks mutex. If already locked, the calling thread shall block until - * the mutex becomes available. - * @returns If successful, zero; otherwise, an error number. - */ + MutexType getType() const { return m_type; } + pthread_mutex_t* getPThreadMutex() { return &m_mutex; } + + // If already locked, the calling thread shall block until the mutex becomes available. int lock(); - /** - * - */ int unlock(); - /** - * If currently locked, the call shall return immediately. - * @returns Zero if a lock acquired. Otherwise, an error number. - */ + // If currently locked, the call shall return immediately. int tryLock( const long int intervalSec = 0, const long int intervalNSec = 0 ); - pthread_mutex_t* getPThreadMutex(); - - private: Mutex(const Mutex& m); Mutex& operator=(const Mutex& m); pthread_mutex_t m_mutex; - + MutexType m_type; + pthread_mutexattr_t m_attr; }; #endif // MUTEX_HPP diff --git a/src/Mutex.cpp b/src/Mutex.cpp index 6b7d54e..fc19966 100644 --- a/src/Mutex.cpp +++ b/src/Mutex.cpp @@ -4,24 +4,26 @@ #include +namespace { -pthread_mutex_t& MutexCtor( pthread_mutex_t& mutex ) -{ - pthread_mutex_init( &mutex, 0 ); - return mutex; -} + pthread_mutex_t& MutexCtor( pthread_mutex_t& mutex ) + { + pthread_mutex_init( &mutex, 0 ); + return mutex; + } +} // anonym namespace -Mutex::Mutex( int kind ) - : m_mutex( MutexCtor( m_mutex ) ) + +Mutex::Mutex( MutexType type ) + : m_mutex( MutexCtor( m_mutex ) ) // init with function + , m_type( type ) { TRACE; - if ( kind != PTHREAD_MUTEX_DEFAULT ) { - /// @bug passing local variables address - pthread_mutexattr_t attr; - pthread_mutexattr_init( &attr ); - pthread_mutexattr_settype( &attr, kind ); - pthread_mutex_init( &m_mutex, &attr ); + if ( type != PTHREAD_MUTEX_DEFAULT ) { + pthread_mutexattr_init( &m_attr ); + pthread_mutexattr_settype( &m_attr, type ); + pthread_mutex_init( &m_mutex, &m_attr ); } } @@ -56,10 +58,4 @@ int Mutex::tryLock( const long int intervalSec, const long int intervalNSec ) timespec tspec = addTotimespec( intervalSec, intervalSec ); return pthread_mutex_timedlock( &m_mutex, &tspec ); } -} - - -pthread_mutex_t* Mutex::getPThreadMutex() -{ - return &m_mutex; -} +} \ No newline at end of file