TRACE macro to Common.hpp, ScopedLock with Mutex* arg, Mutex cctor and assign op is private

master
Denes Matetelki 14 years ago
parent c141568b13
commit 1e82f65ed7

@ -0,0 +1,6 @@
#include <iostream>
#ifndef NOTRACE
#define TRACE(x) std::cout << x << " " << __PRETTY_FUNCTION__ << \
" @ " << __FILE__ << " : " << __LINE__ << std::endl
#endif

@ -8,15 +8,19 @@ class Mutex
public: public:
Mutex(int type = PTHREAD_MUTEX_DEFAULT); Mutex(int kind = PTHREAD_MUTEX_DEFAULT);
~Mutex(); ~Mutex();
void lock(); void lock();
void unlock(); void unlock();
bool tryLock(int interval = 0); bool tryLock(int interval = 0);
private: private:
Mutex(const Mutex& m);
Mutex& operator=(const Mutex& m);
pthread_mutex_t m_mutex; pthread_mutex_t m_mutex;
}; };

@ -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

@ -1,18 +1,20 @@
#include "Mutex.hpp" #include "Mutex.hpp"
#include "Common.hpp"
#include <assert.h> #include <assert.h>
#include <time.h> #include <time.h>
Mutex::Mutex(int type) Mutex::Mutex(int kind) : m_mutex(PTHREAD_MUTEX_INITIALIZER)
{ {
TRACE(this);
int ret; int ret;
if ( type == PTHREAD_MUTEX_DEFAULT ) { if ( kind == PTHREAD_MUTEX_DEFAULT ) {
ret = pthread_mutex_init( &m_mutex, 0 ); ret = pthread_mutex_init( &m_mutex, 0 );
} else { } else {
pthread_mutexattr_t attr; pthread_mutexattr_t attr;
pthread_mutexattr_init( &attr ); pthread_mutexattr_init( &attr );
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); pthread_mutexattr_settype( &attr, kind );
ret = pthread_mutex_init( &m_mutex, &attr ); ret = pthread_mutex_init( &m_mutex, &attr );
} }
assert( ret == 0 ); assert( ret == 0 );
@ -21,6 +23,7 @@ Mutex::Mutex(int type)
Mutex::~Mutex() Mutex::~Mutex()
{ {
TRACE(this);
int ret = pthread_mutex_destroy ( &m_mutex ); int ret = pthread_mutex_destroy ( &m_mutex );
assert( ret == 0); assert( ret == 0);
} }
@ -28,6 +31,7 @@ Mutex::~Mutex()
void Mutex::lock() void Mutex::lock()
{ {
TRACE(this);
int ret = pthread_mutex_lock( &m_mutex ); int ret = pthread_mutex_lock( &m_mutex );
assert( ret == 0); assert( ret == 0);
} }
@ -35,6 +39,7 @@ void Mutex::lock()
void Mutex::unlock() void Mutex::unlock()
{ {
TRACE(this);
int ret = pthread_mutex_unlock ( &m_mutex ); int ret = pthread_mutex_unlock ( &m_mutex );
assert( ret == 0); assert( ret == 0);
} }
@ -42,6 +47,7 @@ void Mutex::unlock()
bool Mutex::tryLock(int interval) bool Mutex::tryLock(int interval)
{ {
TRACE(this);
if ( interval == 0 ) { if ( interval == 0 ) {
int result = pthread_mutex_trylock ( &m_mutex ); int result = pthread_mutex_trylock ( &m_mutex );
return result == 0; return result == 0;

@ -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();
}

@ -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 <stdexcept>
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;
}

@ -0,0 +1,94 @@
#include <assert.h>
#include <pthread.h>
#include <stdexcept>
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;
}
Loading…
Cancel
Save