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.

99 lines
2.5 KiB

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