Leaked source code of windows server 2003
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.

187 lines
5.0 KiB

  1. //-----------------------------------------------------------------------------
  2. //
  3. //
  4. // File: aqdbgcnt.cpp
  5. //
  6. // Description: Implementation of CDeubgCountdown object
  7. //
  8. // Author: Mike Swafford (MikeSwa)
  9. //
  10. // History:
  11. // 10/28/98 - MikeSwa Created
  12. //
  13. // Copyright (C) 1998 Microsoft Corporation
  14. //
  15. //-----------------------------------------------------------------------------
  16. #include "aqprecmp.h"
  17. #include "aqdbgcnt.h"
  18. //---[ CDebugCountdown::ThreadStartRoutine ]-----------------------------------
  19. //
  20. //
  21. // Description:
  22. // This is the main worker routine for the class it keeps on calling
  23. // WaitForSingleObject... and will assert if it times out.
  24. // Parameters:
  25. // pvThis The "this" ptr for this object
  26. // Returns:
  27. // Always 0
  28. // History:
  29. // 10/27/98 - MikeSwa Created
  30. //
  31. //-----------------------------------------------------------------------------
  32. DWORD CDebugCountdown::ThreadStartRoutine(PVOID pvThis)
  33. {
  34. _ASSERT(pvThis);
  35. DWORD dwWaitResult = 0;
  36. DWORD dwTick1 = 0;
  37. DWORD dwTick2 = 0;
  38. CDebugCountdown *pdbgcntThis = (CDebugCountdown *) pvThis;
  39. _ASSERT(DEBUG_COUNTDOWN_SIG == pdbgcntThis->m_dwSignature);
  40. while (DEBUG_COUNTDOWN_ENDED != pdbgcntThis->m_dwFlags)
  41. {
  42. _ASSERT(pdbgcntThis->m_hEvent);
  43. dwTick1 = GetTickCount();
  44. dwWaitResult = WaitForSingleObject(pdbgcntThis->m_hEvent,
  45. pdbgcntThis->m_dwMilliseconds);
  46. dwTick2 = GetTickCount();
  47. if (DEBUG_COUNTDOWN_SUSPENDED != pdbgcntThis->m_dwFlags)
  48. {
  49. //This assert is the whole reason for the existance of this object
  50. _ASSERT((WAIT_TIMEOUT != dwWaitResult) && "Failure to call stop hints... check threads");
  51. }
  52. }
  53. return 0;
  54. }
  55. CDebugCountdown::CDebugCountdown()
  56. {
  57. m_dwSignature = DEBUG_COUNTDOWN_SIG;
  58. m_hEvent = NULL;
  59. m_dwMilliseconds = DEBUG_COUNTDOWN_DEFAULT_WAIT;
  60. m_hThread = NULL;
  61. m_dwFlags = 0;
  62. }
  63. CDebugCountdown::~CDebugCountdown()
  64. {
  65. if (m_hEvent)
  66. _VERIFY(CloseHandle(m_hEvent));
  67. if (m_hThread)
  68. _VERIFY(CloseHandle(m_hThread));
  69. }
  70. //The following group of functions are defined as inline NULL-ops in retail
  71. //builds. Below are there debug implementations
  72. #ifdef DEBUG
  73. //---[ CDebugCountdown::StartCountdown ]---------------------------------------
  74. //
  75. //
  76. // Description:
  77. // Starts the countdown timer... will create an event and a thread to
  78. // wait on that event.
  79. // Parameters:
  80. // dwMilliseconds Milliseconds to wait before ASSERTING
  81. // Returns:
  82. // -
  83. // History:
  84. // 10/28/98 - MikeSwa Created
  85. //
  86. //-----------------------------------------------------------------------------
  87. void CDebugCountdown::StartCountdown(DWORD dwMilliseconds)
  88. {
  89. DWORD dwThreadId = 0;
  90. m_dwMilliseconds = dwMilliseconds;
  91. if (!m_hEvent)
  92. m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  93. if (m_hEvent && !m_hThread)
  94. {
  95. m_hThread = CreateThread (NULL, 0, CDebugCountdown::ThreadStartRoutine,
  96. this, 0, &dwThreadId);
  97. }
  98. }
  99. //---[ CDebugCountdown::SuspendCountdown ]-------------------------------------
  100. //
  101. //
  102. // Description:
  103. // Suspends the countdown until the Next ResetCountdown(). Designed to
  104. // be used when another component's shutdown routine is called (like cat),
  105. // and it is expected that they will provide there own stop hints.
  106. // Parameters:
  107. // -
  108. // Returns:
  109. // -
  110. // History:
  111. // 10/28/98 - MikeSwa Created
  112. //
  113. //-----------------------------------------------------------------------------
  114. void CDebugCountdown::SuspendCountdown()
  115. {
  116. m_dwFlags = DEBUG_COUNTDOWN_SUSPENDED;
  117. }
  118. //---[ CDebugCountdown::ResetCountdown ]---------------------------------------
  119. //
  120. //
  121. // Description:
  122. // Causes thread to wake up and start waiting again. Will also reset a
  123. // suspended countdown.
  124. // Parameters:
  125. // -
  126. // Returns:
  127. // -
  128. // History:
  129. // 10/28/98 - MikeSwa Created
  130. //
  131. //-----------------------------------------------------------------------------
  132. void CDebugCountdown::ResetCountdown()
  133. {
  134. m_dwFlags = 0;
  135. if (m_hEvent)
  136. _VERIFY(SetEvent(m_hEvent));
  137. }
  138. //---[ CDebugCountdown::EndCountdown ]-----------------------------------------
  139. //
  140. //
  141. // Description:
  142. // Terminates the countdown and waits for the waiting thread to exit.
  143. // Parameters:
  144. // -
  145. // Returns:
  146. // -
  147. // History:
  148. // 10/28/98 - MikeSwa Created
  149. //
  150. //-----------------------------------------------------------------------------
  151. void CDebugCountdown::EndCountdown()
  152. {
  153. m_dwFlags = DEBUG_COUNTDOWN_ENDED;
  154. if (m_hEvent)
  155. {
  156. _VERIFY(SetEvent(m_hEvent));
  157. //Wait for thread to exit
  158. if (m_hThread)
  159. {
  160. WaitForSingleObject(m_hThread, INFINITE);
  161. _VERIFY(CloseHandle(m_hThread));
  162. m_hThread = NULL;
  163. }
  164. }
  165. }
  166. #endif //DEBUG