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.

101 lines
2.6 KiB

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