hrtimer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * include/linux/hrtimer.h
  3. *
  4. * hrtimers - High-resolution kernel timers
  5. *
  6. * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
  7. * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
  8. *
  9. * data type definitions, declarations, prototypes
  10. *
  11. * Started by: Thomas Gleixner and Ingo Molnar
  12. *
  13. * For licencing details see kernel-base/COPYING
  14. */
  15. #ifndef _LINUX_HRTIMER_H
  16. #define _LINUX_HRTIMER_H
  17. #include <linux/rbtree.h>
  18. #include <linux/ktime.h>
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/wait.h>
  22. /*
  23. * Mode arguments of xxx_hrtimer functions:
  24. */
  25. enum hrtimer_mode {
  26. HRTIMER_ABS, /* Time value is absolute */
  27. HRTIMER_REL, /* Time value is relative to now */
  28. };
  29. enum hrtimer_restart {
  30. HRTIMER_NORESTART,
  31. HRTIMER_RESTART,
  32. };
  33. /*
  34. * Timer states:
  35. */
  36. enum hrtimer_state {
  37. HRTIMER_INACTIVE, /* Timer is inactive */
  38. HRTIMER_EXPIRED, /* Timer is expired */
  39. HRTIMER_PENDING, /* Timer is pending */
  40. };
  41. struct hrtimer_base;
  42. /**
  43. * struct hrtimer - the basic hrtimer structure
  44. *
  45. * @node: red black tree node for time ordered insertion
  46. * @expires: the absolute expiry time in the hrtimers internal
  47. * representation. The time is related to the clock on
  48. * which the timer is based.
  49. * @state: state of the timer
  50. * @function: timer expiry callback function
  51. * @data: argument for the callback function
  52. * @base: pointer to the timer base (per cpu and per clock)
  53. *
  54. * The hrtimer structure must be initialized by init_hrtimer_#CLOCKTYPE()
  55. */
  56. struct hrtimer {
  57. struct rb_node node;
  58. ktime_t expires;
  59. enum hrtimer_state state;
  60. int (*function)(void *);
  61. void *data;
  62. struct hrtimer_base *base;
  63. };
  64. /**
  65. * struct hrtimer_base - the timer base for a specific clock
  66. *
  67. * @index: clock type index for per_cpu support when moving a timer
  68. * to a base on another cpu.
  69. * @lock: lock protecting the base and associated timers
  70. * @active: red black tree root node for the active timers
  71. * @first: pointer to the timer node which expires first
  72. * @resolution: the resolution of the clock, in nanoseconds
  73. * @get_time: function to retrieve the current time of the clock
  74. * @curr_timer: the timer which is executing a callback right now
  75. */
  76. struct hrtimer_base {
  77. clockid_t index;
  78. spinlock_t lock;
  79. struct rb_root active;
  80. struct rb_node *first;
  81. ktime_t resolution;
  82. ktime_t (*get_time)(void);
  83. struct hrtimer *curr_timer;
  84. };
  85. /*
  86. * clock_was_set() is a NOP for non- high-resolution systems. The
  87. * time-sorted order guarantees that a timer does not expire early and
  88. * is expired in the next softirq when the clock was advanced.
  89. */
  90. #define clock_was_set() do { } while (0)
  91. /* Exported timer functions: */
  92. /* Initialize timers: */
  93. extern void hrtimer_init(struct hrtimer *timer, const clockid_t which_clock);
  94. extern void hrtimer_rebase(struct hrtimer *timer, const clockid_t which_clock);
  95. /* Basic timer operations: */
  96. extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
  97. const enum hrtimer_mode mode);
  98. extern int hrtimer_cancel(struct hrtimer *timer);
  99. extern int hrtimer_try_to_cancel(struct hrtimer *timer);
  100. #define hrtimer_restart(timer) hrtimer_start((timer), (timer)->expires, HRTIMER_ABS)
  101. /* Query timers: */
  102. extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
  103. extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
  104. static inline int hrtimer_active(const struct hrtimer *timer)
  105. {
  106. return timer->state == HRTIMER_PENDING;
  107. }
  108. /* Forward a hrtimer so it expires after now: */
  109. extern unsigned long hrtimer_forward(struct hrtimer *timer, ktime_t interval);
  110. /* Precise sleep: */
  111. extern long hrtimer_nanosleep(struct timespec *rqtp,
  112. struct timespec __user *rmtp,
  113. const enum hrtimer_mode mode,
  114. const clockid_t clockid);
  115. /* Soft interrupt function to run the hrtimer queues: */
  116. extern void hrtimer_run_queues(void);
  117. /* Bootup initialization: */
  118. extern void __init hrtimers_init(void);
  119. #endif