hrtimer.h 4.5 KB

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