pt-timer.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "pt-timer.h"
  2. void
  3. PTTimerTick(pt_timer_t *pool)
  4. {
  5. timer_t *timer = pool->next;
  6. if ( timer )
  7. {
  8. if ( timer->ticks )
  9. {
  10. timer->ticks--;
  11. }
  12. if (0 == timer->ticks)
  13. {
  14. timer->ms = 0;
  15. while( timer ) // 将后续超时的定时器都移除
  16. {
  17. timer = timer->next;
  18. if (timer && (0 == timer->ticks))
  19. {
  20. timer->ms = 0;
  21. }
  22. else
  23. {
  24. break;
  25. }
  26. }
  27. pool->next = timer;
  28. }
  29. }
  30. }
  31. void
  32. PTTimerInitPool(pt_timer_t *pool)
  33. {
  34. pool->ticks = 0;
  35. pool->next = NULL;
  36. }
  37. extern unsigned long sysTimeCounter;
  38. void
  39. PTTimerStart(pt_timer_t *pool, timer_t *timer, uint16_t ms)
  40. {
  41. timer_t *p = pool;
  42. PTTimerStop(pool, timer);
  43. timer->next = NULL;
  44. timer->ms = ms;
  45. timer->ticks = ms;//_ms(ms);
  46. if (0 == ms) return;
  47. while( p->next )
  48. {
  49. if (timer->ticks < p->next->ticks)
  50. {
  51. p->next->ticks -= timer->ticks;
  52. timer->next = p->next;
  53. p->next = timer;
  54. break;
  55. }
  56. else
  57. {
  58. timer->ticks -= p->next->ticks; // timer->ticks 有可能为 0
  59. }
  60. p = p->next;
  61. }
  62. if (NULL == p->next)
  63. {
  64. p->next = timer;
  65. }
  66. }
  67. boolean
  68. PTTimerIsExpired(timer_t *timer)
  69. {
  70. return (0 == timer->ms);
  71. }
  72. void
  73. PTTimerStop(pt_timer_t *pool, timer_t *timer)
  74. {
  75. timer_t *p = pool;
  76. if (timer && timer->ms)
  77. {
  78. while( p->next )
  79. {
  80. if (p->next == timer)
  81. {
  82. if ( timer->next )
  83. {
  84. timer->next->ticks += timer->ticks;
  85. }
  86. p->next = timer->next;
  87. timer->ms = 0;
  88. timer->next = NULL;
  89. break;
  90. }
  91. p = p->next;
  92. }
  93. }
  94. }