timer.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #ifndef _LINUX_TIMER_H
  2. #define _LINUX_TIMER_H
  3. #include <linux/list.h>
  4. #include <linux/ktime.h>
  5. #include <linux/stddef.h>
  6. #include <linux/debugobjects.h>
  7. #include <linux/stringify.h>
  8. struct tvec_base;
  9. struct timer_list {
  10. /*
  11. * All fields that change during normal runtime grouped to the
  12. * same cacheline
  13. */
  14. struct list_head entry;
  15. unsigned long expires;
  16. struct tvec_base *base;
  17. void (*function)(unsigned long);
  18. unsigned long data;
  19. int slack;
  20. #ifdef CONFIG_TIMER_STATS
  21. int start_pid;
  22. void *start_site;
  23. char start_comm[16];
  24. #endif
  25. #ifdef CONFIG_LOCKDEP
  26. struct lockdep_map lockdep_map;
  27. #endif
  28. };
  29. extern struct tvec_base boot_tvec_bases;
  30. #ifdef CONFIG_LOCKDEP
  31. /*
  32. * NB: because we have to copy the lockdep_map, setting the lockdep_map key
  33. * (second argument) here is required, otherwise it could be initialised to
  34. * the copy of the lockdep_map later! We use the pointer to and the string
  35. * "<file>:<line>" as the key resp. the name of the lockdep_map.
  36. */
  37. #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn) \
  38. .lockdep_map = STATIC_LOCKDEP_MAP_INIT(_kn, &_kn),
  39. #else
  40. #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)
  41. #endif
  42. #define TIMER_INITIALIZER(_function, _expires, _data) { \
  43. .entry = { .prev = TIMER_ENTRY_STATIC }, \
  44. .function = (_function), \
  45. .expires = (_expires), \
  46. .data = (_data), \
  47. .base = &boot_tvec_bases, \
  48. .slack = -1, \
  49. __TIMER_LOCKDEP_MAP_INITIALIZER( \
  50. __FILE__ ":" __stringify(__LINE__)) \
  51. }
  52. #define DEFINE_TIMER(_name, _function, _expires, _data) \
  53. struct timer_list _name = \
  54. TIMER_INITIALIZER(_function, _expires, _data)
  55. void init_timer_key(struct timer_list *timer,
  56. const char *name,
  57. struct lock_class_key *key);
  58. void init_timer_deferrable_key(struct timer_list *timer,
  59. const char *name,
  60. struct lock_class_key *key);
  61. #ifdef CONFIG_LOCKDEP
  62. #define init_timer(timer) \
  63. do { \
  64. static struct lock_class_key __key; \
  65. init_timer_key((timer), #timer, &__key); \
  66. } while (0)
  67. #define init_timer_deferrable(timer) \
  68. do { \
  69. static struct lock_class_key __key; \
  70. init_timer_deferrable_key((timer), #timer, &__key); \
  71. } while (0)
  72. #define init_timer_on_stack(timer) \
  73. do { \
  74. static struct lock_class_key __key; \
  75. init_timer_on_stack_key((timer), #timer, &__key); \
  76. } while (0)
  77. #define setup_timer(timer, fn, data) \
  78. do { \
  79. static struct lock_class_key __key; \
  80. setup_timer_key((timer), #timer, &__key, (fn), (data));\
  81. } while (0)
  82. #define setup_timer_on_stack(timer, fn, data) \
  83. do { \
  84. static struct lock_class_key __key; \
  85. setup_timer_on_stack_key((timer), #timer, &__key, \
  86. (fn), (data)); \
  87. } while (0)
  88. #define setup_deferrable_timer_on_stack(timer, fn, data) \
  89. do { \
  90. static struct lock_class_key __key; \
  91. setup_deferrable_timer_on_stack_key((timer), #timer, \
  92. &__key, (fn), \
  93. (data)); \
  94. } while (0)
  95. #else
  96. #define init_timer(timer)\
  97. init_timer_key((timer), NULL, NULL)
  98. #define init_timer_deferrable(timer)\
  99. init_timer_deferrable_key((timer), NULL, NULL)
  100. #define init_timer_on_stack(timer)\
  101. init_timer_on_stack_key((timer), NULL, NULL)
  102. #define setup_timer(timer, fn, data)\
  103. setup_timer_key((timer), NULL, NULL, (fn), (data))
  104. #define setup_timer_on_stack(timer, fn, data)\
  105. setup_timer_on_stack_key((timer), NULL, NULL, (fn), (data))
  106. #define setup_deferrable_timer_on_stack(timer, fn, data)\
  107. setup_deferrable_timer_on_stack_key((timer), NULL, NULL, (fn), (data))
  108. #endif
  109. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  110. extern void init_timer_on_stack_key(struct timer_list *timer,
  111. const char *name,
  112. struct lock_class_key *key);
  113. extern void destroy_timer_on_stack(struct timer_list *timer);
  114. #else
  115. static inline void destroy_timer_on_stack(struct timer_list *timer) { }
  116. static inline void init_timer_on_stack_key(struct timer_list *timer,
  117. const char *name,
  118. struct lock_class_key *key)
  119. {
  120. init_timer_key(timer, name, key);
  121. }
  122. #endif
  123. static inline void setup_timer_key(struct timer_list * timer,
  124. const char *name,
  125. struct lock_class_key *key,
  126. void (*function)(unsigned long),
  127. unsigned long data)
  128. {
  129. timer->function = function;
  130. timer->data = data;
  131. init_timer_key(timer, name, key);
  132. }
  133. static inline void setup_timer_on_stack_key(struct timer_list *timer,
  134. const char *name,
  135. struct lock_class_key *key,
  136. void (*function)(unsigned long),
  137. unsigned long data)
  138. {
  139. timer->function = function;
  140. timer->data = data;
  141. init_timer_on_stack_key(timer, name, key);
  142. }
  143. extern void setup_deferrable_timer_on_stack_key(struct timer_list *timer,
  144. const char *name,
  145. struct lock_class_key *key,
  146. void (*function)(unsigned long),
  147. unsigned long data);
  148. /**
  149. * timer_pending - is a timer pending?
  150. * @timer: the timer in question
  151. *
  152. * timer_pending will tell whether a given timer is currently pending,
  153. * or not. Callers must ensure serialization wrt. other operations done
  154. * to this timer, eg. interrupt contexts, or other CPUs on SMP.
  155. *
  156. * return value: 1 if the timer is pending, 0 if not.
  157. */
  158. static inline int timer_pending(const struct timer_list * timer)
  159. {
  160. return timer->entry.next != NULL;
  161. }
  162. extern void add_timer_on(struct timer_list *timer, int cpu);
  163. extern int del_timer(struct timer_list * timer);
  164. extern int mod_timer(struct timer_list *timer, unsigned long expires);
  165. extern int mod_timer_pending(struct timer_list *timer, unsigned long expires);
  166. extern int mod_timer_pinned(struct timer_list *timer, unsigned long expires);
  167. extern void set_timer_slack(struct timer_list *time, int slack_hz);
  168. #define TIMER_NOT_PINNED 0
  169. #define TIMER_PINNED 1
  170. /*
  171. * The jiffies value which is added to now, when there is no timer
  172. * in the timer wheel:
  173. */
  174. #define NEXT_TIMER_MAX_DELTA ((1UL << 30) - 1)
  175. /*
  176. * Return when the next timer-wheel timeout occurs (in absolute jiffies),
  177. * locks the timer base and does the comparison against the given
  178. * jiffie.
  179. */
  180. extern unsigned long get_next_timer_interrupt(unsigned long now);
  181. /*
  182. * Timer-statistics info:
  183. */
  184. #ifdef CONFIG_TIMER_STATS
  185. extern int timer_stats_active;
  186. #define TIMER_STATS_FLAG_DEFERRABLE 0x1
  187. extern void init_timer_stats(void);
  188. extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
  189. void *timerf, char *comm,
  190. unsigned int timer_flag);
  191. extern void __timer_stats_timer_set_start_info(struct timer_list *timer,
  192. void *addr);
  193. static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
  194. {
  195. if (likely(!timer_stats_active))
  196. return;
  197. __timer_stats_timer_set_start_info(timer, __builtin_return_address(0));
  198. }
  199. static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
  200. {
  201. timer->start_site = NULL;
  202. }
  203. #else
  204. static inline void init_timer_stats(void)
  205. {
  206. }
  207. static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
  208. {
  209. }
  210. static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
  211. {
  212. }
  213. #endif
  214. extern void add_timer(struct timer_list *timer);
  215. #ifdef CONFIG_SMP
  216. extern int try_to_del_timer_sync(struct timer_list *timer);
  217. extern int del_timer_sync(struct timer_list *timer);
  218. #else
  219. # define try_to_del_timer_sync(t) del_timer(t)
  220. # define del_timer_sync(t) del_timer(t)
  221. #endif
  222. #define del_singleshot_timer_sync(t) del_timer_sync(t)
  223. extern void init_timers(void);
  224. extern void run_local_timers(void);
  225. struct hrtimer;
  226. extern enum hrtimer_restart it_real_fn(struct hrtimer *);
  227. unsigned long __round_jiffies(unsigned long j, int cpu);
  228. unsigned long __round_jiffies_relative(unsigned long j, int cpu);
  229. unsigned long round_jiffies(unsigned long j);
  230. unsigned long round_jiffies_relative(unsigned long j);
  231. unsigned long __round_jiffies_up(unsigned long j, int cpu);
  232. unsigned long __round_jiffies_up_relative(unsigned long j, int cpu);
  233. unsigned long round_jiffies_up(unsigned long j);
  234. unsigned long round_jiffies_up_relative(unsigned long j);
  235. #endif