Old fashion design for PC-104
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.

170 lines
4.2 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: Thread.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of Thread.cpp
  13. //----------------------------------------------------------------------------
  14. #include "StdAfx.h"
  15. #include "..\LgLib\Version.h"
  16. #include "..\LgLib\LgLogger.h"
  17. #include <process.h>
  18. #include "Thread.h"
  19. CLgThread::CLgThread()
  20. {
  21. // erase all data
  22. m_dwThreadId = 0UL;
  23. m_hEventStopThread = NULL;
  24. m_hThread = NULL;
  25. m_bSelfDestroy = FALSE;
  26. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CThread::CThread" );
  27. }
  28. CLgThread::~CLgThread()
  29. {
  30. // if it is not self destroy
  31. if( !m_bSelfDestroy )
  32. Stop( TRUE );
  33. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CThread::~CThread" );
  34. }
  35. BOOL CLgThread::Start( BOOL bSelfDestroy )
  36. {
  37. // blocking stop if thread was ran before
  38. if( !Stop( TRUE ) )
  39. {
  40. // log error
  41. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgThread::Start"), _T("Stop failed") );
  42. return FALSE;
  43. }
  44. // store for future use
  45. m_bSelfDestroy = bSelfDestroy;
  46. // create event for thread's stopping
  47. m_hEventStopThread = ::CreateEvent( NULL, TRUE, FALSE, NULL );
  48. if( !m_hEventStopThread )
  49. return FALSE;
  50. // create new thread
  51. m_hThread = (HANDLE)_beginthreadex( NULL, 0, ThreadProc_, this, 0, (PUINT)&m_dwThreadId );
  52. if( !m_hThread )
  53. return FALSE;
  54. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CThread::Create", "thread id %08X", m_dwThreadId );
  55. // all work was done successefully
  56. return TRUE;
  57. }
  58. BOOL CLgThread::Stop( BOOL bWaitForComplete )
  59. {
  60. // force thread to close
  61. if( m_hEventStopThread )
  62. ::SetEvent( m_hEventStopThread );
  63. // if don't need to wait until thread will closed
  64. if( !bWaitForComplete )
  65. {
  66. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CThread::Stop", "thread id %08X", m_dwThreadId );
  67. return TRUE;
  68. }
  69. // prevent dead lock if tryed to close from thread itself
  70. if( m_dwThreadId == ::GetCurrentThreadId() )
  71. {
  72. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, "CThread::Stop", "thread stop with wait from itself (deadlock)", m_dwThreadId );
  73. return FALSE;
  74. }
  75. // assume success
  76. BOOL bResult = TRUE;
  77. // if thread exists and works
  78. if( m_hThread )
  79. {
  80. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CThread::Stop", "thread id %08X", m_dwThreadId );
  81. // wait until thread will closed
  82. bResult = ::WaitForSingleObject( m_hThread, MAX_THREAD_CLOSE_TIME ) == WAIT_OBJECT_0;
  83. // if thread doesn't closed normally
  84. if( !bResult )
  85. {
  86. // log error
  87. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgThread::Stop"), _T("WaitForSingleObject failed") );
  88. // force thread stop
  89. _endthreadex( (UINT)m_hThread );
  90. }
  91. // close thread handle
  92. ::CloseHandle( m_hThread );
  93. // erase old value
  94. m_hThread = NULL;
  95. }
  96. // if event handle is valid
  97. if( m_hEventStopThread )
  98. {
  99. // close event handle
  100. ::CloseHandle( m_hEventStopThread );
  101. // erase old value
  102. m_hEventStopThread = NULL;
  103. }
  104. // return result of close operation
  105. return bResult;
  106. }
  107. UINT CALLBACK CLgThread::ThreadProc_( LPVOID lpvParam )
  108. {
  109. // get pointer to this
  110. CLgThread *pThread = (CLgThread*) lpvParam;
  111. __try
  112. {
  113. // call thread procedure
  114. UINT nResult = pThread->ThreadProc();
  115. // ensure to close thread normally
  116. // _endthreadex( (UINT)nResult );
  117. // self destroy if nessessary
  118. if( pThread->m_bSelfDestroy )
  119. {
  120. // close event handle
  121. if( pThread->m_hEventStopThread )
  122. ::CloseHandle( pThread->m_hEventStopThread );
  123. // close thread handle
  124. if( pThread->m_hThread )
  125. ::CloseHandle( pThread->m_hThread );
  126. // delete itself
  127. delete pThread;
  128. }
  129. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, pThread, "CThread::ThreadProc_" );
  130. //return with result
  131. return nResult;
  132. }
  133. __except( EXCEPTION_EXECUTE_HANDLER )
  134. {
  135. // show information
  136. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, pThread, _T("CThread::ThreadProc_"), _T("Exception 0x%08X"), _exception_code() );
  137. }
  138. return -1;
  139. }