hrtimer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #define HRTIMER_INACTIVE ((void *)1UL)
  34. struct hrtimer_base;
  35. /**
  36. * struct hrtimer - the basic hrtimer structure
  37. * @node: red black tree node for time ordered insertion
  38. * @expires: the absolute expiry time in the hrtimers internal
  39. * representation. The time is related to the clock on
  40. * which the timer is based.
  41. * @function: timer expiry callback function
  42. * @base: pointer to the timer base (per cpu and per clock)
  43. *
  44. * The hrtimer structure must be initialized by init_hrtimer_#CLOCKTYPE()
  45. */
  46. struct hrtimer {
  47. struct rb_node node;
  48. ktime_t expires;
  49. int (*function)(struct hrtimer *);
  50. struct hrtimer_base *base;
  51. };
  52. /**
  53. * struct hrtimer_sleeper - simple sleeper structure
  54. * @timer: embedded timer structure
  55. * @task: task to wake up
  56. *
  57. * task is set to NULL, when the timer expires.
  58. */
  59. struct hrtimer_sleeper {
  60. struct hrtimer timer;
  61. struct task_struct *task;
  62. };
  63. /**
  64. * struct hrtimer_base - the timer base for a specific clock
  65. * @index: clock type index for per_cpu support when moving a timer
  66. * to a base on another cpu.
  67. * @lock: lock protecting the base and associated timers
  68. * @active: red black tree root node for the active timers
  69. * @first: pointer to the timer node which expires first
  70. * @resolution: the resolution of the clock, in nanoseconds
  71. * @get_time: function to retrieve the current time of the clock
  72. * @get_softirq_time: function to retrieve the current time from the softirq
  73. * @curr_timer: the timer which is executing a callback right now
  74. * @softirq_time: the time when running the hrtimer queue in the softirq
  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. ktime_t (*get_softirq_time)(void);
  84. struct hrtimer *curr_timer;
  85. ktime_t softirq_time;
  86. struct lock_class_key lock_key;
  87. };
  88. /*
  89. * clock_was_set() is a NOP for non- high-resolution systems. The
  90. * time-sorted order guarantees that a timer does not expire early and
  91. * is expired in the next softirq when the clock was advanced.
  92. */
  93. #define clock_was_set() do { } while (0)
  94. /* Exported timer functions: */
  95. /* Initialize timers: */
  96. extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
  97. enum hrtimer_mode mode);
  98. /* Basic timer operations: */
  99. extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
  100. const enum hrtimer_mode mode);
  101. extern int hrtimer_cancel(struct hrtimer *timer);
  102. extern int hrtimer_try_to_cancel(struct hrtimer *timer);
  103. #define hrtimer_restart(timer) hrtimer_start((timer), (timer)->expires, HRTIMER_ABS)
  104. /* Query timers: */
  105. extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
  106. extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
  107. #ifdef CONFIG_NO_IDLE_HZ
  108. extern ktime_t hrtimer_get_next_event(void);
  109. #endif
  110. static inline int hrtimer_active(const struct hrtimer *timer)
  111. {
  112. return rb_parent(&timer->node) != &timer->node;
  113. }
  114. /* Forward a hrtimer so it expires after now: */
  115. extern unsigned long
  116. hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
  117. /* Precise sleep: */
  118. extern long hrtimer_nanosleep(struct timespec *rqtp,
  119. struct timespec __user *rmtp,
  120. const enum hrtimer_mode mode,
  121. const clockid_t clockid);
  122. extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
  123. struct task_struct *tsk);
  124. /* Soft interrupt function to run the hrtimer queues: */
  125. extern void hrtimer_run_queues(void);
  126. /* Bootup initialization: */
  127. extern void __init hrtimers_init(void);
  128. #endif