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.

112 lines
2.9 KiB

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