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.

196 lines
5.4 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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 ) {
  72. mThis->m_fThreadAProc();
  73. mThis->m_bThreadAStatus = TH_EXIT;
  74. };
  75. #ifdef DEBUG_EXTRA
  76. mThis->SLOG("ThreadAEntryPoint Exit.");
  77. #endif
  78. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  79. pthread_exit( NULL );
  80. return NULL;
  81. #else
  82. return 0;
  83. #endif
  84. };
  85. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  86. void *basicDualThread::m_fThreadBEntryPoint( void *ptr ){
  87. #else
  88. DWORD WINAPI basicDualThread::m_fThreadBEntryPoint( LPVOID ptr){
  89. #endif
  90. basicDualThread *mThis;
  91. mThis = ( basicDualThread * ) ptr;
  92. if( mThis->m_bThreadBStatus == TH_ARMED ) {
  93. mThis->m_fThreadBProc();
  94. mThis->m_bThreadBStatus = TH_EXIT;
  95. };
  96. #ifdef DEBUG_EXTRA
  97. mThis->SLOG("ThreadBEntryPoint Exit.");
  98. #endif
  99. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  100. pthread_exit( NULL );
  101. return NULL;
  102. #else
  103. return 0;
  104. #endif
  105. };
  106. void basicDualThread::waitThreadAEnd( bool Force ){
  107. if( m_bThreadAStatus > TH_WORKING || m_bThreadAStatus < TH_ARMED ) {
  108. #ifdef DEBUG_EXTRA
  109. LOG("Thread A already Dead!");
  110. #endif
  111. return;
  112. };
  113. if( Force ){
  114. #ifdef DEBUG_EXTRA
  115. LOG("Thread A Requested Exit.");
  116. #endif
  117. m_bThreadAStatus = TH_ENDED;
  118. } else {
  119. #ifdef DEBUG_EXTRA
  120. LOG("Thread A Wait Exit.");
  121. #endif
  122. };
  123. while( m_bThreadAStatus != TH_EXIT ) SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  124. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  125. pthread_join(m_sThreadAHandler, NULL);
  126. #else
  127. WaitForSingleObject(m_hThreadAHandle, 2000);
  128. CloseHandle(m_hThreadAHandle);
  129. #endif
  130. #ifdef DEBUG_EXTRA
  131. LOG("Thread A Ended.");
  132. #endif
  133. };
  134. void basicDualThread::waitThreadBEnd( bool Force ){
  135. if( m_bThreadBStatus > TH_WORKING || m_bThreadBStatus < TH_ARMED ){
  136. #ifdef DEBUG_EXTRA
  137. LOG("Thread B already Dead!");
  138. #endif
  139. return;
  140. };
  141. if( Force ){
  142. #ifdef DEBUG_EXTRA
  143. LOG("Thread B Requested Exit.");
  144. #endif
  145. m_bThreadBStatus = TH_ENDED;
  146. } else {
  147. #ifdef DEBUG_EXTRA
  148. LOG("Thread B Wait Exit.");
  149. #endif
  150. };
  151. while( m_bThreadBStatus != TH_EXIT ) SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  152. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  153. pthread_join(m_sThreadBHandler, NULL);
  154. #else
  155. WaitForSingleObject(m_hThreadBHandle, 2000);
  156. CloseHandle(m_hThreadBHandle);
  157. #endif
  158. #ifdef DEBUG_EXTRA
  159. LOG("Thread B Ended.");
  160. #endif
  161. };
  162. bool basicDualThread::uninit( void ){
  163. if( m_bThreadAStatus == TH_PRE_INIT && m_bThreadBStatus == TH_PRE_INIT ) return true;
  164. while( m_bThreadAStatus < TH_WORKING || m_bThreadBStatus < TH_WORKING ) SLEEP(1);
  165. m_bThreadBStatus = m_bThreadAStatus = TH_ENDED;
  166. while( m_bThreadAStatus < TH_EXIT || m_bThreadBStatus < TH_EXIT ) SLEEP(1);
  167. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  168. pthread_join(m_sThreadAHandler, NULL);
  169. pthread_join(m_sThreadBHandler, NULL);
  170. #else
  171. WaitForSingleObject(m_hThreadAHandle, 2000);
  172. WaitForSingleObject(m_hThreadBHandle, 2000);
  173. CloseHandle(m_hThreadAHandle);
  174. CloseHandle(m_hThreadBHandle);
  175. #endif
  176. #ifdef DEBUG_EXTRA
  177. LOG("UNINIT done!");
  178. #endif
  179. m_bThreadAStatus = TH_PRE_INIT;
  180. m_bThreadBStatus = TH_PRE_INIT;
  181. return true;
  182. };
  183. #undef LOG
  184. #undef LOG_ERROR
  185. #undef SLOG
  186. #undef SLOG_ERROR