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.

194 lines
3.8 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: LgLock.h
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Header file of LgLock.h
  13. //----------------------------------------------------------------------------
  14. #ifndef __LG__LOCK__H__INCLUDED__
  15. #define __LG__LOCK__H__INCLUDED__
  16. class CLgCriticalSection
  17. {
  18. public:
  19. CLgCriticalSection()
  20. {
  21. memset( &m_CriticalSection, 0, sizeof(m_CriticalSection) );
  22. ::InitializeCriticalSection( &m_CriticalSection );
  23. }
  24. // have to override to prevent section spreading
  25. CLgCriticalSection( CLgCriticalSection& )
  26. {
  27. memset( &m_CriticalSection, 0, sizeof(m_CriticalSection) );
  28. ::InitializeCriticalSection( &m_CriticalSection );
  29. }
  30. ~CLgCriticalSection()
  31. {
  32. ::DeleteCriticalSection( &m_CriticalSection );
  33. memset( &m_CriticalSection, 0, sizeof(m_CriticalSection) );
  34. }
  35. public:
  36. // have to override to prevent section spreading
  37. CLgCriticalSection& operator=( CLgCriticalSection& ) { return *this; }
  38. public:
  39. void Lock()
  40. {
  41. ::EnterCriticalSection( &m_CriticalSection );
  42. }
  43. void Unlock()
  44. {
  45. ::LeaveCriticalSection( &m_CriticalSection );
  46. }
  47. private:
  48. CRITICAL_SECTION m_CriticalSection;
  49. };
  50. class CLgMutex
  51. {
  52. public:
  53. CLgMutex( BOOL bInitiallyOwn = FALSE, LPCTSTR lpszName = NULL )
  54. {
  55. m_hMutex = NULL;
  56. Create( bInitiallyOwn, lpszName );
  57. }
  58. ~CLgMutex()
  59. {
  60. Close();
  61. }
  62. public:
  63. BOOL Create( BOOL bInitiallyOwn, LPCTSTR lpszName )
  64. {
  65. Close();
  66. return ( m_hMutex = ::CreateMutex( NULL, bInitiallyOwn, lpszName ) ) != NULL;
  67. }
  68. BOOL Open( DWORD dwDesiredAccess, BOOL bInheritHandle, LPCTSTR lpName )
  69. {
  70. Close();
  71. return ( m_hMutex = ::OpenMutex( dwDesiredAccess, bInheritHandle, lpName ) ) != NULL;
  72. }
  73. BOOL Release()
  74. {
  75. if( !m_hMutex )
  76. return FALSE;
  77. return ::ReleaseMutex( m_hMutex );
  78. }
  79. void Close()
  80. {
  81. if( m_hMutex )
  82. {
  83. ::CloseHandle( m_hMutex );
  84. m_hMutex = NULL;
  85. }
  86. }
  87. public:
  88. operator HANDLE() { return m_hMutex; }
  89. private:
  90. HANDLE m_hMutex;
  91. };
  92. class CLgEvent
  93. {
  94. public:
  95. CLgEvent( BOOL bManualReset = FALSE, BOOL bInitialState = FALSE, LPCTSTR lpName = NULL )
  96. {
  97. m_hEvent = NULL;
  98. Create( bManualReset, bInitialState, lpName );
  99. }
  100. // have to override to prevent section spreading
  101. CLgEvent( CLgEvent& )
  102. {
  103. m_hEvent = NULL;
  104. Create( FALSE, FALSE, NULL );
  105. }
  106. ~CLgEvent()
  107. {
  108. Close();
  109. }
  110. public:
  111. // have to override to prevent section spreading
  112. CLgEvent& operator=( CLgEvent& ) { return *this; }
  113. public:
  114. BOOL Create( BOOL bManualReset, BOOL bInitialState, LPCTSTR lpName )
  115. {
  116. Close();
  117. return (m_hEvent = ::CreateEvent( NULL, bManualReset, bInitialState, lpName )) != NULL;
  118. }
  119. BOOL Open( DWORD dwDesiredAccess, BOOL bInheritHandle, LPCTSTR lpName )
  120. {
  121. Close();
  122. return (m_hEvent = ::OpenEvent( dwDesiredAccess, bInheritHandle, lpName )) != NULL;
  123. }
  124. void Close()
  125. {
  126. if( m_hEvent )
  127. ::CloseHandle( m_hEvent );
  128. }
  129. BOOL Set()
  130. {
  131. if( m_hEvent )
  132. return ::SetEvent( m_hEvent );
  133. return FALSE;
  134. }
  135. BOOL Pulse()
  136. {
  137. if( m_hEvent )
  138. return ::PulseEvent( m_hEvent );
  139. return FALSE;
  140. }
  141. BOOL Reset()
  142. {
  143. if( m_hEvent )
  144. return ::ResetEvent( m_hEvent );
  145. return FALSE;
  146. }
  147. public:
  148. operator HANDLE() { return m_hEvent; }
  149. private:
  150. HANDLE m_hEvent;
  151. };
  152. class CLgLocker
  153. {
  154. public:
  155. CLgLocker( CLgCriticalSection * pCriticalSection )
  156. {
  157. m_pCriticalSection = pCriticalSection;
  158. m_pCriticalSection->Lock();
  159. }
  160. ~CLgLocker()
  161. {
  162. m_pCriticalSection->Unlock();
  163. }
  164. private:
  165. CLgCriticalSection *m_pCriticalSection;
  166. };
  167. #endif