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.

148 lines
4.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "basicDualThread.h"
  2. #define LOG( X ) m_pLog->Log( "basicDualThread", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "basicDualThread", this, _LOG_ERROR, X )
  4. #define SLOG( X ) m_pLog->Log( "basicDualThreadS", mThis, X )
  5. #define SLOG_ERROR( X ) m_pLog->Log( "basicDualThreadS", mThis, _LOG_ERROR, X )
  6. bool basicDualThread::init( void ){
  7. //LOG("Init Object");
  8. if (m_bThreadAStatus == TH_PRE_INIT) {
  9. m_bThreadAStatus = TH_ARMED;
  10. #if defined( PTHREAD ) || defined(WIN_PTHREAD)
  11. int RetVal = pthread_create( &m_sThreadAHandler, NULL, m_fThreadAEntryPoint, ( void * ) this );
  12. if( RetVal == -1 ) {
  13. LOG_ERROR("Thread A Creation Faled.");
  14. return false;
  15. };
  16. #endif
  17. #ifdef BOOST_THREAD
  18. void *tPTR = this;
  19. m_hThreadAHandle = new boost::thread( &basicDualThread::m_fThreadAEntryPoint, this );
  20. if( m_hThreadAHandle == NULL ) {
  21. LOG_ERROR("Thread A Creation Faled.");
  22. return false;
  23. };
  24. #endif
  25. #ifdef WIN_THREAD
  26. m_hThreadAHandle = CreateThread(
  27. NULL,
  28. 0,
  29. (LPTHREAD_START_ROUTINE)m_fThreadAEntryPoint,
  30. (LPVOID)this,
  31. 0,
  32. &m_dwThreadAID );
  33. //TODO: Add thread creation check
  34. #endif
  35. } else {
  36. LOG_ERROR("Thread object A not ready to be created.");
  37. return false;
  38. };
  39. if (m_bThreadBStatus == TH_PRE_INIT) {
  40. m_bThreadBStatus = TH_ARMED;
  41. #if defined( PTHREAD ) || defined(WIN_PTHREAD)
  42. int RetVal = pthread_create( &m_sThreadBHandler, NULL, m_fThreadBEntryPoint, ( void * ) this );
  43. if( RetVal == -1 ) {
  44. LOG_ERROR("Thread B Creation Faled.");
  45. return false;
  46. };
  47. #endif
  48. #ifdef WIN_THREAD
  49. m_hThreadBHandle = CreateThread(
  50. NULL,
  51. 0,
  52. (LPTHREAD_START_ROUTINE)m_fThreadBEntryPoint,
  53. (LPVOID)this,
  54. 0,
  55. &m_dwThreadBID );
  56. //TODO: Add thread creation check
  57. #endif
  58. #ifdef BOOST_THREAD
  59. void *tPTR = this;
  60. m_hThreadBHandle = new boost::thread( &basicDualThread::m_fThreadBEntryPoint, this );
  61. if( m_hThreadBHandle == NULL ) {
  62. LOG_ERROR("Thread Creation Faled.");
  63. return false;
  64. };
  65. #endif
  66. } else {
  67. LOG_ERROR("Thread object B not ready to be created.");
  68. return false;
  69. };
  70. return true;
  71. };
  72. void basicDualThread::m_fThreadAProc( void ){
  73. m_bThreadAStatus = TH_WORKING;
  74. while( m_bThreadAStatus < TH_ENDED ) SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  75. };
  76. void basicDualThread::m_fThreadBProc( void ){
  77. m_bThreadBStatus = TH_WORKING;
  78. while( m_bThreadBStatus < TH_ENDED ) SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  79. };
  80. THREAD_EXECUT_PROC basicDualThread::m_fThreadAEntryPoint( THREAD_PARAM_TYPE ptr){
  81. basicDualThread *mThis;
  82. mThis = ( basicDualThread * ) ptr;
  83. if( mThis->m_bThreadAStatus == TH_ARMED ) {
  84. mThis->m_fThreadAProc();
  85. mThis->m_bThreadAStatus = TH_EXIT;
  86. };
  87. mThis->SLOG("ThreadAEntryPoint Exit.");
  88. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  89. pthread_exit( NULL );
  90. return NULL;
  91. #else
  92. return 0;
  93. #endif
  94. };
  95. THREAD_EXECUT_PROC basicDualThread::m_fThreadBEntryPoint( THREAD_PARAM_TYPE ptr){
  96. basicDualThread *mThis;
  97. mThis = ( basicDualThread * ) ptr;
  98. if( mThis->m_bThreadBStatus == TH_ARMED ) {
  99. mThis->m_fThreadBProc();
  100. mThis->m_bThreadBStatus = TH_EXIT;
  101. };
  102. mThis->SLOG("ThreadBEntryPoint Exit.");
  103. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  104. pthread_exit( NULL );
  105. return NULL;
  106. #else
  107. return 0;
  108. #endif
  109. };
  110. void basicDualThread::waitThreadAEnd( bool Force ){
  111. if (m_bThreadAStatus != TH_WORKING ) return;
  112. if( Force ) m_bThreadAStatus = TH_ENDED;
  113. LOG("Thread A Requested Exit.");
  114. while( m_bThreadAStatus != TH_EXIT ) SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  115. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  116. pthread_join(m_sThreadAHandler, NULL);
  117. #else
  118. WaitForSingleObject(m_hThreadAHandle, 2000);
  119. CloseHandle(m_hThreadAHandle);
  120. #endif
  121. LOG("Thread A Ended.");
  122. };
  123. void basicDualThread::waitThreadBEnd( bool Force ){
  124. if (m_bThreadBStatus != TH_WORKING ) return;
  125. if( Force ) m_bThreadBStatus = TH_ENDED;
  126. LOG("Thread B Requested Exit.");
  127. while( m_bThreadBStatus != TH_EXIT ) SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  128. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  129. pthread_join(m_sThreadBHandler, NULL);
  130. #else
  131. WaitForSingleObject(m_hThreadBHandle, 2000);
  132. CloseHandle(m_hThreadBHandle);
  133. #endif
  134. LOG("Thread B Ended.");
  135. };
  136. #undef LOG
  137. #undef LOG_ERROR
  138. #undef SLOG
  139. #undef SLOG_ERROR