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.

134 lines
2.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "basicThread.h"
  2. #ifndef BTH_NO_LOGGING
  3. #define LOG(X) m_pLog->Log("BasicThread", this, X)
  4. #define LOG_ERROR(X) m_pLog->Log("BasicThread", this, _LOG_ERROR, X)
  5. // Thread Logging
  6. #define SLOG(X) mThis->m_pLog->Log("BasicThreadS", mThis, X)
  7. #define SLOG_ERROR(X) mThis->m_pLog->Log("BasicThreadS", mThis, _LOG_ERROR, X)
  8. #endif
  9. bool basicThread::init(void)
  10. {
  11. #ifndef BTH_NO_LOGGING
  12. #ifdef DEBUG_EXTRA
  13. LOG("Init Single Thread");
  14. #endif
  15. #endif
  16. if (m_bThreadStatus == TH_PRE_INIT)
  17. {
  18. m_bThreadStatus = TH_ARMED;
  19. #if defined(__linux__) || defined(WIN_PTHREAD)
  20. int RetVal = pthread_create(&m_sThreadHandler, NULL, m_fThreadEntryPoint, (void *) this);
  21. if (RetVal == -1)
  22. {
  23. #ifndef BTH_NO_LOGGING
  24. LOG_ERROR("Thread Creation Failed.");
  25. #endif
  26. return false;
  27. };
  28. #else
  29. m_hThreadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) m_fThreadEntryPoint, (LPVOID) this, 0, &m_dwThreadID);
  30. // TODO: Add thread creation check
  31. #endif
  32. }
  33. else
  34. {
  35. #ifndef BTH_NO_LOGGING
  36. LOG_ERROR("Thread object not ready to be created.");
  37. #endif
  38. return false;
  39. };
  40. return true;
  41. }
  42. void basicThread::m_fThreadProc(void)
  43. {
  44. m_bThreadStatus = TH_WORKING;
  45. while (m_bThreadStatus < TH_ENDED)
  46. {
  47. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  48. };
  49. }
  50. #if defined(__linux__) || defined(WIN_PTHREAD)
  51. void * basicThread::m_fThreadEntryPoint(void * ptr)
  52. {
  53. #else
  54. DWORD WINAPI basicThread::m_fThreadEntryPoint(LPVOID ptr)
  55. {
  56. #endif
  57. basicThread * mThis;
  58. mThis = (basicThread *) ptr;
  59. if (mThis->m_bThreadStatus == TH_ARMED)
  60. {
  61. mThis->m_fThreadProc();
  62. }
  63. else
  64. {
  65. #ifndef BTH_NO_LOGGING
  66. SLOG_ERROR("Thread is not ARMED!");
  67. #endif
  68. };
  69. mThis->m_bThreadStatus = TH_EXIT;
  70. #ifdef __linux__
  71. pthread_exit(NULL);
  72. return NULL;
  73. #else
  74. return 0;
  75. #endif
  76. }
  77. void basicThread::waitThreadEnd(bool forced)
  78. {
  79. if (m_bThreadStatus > TH_WORKING || m_bThreadStatus < TH_ARMED)
  80. {
  81. return;
  82. }
  83. if (forced && m_bThreadStatus < TH_EXIT)
  84. {
  85. m_bThreadStatus = TH_ENDED;
  86. }
  87. while (m_bThreadStatus < TH_EXIT)
  88. {
  89. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  90. }
  91. #if defined(__linux__) || defined(WIN_PTHREAD)
  92. pthread_join(m_sThreadHandler, NULL);
  93. #else
  94. WaitForSingleObject(m_hThreadHandle, DEFAULT_THREAD_SLEEP_TIME);
  95. CloseHandle(m_hThreadHandle);
  96. #endif
  97. }
  98. bool basicThread::uninit(void)
  99. {
  100. if (m_bThreadStatus == TH_PRE_INIT)
  101. {
  102. return true;
  103. }
  104. waitThreadEnd(true);
  105. #ifndef BTH_NO_LOGGING
  106. #ifdef DEBUG_EXTRA
  107. LOG("UNINIT done!");
  108. #endif
  109. #endif
  110. m_bThreadStatus = TH_PRE_INIT;
  111. return true;
  112. }
  113. void basicThread::abort()
  114. {
  115. pthread_cancel(m_sThreadHandler);
  116. pthread_join(m_sThreadHandler, NULL);
  117. }
  118. #ifndef BTH_NO_LOGGING
  119. #undef LOG
  120. #undef LOG_ERROR
  121. #undef SLOG
  122. #undef SLOG_ERROR
  123. #endif