timer.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 hlist_node entry;
  15. unsigned long expires;
  16. void (*function)(unsigned long);
  17. unsigned long data;
  18. u32 flags;
  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. #ifdef CONFIG_LOCKDEP
  30. /*
  31. * NB: because we have to copy the lockdep_map, setting the lockdep_map key
  32. * (second argument) here is required, otherwise it could be initialised to
  33. * the copy of the lockdep_map later! We use the pointer to and the string
  34. * "<file>:<line>" as the key resp. the name of the lockdep_map.
  35. */
  36. #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn) \
  37. .lockdep_map = STATIC_LOCKDEP_MAP_INIT(_kn, &_kn),
  38. #else
  39. #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)
  40. #endif
  41. /*
  42. * A deferrable timer will work normally when the system is busy, but
  43. * will not cause a CPU to come out of idle just to service it; instead,
  44. * the timer will be serviced when the CPU eventually wakes up with a
  45. * subsequent non-deferrable timer.
  46. *
  47. * An irqsafe timer is executed with IRQ disabled and it's safe to wait for
  48. * the completion of the running instance from IRQ handlers, for example,
  49. * by calling del_timer_sync().
  50. *
  51. * Note: The irq disabled callback execution is a special case for
  52. * workqueue locking issues. It's not meant for executing random crap
  53. * with interrupts disabled. Abuse is monitored!
  54. */
  55. #define TIMER_CPUMASK 0x0007FFFF
  56. #define TIMER_MIGRATING 0x00080000
  57. #define TIMER_BASEMASK (TIMER_CPUMASK | TIMER_MIGRATING)
  58. #define TIMER_DEFERRABLE 0x00100000
  59. #define TIMER_IRQSAFE 0x00200000
  60. #define __TIMER_INITIALIZER(_function, _expires, _data, _flags) { \
  61. .entry = { .next = TIMER_ENTRY_STATIC }, \
  62. .function = (_function), \
  63. .expires = (_expires), \
  64. .data = (_data), \
  65. .flags = (_flags), \
  66. .slack = -1, \
  67. __TIMER_LOCKDEP_MAP_INITIALIZER( \
  68. __FILE__ ":" __stringify(__LINE__)) \
  69. }
  70. #define TIMER_INITIALIZER(_function, _expires, _data) \
  71. __TIMER_INITIALIZER((_function), (_expires), (_data), 0)
  72. #define TIMER_DEFERRED_INITIALIZER(_function, _expires, _data) \
  73. __TIMER_INITIALIZER((_function), (_expires), (_data), TIMER_DEFERRABLE)
  74. #define DEFINE_TIMER(_name, _function, _expires, _data) \
  75. struct timer_list _name = \
  76. TIMER_INITIALIZER(_function, _expires, _data)
  77. void init_timer_key(struct timer_list *timer, unsigned int flags,
  78. const char *name, struct lock_class_key *key);
  79. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  80. extern void init_timer_on_stack_key(struct timer_list *timer,
  81. unsigned int flags, const char *name,
  82. struct lock_class_key *key);
  83. extern void destroy_timer_on_stack(struct timer_list *timer);
  84. #else
  85. static inline void destroy_timer_on_stack(struct timer_list *timer) { }
  86. static inline void init_timer_on_stack_key(struct timer_list *timer,
  87. unsigned int flags, const char *name,
  88. struct lock_class_key *key)
  89. {
  90. init_timer_key(timer, flags, name, key);
  91. }
  92. #endif
  93. #ifdef CONFIG_LOCKDEP
  94. #define __init_timer(_timer, _flags) \
  95. do { \
  96. static struct lock_class_key __key; \
  97. init_timer_key((_timer), (_flags), #_timer, &__key); \
  98. } while (0)
  99. #define __init_timer_on_stack(_timer, _flags) \
  100. do { \
  101. static struct lock_class_key __key; \
  102. init_timer_on_stack_key((_timer), (_flags), #_timer, &__key); \
  103. } while (0)
  104. #else
  105. #define __init_timer(_timer, _flags) \
  106. init_timer_key((_timer), (_flags), NULL, NULL)
  107. #define __init_timer_on_stack(_timer, _flags) \
  108. init_timer_on_stack_key((_timer), (_flags), NULL, NULL)
  109. #endif
  110. #define init_timer(timer) \
  111. __init_timer((timer), 0)
  112. #define init_timer_deferrable(timer) \
  113. __init_timer((timer), TIMER_DEFERRABLE)
  114. #define init_timer_on_stack(timer) \
  115. __init_timer_on_stack((timer), 0)
  116. #define __setup_timer(_timer, _fn, _data, _flags) \
  117. do { \
  118. __init_timer((_timer), (_flags)); \
  119. (_timer)->function = (_fn); \
  120. (_timer)->data = (_data); \
  121. } while (0)
  122. #define __setup_timer_on_stack(_timer, _fn, _data, _flags) \
  123. do { \
  124. __init_timer_on_stack((_timer), (_flags)); \
  125. (_timer)->function = (_fn); \
  126. (_timer)->data = (_data); \
  127. } while (0)
  128. #define setup_timer(timer, fn, data) \
  129. __setup_timer((timer), (fn), (data), 0)
  130. #define setup_timer_on_stack(timer, fn, data) \
  131. __setup_timer_on_stack((timer), (fn), (data), 0)
  132. #define setup_deferrable_timer_on_stack(timer, fn, data) \
  133. __setup_timer_on_stack((timer), (fn), (data), TIMER_DEFERRABLE)
  134. /**
  135. * timer_pending - is a timer pending?
  136. * @timer: the timer in question
  137. *
  138. * timer_pending will tell whether a given timer is currently pending,
  139. * or not. Callers must ensure serialization wrt. other operations done
  140. * to this timer, eg. interrupt contexts, or other CPUs on SMP.
  141. *
  142. * return value: 1 if the timer is pending, 0 if not.
  143. */
  144. static inline int timer_pending(const struct timer_list * timer)
  145. {
  146. return timer->entry.pprev != NULL;
  147. }
  148. extern void add_timer_on(struct timer_list *timer, int cpu);
  149. extern int del_timer(struct timer_list * timer);
  150. extern int mod_timer(struct timer_list *timer, unsigned long expires);
  151. extern int mod_timer_pending(struct timer_list *timer, unsigned long expires);
  152. extern int mod_timer_pinned(struct timer_list *timer, unsigned long expires);
  153. extern void set_timer_slack(struct timer_list *time, int slack_hz);
  154. #define TIMER_NOT_PINNED 0
  155. #define TIMER_PINNED 1
  156. /*
  157. * The jiffies value which is added to now, when there is no timer
  158. * in the timer wheel:
  159. */
  160. #define NEXT_TIMER_MAX_DELTA ((1UL << 30) - 1)
  161. /*
  162. * Timer-statistics info:
  163. */
  164. #ifdef CONFIG_TIMER_STATS
  165. extern int timer_stats_active;
  166. extern void init_timer_stats(void);
  167. extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
  168. void *timerf, char *comm, u32 flags);
  169. extern void __timer_stats_timer_set_start_info(struct timer_list *timer,
  170. void *addr);
  171. static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
  172. {
  173. if (likely(!timer_stats_active))
  174. return;
  175. __timer_stats_timer_set_start_info(timer, __builtin_return_address(0));
  176. }
  177. static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
  178. {
  179. timer->start_site = NULL;
  180. }
  181. #else
  182. static inline void init_timer_stats(void)
  183. {
  184. }
  185. static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
  186. {
  187. }
  188. static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
  189. {
  190. }
  191. #endif
  192. extern void add_timer(struct timer_list *timer);
  193. extern int try_to_del_timer_sync(struct timer_list *timer);
  194. #ifdef CONFIG_SMP
  195. extern int del_timer_sync(struct timer_list *timer);
  196. #else
  197. # define del_timer_sync(t) del_timer(t)
  198. #endif
  199. #define del_singleshot_timer_sync(t) del_timer_sync(t)
  200. extern void init_timers(void);
  201. extern void run_local_timers(void);
  202. struct hrtimer;
  203. extern enum hrtimer_restart it_real_fn(struct hrtimer *);
  204. unsigned long __round_jiffies(unsigned long j, int cpu);
  205. unsigned long __round_jiffies_relative(unsigned long j, int cpu);
  206. unsigned long round_jiffies(unsigned long j);
  207. unsigned long round_jiffies_relative(unsigned long j);
  208. unsigned long __round_jiffies_up(unsigned long j, int cpu);
  209. unsigned long __round_jiffies_up_relative(unsigned long j, int cpu);
  210. unsigned long round_jiffies_up(unsigned long j);
  211. unsigned long round_jiffies_up_relative(unsigned long j);
  212. #endif