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.
190 lines
4.1 KiB
190 lines
4.1 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)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Init Dual Thread");
|
|
#endif
|
|
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_EXTRA
|
|
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_EXTRA
|
|
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)
|
|
{
|
|
return;
|
|
}
|
|
if (Force && m_bThreadAStatus < TH_EXIT)
|
|
{
|
|
m_bThreadAStatus = TH_ENDED;
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
void basicDualThread::waitThreadBEnd(bool Force)
|
|
{
|
|
if (m_bThreadBStatus > TH_WORKING || m_bThreadBStatus < TH_ARMED)
|
|
{
|
|
return;
|
|
}
|
|
if (Force && m_bThreadBStatus < TH_EXIT)
|
|
{
|
|
m_bThreadBStatus = TH_ENDED;
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
bool basicDualThread::uninit(void)
|
|
{
|
|
if (m_bThreadAStatus == TH_PRE_INIT && m_bThreadBStatus == TH_PRE_INIT)
|
|
{
|
|
return true;
|
|
}
|
|
waitThreadAEnd(true);
|
|
waitThreadBEnd(true);
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("UNINIT done!");
|
|
#endif
|
|
m_bThreadAStatus = m_bThreadBStatus = TH_PRE_INIT;
|
|
return true;
|
|
}
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|
|
#undef SLOG
|
|
#undef SLOG_ERROR
|