- now the mess with threads has gone away
- we have a class BaseThread -- this is a base class -- we can inherit from it when creating a new thread - others treads are correctly stopped (when signal comes) -- pthread_join - we have a special thread only for signals git-svn-id: svn://ttmath.org/publicrep/winix/trunk@685 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
208
core/basethread.cpp
Executable file
208
core/basethread.cpp
Executable file
@@ -0,0 +1,208 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2010, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include "basethread.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
BaseThread::BaseThread()
|
||||
{
|
||||
synchro = 0;
|
||||
thread_signal = 0;
|
||||
thread_id = 0;
|
||||
}
|
||||
|
||||
|
||||
void BaseThread::SetSynchro(Synchro * psynchro)
|
||||
{
|
||||
synchro = psynchro;
|
||||
}
|
||||
|
||||
void BaseThread::SetSignal(pthread_cond_t * psignal)
|
||||
{
|
||||
thread_signal = psignal;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool BaseThread::Lock()
|
||||
{
|
||||
return synchro->Lock();
|
||||
}
|
||||
|
||||
|
||||
void BaseThread::Unlock()
|
||||
{
|
||||
synchro->Unlock();
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool BaseThread::IsExitSignal()
|
||||
{
|
||||
bool res = true;
|
||||
|
||||
if( Lock() )
|
||||
{
|
||||
res = synchro->was_stop_signal;
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
bool BaseThread::BaseInit()
|
||||
{
|
||||
bool init_status = false;
|
||||
|
||||
if( Lock() )
|
||||
{
|
||||
init_status = Init(); // your virtual method
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return init_status;
|
||||
}
|
||||
|
||||
|
||||
void BaseThread::BaseUninit()
|
||||
{
|
||||
if( Lock() )
|
||||
{
|
||||
Uninit(); // your virtual method
|
||||
Unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool BaseThread::BaseSignalReceived()
|
||||
{
|
||||
bool make_do;
|
||||
|
||||
try
|
||||
{
|
||||
make_do = SignalReceived(); // your short-time virtual method (objects are locked)
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
}
|
||||
|
||||
return make_do;
|
||||
}
|
||||
|
||||
|
||||
// this is called only if your SignalReceived() returned true
|
||||
void BaseThread::BaseDo()
|
||||
{
|
||||
try
|
||||
{
|
||||
Do(); // your long-time virtual method (objects are *not* locked)
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// use it with Lock and Unlock
|
||||
bool BaseThread::WaitForSignal()
|
||||
{
|
||||
return pthread_cond_wait(thread_signal, &synchro->mutex) == 0;
|
||||
}
|
||||
|
||||
|
||||
// use it with Lock and Unlock
|
||||
// it breaks only if there was a stop signal a the time has expired
|
||||
bool BaseThread::WaitForSignalSleep(time_t second)
|
||||
{
|
||||
timespec t;
|
||||
int res;
|
||||
|
||||
t.tv_sec = time(0) + second;
|
||||
t.tv_nsec = 0;
|
||||
|
||||
do
|
||||
{
|
||||
res = pthread_cond_timedwait(thread_signal, &synchro->mutex, &t);
|
||||
}
|
||||
while( res == 0 && !synchro->was_stop_signal );
|
||||
// above condition means there was a signal
|
||||
// but it was not a stop signal so we should still wait
|
||||
|
||||
return res == 0 || res == ETIMEDOUT;
|
||||
}
|
||||
|
||||
|
||||
void BaseThread::WaitForThread()
|
||||
{
|
||||
pthread_join(thread_id, 0);
|
||||
}
|
||||
|
||||
|
||||
void BaseThread::StartRoutine()
|
||||
{
|
||||
bool make_do;
|
||||
|
||||
do
|
||||
{
|
||||
if( Lock() )
|
||||
{
|
||||
make_do = false;
|
||||
|
||||
if( !synchro->was_stop_signal && WaitForSignal() ) // automatically unlock, wait and lock again when signal comes
|
||||
if( !synchro->was_stop_signal )
|
||||
make_do = BaseSignalReceived(); // your short-time virtual method will be called (objects locked)
|
||||
|
||||
Unlock(); // unlocking from WaitForSignal()
|
||||
|
||||
if( make_do )
|
||||
BaseDo(); // your long-time virtual method will be called (objects *not* locked)
|
||||
}
|
||||
}
|
||||
while( !IsExitSignal() );
|
||||
}
|
||||
|
||||
|
||||
void * BaseThread::StartRoutine(void * this_object)
|
||||
{
|
||||
BaseThread * base = reinterpret_cast<BaseThread*>(this_object);
|
||||
|
||||
if( base->synchro && base->thread_signal )
|
||||
{
|
||||
if( base->BaseInit() )
|
||||
{
|
||||
base->StartRoutine();
|
||||
base->BaseUninit();
|
||||
}
|
||||
}
|
||||
|
||||
pthread_exit(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool BaseThread::StartThread()
|
||||
{
|
||||
pthread_attr_t attr;
|
||||
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
||||
|
||||
int res = pthread_create(&thread_id, &attr, StartRoutine, this);
|
||||
|
||||
pthread_attr_destroy(&attr);
|
||||
|
||||
return res == 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user