#include "basicThread.h" #ifndef BTH_NO_LOGGING #define LOG(X) m_pLog->Log("BasicThread", this, X) #define LOG_ERROR(X) m_pLog->Log("BasicThread", this, _LOG_ERROR, X) // Thread Logging #define SLOG(X) mThis->m_pLog->Log("BasicThreadS", mThis, X) #define SLOG_ERROR(X) mThis->m_pLog->Log("BasicThreadS", mThis, _LOG_ERROR, X) #endif bool basicThread::init(void) { #ifndef BTH_NO_LOGGING #ifdef DEBUG_EXTRA LOG("Init Single Thread"); #endif #endif if (m_bThreadStatus == TH_PRE_INIT) { m_bThreadStatus = TH_ARMED; #if defined(__linux__) || defined(WIN_PTHREAD) int RetVal = pthread_create(&m_sThreadHandler, NULL, m_fThreadEntryPoint, (void *) this); if (RetVal != 0) // pthread_create returns an error number, not -1 { #ifndef BTH_NO_LOGGING LOG_ERROR("Thread Creation Failed."); #endif m_bThreadStatus = TH_PRE_INIT; return false; }; m_bThreadJoinable = true; #else m_hThreadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) m_fThreadEntryPoint, (LPVOID) this, 0, &m_dwThreadID); // TODO: Add thread creation check m_bThreadJoinable = true; #endif } else { #ifndef BTH_NO_LOGGING LOG_ERROR("Thread object not ready to be created."); #endif return false; }; return true; } void basicThread::m_fThreadProc(void) { m_bThreadStatus = TH_WORKING; while (m_bThreadStatus < TH_ENDED) { SLEEP(DEFAULT_THREAD_SLEEP_TIME); }; } #if defined(__linux__) || defined(WIN_PTHREAD) void * basicThread::m_fThreadEntryPoint(void * ptr) { #else DWORD WINAPI basicThread::m_fThreadEntryPoint(LPVOID ptr) { #endif basicThread * mThis; mThis = (basicThread *) ptr; if (mThis->m_bThreadStatus == TH_ARMED) { mThis->m_fThreadProc(); } else { #ifndef BTH_NO_LOGGING SLOG_ERROR("Thread is not ARMED!"); #endif }; mThis->m_bThreadStatus = TH_EXIT; #ifdef __linux__ pthread_exit(NULL); return NULL; #else return 0; #endif } void basicThread::waitThreadEnd(bool forced) { // Nothing to wait for if no thread was created, or it was already joined. // NOTE: a thread that ended by itself (TH_ENDED / TH_EXIT) must still be joined, // otherwise its stack and descriptor are never released. if (! m_bThreadJoinable || m_bThreadStatus < TH_ARMED) { return; } if (forced && m_bThreadStatus < TH_ENDED) { m_bThreadStatus = TH_ENDED; } #if defined(__linux__) || defined(WIN_PTHREAD) if (pthread_equal(m_sThreadHandler, pthread_self())) { // Called from the thread itself: it cannot wait for / join itself. Detach so the // stack is released automatically when the thread procedure returns. pthread_detach(m_sThreadHandler); m_bThreadJoinable = false; return; } #endif while (m_bThreadStatus < TH_EXIT) { SLEEP(DEFAULT_THREAD_SLEEP_TIME); } #if defined(__linux__) || defined(WIN_PTHREAD) pthread_join(m_sThreadHandler, NULL); #else WaitForSingleObject(m_hThreadHandle, DEFAULT_THREAD_SLEEP_TIME); CloseHandle(m_hThreadHandle); #endif m_bThreadJoinable = false; } bool basicThread::uninit(void) { if (m_bThreadStatus == TH_PRE_INIT) { return true; } waitThreadEnd(true); #ifndef BTH_NO_LOGGING #ifdef DEBUG_EXTRA LOG("UNINIT done!"); #endif #endif m_bThreadStatus = TH_PRE_INIT; return true; } void basicThread::abort() { if (! m_bThreadJoinable) { return; } pthread_cancel(m_sThreadHandler); pthread_join(m_sThreadHandler, NULL); m_bThreadJoinable = false; m_bThreadStatus = TH_EXIT; } #ifndef BTH_NO_LOGGING #undef LOG #undef LOG_ERROR #undef SLOG #undef SLOG_ERROR #endif