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.

171 lines
3.9 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. TSS.CPP
  5. Abstract:
  6. This file defines the classes used by the Timer Subsystem.
  7. Classes defined:
  8. RecurrenceInstruction Complex recurrence information.
  9. TimerInstruction Single instruction for the timer
  10. History:
  11. 26-Nov-96 raymcc Draft
  12. 28-Dec-96 a-richm Alpha PDK Release
  13. 12-Apr-97 a-levn Extensive changes
  14. --*/
  15. #ifndef _TSS_H_
  16. #define _TSS_H_
  17. #include <functional>
  18. #include <wbemidl.h>
  19. #include <stdio.h>
  20. #include "sync.h"
  21. #include "CWbemTime.h"
  22. #include "parmdefs.h"
  23. #define INSTTYPE_WBEM 1
  24. #define INSTTYPE_INTERNAL 2
  25. #define INSTTYPE_AGGREGATION 3
  26. #define INSTTYPE_UNLOAD 4
  27. #define INSTTYPE_FREE_LIB 5
  28. class POLARITY CTimerInstruction
  29. {
  30. public:
  31. CTimerInstruction(){}
  32. virtual ~CTimerInstruction()
  33. {
  34. }
  35. virtual void AddRef() = 0;
  36. virtual void Release() = 0;
  37. virtual int GetInstructionType() = 0;
  38. public:
  39. virtual CWbemTime GetNextFiringTime(CWbemTime LastFiringTime,
  40. OUT long* plFiringCount) const = 0;
  41. virtual CWbemTime GetFirstFiringTime() const = 0;
  42. virtual HRESULT Fire(long lNumTimes, CWbemTime NextFiringTime) = 0;
  43. virtual HRESULT MarkForRemoval(){return S_OK;}
  44. };
  45. class POLARITY CInstructionTest
  46. {
  47. public:
  48. virtual BOOL operator()(CTimerInstruction* pToTest) = 0;
  49. };
  50. class POLARITY CIdentityTest : public CInstructionTest
  51. {
  52. protected:
  53. CTimerInstruction* m_pInst;
  54. public:
  55. CIdentityTest(CTimerInstruction* pInst) : m_pInst(pInst)
  56. {
  57. pInst->AddRef();
  58. }
  59. ~CIdentityTest() {m_pInst->Release();}
  60. BOOL operator()(CTimerInstruction* pToTest) {return pToTest == m_pInst;}
  61. };
  62. class POLARITY CInstructionQueue
  63. {
  64. public:
  65. CInstructionQueue();
  66. ~CInstructionQueue();
  67. HRESULT Enqueue(IN CWbemTime When, IN ADDREF CTimerInstruction* pInst);
  68. HRESULT Remove(IN CInstructionTest* pPred,
  69. OUT RELEASE_ME CTimerInstruction** ppInst = NULL);
  70. HRESULT Change(CTimerInstruction* pInst, CWbemTime When);
  71. HRESULT WaitAndPeek(OUT RELEASE_ME CTimerInstruction*& pInst,
  72. OUT CWbemTime& When);
  73. void BreakWait();
  74. BOOL IsEmpty();
  75. HRESULT Dequeue(OUT RELEASE_ME CTimerInstruction*& pInst,
  76. OUT CWbemTime& When);
  77. long GetNumInstructions();
  78. protected:
  79. CWbemInterval TimeToWait();
  80. void TouchHead();
  81. public:
  82. protected:
  83. struct CQueueEl
  84. {
  85. CWbemTime m_When;
  86. CTimerInstruction* m_pInst;
  87. CQueueEl* m_pNext;
  88. CQueueEl() : m_pInst(NULL), m_pNext(NULL){}
  89. CQueueEl(ADDREF CTimerInstruction* pInst, CWbemTime When)
  90. : m_pInst(pInst), m_pNext(NULL), m_When(When)
  91. {
  92. if(pInst)pInst->AddRef();
  93. }
  94. ~CQueueEl()
  95. {
  96. if(m_pInst) m_pInst->Release();
  97. }
  98. };
  99. CQueueEl* m_pQueue;
  100. CCritSec m_csQueue;
  101. HANDLE m_hNewHead;
  102. BOOL m_bBreak;
  103. };
  104. ///*****************************************************************************
  105. //
  106. // class CTimerGenerator
  107. //
  108. // Primary Timer Subsystem class. Accepts timer instructions and fires them
  109. // at approriate times.
  110. //
  111. //*****************************************************************************
  112. class POLARITY CTimerGenerator : public CHaltable
  113. {
  114. public:
  115. CTimerGenerator();
  116. ~CTimerGenerator();
  117. HRESULT Set(ADDREF CTimerInstruction *pInst,
  118. CWbemTime NextFiring = CWbemTime::GetZero());
  119. HRESULT Remove(CInstructionTest* pPred);
  120. virtual HRESULT Shutdown();
  121. void ScheduleFreeUnusedLibraries();
  122. protected:
  123. virtual void NotifyStartingThread(){}
  124. virtual void NotifyStoppingThread(){}
  125. private:
  126. static DWORD SchedulerThread(LPVOID pArg);
  127. void EnsureRunning();
  128. protected:
  129. CInstructionQueue m_Queue;
  130. HANDLE m_hSchedulerThread;
  131. BOOL m_fExitNow;
  132. CCritSec m_cs;
  133. };
  134. #endif