periodic.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Copyright (c) 2015, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #ifndef TOR_PERIODIC_H
  4. #define TOR_PERIODIC_H
  5. /** Callback function for a periodic event to take action.
  6. * The return value influences the next time the function will get called.
  7. * Return -1 to not update <b>last_action_time</b> and be polled again in
  8. * the next second. If a positive value is returned it will update the
  9. * interval time. If the returned value is larger than <b>now</b> then it
  10. * is assumed to be a future time to poll again. */
  11. typedef int (*periodic_event_helper_t)(time_t now,
  12. const or_options_t *options);
  13. /** A single item for the periodic-events-function table. */
  14. typedef struct periodic_event_item_t {
  15. periodic_event_helper_t fn; /**< The function to run the event */
  16. int interval; /**< The interval for running the function (In seconds). */
  17. time_t last_action_time; /**< The last time the function did something */
  18. periodic_timer_t *timer; /**< Timer object for this event */
  19. const char *name; /**< Name of the function -- for debug */
  20. } periodic_event_item_t;
  21. /** events will get their interval from first execution */
  22. #define PERIODIC_EVENT(fn) { fn##_callback, 0, 0, NULL, #fn }
  23. #if 0
  24. /** Refactor test, check the last_action_time was now or (now - delta - 1)
  25. * It returns an incremented <b>now</b> value and accounts for the current
  26. * implementation's off by one error in it's comparisons. */
  27. #define INCREMENT_DELTA_AND_TEST(id, now, delta) \
  28. (now+delta); \
  29. STMT_BEGIN \
  30. periodic_event_item_t *ev = &periodic_events[id]; \
  31. if (ev->last_action_time != now - delta - 1 && \
  32. ev->last_action_time != now) { \
  33. log_err(LD_BUG, "[Refactor Bug] Missed an interval " \
  34. "for %s, Got %lu, wanted %lu or %lu.", ev->name, \
  35. ev->last_action_time, now, now-delta); \
  36. tor_assert(0); \
  37. } \
  38. STMT_END
  39. #endif
  40. void periodic_event_assert_in_range(periodic_event_item_t *event,
  41. time_t start, time_t end);
  42. void periodic_event_launch(periodic_event_item_t *event);
  43. void periodic_event_destroy(periodic_event_item_t *event);
  44. void periodic_event_reschedule(periodic_event_item_t *event);
  45. #endif