|
|
@ -15,14 +15,17 @@ bool basicDualThread::init(void) |
|
|
m_bThreadAStatus = TH_ARMED; |
|
|
m_bThreadAStatus = TH_ARMED; |
|
|
#if defined(__linux__) || defined(WIN_PTHREAD)
|
|
|
#if defined(__linux__) || defined(WIN_PTHREAD)
|
|
|
int RetVal = pthread_create(&m_sThreadAHandler, NULL, m_fThreadAEntryPoint, (void *) this); |
|
|
int RetVal = pthread_create(&m_sThreadAHandler, NULL, m_fThreadAEntryPoint, (void *) this); |
|
|
if (RetVal == -1) |
|
|
|
|
|
|
|
|
if (RetVal != 0) // pthread_create returns an error number, not -1
|
|
|
{ |
|
|
{ |
|
|
LOG_ERROR("Thread A Creation Faled."); |
|
|
LOG_ERROR("Thread A Creation Faled."); |
|
|
|
|
|
m_bThreadAStatus = TH_PRE_INIT; |
|
|
return false; |
|
|
return false; |
|
|
}; |
|
|
}; |
|
|
|
|
|
m_bThreadAJoinable = true; |
|
|
#else
|
|
|
#else
|
|
|
m_hThreadAHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) m_fThreadAEntryPoint, (LPVOID) this, 0, &m_dwThreadAID); |
|
|
m_hThreadAHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) m_fThreadAEntryPoint, (LPVOID) this, 0, &m_dwThreadAID); |
|
|
// TODO: Add thread creation check
|
|
|
// TODO: Add thread creation check
|
|
|
|
|
|
m_bThreadAJoinable = true; |
|
|
#endif
|
|
|
#endif
|
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
@ -35,14 +38,17 @@ bool basicDualThread::init(void) |
|
|
m_bThreadBStatus = TH_ARMED; |
|
|
m_bThreadBStatus = TH_ARMED; |
|
|
#if defined(__linux__) || defined(WIN_PTHREAD)
|
|
|
#if defined(__linux__) || defined(WIN_PTHREAD)
|
|
|
int RetVal = pthread_create(&m_sThreadBHandler, NULL, m_fThreadBEntryPoint, (void *) this); |
|
|
int RetVal = pthread_create(&m_sThreadBHandler, NULL, m_fThreadBEntryPoint, (void *) this); |
|
|
if (RetVal == -1) |
|
|
|
|
|
|
|
|
if (RetVal != 0) // pthread_create returns an error number, not -1
|
|
|
{ |
|
|
{ |
|
|
LOG_ERROR("Thread B Creation Faled."); |
|
|
LOG_ERROR("Thread B Creation Faled."); |
|
|
|
|
|
m_bThreadBStatus = TH_PRE_INIT; |
|
|
return false; |
|
|
return false; |
|
|
}; |
|
|
}; |
|
|
|
|
|
m_bThreadBJoinable = true; |
|
|
#else
|
|
|
#else
|
|
|
m_hThreadBHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) m_fThreadBEntryPoint, (LPVOID) this, 0, &m_dwThreadBID); |
|
|
m_hThreadBHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) m_fThreadBEntryPoint, (LPVOID) this, 0, &m_dwThreadBID); |
|
|
// TODO: Add thread creation check
|
|
|
// TODO: Add thread creation check
|
|
|
|
|
|
m_bThreadBJoinable = true; |
|
|
#endif
|
|
|
#endif
|
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
@ -123,15 +129,27 @@ DWORD WINAPI basicDualThread::m_fThreadBEntryPoint(LPVOID ptr) |
|
|
|
|
|
|
|
|
void basicDualThread::waitThreadAEnd(bool Force) |
|
|
void basicDualThread::waitThreadAEnd(bool Force) |
|
|
{ |
|
|
{ |
|
|
if (m_bThreadAStatus > TH_WORKING || m_bThreadAStatus < TH_ARMED) |
|
|
|
|
|
|
|
|
// A thread that ended by itself (TH_ENDED / TH_EXIT) must still be joined,
|
|
|
|
|
|
// otherwise its stack and descriptor are never released.
|
|
|
|
|
|
if (! m_bThreadAJoinable || m_bThreadAStatus < TH_ARMED) |
|
|
{ |
|
|
{ |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
if (Force && m_bThreadAStatus < TH_EXIT) |
|
|
|
|
|
|
|
|
if (Force && m_bThreadAStatus < TH_ENDED) |
|
|
{ |
|
|
{ |
|
|
m_bThreadAStatus = TH_ENDED; |
|
|
m_bThreadAStatus = TH_ENDED; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#if defined(__linux__) || defined(WIN_PTHREAD)
|
|
|
|
|
|
if (pthread_equal(m_sThreadAHandler, pthread_self())) |
|
|
|
|
|
{ |
|
|
|
|
|
// Called from the thread itself: it cannot wait for / join itself.
|
|
|
|
|
|
pthread_detach(m_sThreadAHandler); |
|
|
|
|
|
m_bThreadAJoinable = false; |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
while (m_bThreadAStatus < TH_EXIT) |
|
|
while (m_bThreadAStatus < TH_EXIT) |
|
|
{ |
|
|
{ |
|
|
SLEEP(DEFAULT_THREAD_SLEEP_TIME); |
|
|
SLEEP(DEFAULT_THREAD_SLEEP_TIME); |
|
|
@ -139,23 +157,37 @@ void basicDualThread::waitThreadAEnd(bool Force) |
|
|
|
|
|
|
|
|
#if defined(__linux__) || defined(WIN_PTHREAD)
|
|
|
#if defined(__linux__) || defined(WIN_PTHREAD)
|
|
|
pthread_join(m_sThreadAHandler, NULL); |
|
|
pthread_join(m_sThreadAHandler, NULL); |
|
|
|
|
|
m_bThreadAJoinable = false; |
|
|
#else
|
|
|
#else
|
|
|
WaitForSingleObject(m_hThreadAHandle, 2000); |
|
|
WaitForSingleObject(m_hThreadAHandle, 2000); |
|
|
CloseHandle(m_hThreadAHandle); |
|
|
CloseHandle(m_hThreadAHandle); |
|
|
|
|
|
m_bThreadAJoinable = false; |
|
|
#endif
|
|
|
#endif
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void basicDualThread::waitThreadBEnd(bool Force) |
|
|
void basicDualThread::waitThreadBEnd(bool Force) |
|
|
{ |
|
|
{ |
|
|
if (m_bThreadBStatus > TH_WORKING || m_bThreadBStatus < TH_ARMED) |
|
|
|
|
|
|
|
|
// A thread that ended by itself (TH_ENDED / TH_EXIT) must still be joined,
|
|
|
|
|
|
// otherwise its stack and descriptor are never released.
|
|
|
|
|
|
if (! m_bThreadBJoinable || m_bThreadBStatus < TH_ARMED) |
|
|
{ |
|
|
{ |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
if (Force && m_bThreadBStatus < TH_EXIT) |
|
|
|
|
|
|
|
|
if (Force && m_bThreadBStatus < TH_ENDED) |
|
|
{ |
|
|
{ |
|
|
m_bThreadBStatus = TH_ENDED; |
|
|
m_bThreadBStatus = TH_ENDED; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#if defined(__linux__) || defined(WIN_PTHREAD)
|
|
|
|
|
|
if (pthread_equal(m_sThreadBHandler, pthread_self())) |
|
|
|
|
|
{ |
|
|
|
|
|
// Called from the thread itself: it cannot wait for / join itself.
|
|
|
|
|
|
pthread_detach(m_sThreadBHandler); |
|
|
|
|
|
m_bThreadBJoinable = false; |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
while (m_bThreadBStatus < TH_EXIT) |
|
|
while (m_bThreadBStatus < TH_EXIT) |
|
|
{ |
|
|
{ |
|
|
SLEEP(DEFAULT_THREAD_SLEEP_TIME); |
|
|
SLEEP(DEFAULT_THREAD_SLEEP_TIME); |
|
|
@ -163,9 +195,11 @@ void basicDualThread::waitThreadBEnd(bool Force) |
|
|
|
|
|
|
|
|
#if defined(__linux__) || defined(WIN_PTHREAD)
|
|
|
#if defined(__linux__) || defined(WIN_PTHREAD)
|
|
|
pthread_join(m_sThreadBHandler, NULL); |
|
|
pthread_join(m_sThreadBHandler, NULL); |
|
|
|
|
|
m_bThreadBJoinable = false; |
|
|
#else
|
|
|
#else
|
|
|
WaitForSingleObject(m_hThreadBHandle, 2000); |
|
|
WaitForSingleObject(m_hThreadBHandle, 2000); |
|
|
CloseHandle(m_hThreadBHandle); |
|
|
CloseHandle(m_hThreadBHandle); |
|
|
|
|
|
m_bThreadBJoinable = false; |
|
|
#endif
|
|
|
#endif
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|