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.

184 lines
4.0 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: QueueCommon.h
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Header file of QueueCommon.h
  13. //----------------------------------------------------------------------------
  14. #ifndef __COMMON__QUEUE__H__
  15. #define __COMMON__QUEUE__H__
  16. // This common class use for inter thread objects transfer
  17. template < class TYPE, int MAXGROW=100, int MAXCHAIN=10 >
  18. class CQueueCommon
  19. {
  20. public:
  21. CQueueCommon( int nMaxGrow = MAXGROW, int nMaxChain = MAXCHAIN );
  22. virtual ~CQueueCommon();
  23. public:
  24. BOOL PostMessage( TYPE& data );
  25. BOOL GetMessage( TYPE& data );
  26. void Clear();
  27. BOOL IsEmpty();
  28. protected:
  29. struct CHAIN
  30. {
  31. CHAIN( TYPE data, int nMaxGrow ) : nFirst(0), nCount(1), pNext(NULL)
  32. {
  33. *( pData = new TYPE[ nMaxGrow ] ) = data;
  34. }
  35. ~CHAIN()
  36. {
  37. delete [] pData;
  38. }
  39. TYPE *pData;
  40. int nFirst;
  41. int nCount;
  42. CHAIN *pNext;
  43. };
  44. HANDLE m_hMutex;
  45. CHAIN *m_pFirst;
  46. CHAIN *m_pLast;
  47. int m_nCount;
  48. int m_nMaxGrow;
  49. int m_nMaxChain;
  50. };
  51. template < class TYPE, int MAXGROW, int MAXCHAIN >
  52. CQueueCommon< TYPE,MAXGROW,MAXCHAIN >::CQueueCommon( int nMaxGrow, int nMaxChain )
  53. {
  54. m_pFirst = m_pLast = NULL;
  55. m_hMutex = ::CreateMutex( NULL, FALSE, NULL );
  56. m_nCount = 0;
  57. m_nMaxGrow = nMaxGrow;
  58. m_nMaxChain = nMaxChain;
  59. }
  60. template < class TYPE, int MAXGROW, int MAXCHAIN >
  61. CQueueCommon< TYPE,MAXGROW,MAXCHAIN >::~CQueueCommon()
  62. {
  63. ::WaitForSingleObject( m_hMutex, INFINITE );
  64. CHAIN *pChain = m_pFirst;
  65. while( pChain )
  66. {
  67. CHAIN *pDelete = pChain;
  68. pChain = pChain->pNext;
  69. delete pDelete;
  70. }
  71. m_pFirst = m_pLast = NULL;
  72. m_nCount = m_nMaxGrow = m_nMaxChain = 0;
  73. ::ReleaseMutex( m_hMutex );
  74. ::CloseHandle( m_hMutex );
  75. m_hMutex = NULL;
  76. }
  77. template < class TYPE, int MAXGROW, int MAXCHAIN >
  78. BOOL CQueueCommon< TYPE,MAXGROW,MAXCHAIN >::PostMessage( TYPE& data )
  79. {
  80. ::WaitForSingleObject( m_hMutex, INFINITE );
  81. if( !m_pLast )
  82. {
  83. m_nCount = 1;
  84. m_pLast = m_pFirst = new CHAIN( data, m_nMaxGrow );
  85. if( !m_pLast )
  86. {
  87. ::ReleaseMutex( m_hMutex );
  88. return FALSE;
  89. }
  90. }
  91. else
  92. {
  93. if( m_pLast->nCount < m_nMaxGrow )
  94. {
  95. int nPos = ( m_pLast->nFirst + m_pLast->nCount ) % m_nMaxGrow;
  96. m_pLast->pData[ nPos ] = data;
  97. m_pLast->nCount++;
  98. }
  99. else
  100. {
  101. m_nCount++;
  102. if( m_nCount > m_nMaxChain )
  103. {
  104. ::ReleaseMutex( m_hMutex );
  105. return FALSE;
  106. }
  107. CHAIN *pChain = new CHAIN( data, m_nMaxGrow );
  108. if( !pChain )
  109. {
  110. ::ReleaseMutex( m_hMutex );
  111. return FALSE;
  112. }
  113. m_pLast->pNext = pChain;
  114. m_pLast = pChain;
  115. }
  116. }
  117. ::ReleaseMutex( m_hMutex );
  118. return TRUE;
  119. }
  120. template < class TYPE, int MAXGROW, int MAXCHAIN >
  121. BOOL CQueueCommon< TYPE,MAXGROW,MAXCHAIN >::GetMessage( TYPE& data )
  122. {
  123. ::WaitForSingleObject( m_hMutex, INFINITE );
  124. if( m_pFirst && m_pFirst->nCount > 0 )
  125. {
  126. data = m_pFirst->pData[ m_pFirst->nFirst ];
  127. m_pFirst->nFirst = ( m_pFirst->nFirst + 1 ) % m_nMaxGrow;
  128. m_pFirst->nCount--;
  129. if( m_pFirst->pNext && !m_pFirst->nCount )
  130. {
  131. CHAIN *pChain = m_pFirst->pNext;
  132. delete m_pFirst;
  133. m_pFirst = pChain;
  134. m_nCount--;
  135. }
  136. ::ReleaseMutex( m_hMutex );
  137. return TRUE;
  138. }
  139. ::ReleaseMutex( m_hMutex );
  140. return FALSE;
  141. }
  142. template < class TYPE, int MAXGROW, int MAXCHAIN >
  143. void CQueueCommon< TYPE,MAXGROW,MAXCHAIN >::Clear()
  144. {
  145. ::WaitForSingleObject( m_hMutex, INFINITE );
  146. CHAIN *pCur = m_pFirst;
  147. while( pCur )
  148. {
  149. CHAIN *pDelete = pCur;
  150. pCur = pCur->pNext;
  151. delete pDelete;
  152. }
  153. m_pFirst = m_pLast = NULL;
  154. m_nCount = 0;
  155. ::ReleaseMutex( m_hMutex );
  156. }
  157. template < class TYPE, int MAXGROW, int MAXCHAIN >
  158. BOOL CQueueCommon< TYPE,MAXGROW,MAXCHAIN >::IsEmpty()
  159. {
  160. ::WaitForSingleObject( m_hMutex, INFINITE );
  161. BOOL bEmpty = (m_nCount == 0);
  162. ::ReleaseMutex( m_hMutex );
  163. return bEmpty;
  164. }
  165. #endif