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.

151 lines
4.4 KiB

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