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.

95 lines
2.6 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
  1. #include "basicThread.h"
  2. #define LOG( X ) m_pLog->Log( "BasicThread", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "BasicThread", this, _LOG_ERROR, X )
  4. //Thread Logging
  5. #define SLOG( X ) mThis->m_pLog->Log( "BasicThreadS", mThis, X )
  6. #define SLOG_ERROR( X ) mThis->m_pLog->Log( "BasicThreadS", mThis, _LOG_ERROR, X )
  7. bool basicThread::init( void ){
  8. if (m_bThreadStatus == TH_PRE_INIT) {
  9. m_bThreadStatus = TH_ARMED;
  10. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  11. int RetVal = pthread_create( &m_sThreadHandler, NULL, m_fThreadEntryPoint, ( void * ) this );
  12. if( RetVal == -1 ) {
  13. LOG_ERROR("Thread Creation Failed.");
  14. return false;
  15. };
  16. #else
  17. m_hThreadHandle = CreateThread(
  18. NULL,
  19. 0,
  20. (LPTHREAD_START_ROUTINE)m_fThreadEntryPoint,
  21. (LPVOID)this,
  22. 0,
  23. &m_dwThreadID );
  24. //TODO: Add thread creation check
  25. #endif
  26. } else {
  27. LOG_ERROR("Thread object not ready to be created.");
  28. return false;
  29. };
  30. return true;
  31. };
  32. void basicThread::m_fThreadProc( void ){
  33. m_bThreadStatus = TH_WORKING;
  34. while( m_bThreadStatus < TH_ENDED ) {
  35. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  36. };
  37. };
  38. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  39. void *basicThread::m_fThreadEntryPoint( void *ptr ){
  40. #else
  41. DWORD WINAPI basicThread::m_fThreadEntryPoint( LPVOID ptr){
  42. #endif
  43. basicThread *mThis;
  44. mThis = ( basicThread * ) ptr;
  45. if( mThis->m_bThreadStatus == TH_ARMED ) {
  46. mThis->m_fThreadProc();
  47. mThis->m_bThreadStatus = TH_EXIT;
  48. } else {
  49. SLOG_ERROR("Thread is not ARMED!");
  50. };
  51. #ifdef __linux__
  52. pthread_exit( NULL );
  53. return NULL;
  54. #else
  55. return 0;
  56. #endif
  57. };
  58. void basicThread::waitThreadEnd(bool forced){
  59. if( m_bThreadStatus > TH_WORKING || m_bThreadStatus < TH_ARMED ) return;
  60. if( forced ) m_bThreadStatus = TH_ENDED;
  61. while (m_bThreadStatus != TH_EXIT){
  62. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  63. };
  64. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  65. pthread_join(m_sThreadHandler, NULL);
  66. #else
  67. WaitForSingleObject(m_hThreadHandle, 2000);
  68. CloseHandle(m_hThreadHandle);
  69. #endif
  70. };
  71. bool basicThread::uninit( void ){
  72. if( m_bThreadStatus == TH_PRE_INIT ) return true;
  73. while( m_bThreadStatus < TH_WORKING ) SLEEP(1);
  74. m_bThreadStatus = TH_ENDED;
  75. while( m_bThreadStatus < TH_EXIT ) SLEEP(1);
  76. #if defined( __linux__ ) || defined(WIN_PTHREAD)
  77. pthread_join(m_sThreadHandler, NULL);
  78. #else
  79. WaitForSingleObject(m_hThreadHandle, 2000);
  80. CloseHandle(m_hThreadHandle);
  81. #endif
  82. return true;
  83. };
  84. #undef LOG
  85. #undef LOG_ERROR
  86. #undef SLOG
  87. #undef SLOG_ERROR