You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

148 lines
4.4 KiB

#include "basicDualThread.h"
#define LOG( X ) m_pLog->Log( "basicDualThread", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "basicDualThread", this, _LOG_ERROR, X )
#define SLOG( X ) m_pLog->Log( "basicDualThreadS", mThis, X )
#define SLOG_ERROR( X ) m_pLog->Log( "basicDualThreadS", mThis, _LOG_ERROR, X )
bool basicDualThread::init( void ){
//LOG("Init Object");
if (m_bThreadAStatus == TH_PRE_INIT) {
m_bThreadAStatus = TH_ARMED;
#if defined( PTHREAD ) || defined(WIN_PTHREAD)
int RetVal = pthread_create( &m_sThreadAHandler, NULL, m_fThreadAEntryPoint, ( void * ) this );
if( RetVal == -1 ) {
LOG_ERROR("Thread A Creation Faled.");
return false;
};
#endif
#ifdef BOOST_THREAD
void *tPTR = this;
m_hThreadAHandle = new boost::thread( &basicDualThread::m_fThreadAEntryPoint, this );
if( m_hThreadAHandle == NULL ) {
LOG_ERROR("Thread A Creation Faled.");
return false;
};
#endif
#ifdef WIN_THREAD
m_hThreadAHandle = CreateThread(
NULL,
0,
(LPTHREAD_START_ROUTINE)m_fThreadAEntryPoint,
(LPVOID)this,
0,
&m_dwThreadAID );
//TODO: Add thread creation check
#endif
} else {
LOG_ERROR("Thread object A not ready to be created.");
return false;
};
if (m_bThreadBStatus == TH_PRE_INIT) {
m_bThreadBStatus = TH_ARMED;
#if defined( PTHREAD ) || defined(WIN_PTHREAD)
int RetVal = pthread_create( &m_sThreadBHandler, NULL, m_fThreadBEntryPoint, ( void * ) this );
if( RetVal == -1 ) {
LOG_ERROR("Thread B Creation Faled.");
return false;
};
#endif
#ifdef WIN_THREAD
m_hThreadBHandle = CreateThread(
NULL,
0,
(LPTHREAD_START_ROUTINE)m_fThreadBEntryPoint,
(LPVOID)this,
0,
&m_dwThreadBID );
//TODO: Add thread creation check
#endif
#ifdef BOOST_THREAD
void *tPTR = this;
m_hThreadBHandle = new boost::thread( &basicDualThread::m_fThreadBEntryPoint, this );
if( m_hThreadBHandle == NULL ) {
LOG_ERROR("Thread Creation Faled.");
return false;
};
#endif
} else {
LOG_ERROR("Thread object B not ready to be created.");
return false;
};
return true;
};
void basicDualThread::m_fThreadAProc( void ){
m_bThreadAStatus = TH_WORKING;
while( m_bThreadAStatus < TH_ENDED ) SLEEP( DEFAULT_THREAD_SLEEP_TIME );
};
void basicDualThread::m_fThreadBProc( void ){
m_bThreadBStatus = TH_WORKING;
while( m_bThreadBStatus < TH_ENDED ) SLEEP( DEFAULT_THREAD_SLEEP_TIME );
};
THREAD_EXECUT_PROC basicDualThread::m_fThreadAEntryPoint( THREAD_PARAM_TYPE ptr){
basicDualThread *mThis;
mThis = ( basicDualThread * ) ptr;
if( mThis->m_bThreadAStatus == TH_ARMED ) {
mThis->m_fThreadAProc();
mThis->m_bThreadAStatus = TH_EXIT;
};
mThis->SLOG("ThreadAEntryPoint Exit.");
#if defined( __linux__ ) || defined(WIN_PTHREAD)
pthread_exit( NULL );
return NULL;
#else
return 0;
#endif
};
THREAD_EXECUT_PROC basicDualThread::m_fThreadBEntryPoint( THREAD_PARAM_TYPE ptr){
basicDualThread *mThis;
mThis = ( basicDualThread * ) ptr;
if( mThis->m_bThreadBStatus == TH_ARMED ) {
mThis->m_fThreadBProc();
mThis->m_bThreadBStatus = TH_EXIT;
};
mThis->SLOG("ThreadBEntryPoint Exit.");
#if defined( __linux__ ) || defined(WIN_PTHREAD)
pthread_exit( NULL );
return NULL;
#else
return 0;
#endif
};
void basicDualThread::waitThreadAEnd( bool Force ){
if (m_bThreadAStatus != TH_WORKING ) return;
if( Force ) m_bThreadAStatus = TH_ENDED;
LOG("Thread A Requested Exit.");
while( m_bThreadAStatus != TH_EXIT ) SLEEP(DEFAULT_THREAD_SLEEP_TIME);
#if defined( __linux__ ) || defined(WIN_PTHREAD)
pthread_join(m_sThreadAHandler, NULL);
#else
WaitForSingleObject(m_hThreadAHandle, 2000);
CloseHandle(m_hThreadAHandle);
#endif
LOG("Thread A Ended.");
};
void basicDualThread::waitThreadBEnd( bool Force ){
if (m_bThreadBStatus != TH_WORKING ) return;
if( Force ) m_bThreadBStatus = TH_ENDED;
LOG("Thread B Requested Exit.");
while( m_bThreadBStatus != TH_EXIT ) SLEEP(DEFAULT_THREAD_SLEEP_TIME);
#if defined( __linux__ ) || defined(WIN_PTHREAD)
pthread_join(m_sThreadBHandler, NULL);
#else
WaitForSingleObject(m_hThreadBHandle, 2000);
CloseHandle(m_hThreadBHandle);
#endif
LOG("Thread B Ended.");
};
#undef LOG
#undef LOG_ERROR
#undef SLOG
#undef SLOG_ERROR