From 1e82f65ed7c23029c8e7063498d7d877e7bd1cc8 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Sun, 27 Feb 2011 00:45:30 +0100 Subject: [PATCH] TRACE macro to Common.hpp, ScopedLock with Mutex* arg, Mutex cctor and assign op is private --- include/Common.hpp | 6 ++ include/Mutex.hpp | 6 +- include/ScopedLock.hpp | 23 ++++++ src/Mutex.cpp | 14 +++- src/ScopedLock.cpp | 16 ++++ test/main_ScopedLock.cpp | 40 ++++++++++ test/main_standalone_Mutex_ScopedLock.cpp | 94 +++++++++++++++++++++++ 7 files changed, 194 insertions(+), 5 deletions(-) create mode 100644 include/Common.hpp create mode 100644 include/ScopedLock.hpp create mode 100644 src/ScopedLock.cpp create mode 100644 test/main_ScopedLock.cpp create mode 100644 test/main_standalone_Mutex_ScopedLock.cpp diff --git a/include/Common.hpp b/include/Common.hpp new file mode 100644 index 0000000..28cc55d --- /dev/null +++ b/include/Common.hpp @@ -0,0 +1,6 @@ +#include + +#ifndef NOTRACE + #define TRACE(x) std::cout << x << " " << __PRETTY_FUNCTION__ << \ + " @ " << __FILE__ << " : " << __LINE__ << std::endl +#endif \ No newline at end of file diff --git a/include/Mutex.hpp b/include/Mutex.hpp index 6c45510..6f35603 100644 --- a/include/Mutex.hpp +++ b/include/Mutex.hpp @@ -8,15 +8,19 @@ class Mutex public: - Mutex(int type = PTHREAD_MUTEX_DEFAULT); + Mutex(int kind = PTHREAD_MUTEX_DEFAULT); ~Mutex(); void lock(); void unlock(); bool tryLock(int interval = 0); + private: + Mutex(const Mutex& m); + Mutex& operator=(const Mutex& m); + pthread_mutex_t m_mutex; }; diff --git a/include/ScopedLock.hpp b/include/ScopedLock.hpp new file mode 100644 index 0000000..1ff51ae --- /dev/null +++ b/include/ScopedLock.hpp @@ -0,0 +1,23 @@ +#ifndef SCOPED_LOCK_HPP +#define SCOPED_LOCK_HPP + +#include "Mutex.hpp" + +class ScopedLock +{ + +public: + + ScopedLock(Mutex* m); + ~ScopedLock(); + +private: + + ScopedLock(const ScopedLock&); + ScopedLock& operator=(const ScopedLock&); + + Mutex* m_mutex; + +}; + +#endif // SCOPED_LOCK_HPP diff --git a/src/Mutex.cpp b/src/Mutex.cpp index b533ff3..f975cde 100644 --- a/src/Mutex.cpp +++ b/src/Mutex.cpp @@ -1,18 +1,20 @@ #include "Mutex.hpp" +#include "Common.hpp" #include #include -Mutex::Mutex(int type) +Mutex::Mutex(int kind) : m_mutex(PTHREAD_MUTEX_INITIALIZER) { + TRACE(this); int ret; - if ( type == PTHREAD_MUTEX_DEFAULT ) { + if ( kind == 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); + pthread_mutexattr_init( &attr ); + pthread_mutexattr_settype( &attr, kind ); ret = pthread_mutex_init( &m_mutex, &attr ); } assert( ret == 0 ); @@ -21,6 +23,7 @@ Mutex::Mutex(int type) Mutex::~Mutex() { + TRACE(this); int ret = pthread_mutex_destroy ( &m_mutex ); assert( ret == 0); } @@ -28,6 +31,7 @@ Mutex::~Mutex() void Mutex::lock() { + TRACE(this); int ret = pthread_mutex_lock( &m_mutex ); assert( ret == 0); } @@ -35,6 +39,7 @@ void Mutex::lock() void Mutex::unlock() { + TRACE(this); int ret = pthread_mutex_unlock ( &m_mutex ); assert( ret == 0); } @@ -42,6 +47,7 @@ void Mutex::unlock() bool Mutex::tryLock(int interval) { + TRACE(this); if ( interval == 0 ) { int result = pthread_mutex_trylock ( &m_mutex ); return result == 0; diff --git a/src/ScopedLock.cpp b/src/ScopedLock.cpp new file mode 100644 index 0000000..dde7c83 --- /dev/null +++ b/src/ScopedLock.cpp @@ -0,0 +1,16 @@ +#include "ScopedLock.hpp" +#include "Common.hpp" + + +ScopedLock::ScopedLock(Mutex* m) : m_mutex(m) +{ + TRACE(this); + m_mutex->lock(); +} + + +ScopedLock::~ScopedLock() +{ + TRACE(this); + m_mutex->unlock(); +} diff --git a/test/main_ScopedLock.cpp b/test/main_ScopedLock.cpp new file mode 100644 index 0000000..9f91c70 --- /dev/null +++ b/test/main_ScopedLock.cpp @@ -0,0 +1,40 @@ +// g++ -Wall -Wextra src/*.cpp test/main_Mutex.cpp -Iinclude -lpthread -lrt + + +#include "ScopedLock.hpp" +#include "Mutex.hpp" +#include "Common.hpp" + +#include + +class User +{ +public: + User() : m_mutex() { + TRACE(this); + } + ~User() { + TRACE(this); + } + void fv() { + TRACE(this); + ScopedLock sl(&m_mutex); + throw std::logic_error("whoops"); + } +private: + Mutex m_mutex; +}; + +int main() +{ + User u; + + try { + u.fv(); + } catch (std::logic_error ex) { + std::cout << "std::logicexception: " << ex.what() << std::endl; + } + + return 0; +} + diff --git a/test/main_standalone_Mutex_ScopedLock.cpp b/test/main_standalone_Mutex_ScopedLock.cpp new file mode 100644 index 0000000..c8e5956 --- /dev/null +++ b/test/main_standalone_Mutex_ScopedLock.cpp @@ -0,0 +1,94 @@ + + +#include +#include + +#include + +class Mutex +{ +public: + + Mutex() : m_mutex(PTHREAD_MUTEX_INITIALIZER) { + TRACE(this); + int ret = pthread_mutex_init( &m_mutex, 0 ); + assert( ret == 0 ); + } + ~Mutex() { + TRACE(this); + int ret = pthread_mutex_destroy ( &m_mutex ); + assert( ret == 0 ); + } + void lock() { + TRACE(this); + int ret = pthread_mutex_lock( &m_mutex ); + assert( ret == 0 ); + } + void unlock() { + TRACE(this); + int ret = pthread_mutex_unlock( &m_mutex ); + assert( ret == 0 ); + } +private: + Mutex(const Mutex& m); + Mutex& operator=(const Mutex& m); + +private: + pthread_mutex_t m_mutex; +}; + + +class ScopedLock +{ +public: + + ScopedLock(Mutex* m) : m_mutex(m) { + TRACE(this); + m_mutex->lock(); + } + ~ScopedLock() { + TRACE(this); + m_mutex->unlock(); + } + +private: + ScopedLock(const ScopedLock&); + ScopedLock& operator=(const ScopedLock&); + + Mutex* m_mutex; +}; + +class User +{ +public: + User() : m_mutex() { + TRACE(this); + } + ~User() { + TRACE(this); + } + void fv() { + TRACE(this); + ScopedLock sl(&m_mutex); + throw std::logic_error("whoops"); + } +private: + Mutex m_mutex; +}; + +int main() +{ + TRACE("main begin"); + + User u; + + try { + u.fv(); + } catch (std::logic_error ex) { + std::cout << "std::logicexception: " << ex.what() << std::endl; + } + + + TRACE("main end"); + return 0; +}