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.

145 lines
3.1 KiB

  1. #include "stpwatch.h"
  2. StopWatch_cl::StopWatch_cl()
  3. {
  4. m_Zero();
  5. }
  6. ULONG StopWatch_cl::sm_TicksPerSecond;
  7. //
  8. // Init global/static state of the StopWatch class.
  9. //
  10. BOOL
  11. StopWatch_cl::m_ClassInit()
  12. {
  13. LARGE_INTEGER liTPS;
  14. #ifdef WIN32
  15. if(!QueryPerformanceFrequency(&liTPS) )
  16. {
  17. MessageBox(0,
  18. TEXT("Can't read frequency"),
  19. TEXT("QueryPerformanceFrequency"),
  20. MB_OK);
  21. return FALSE;
  22. }
  23. if (liTPS.HighPart != 0)
  24. {
  25. MessageBox(0,
  26. TEXT("Ticks Per Second is to great"),
  27. TEXT("QueryPerformanceFrequency"),
  28. MB_OK);
  29. return FALSE;
  30. }
  31. sm_TicksPerSecond = liTPS.LowPart;
  32. #else
  33. sm_TicksPerSecond = 1000;
  34. #endif
  35. return TRUE;
  36. }
  37. void
  38. StopWatch_cl::m_Zero()
  39. {
  40. LISet32(m_liStart, 0);
  41. LISet32(m_liStop, 0);
  42. m_State = ZEROED;
  43. }
  44. BOOL
  45. StopWatch_cl::m_Start()
  46. {
  47. #ifdef WIN32
  48. if(!QueryPerformanceCounter(&m_liStart))
  49. {
  50. MessageBox(0,
  51. TEXT("Get Start Time Failure"),
  52. TEXT("QueryPerformancecounter Failed"),
  53. MB_OK);
  54. return FALSE;
  55. }
  56. #else
  57. m_liStart.LowPart = GetTickCount();
  58. m_liStart.HighPart = 0;
  59. #endif
  60. m_State = RUNNING;
  61. return TRUE;
  62. }
  63. // m_MeasureStop()
  64. // Returns microseconds per single iteration.
  65. //
  66. BOOL
  67. StopWatch_cl::m_Stop()
  68. {
  69. #ifdef WIN32
  70. if(!QueryPerformanceCounter(&m_liStop))
  71. {
  72. MessageBox(0,
  73. TEXT("Get Stop Time Failure"),
  74. TEXT("QueryPerformancecounter Failed"),
  75. MB_OK);
  76. return FALSE;
  77. }
  78. #else
  79. m_liStop.LowPart = GetTickCount();
  80. m_liStop.HighPart = 0;
  81. #endif
  82. m_State = STOPPED;
  83. return TRUE;
  84. }
  85. BOOL
  86. StopWatch_cl::m_Sleep(UINT msecs)
  87. {
  88. #ifdef WIN32
  89. Sleep(msecs);
  90. #else
  91. UINT start, elapsed;
  92. start = GetTickCount();
  93. do
  94. {
  95. elapsed = GetTickCount() - start;
  96. } while ( msecs > elapsed );
  97. #endif
  98. return TRUE;
  99. }
  100. //
  101. // Return a ULONG count of the number of Microseconds on the timer.
  102. // I would return LARGE_INTEGER but there doesn't seem to be facilities
  103. // to user them easily under 16 bit.
  104. //
  105. BOOL
  106. StopWatch_cl::m_Read(ULONG *p_cMicroSeconds)
  107. {
  108. LARGE_INTEGER liTicks;
  109. int borrow = 0;
  110. if(m_liStart.LowPart > m_liStop.LowPart)
  111. borrow = 1;
  112. liTicks.LowPart = m_liStop.LowPart - m_liStart.LowPart;
  113. liTicks.HighPart = m_liStop.HighPart - m_liStart.HighPart - borrow;
  114. if(0 != liTicks.HighPart)
  115. {
  116. MessageBox(0,
  117. TEXT("Time interval was too great"),
  118. TEXT("Failure"),
  119. MB_OK);
  120. return(FALSE);
  121. }
  122. // result has units of (ticks/ loop of iterations). Where the ticks
  123. // are timer specific. This will convert result into:
  124. // (Milli_ticks) / (single iteration)
  125. #ifdef WIN32
  126. *p_cMicroSeconds = MulDiv(liTicks.LowPart, 1000000, sm_TicksPerSecond);
  127. #else
  128. *p_cMicroSeconds = liTicks.LowPart * 1000;
  129. #endif
  130. return TRUE;
  131. }
  132.