timerlist.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef __TIMERLIST_H__
  2. #define __TIMERLIST_H__
  3. #include "list.h"
  4. #include "timestamp.h"
  5. class CTimerObject;
  6. class CTimerUnit;
  7. class CTimerList {
  8. private:
  9. CListObject<CTimerObject> tlist;
  10. int timeout;
  11. CTimerList *next;
  12. CTimerUnit * timerUnitOwner;
  13. public:
  14. friend class CTimerUnit;
  15. friend class CTimerObject;
  16. CTimerList(int t) : timeout(t), next(NULL)
  17. { }
  18. CTimerList(int t, CTimerUnit* timerUnitOwner ) : timeout(t), next(NULL),timerUnitOwner(timerUnitOwner)
  19. {
  20. }
  21. int64_t GetTimeUnitNowTime();
  22. ~CTimerList(void) { tlist.FreeList(); }
  23. int CheckExpired(int64_t now=0);
  24. };
  25. class CTimerUnit {
  26. private:
  27. CTimerList pending;
  28. CTimerList *next;
  29. int64_t m_SystemTime;/*系统时间*/
  30. int64_t m_NowTime;/*应用层时间*/
  31. int64_t m_TimeOffSet;/*时间拨动后的修正量*/
  32. public:
  33. friend class CTimerObject;
  34. CTimerUnit(void);
  35. ~CTimerUnit(void);
  36. int64_t GetNowTime()
  37. {
  38. return m_NowTime;
  39. }
  40. void UpdateNowTime(int max_wait = 0, int interrupted = 0);
  41. CTimerList *GetTimerListByMSeconds(int);
  42. CTimerList *GetTimerList(int t) {return GetTimerListByMSeconds(t*1000);}
  43. int ExpireMicroSeconds(int,int=0); // arg: max/min msec
  44. int CheckExpired(int64_t now=0);
  45. int CheckReady(void);
  46. };
  47. class CTimerObject: private CListObject<CTimerObject> {
  48. private:
  49. int64_t objexp;
  50. public:
  51. friend class CTimerList;
  52. friend class CTimerUnit;
  53. CTimerObject() { }
  54. virtual ~CTimerObject(void);
  55. virtual void TimerNotify(void);
  56. void DisableTimer(void) { ResetList(); }
  57. void AttachTimer(class CTimerList *o);
  58. void AttachReadyTimer(class CTimerUnit *o) { ListMoveTail(o->pending.tlist); }
  59. };
  60. template<class T>
  61. class CTimerMember: public CTimerObject
  62. {
  63. private:
  64. T *owner;
  65. virtual void TimerNotify(void) { owner->TimerNotify(); }
  66. public:
  67. CTimerMember(T *o) : owner(o) {}
  68. virtual ~CTimerMember() {}
  69. };
  70. class CReadyObject;
  71. class CReadyUnit
  72. {
  73. private:
  74. CListObject<CReadyObject> pending1;
  75. CListObject<CReadyObject> pending2;
  76. CListObject<CReadyObject> * pending;
  77. CListObject<CReadyObject> * processing;
  78. public:
  79. friend class CReadyObject;
  80. CReadyUnit() { pending = &pending1; processing = &pending2;}
  81. virtual ~CReadyUnit() {}
  82. int CheckReady(uint64_t now);
  83. };
  84. class CReadyObject : private CListObject<CReadyObject>
  85. {
  86. public:
  87. friend class CReadyUnit;
  88. CReadyObject() {}
  89. virtual ~CReadyObject() {}
  90. virtual void ReadyNotify(uint64_t now);
  91. void AttachReady(CReadyUnit * o) { ListMoveTail(o->pending); }
  92. void DisableReady() { ResetList(); }
  93. };
  94. #endif