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.

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