timer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* LGPL (v2.1 or any later version) - see LICENSE file for details */
  2. #ifndef CCAN_TIMER_H
  3. #define CCAN_TIMER_H
  4. #include <ccan/time/time.h>
  5. #include <ccan/list/list.h>
  6. #include <stdint.h>
  7. /* We divide all nsec values by 1000, reducing it to usec granularity. */
  8. #define TIMER_GRANULARITY 1000
  9. /* This gives 16 pointers per level, up to 13 levels deep. */
  10. #define TIMER_LEVEL_BITS 4
  11. struct timers;
  12. struct timer;
  13. /**
  14. * timers_init - initialize a timers struct.
  15. * @timers: the struct timers
  16. * @start: the minimum time which will ever be added.
  17. *
  18. * This sets up a timers struct: any timers added before @start will be
  19. * set to expire immediately.
  20. */
  21. void timers_init(struct timers *timers, struct timespec start);
  22. /**
  23. * timers_cleanup - free allocations within timers struct.
  24. * @timers: the struct timers
  25. *
  26. * This frees any timer layers allocated during use.
  27. */
  28. void timers_cleanup(struct timers *timers);
  29. /**
  30. * timer_add - insert a timer.
  31. * @timers: the struct timers
  32. * @timer: the (uninitialized) timer to add
  33. * @when: when @timer expires.
  34. *
  35. * This efficiently adds @timer to @timers, to expire @when (rounded to
  36. * TIMER_GRANULARITY nanoseconds).
  37. */
  38. void timer_add(struct timers *timers, struct timer *timer,
  39. struct timespec when);
  40. /**
  41. * timer_del - remove an unexpired timer.
  42. * @timers: the struct timers
  43. * @timer: the timer previously added with timer_add()
  44. *
  45. * This efficiently removes @timer from @timers.
  46. */
  47. void timer_del(struct timers *timers, struct timer *timer);
  48. /**
  49. * timer_earliest - find out the first time when a timer will expire
  50. * @timers: the struct timers
  51. * @first: the time, only set if there is a timer.
  52. *
  53. * This returns false, and doesn't alter @first if there are no
  54. * timers. Otherwise, it sets @first to the expiry time of the first
  55. * timer (rounded to TIMER_GRANULARITY nanoseconds), and returns true.
  56. */
  57. bool timer_earliest(const struct timers *timers, struct timespec *first);
  58. /**
  59. * timer_expire - update timers structure and remove expired timers.
  60. * @timers: the struct timers
  61. * @expire: the current time
  62. * @list: the list for expired timers.
  63. *
  64. * @list will be initialized to the empty list, then all timers added
  65. * with a @when arg less than or equal to @expire will be added to it in
  66. * expiry order (within TIMER_GRANULARITY nanosecond precision).
  67. *
  68. * After this, @expire is considered the current time, and adding any
  69. * timers with @when before this value will be silently changed to
  70. * adding them with immediate expiration.
  71. *
  72. * You should not move @expire backwards, though it need not move
  73. * forwards.
  74. */
  75. void timers_expire(struct timers *timers,
  76. struct timespec expire,
  77. struct list_head *list);
  78. /**
  79. * timers_check - check timer structure for consistency
  80. * @t: the struct timers
  81. * @abortstr: the location to print on aborting, or NULL.
  82. *
  83. * Because timers have redundant information, consistency checking can
  84. * be done on the tree. This is useful as a debugging check. If
  85. * @abortstr is non-NULL, that will be printed in a diagnostic if the
  86. * timers structure is inconsistent, and the function will abort.
  87. *
  88. * Returns the timers struct if it is consistent, NULL if not (it can
  89. * never return NULL if @abortstr is set).
  90. */
  91. struct timers *timers_check(const struct timers *t, const char *abortstr);
  92. #ifdef CCAN_TIMER_DEBUG
  93. #include <stdio.h>
  94. /**
  95. * timers_dump - dump the timers datastructure (for debugging it)
  96. * @t: the struct timers
  97. * @fp: the FILE to dump to (stderr if @fp is NULL)
  98. */
  99. void timers_dump(const struct timers *timers, FILE *fp);
  100. #endif
  101. /**
  102. * struct timers - structure to hold a set of timers.
  103. *
  104. * Initialized using timers_init, the levels of the timer are
  105. * allocated as necessary, using malloc.
  106. *
  107. * See Also:
  108. * timers_init(), timers_cleanup()
  109. */
  110. struct timers {
  111. /* Far in the future. */
  112. struct list_head far;
  113. uint64_t base;
  114. struct timer_level *level[(64 + TIMER_LEVEL_BITS-1) / TIMER_LEVEL_BITS];
  115. };
  116. /**
  117. * struct timer - a single timer.
  118. *
  119. * Set up by timer_add(), this is usually contained within an
  120. * application-specific structure.
  121. *
  122. * See Also:
  123. * ccan/container_of, timer_add(), timer_del()
  124. */
  125. struct timer {
  126. struct list_node list;
  127. uint64_t time;
  128. };
  129. #endif /* CCAN_TIMER_H */