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.

97 lines
2.7 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
​
​
​
​
  1. #include "basicThread.h"
  2. #define LOG( X ) m_pLog->Log( "BasicThread", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "BasicThread", this, _LOG_ERROR, X )
  4. //Thread Logging
  5. #define SLOG( X ) mThis->m_pLog->Log( "BasicThreadS", mThis, X )
  6. #define SLOG_ERROR( X ) mThis->m_pLog->Log( "BasicThreadS", mThis, _LOG_ERROR, X )
  7. bool basicThread::init( void ){
  8. if (m_bThreadStatus == TH_PRE_INIT) {
  9. m_bThreadStatus = TH_ARMED;
  10. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  11. int RetVal = pthread_create( &m_sThreadHandler, NULL, m_fThreadEntryPoint, ( void * ) this );
  12. if( RetVal == -1 ) {
  13. LOG_ERROR("Thread Creation Failed.");
  14. return false;
  15. };
  16. #else
  17. m_hThreadHandle = CreateThread(
  18. NULL,
  19. 0,
  20. (LPTHREAD_START_ROUTINE)m_fThreadEntryPoint,
  21. (LPVOID)this,
  22. 0,
  23. &m_dwThreadID );
  24. //TODO: Add thread creation check
  25. #endif
  26. } else {
  27. LOG_ERROR("Thread object not ready to be created.");
  28. return false;
  29. };
  30. return true;
  31. };
  32. void basicThread::m_fThreadProc( void ){
  33. m_bThreadStatus = TH_WORKING;
  34. while( m_bThreadStatus < TH_ENDED ) {
  35. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  36. };
  37. };
  38. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  39. void *basicThread::m_fThreadEntryPoint( void *ptr ){
  40. #else
  41. DWORD WINAPI basicThread::m_fThreadEntryPoint( LPVOID ptr){
  42. #endif
  43. basicThread *mThis;
  44. mThis = ( basicThread * ) ptr;
  45. if( mThis->m_bThreadStatus == TH_ARMED ) {
  46. SLOG("Proc Start!");
  47. mThis->m_fThreadProc();
  48. SLOG("Proc Exited!");
  49. } else {
  50. SLOG_ERROR("Thread is not ARMED!");
  51. };
  52. mThis->m_bThreadStatus = TH_EXIT;
  53. #ifdef __linux__
  54. pthread_exit( NULL );
  55. return NULL;
  56. #else
  57. return 0;
  58. #endif
  59. };
  60. void basicThread::waitThreadEnd(bool forced){
  61. LOG("ENDING");
  62. if( m_bThreadStatus > TH_WORKING || m_bThreadStatus < TH_ARMED ) return;
  63. LOG("WAITING");
  64. if( forced ) m_bThreadStatus = TH_ENDED;
  65. while (m_bThreadStatus != TH_EXIT) SLEEP(1);//DEFAULT_THREAD_SLEEP_TIME);
  66. LOG("DONE -> RIPING");
  67. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  68. pthread_join(m_sThreadHandler, NULL);
  69. #else
  70. WaitForSingleObject(m_hThreadHandle, 2000);
  71. CloseHandle(m_hThreadHandle);
  72. #endif
  73. LOG("FINISH");
  74. };
  75. bool basicThread::uninit( void ){
  76. if( m_bThreadStatus == TH_PRE_INIT ) return true;
  77. while( m_bThreadStatus < TH_WORKING ) SLEEP(1);
  78. m_bThreadStatus = TH_ENDED;
  79. while( m_bThreadStatus < TH_EXIT ) SLEEP(1);
  80. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  81. pthread_join(m_sThreadHandler, NULL);
  82. #else
  83. WaitForSingleObject(m_hThreadHandle, 2000);
  84. CloseHandle(m_hThreadHandle);
  85. #endif
  86. return true;
  87. };
  88. #undef LOG
  89. #undef LOG_ERROR
  90. #undef SLOG
  91. #undef SLOG_ERROR