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.

189 lines
5.2 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
7 years ago
7 years ago
​
7 years ago
7 years ago
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
7 years ago
7 years ago
​
​
7 years ago
7 years ago
​
7 years ago
7 years ago
​
​
​
​
​
​
​
7 years ago
7 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. //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. #ifdef DEBUG
  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 ) {
  91. mThis->m_fThreadBProc();
  92. mThis->m_bThreadBStatus = TH_EXIT;
  93. };
  94. #ifdef DEBUG
  95. mThis->SLOG("ThreadBEntryPoint Exit.");
  96. #endif
  97. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  98. pthread_exit( NULL );
  99. return NULL;
  100. #else
  101. return 0;
  102. #endif
  103. };
  104. void basicDualThread::waitThreadAEnd( bool Force ){
  105. if( m_bThreadAStatus > TH_WORKING || m_bThreadAStatus < TH_ARMED ) {
  106. #ifdef DEBUG
  107. LOG("Thread A already Dead!");
  108. #endif
  109. return;
  110. };
  111. if( Force ){
  112. #ifdef DEBUG
  113. LOG("Thread A Requested Exit.");
  114. #endif
  115. m_bThreadAStatus = TH_ENDED;
  116. } else {
  117. #ifdef DEBUG
  118. LOG("Thread A Wait Exit.");
  119. #endif
  120. };
  121. while( m_bThreadAStatus != TH_EXIT ) SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  122. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  123. pthread_join(m_sThreadAHandler, NULL);
  124. #else
  125. WaitForSingleObject(m_hThreadAHandle, 2000);
  126. CloseHandle(m_hThreadAHandle);
  127. #endif
  128. #ifdef DEBUG
  129. LOG("Thread A Ended.");
  130. #endif
  131. };
  132. void basicDualThread::waitThreadBEnd( bool Force ){
  133. if( m_bThreadBStatus > TH_WORKING || m_bThreadBStatus < TH_ARMED ){
  134. #ifdef DEBUG
  135. LOG("Thread B already Dead!");
  136. #endif
  137. return;
  138. };
  139. if( Force ){
  140. #ifdef DEBUG
  141. LOG("Thread B Requested Exit.");
  142. #endif
  143. m_bThreadBStatus = TH_ENDED;
  144. } else {
  145. #ifdef DEBUG
  146. LOG("Thread B Wait Exit.");
  147. #endif
  148. };
  149. while( m_bThreadBStatus != TH_EXIT ) SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  150. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  151. pthread_join(m_sThreadBHandler, NULL);
  152. #else
  153. WaitForSingleObject(m_hThreadBHandle, 2000);
  154. CloseHandle(m_hThreadBHandle);
  155. #endif
  156. #ifdef DEBUG
  157. LOG("Thread B Ended.");
  158. #endif
  159. };
  160. bool basicDualThread::uninit( void ){
  161. if( m_bThreadAStatus == TH_PRE_INIT && m_bThreadBStatus == TH_PRE_INIT ) return true;
  162. while( m_bThreadAStatus < TH_WORKING || m_bThreadBStatus < TH_WORKING ) SLEEP(1);
  163. m_bThreadBStatus = m_bThreadAStatus = TH_ENDED;
  164. while( m_bThreadAStatus < TH_EXIT || m_bThreadBStatus < TH_EXIT ) SLEEP(1);
  165. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  166. pthread_join(m_sThreadAHandler, NULL);
  167. pthread_join(m_sThreadBHandler, NULL);
  168. #else
  169. WaitForSingleObject(m_hThreadAHandle, 2000);
  170. WaitForSingleObject(m_hThreadBHandle, 2000);
  171. CloseHandle(m_hThreadAHandle);
  172. CloseHandle(m_hThreadBHandle);
  173. #endif
  174. return true;
  175. };
  176. #undef LOG
  177. #undef LOG_ERROR
  178. #undef SLOG
  179. #undef SLOG_ERROR