#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( __linux__ ) || 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; }; #else 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( __linux__ ) || 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; }; #else m_hThreadBHandle = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)m_fThreadBEntryPoint, (LPVOID)this, 0, &m_dwThreadBID ); //TODO: Add thread creation check #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 ); }; #if defined( __linux__ ) || defined(WIN_PTHREAD) void *basicDualThread::m_fThreadAEntryPoint( void *ptr ){ #else DWORD WINAPI basicDualThread::m_fThreadAEntryPoint( LPVOID ptr){ #endif basicDualThread *mThis; mThis = ( basicDualThread * ) ptr; if( mThis->m_bThreadAStatus == TH_ARMED ) { mThis->m_fThreadAProc(); mThis->m_bThreadAStatus = TH_EXIT; }; #ifdef DEBUG mThis->SLOG("ThreadAEntryPoint Exit."); #endif #if defined( __linux__ ) || defined(WIN_PTHREAD) pthread_exit( NULL ); return NULL; #else return 0; #endif }; #if defined( __linux__ ) || defined(WIN_PTHREAD) void *basicDualThread::m_fThreadBEntryPoint( void *ptr ){ #else DWORD WINAPI basicDualThread::m_fThreadBEntryPoint( LPVOID ptr){ #endif basicDualThread *mThis; mThis = ( basicDualThread * ) ptr; if( mThis->m_bThreadBStatus == TH_ARMED ) { mThis->m_fThreadBProc(); mThis->m_bThreadBStatus = TH_EXIT; }; #ifdef DEBUG mThis->SLOG("ThreadBEntryPoint Exit."); #endif #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 || m_bThreadAStatus < TH_ARMED ) { #ifdef DEBUG LOG("Thread A already Dead!"); #endif return; }; if( Force ){ #ifdef DEBUG LOG("Thread A Requested Exit."); #endif m_bThreadAStatus = TH_ENDED; } else { #ifdef DEBUG LOG("Thread A Wait Exit."); #endif }; 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 #ifdef DEBUG LOG("Thread A Ended."); #endif }; void basicDualThread::waitThreadBEnd( bool Force ){ if( m_bThreadBStatus > TH_WORKING || m_bThreadBStatus < TH_ARMED ){ #ifdef DEBUG LOG("Thread B already Dead!"); #endif return; }; if( Force ){ #ifdef DEBUG LOG("Thread B Requested Exit."); #endif m_bThreadBStatus = TH_ENDED; } else { #ifdef DEBUG LOG("Thread B Wait Exit."); #endif }; 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 #ifdef DEBUG LOG("Thread B Ended."); #endif }; bool basicDualThread::uninit( void ){ if( m_bThreadAStatus == TH_PRE_INIT && m_bThreadBStatus == TH_PRE_INIT ) return true; while( m_bThreadAStatus < TH_WORKING || m_bThreadBStatus < TH_WORKING ) SLEEP(1); m_bThreadBStatus = m_bThreadAStatus = TH_ENDED; while( m_bThreadAStatus < TH_EXIT || m_bThreadBStatus < TH_EXIT ) SLEEP(1); #if defined( __linux__ ) || defined(WIN_PTHREAD) pthread_join(m_sThreadAHandler, NULL); pthread_join(m_sThreadBHandler, NULL); #else WaitForSingleObject(m_hThreadAHandle, 2000); WaitForSingleObject(m_hThreadBHandle, 2000); CloseHandle(m_hThreadAHandle); CloseHandle(m_hThreadBHandle); #endif return true; }; #undef LOG #undef LOG_ERROR #undef SLOG #undef SLOG_ERROR