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.

133 lines
2.6 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: T I M E R . H
  7. //
  8. // Contents: Timer queue helper class
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 2 Nov 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #pragma once
  16. #include "upsync.h"
  17. #pragma warning(disable : 4355)
  18. class CTimerQueue
  19. {
  20. public:
  21. ~CTimerQueue();
  22. static CTimerQueue & Instance();
  23. HRESULT HrInitialize();
  24. HRESULT HrShutdown(HANDLE hCompletionEvent);
  25. HRESULT HrGetHandle(HANDLE * phTimerQueue);
  26. private:
  27. CTimerQueue();
  28. CTimerQueue(const CTimerQueue &);
  29. CTimerQueue & operator=(const CTimerQueue &);
  30. HANDLE m_hTimerQueue;
  31. CUCriticalSection m_critSec;
  32. volatile long m_nRefCount;
  33. static CTimerQueue s_instance;
  34. };
  35. class CTimerBase
  36. {
  37. public:
  38. CTimerBase();
  39. virtual ~CTimerBase();
  40. HRESULT HrSetTimer(
  41. DWORD dwIntervalInMillis);
  42. HRESULT HrSetTimerInFired(
  43. DWORD dwIntervalInMillis);
  44. HRESULT HrDelete(HANDLE hCompletionEvent);
  45. HRESULT HrResetTimer(
  46. DWORD dwIntervalInMillis);
  47. protected:
  48. virtual void OnExecute() = 0;
  49. virtual BOOL OnTryToLock() = 0;
  50. virtual void OnUnlock() = 0;
  51. private:
  52. CTimerBase(const CTimerBase &);
  53. CTimerBase & operator=(const CTimerBase &);
  54. static void CALLBACK Callback(void * pvParam, BOOLEAN);
  55. HANDLE m_hTimerQueueTimer;
  56. CUCriticalSection m_critSec;
  57. volatile BOOL m_bShutdown;
  58. protected:
  59. #ifdef DBG
  60. long m_nSetTimer;
  61. long m_nSetTimerSuccess;
  62. long m_nDeleteTimer;
  63. long m_nDeleteTimerActual;
  64. long m_nTimerFiredEnter;
  65. long m_nTimerFiredExit;
  66. long m_nCallbackEnter;
  67. static long s_nCallbackEnter;
  68. static long s_nCallbackExit;
  69. #endif // DBG
  70. };
  71. template <class Type>
  72. class CTimer : public CTimerBase
  73. {
  74. public:
  75. CTimer(Type & type) : m_type(type) {}
  76. ~CTimer() {}
  77. void OnExecute()
  78. {
  79. #ifdef DBG
  80. InterlockedIncrement(&m_nTimerFiredEnter);
  81. #endif // DBG
  82. m_type.TimerFired();
  83. #ifdef DBG
  84. InterlockedIncrement(&m_nTimerFiredExit);
  85. #endif // DBG
  86. }
  87. BOOL OnTryToLock()
  88. {
  89. return m_type.TimerTryToLock();
  90. }
  91. void OnUnlock()
  92. {
  93. m_type.TimerUnlock();
  94. }
  95. private:
  96. CTimer(const CTimer &);
  97. CTimer & operator=(const CTimer &);
  98. Type & m_type;
  99. };
  100. inline ULONGLONG ULONGLONG_FROM_FILETIME(FILETIME ft)
  101. {
  102. return (*((ULONGLONG *)&(ft)));
  103. }
  104. inline FILETIME FILETIME_FROM_ULONGLONG(ULONGLONG ll)
  105. {
  106. return (*((FILETIME *)&(ll)));
  107. }
  108. VOID
  109. FileTimeToString(FILETIME FileTime, CHAR *szBuf, INT BufSize);