hrtimer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. struct hrtimer_clock_base;
  23. struct hrtimer_cpu_base;
  24. /*
  25. * Mode arguments of xxx_hrtimer functions:
  26. */
  27. enum hrtimer_mode {
  28. HRTIMER_MODE_ABS, /* Time value is absolute */
  29. HRTIMER_MODE_REL, /* Time value is relative to now */
  30. };
  31. /*
  32. * Return values for the callback function
  33. */
  34. enum hrtimer_restart {
  35. HRTIMER_NORESTART, /* Timer is not restarted */
  36. HRTIMER_RESTART, /* Timer must be restarted */
  37. };
  38. /**
  39. * struct hrtimer - the basic hrtimer structure
  40. * @node: red black tree node for time ordered insertion
  41. * @expires: the absolute expiry time in the hrtimers internal
  42. * representation. The time is related to the clock on
  43. * which the timer is based.
  44. * @function: timer expiry callback function
  45. * @base: pointer to the timer base (per cpu and per clock)
  46. *
  47. * The hrtimer structure must be initialized by init_hrtimer_#CLOCKTYPE()
  48. */
  49. struct hrtimer {
  50. struct rb_node node;
  51. ktime_t expires;
  52. enum hrtimer_restart (*function)(struct hrtimer *);
  53. struct hrtimer_clock_base *base;
  54. };
  55. /**
  56. * struct hrtimer_sleeper - simple sleeper structure
  57. * @timer: embedded timer structure
  58. * @task: task to wake up
  59. *
  60. * task is set to NULL, when the timer expires.
  61. */
  62. struct hrtimer_sleeper {
  63. struct hrtimer timer;
  64. struct task_struct *task;
  65. };
  66. /**
  67. * struct hrtimer_base - the timer base for a specific clock
  68. * @index: clock type index for per_cpu support when moving a
  69. * timer to a base on another cpu.
  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. * @get_softirq_time: function to retrieve the current time from the softirq
  75. * @softirq_time: the time when running the hrtimer queue in the softirq
  76. */
  77. struct hrtimer_clock_base {
  78. struct hrtimer_cpu_base *cpu_base;
  79. clockid_t index;
  80. struct rb_root active;
  81. struct rb_node *first;
  82. ktime_t resolution;
  83. ktime_t (*get_time)(void);
  84. ktime_t (*get_softirq_time)(void);
  85. ktime_t softirq_time;
  86. };
  87. #define HRTIMER_MAX_CLOCK_BASES 2
  88. /*
  89. * struct hrtimer_cpu_base - the per cpu clock bases
  90. * @lock: lock protecting the base and associated clock bases
  91. * and timers
  92. * @lock_key: the lock_class_key for use with lockdep
  93. * @clock_base: array of clock bases for this cpu
  94. * @curr_timer: the timer which is executing a callback right now
  95. */
  96. struct hrtimer_cpu_base {
  97. spinlock_t lock;
  98. struct lock_class_key lock_key;
  99. struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
  100. struct hrtimer *curr_timer;
  101. };
  102. /*
  103. * clock_was_set() is a NOP for non- high-resolution systems. The
  104. * time-sorted order guarantees that a timer does not expire early and
  105. * is expired in the next softirq when the clock was advanced.
  106. */
  107. #define clock_was_set() do { } while (0)
  108. /* Exported timer functions: */
  109. /* Initialize timers: */
  110. extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
  111. enum hrtimer_mode mode);
  112. /* Basic timer operations: */
  113. extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
  114. const enum hrtimer_mode mode);
  115. extern int hrtimer_cancel(struct hrtimer *timer);
  116. extern int hrtimer_try_to_cancel(struct hrtimer *timer);
  117. static inline int hrtimer_restart(struct hrtimer *timer)
  118. {
  119. return hrtimer_start(timer, timer->expires, HRTIMER_MODE_ABS);
  120. }
  121. /* Query timers: */
  122. extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
  123. extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
  124. #ifdef CONFIG_NO_IDLE_HZ
  125. extern ktime_t hrtimer_get_next_event(void);
  126. #endif
  127. static inline int hrtimer_active(const struct hrtimer *timer)
  128. {
  129. return rb_parent(&timer->node) != &timer->node;
  130. }
  131. /* Forward a hrtimer so it expires after now: */
  132. extern unsigned long
  133. hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
  134. /* Precise sleep: */
  135. extern long hrtimer_nanosleep(struct timespec *rqtp,
  136. struct timespec __user *rmtp,
  137. const enum hrtimer_mode mode,
  138. const clockid_t clockid);
  139. extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
  140. extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
  141. struct task_struct *tsk);
  142. /* Soft interrupt function to run the hrtimer queues: */
  143. extern void hrtimer_run_queues(void);
  144. /* Resume notification */
  145. void hrtimer_notify_resume(void);
  146. /* Bootup initialization: */
  147. extern void __init hrtimers_init(void);
  148. #endif