From c141568b13bdff9e69342b9b6992531dfe7a86ec Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Sat, 26 Feb 2011 22:38:34 +0100 Subject: [PATCH] mutex ctor can have a type, class has tryLock method --- include/Mutex.hpp | 13 ++++++++----- src/Mutex.cpp | 44 +++++++++++++++++++++++++++++++++++++++----- test/main_Mutex.cpp | 32 +++++++++++++++++++++----------- 3 files changed, 68 insertions(+), 21 deletions(-) diff --git a/include/Mutex.hpp b/include/Mutex.hpp index e15dd39..6c45510 100644 --- a/include/Mutex.hpp +++ b/include/Mutex.hpp @@ -5,16 +5,19 @@ class Mutex { + public: - Mutex(); - ~Mutex(); - void lock(); - void unlock(); + Mutex(int type = PTHREAD_MUTEX_DEFAULT); + ~Mutex(); + + void lock(); + void unlock(); + bool tryLock(int interval = 0); private: - pthread_mutex_t m_mutex; + pthread_mutex_t m_mutex; }; diff --git a/src/Mutex.cpp b/src/Mutex.cpp index 9b051b7..b533ff3 100644 --- a/src/Mutex.cpp +++ b/src/Mutex.cpp @@ -1,26 +1,60 @@ #include "Mutex.hpp" +#include +#include -Mutex::Mutex() + +Mutex::Mutex(int type) { - pthread_mutex_init( &m_mutex, 0 ); + int ret; + if ( type == PTHREAD_MUTEX_DEFAULT ) { + ret = pthread_mutex_init( &m_mutex, 0 ); + } else { + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); + ret = pthread_mutex_init( &m_mutex, &attr ); + } + assert( ret == 0 ); } Mutex::~Mutex() { - pthread_mutex_destroy ( &m_mutex ); + int ret = pthread_mutex_destroy ( &m_mutex ); + assert( ret == 0); } void Mutex::lock() { - pthread_mutex_lock( &m_mutex ); + int ret = pthread_mutex_lock( &m_mutex ); + assert( ret == 0); } void Mutex::unlock() { - pthread_mutex_unlock ( &m_mutex ); + int ret = pthread_mutex_unlock ( &m_mutex ); + assert( ret == 0); +} + + +bool Mutex::tryLock(int interval) +{ + if ( interval == 0 ) { + int result = pthread_mutex_trylock ( &m_mutex ); + return result == 0; + } else { + timespec abs_time; + clock_gettime ( CLOCK_REALTIME, &abs_time ); + abs_time.tv_nsec += interval * 1000000; + if ( abs_time.tv_nsec >= 1000000000 ) { + abs_time.tv_nsec -= 1000000000; + abs_time.tv_sec += 1; + } + int result = pthread_mutex_timedlock ( &m_mutex, &abs_time ); + return result == 0; + } } diff --git a/test/main_Mutex.cpp b/test/main_Mutex.cpp index 2130d4e..1b4f560 100644 --- a/test/main_Mutex.cpp +++ b/test/main_Mutex.cpp @@ -1,3 +1,5 @@ +// g++ -Wall -Wextra src/*.cpp test/main_Mutex.cpp -Iinclude -lpthread -lrt + #include #include @@ -8,24 +10,32 @@ int counter = 0; void *functionC( void* params ) { - m.lock(); - counter++; - std::cout << "Counter value: " << counter << std::endl; - m.unlock(); - return 0; + m.lock(); + counter++; + std::cout << "Counter value: " << counter << std::endl; + m.unlock(); + return 0; } int main() { - pthread_t thread1, thread2; + pthread_t thread1, thread2; + + pthread_create( &thread1, 0, &functionC, 0 ); + pthread_create( &thread2, 0, &functionC, 0 ); + + pthread_join( thread1, 0 ); + pthread_join( thread2, 0 ); + + Mutex m2(PTHREAD_MUTEX_ERRORCHECK); - pthread_create( &thread1, 0, &functionC, 0 ); - pthread_create( &thread2, 0, &functionC, 0 ); + m2.lock(); + m2.unlock(); - pthread_join( thread1, 0 ); - pthread_join( thread2, 0 ); + m2.unlock(); + m2.unlock(); - return 0; + return 0; }