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.

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