init functions for the Mutex and CondVar ctior init lists

master
Denes Matetelki 14 years ago
parent 097db4c80d
commit b328c6f9dd

@ -12,7 +12,6 @@ public:
Timer(const int signal = SIGALRM ); Timer(const int signal = SIGALRM );
virtual ~Timer() {} virtual ~Timer() {}
virtual void timerExpired() {} virtual void timerExpired() {}
virtual void periodicTimerExpired() {} virtual void periodicTimerExpired() {}
@ -31,6 +30,12 @@ public:
private: private:
// after turning on all warnings, gcc reports that the class has pointer
// data members (time_t, which is an int by the way) so copy ctor and
// assign op shall be inmplemented
Timer(const Timer& timer);
Timer& operator=(const Timer&) { return *this; }
int m_signal; int m_signal;
struct sigaction m_sigAction; struct sigaction m_sigAction;
timer_t m_timerId; timer_t m_timerId;

@ -3,9 +3,16 @@
#include <time.h> #include <time.h>
pthread_cond_t& CondVarCtor(pthread_cond_t& condVar)
{
pthread_cond_init( &condVar, 0 );
return condVar;
}
ConditionVariable::ConditionVariable(Mutex& mutex) ConditionVariable::ConditionVariable(Mutex& mutex)
: m_mutex(mutex) : m_mutex(mutex)
, m_condVar(CondVarCtor(m_condVar))
{ {
TRACE; TRACE;
pthread_cond_init( &m_condVar, 0 ); pthread_cond_init( &m_condVar, 0 );

@ -4,13 +4,18 @@
#include <time.h> #include <time.h>
pthread_mutex_t& MutexCtor(pthread_mutex_t& mutex)
{
pthread_mutex_init( &mutex, 0 );
return mutex;
}
Mutex::Mutex(int kind) Mutex::Mutex(int kind)
: m_mutex(MutexCtor(m_mutex))
{ {
TRACE; TRACE;
if ( kind == PTHREAD_MUTEX_DEFAULT ) { if ( kind != PTHREAD_MUTEX_DEFAULT ) {
pthread_mutex_init( &m_mutex, 0 );
} else {
pthread_mutexattr_t attr; pthread_mutexattr_t attr;
pthread_mutexattr_init( &attr ); pthread_mutexattr_init( &attr );
pthread_mutexattr_settype( &attr, kind ); pthread_mutexattr_settype( &attr, kind );

@ -6,17 +6,25 @@
#include <time.h> // timer_t #include <time.h> // timer_t
#include <string.h> // strerror #include <string.h> // strerror
Timer::Timer(const int signal)
: m_signal(signal) struct sigaction& sigActionCtor(struct sigaction &sigAct, const int signal)
{
sigAct.sa_flags = SA_SIGINFO;
sigemptyset(&sigAct.sa_mask);
sigaddset( &sigAct.sa_mask, signal );
sigaction( signal, &sigAct, 0 );
return sigAct;
}
Timer::Timer(const Timer& timer)
: m_signal(timer.m_signal)
, m_sigAction(sigActionCtor( m_sigAction , m_signal ) )
, m_timerId(0)
, m_periodic(false) , m_periodic(false)
, m_running(true) , m_running(true)
{ {
TRACE; TRACE;
m_sigAction.sa_flags = SA_SIGINFO;
sigemptyset(&m_sigAction.sa_mask);
sigaddset( &m_sigAction.sa_mask, m_signal );
sigaction( m_signal, &m_sigAction, 0 );
} }
@ -80,9 +88,21 @@ void Timer::stopTimer()
m_running = false; m_running = false;
} }
void Timer::gracefulStop() void Timer::gracefulStop()
{ {
TRACE; TRACE;
m_running = false; m_running = false;
} }
Timer::Timer(const int signal)
: m_signal(signal)
, m_sigAction(sigActionCtor( m_sigAction , m_signal ) )
, m_timerId(0)
, m_periodic(false)
, m_running(true)
{
TRACE;
}

@ -34,7 +34,7 @@ private:
public: public:
void nemtestBasic( void ) void testBasic( void )
{ {
TEST_HEADER; TEST_HEADER;
TimerThread* tt = new TimerThread(); TimerThread* tt = new TimerThread();
@ -54,7 +54,7 @@ public:
delete user; delete user;
} }
void nemtestBasicTimeSpec( void ) void testBasicTimeSpec( void )
{ {
TEST_HEADER; TEST_HEADER;
TimerThread* tt = new TimerThread(); TimerThread* tt = new TimerThread();
@ -75,7 +75,7 @@ public:
delete user; delete user;
} }
void nemtestPeriodic( void ) void testPeriodic( void )
{ {
TEST_HEADER; TEST_HEADER;
TimerThread* tt = new TimerThread(); TimerThread* tt = new TimerThread();
@ -120,14 +120,15 @@ public:
tt->stop(); tt->stop();
sleep(1); sleep(1);
TS_ASSERT_EQUALS( user->m_counter, 100 + 4 ); // 4 times TS_ASSERT_EQUALS( user->m_counter, 4 ); // 4 times
TS_ASSERT_EQUALS( user2->m_counter, 100 + perMinute*4 ); // TS_ASSERT_EQUALS( user2->m_counter, perMinute*4 );
delete tt; delete tt;
delete user; delete user;
delete user2;
} }
void nemtestDestroyed( void ) void testDestroyed( void )
{ {
TEST_HEADER; TEST_HEADER;
TimerThread* tt = new TimerThread(); TimerThread* tt = new TimerThread();
@ -147,7 +148,7 @@ public:
delete user; delete user;
} }
void nemtestRemoved( void ) void testRemoved( void )
{ {
TEST_HEADER; TEST_HEADER;
TimerThread* tt = new TimerThread(); TimerThread* tt = new TimerThread();
@ -190,7 +191,7 @@ public:
delete user3; delete user3;
} }
void nemtestRemovedMultiple( void ) void testRemovedMultiple( void )
{ {
TEST_HEADER; TEST_HEADER;
TimerThread* tt = new TimerThread(); TimerThread* tt = new TimerThread();

Loading…
Cancel
Save