timerqueue.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_TIMERQUEUE_H
  3. #define _LINUX_TIMERQUEUE_H
  4. #include <linux/rbtree.h>
  5. #include <linux/ktime.h>
  6. struct timerqueue_node {
  7. struct rb_node node;
  8. ktime_t expires;
  9. };
  10. struct timerqueue_head {
  11. struct rb_root_cached rb_root;
  12. };
  13. extern bool timerqueue_add(struct timerqueue_head *head,
  14. struct timerqueue_node *node);
  15. extern bool timerqueue_del(struct timerqueue_head *head,
  16. struct timerqueue_node *node);
  17. extern struct timerqueue_node *timerqueue_iterate_next(
  18. struct timerqueue_node *node);
  19. /**
  20. * timerqueue_getnext - Returns the timer with the earliest expiration time
  21. *
  22. * @head: head of timerqueue
  23. *
  24. * Returns a pointer to the timer node that has the earliest expiration time.
  25. */
  26. static inline
  27. struct timerqueue_node *timerqueue_getnext(struct timerqueue_head *head)
  28. {
  29. struct rb_node *leftmost = rb_first_cached(&head->rb_root);
  30. return rb_entry_safe(leftmost, struct timerqueue_node, node);
  31. }
  32. static inline void timerqueue_init(struct timerqueue_node *node)
  33. {
  34. RB_CLEAR_NODE(&node->node);
  35. }
  36. static inline bool timerqueue_node_queued(struct timerqueue_node *node)
  37. {
  38. return !RB_EMPTY_NODE(&node->node);
  39. }
  40. static inline bool timerqueue_node_expires(struct timerqueue_node *node)
  41. {
  42. return node->expires;
  43. }
  44. static inline void timerqueue_init_head(struct timerqueue_head *head)
  45. {
  46. head->rb_root = RB_ROOT_CACHED;
  47. }
  48. #endif /* _LINUX_TIMERQUEUE_H */