Source code of Windows XP (NT5)
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.

185 lines
3.7 KiB

  1. //
  2. // MODULE: APGTSPL.CPP
  3. //
  4. // PURPOSE: Pool Queue shared variables
  5. // Fully implement class PoolQueue
  6. //
  7. // PROJECT: Generic Troubleshooter DLL for Microsoft AnswerPoint
  8. //
  9. // COMPANY: Saltmine Creative, Inc. (206)-284-7511 [email protected]
  10. //
  11. // AUTHOR: Roman Mach
  12. //
  13. // ORIGINAL DATE: 8-2-96
  14. //
  15. // NOTES:
  16. // 1. Based on Print Troubleshooter DLL
  17. //
  18. // Version Date By Comments
  19. //--------------------------------------------------------------------
  20. // V0.1 - RM Original
  21. // V3.0 9/21/98 JM Working on encapsulation
  22. //
  23. #pragma warning(disable:4786)
  24. #include "stdafx.h"
  25. #include "apgtspl.h"
  26. #include "event.h"
  27. #include "apgtscls.h"
  28. #include "CharConv.h"
  29. //
  30. //
  31. CPoolQueue::CPoolQueue() :
  32. m_dwErr(0),
  33. m_cInProcess(0),
  34. m_timeLastAdd(0),
  35. m_timeLastRemove(0)
  36. {
  37. ::InitializeCriticalSection( &m_csQueueLock );
  38. m_hWorkSem = CreateSemaphore(NULL,
  39. 0,
  40. 0x7fffffff,
  41. NULL );
  42. if (m_hWorkSem == NULL)
  43. m_dwErr = EV_GTS_ERROR_POOL_SEMA;
  44. }
  45. //
  46. //
  47. CPoolQueue::~CPoolQueue()
  48. {
  49. if (m_hWorkSem)
  50. ::CloseHandle(m_hWorkSem);
  51. while ( !m_WorkQueue.empty() )
  52. {
  53. delete m_WorkQueue.back();
  54. m_WorkQueue.pop_back();
  55. }
  56. ::DeleteCriticalSection( &m_csQueueLock );
  57. }
  58. void CPoolQueue::Lock()
  59. {
  60. ::EnterCriticalSection( &m_csQueueLock );
  61. }
  62. void CPoolQueue::Unlock()
  63. {
  64. ::LeaveCriticalSection( &m_csQueueLock );
  65. }
  66. //
  67. //
  68. DWORD CPoolQueue::GetStatus()
  69. {
  70. return m_dwErr;
  71. }
  72. // put it at the tail of the queue & Signal the pool threads there is work to be done
  73. // OK if we're already locked when this is called; OK if we're not.
  74. void CPoolQueue::PushBack(WORK_QUEUE_ITEM * pwqi)
  75. {
  76. Lock();
  77. // Some data passed to thread just for statistical purposes
  78. // Thread can pass this info back over web; we can't.
  79. pwqi->GTSStat.dwQueueItems = GetTotalQueueItems();
  80. pwqi->GTSStat.dwWorkItems = GetTotalWorkItems();
  81. try
  82. {
  83. m_WorkQueue.push_back(pwqi);
  84. time(&m_timeLastAdd);
  85. // Signal the pool threads there is work to be done only if it was
  86. // successfully added to the work queue.
  87. ::ReleaseSemaphore( m_hWorkSem, 1, NULL );
  88. }
  89. catch (exception& x)
  90. {
  91. CString str;
  92. // Note STL exception in event log.
  93. CBuildSrcFileLinenoStr SrcLoc( __FILE__, __LINE__ );
  94. CEvent::ReportWFEvent( SrcLoc.GetSrcFileLineStr(),
  95. SrcLoc.GetSrcFileLineStr(),
  96. CCharConversion::ConvertACharToString(x.what(), str),
  97. _T(""),
  98. EV_GTS_STL_EXCEPTION );
  99. }
  100. Unlock();
  101. }
  102. // Get the item at the front of the queue in order to act on it.
  103. WORK_QUEUE_ITEM * CPoolQueue::GetWorkItem()
  104. {
  105. WORK_QUEUE_ITEM * pwqi;
  106. Lock();
  107. if ( !m_WorkQueue.empty() )
  108. {
  109. vector<WORK_QUEUE_ITEM *>::iterator it = m_WorkQueue.begin();
  110. pwqi = *it;
  111. m_WorkQueue.erase(it);
  112. time(&m_timeLastRemove);
  113. ++m_cInProcess;
  114. }
  115. else
  116. pwqi = NULL;
  117. Unlock();
  118. return pwqi;
  119. }
  120. // When we are totally done with a work item, reduce the count of those which are
  121. // in process.
  122. // Arbitrary, but acceptable, decision to track m_cInProcess in this class. JM 11/30/98
  123. void CPoolQueue::DecrementWorkItems()
  124. {
  125. Lock();
  126. --m_cInProcess;
  127. Unlock();
  128. }
  129. // Called by a pool thread to wait for there to be something in this queue.
  130. DWORD CPoolQueue::WaitForWork()
  131. {
  132. return ::WaitForSingleObject( m_hWorkSem, INFINITE );
  133. }
  134. // Arbitrary, but acceptable, decision to track m_cInProcess in this class. JM 11/30/98
  135. DWORD CPoolQueue::GetTotalWorkItems()
  136. {
  137. Lock();
  138. DWORD ret = m_cInProcess + GetTotalQueueItems();
  139. Unlock();
  140. return ret;
  141. }
  142. DWORD CPoolQueue::GetTotalQueueItems()
  143. {
  144. Lock();
  145. DWORD ret = m_WorkQueue.size();
  146. Unlock();
  147. return ret;
  148. }
  149. time_t CPoolQueue::GetTimeLastAdd()
  150. {
  151. Lock();
  152. time_t ret = m_timeLastAdd;
  153. Unlock();
  154. return ret;
  155. }
  156. time_t CPoolQueue::GetTimeLastRemove()
  157. {
  158. Lock();
  159. time_t ret = m_timeLastRemove;
  160. Unlock();
  161. return ret;
  162. }