1
0

timermul.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #ifndef __TIMER_MULTIPLEXER_H
  2. #define __TIMER_MULTIPLEXER_H
  3. #include <bfc/common.h>
  4. #include <bfc/ptrlist.h>
  5. #include <api/dependency/api_dependentviewer.h>
  6. // FG> not too sure how to get a callback for attribute change, if anyone wants to change it be my guest ;)
  7. #define RESOLUTION_CHECK_DELAY 1000 // check for resolution changes every second
  8. // if uioptions CfgItem not found, use this value for resolution
  9. #define DEF_RES 20
  10. // below MAX_TIMER_DELAY, timer are multiplexed using a 'wheel' algorithm (mem used = MAX_TIMER_DELAY/resolution * sizeof(PtrList) + ntimers*sizeof(MultiplexedTimer), but fast (no lookup) )
  11. // above MAX_TIMER_DELAY, resolution drops to MAX_TIMER_DELAY/LOW_RES_DIV and uses ntimers*sizeof(MultiplexedTimer) bytes
  12. #define MAX_TIMER_DELAY 1000 // keep this dividable by LOW_RES_DIV please
  13. #define LOW_RES_DIV 4
  14. class CfgItem;
  15. class api_config;
  16. class TimerMultiplexerClient {
  17. public:
  18. virtual void onMultiplexedTimer(void *data, int skip, int mssincelast)=0;
  19. };
  20. class MultiplexedTimer {
  21. public:
  22. MultiplexedTimer(int _ms, void *_data) : ms(_ms), data(_data) {
  23. nexttick=0;
  24. flag=0;
  25. lost = 0;
  26. lastmscount=0;
  27. lastdelay=0;
  28. }
  29. virtual ~MultiplexedTimer() { }
  30. int ms;
  31. void *data;
  32. DWORD nexttick; // only used by low precision timers
  33. int flag; // only used by hi precision timers
  34. float lost; // only used by hi precision timers
  35. DWORD lastmscount;
  36. int lastdelay;
  37. };
  38. class TimerMultiplexer : public ifc_dependentviewer
  39. {
  40. public:
  41. TimerMultiplexer();
  42. virtual ~TimerMultiplexer();
  43. virtual void setClient(TimerMultiplexerClient *client);
  44. virtual void onServerTimer();
  45. virtual void addTimer(int ms, void *data);
  46. virtual void removeTimer(void *data);
  47. virtual void setResolution(int ms);
  48. virtual void shutdown();
  49. virtual int getNumTimers();
  50. virtual int getNumTimersLP();
  51. private:
  52. void checkResolution(DWORD now);
  53. void resetTimer(int newresolution);
  54. void resetWheel();
  55. void distributeAll();
  56. void distribute(MultiplexedTimer *t);
  57. void runCurSlice(DWORD now);
  58. void runTimer(DWORD now, DWORD last, MultiplexedTimer *t, PtrList<MultiplexedTimer> *slice, int pos);
  59. void removeFromWheel(MultiplexedTimer *t);
  60. void runLowPrecisionTimers(DWORD now);
  61. void removeFromLowPrecision(MultiplexedTimer *t);
  62. void doShutdown();
  63. PtrList<MultiplexedTimer> *getSlice(int n);
  64. TimerMultiplexerClient *client;
  65. int resolution;
  66. bool check_resolution;
  67. int timerset;
  68. int curslice;
  69. int nslices;
  70. int justexited;
  71. int firstevent;
  72. PtrList< PtrList< MultiplexedTimer > > wheel;
  73. PtrList< MultiplexedTimer > timers;
  74. PtrList< MultiplexedTimer > lptimers;
  75. MultiplexedTimer *running_timer;
  76. int dependentViewer_callback(ifc_dependent *item, const GUID *classguid, int cb, intptr_t param1 = 0, intptr_t param2 = 0, void *ptr = NULL, size_t ptrlen = 0);
  77. CfgItem *uioptions;
  78. RECVS_DISPATCH;
  79. };
  80. class MultiplexerServer {
  81. public:
  82. MultiplexerServer(TimerMultiplexer *mux, UINT tid) : m_mux(mux), m_tid(tid) {}
  83. virtual ~MultiplexerServer() {}
  84. TimerMultiplexer *getMultiplexer() { return m_mux; }
  85. UINT_PTR getId() { return m_tid; }
  86. void setId(UINT_PTR id) { m_tid = id; }
  87. private:
  88. TimerMultiplexer *m_mux;
  89. UINT_PTR m_tid;
  90. };
  91. class MultiplexerServerComparatorTID {
  92. public:
  93. // comparator for sorting
  94. static int compareItem(MultiplexerServer *p1, MultiplexerServer* p2) {
  95. if (p1->getId() < p2->getId()) return -1;
  96. if (p1->getId() > p2->getId()) return 1;
  97. return 0;
  98. }
  99. // comparator for searching
  100. static int compareAttrib(const wchar_t *attrib, MultiplexerServer *item) {
  101. if (*((UINT *)attrib) < item->getId()) return -1;
  102. if (*((UINT *)attrib) < item->getId()) return 1;
  103. return 0;
  104. }
  105. };
  106. class MultiplexerServerComparatorMux{
  107. public:
  108. // comparator for sorting
  109. static int compareItem(MultiplexerServer *p1, MultiplexerServer* p2) {
  110. if (p1->getMultiplexer() < p2->getMultiplexer()) return -1;
  111. if (p1->getMultiplexer() > p2->getMultiplexer()) return 1;
  112. return 0;
  113. }
  114. // comparator for searching
  115. static int compareAttrib(const wchar_t *attrib, MultiplexerServer *item) {
  116. if ((TimerMultiplexer *)attrib < item->getMultiplexer()) return -1;
  117. if ((TimerMultiplexer *)attrib < item->getMultiplexer()) return 1;
  118. return 0;
  119. }
  120. };
  121. #endif