schedule.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //------------------------------------------------------------------------------
  2. // File: Schedule.h
  3. //
  4. // Desc: DirectShow base classes.
  5. //
  6. // Copyright (c) 1996-2001 Microsoft Corporation. All rights reserved.
  7. //------------------------------------------------------------------------------
  8. #ifndef __CAMSchedule__
  9. #define __CAMSchedule__
  10. class CAMSchedule : private CBaseObject
  11. {
  12. public:
  13. virtual ~CAMSchedule();
  14. // ev is the event we should fire if the advise time needs re-evaluating
  15. CAMSchedule( HANDLE ev );
  16. DWORD GetAdviseCount();
  17. REFERENCE_TIME GetNextAdviseTime();
  18. // We need a method for derived classes to add advise packets, we return the cookie
  19. DWORD_PTR AddAdvisePacket( const REFERENCE_TIME & time1, const REFERENCE_TIME & time2, HANDLE h, BOOL periodic );
  20. // And a way to cancel
  21. HRESULT Unadvise(DWORD_PTR dwAdviseCookie);
  22. // Tell us the time please, and we'll dispatch the expired events. We return the time of the next event.
  23. // NB: The time returned will be "useless" if you start adding extra Advises. But that's the problem of
  24. // whoever is using this helper class (typically a clock).
  25. REFERENCE_TIME Advise( const REFERENCE_TIME & rtTime );
  26. // Get the event handle which will be set if advise time requires re-evaluation.
  27. HANDLE GetEvent() const { return m_ev; }
  28. private:
  29. // We define the nodes that will be used in our singly linked list
  30. // of advise packets. The list is ordered by time, with the
  31. // elements that will expire first at the front.
  32. class CAdvisePacket
  33. {
  34. public:
  35. CAdvisePacket()
  36. {}
  37. CAdvisePacket * m_next;
  38. DWORD_PTR m_dwAdviseCookie;
  39. REFERENCE_TIME m_rtEventTime; // Time at which event should be set
  40. REFERENCE_TIME m_rtPeriod; // Periodic time
  41. HANDLE m_hNotify; // Handle to event or semephore
  42. BOOL m_bPeriodic; // TRUE => Periodic event
  43. CAdvisePacket( __inout_opt CAdvisePacket * next, LONGLONG time ) : m_next(next), m_rtEventTime(time)
  44. {}
  45. void InsertAfter( __inout CAdvisePacket * p )
  46. {
  47. p->m_next = m_next;
  48. m_next = p;
  49. }
  50. int IsZ() const // That is, is it the node that represents the end of the list
  51. { return m_next == 0; }
  52. CAdvisePacket * RemoveNext()
  53. {
  54. CAdvisePacket *const next = m_next;
  55. CAdvisePacket *const new_next = next->m_next;
  56. m_next = new_next;
  57. return next;
  58. }
  59. void DeleteNext()
  60. {
  61. delete RemoveNext();
  62. }
  63. CAdvisePacket * Next() const
  64. {
  65. CAdvisePacket * result = m_next;
  66. if (result->IsZ()) result = 0;
  67. return result;
  68. }
  69. DWORD_PTR Cookie() const
  70. { return m_dwAdviseCookie; }
  71. };
  72. // Structure is:
  73. // _head -> elmt1 -> elmt2 -> z -> null
  74. // So an empty list is: _head -> z -> null
  75. // Having _head & z as links makes insertaion,
  76. // deletion and shunting much easier.
  77. CAdvisePacket head, z; // z is both a _tail and a sentry
  78. volatile DWORD_PTR m_dwNextCookie; // Strictly increasing
  79. volatile DWORD m_dwAdviseCount; // Number of elements on list
  80. CCritSec m_Serialize;
  81. // AddAdvisePacket: adds the packet, returns the cookie (0 if failed)
  82. DWORD_PTR AddAdvisePacket( __inout CAdvisePacket * pPacket );
  83. // Event that we should set if the packed added above will be the next to fire.
  84. const HANDLE m_ev;
  85. // A Shunt is where we have changed the first element in the
  86. // list and want it re-evaluating (i.e. repositioned) in
  87. // the list.
  88. void ShuntHead();
  89. // Rather than delete advise packets, we cache them for future use
  90. CAdvisePacket * m_pAdviseCache;
  91. DWORD m_dwCacheCount;
  92. enum { dwCacheMax = 5 }; // Don't bother caching more than five
  93. void Delete( __inout CAdvisePacket * pLink );// This "Delete" will cache the Link
  94. // Attributes and methods for debugging
  95. public:
  96. #ifdef DEBUG
  97. void DumpLinkedList();
  98. #else
  99. void DumpLinkedList() {}
  100. #endif
  101. };
  102. #endif // __CAMSchedule__