timer.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. /*
  43. * Note that all tvec_bases are 2 byte aligned and lower bit of
  44. * base in timer_list is guaranteed to be zero. Use the LSB to
  45. * indicate whether the timer is deferrable.
  46. *
  47. * A deferrable timer will work normally when the system is busy, but
  48. * will not cause a CPU to come out of idle just to service it; instead,
  49. * the timer will be serviced when the CPU eventually wakes up with a
  50. * subsequent non-deferrable timer.
  51. */
  52. #define TBASE_DEFERRABLE_FLAG (0x1)
  53. #define TIMER_INITIALIZER(_function, _expires, _data) { \
  54. .entry = { .prev = TIMER_ENTRY_STATIC }, \
  55. .function = (_function), \
  56. .expires = (_expires), \
  57. .data = (_data), \
  58. .base = &boot_tvec_bases, \
  59. .slack = -1, \
  60. __TIMER_LOCKDEP_MAP_INITIALIZER( \
  61. __FILE__ ":" __stringify(__LINE__)) \
  62. }
  63. #define TBASE_MAKE_DEFERRED(ptr) ((struct tvec_base *) \
  64. ((unsigned char *)(ptr) + TBASE_DEFERRABLE_FLAG))
  65. #define TIMER_DEFERRED_INITIALIZER(_function, _expires, _data) {\
  66. .entry = { .prev = TIMER_ENTRY_STATIC }, \
  67. .function = (_function), \
  68. .expires = (_expires), \
  69. .data = (_data), \
  70. .base = TBASE_MAKE_DEFERRED(&boot_tvec_bases), \
  71. __TIMER_LOCKDEP_MAP_INITIALIZER( \
  72. __FILE__ ":" __stringify(__LINE__)) \
  73. }
  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,
  78. const char *name,
  79. struct lock_class_key *key);
  80. void init_timer_deferrable_key(struct timer_list *timer,
  81. const char *name,
  82. struct lock_class_key *key);
  83. #ifdef CONFIG_LOCKDEP
  84. #define init_timer(timer) \
  85. do { \
  86. static struct lock_class_key __key; \
  87. init_timer_key((timer), #timer, &__key); \
  88. } while (0)
  89. #define init_timer_deferrable(timer) \
  90. do { \
  91. static struct lock_class_key __key; \
  92. init_timer_deferrable_key((timer), #timer, &__key); \
  93. } while (0)
  94. #define init_timer_on_stack(timer) \
  95. do { \
  96. static struct lock_class_key __key; \
  97. init_timer_on_stack_key((timer), #timer, &__key); \
  98. } while (0)
  99. #define setup_timer(timer, fn, data) \
  100. do { \
  101. static struct lock_class_key __key; \
  102. setup_timer_key((timer), #timer, &__key, (fn), (data));\
  103. } while (0)
  104. #define setup_timer_on_stack(timer, fn, data) \
  105. do { \
  106. static struct lock_class_key __key; \
  107. setup_timer_on_stack_key((timer), #timer, &__key, \
  108. (fn), (data)); \
  109. } while (0)
  110. #define setup_deferrable_timer_on_stack(timer, fn, data) \
  111. do { \
  112. static struct lock_class_key __key; \
  113. setup_deferrable_timer_on_stack_key((timer), #timer, \
  114. &__key, (fn), \
  115. (data)); \
  116. } while (0)
  117. #else
  118. #define init_timer(timer)\
  119. init_timer_key((timer), NULL, NULL)
  120. #define init_timer_deferrable(timer)\
  121. init_timer_deferrable_key((timer), NULL, NULL)
  122. #define init_timer_on_stack(timer)\
  123. init_timer_on_stack_key((timer), NULL, NULL)
  124. #define setup_timer(timer, fn, data)\
  125. setup_timer_key((timer), NULL, NULL, (fn), (data))
  126. #define setup_timer_on_stack(timer, fn, data)\
  127. setup_timer_on_stack_key((timer), NULL, NULL, (fn), (data))
  128. #define setup_deferrable_timer_on_stack(timer, fn, data)\
  129. setup_deferrable_timer_on_stack_key((timer), NULL, NULL, (fn), (data))
  130. #endif
  131. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  132. extern void init_timer_on_stack_key(struct timer_list *timer,
  133. const char *name,
  134. struct lock_class_key *key);
  135. extern void destroy_timer_on_stack(struct timer_list *timer);
  136. #else
  137. static inline void destroy_timer_on_stack(struct timer_list *timer) { }
  138. static inline void init_timer_on_stack_key(struct timer_list *timer,
  139. const char *name,
  140. struct lock_class_key *key)
  141. {
  142. init_timer_key(timer, name, key);
  143. }
  144. #endif
  145. static inline void setup_timer_key(struct timer_list * timer,
  146. const char *name,
  147. struct lock_class_key *key,
  148. void (*function)(unsigned long),
  149. unsigned long data)
  150. {
  151. timer->function = function;
  152. timer->data = data;
  153. init_timer_key(timer, name, key);
  154. }
  155. static inline void setup_timer_on_stack_key(struct timer_list *timer,
  156. const char *name,
  157. struct lock_class_key *key,
  158. void (*function)(unsigned long),
  159. unsigned long data)
  160. {
  161. timer->function = function;
  162. timer->data = data;
  163. init_timer_on_stack_key(timer, name, key);
  164. }
  165. extern void setup_deferrable_timer_on_stack_key(struct timer_list *timer,
  166. const char *name,
  167. struct lock_class_key *key,
  168. void (*function)(unsigned long),
  169. unsigned long data);
  170. /**
  171. * timer_pending - is a timer pending?
  172. * @timer: the timer in question
  173. *
  174. * timer_pending will tell whether a given timer is currently pending,
  175. * or not. Callers must ensure serialization wrt. other operations done
  176. * to this timer, eg. interrupt contexts, or other CPUs on SMP.
  177. *
  178. * return value: 1 if the timer is pending, 0 if not.
  179. */
  180. static inline int timer_pending(const struct timer_list * timer)
  181. {
  182. return timer->entry.next != NULL;
  183. }
  184. extern void add_timer_on(struct timer_list *timer, int cpu);
  185. extern int del_timer(struct timer_list * timer);
  186. extern int mod_timer(struct timer_list *timer, unsigned long expires);
  187. extern int mod_timer_pending(struct timer_list *timer, unsigned long expires);
  188. extern int mod_timer_pinned(struct timer_list *timer, unsigned long expires);
  189. extern void set_timer_slack(struct timer_list *time, int slack_hz);
  190. #define TIMER_NOT_PINNED 0
  191. #define TIMER_PINNED 1
  192. /*
  193. * The jiffies value which is added to now, when there is no timer
  194. * in the timer wheel:
  195. */
  196. #define NEXT_TIMER_MAX_DELTA ((1UL << 30) - 1)
  197. /*
  198. * Return when the next timer-wheel timeout occurs (in absolute jiffies),
  199. * locks the timer base and does the comparison against the given
  200. * jiffie.
  201. */
  202. extern unsigned long get_next_timer_interrupt(unsigned long now);
  203. /*
  204. * Timer-statistics info:
  205. */
  206. #ifdef CONFIG_TIMER_STATS
  207. extern int timer_stats_active;
  208. #define TIMER_STATS_FLAG_DEFERRABLE 0x1
  209. extern void init_timer_stats(void);
  210. extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
  211. void *timerf, char *comm,
  212. unsigned int timer_flag);
  213. extern void __timer_stats_timer_set_start_info(struct timer_list *timer,
  214. void *addr);
  215. static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
  216. {
  217. if (likely(!timer_stats_active))
  218. return;
  219. __timer_stats_timer_set_start_info(timer, __builtin_return_address(0));
  220. }
  221. static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
  222. {
  223. timer->start_site = NULL;
  224. }
  225. #else
  226. static inline void init_timer_stats(void)
  227. {
  228. }
  229. static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
  230. {
  231. }
  232. static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
  233. {
  234. }
  235. #endif
  236. extern void add_timer(struct timer_list *timer);
  237. #ifdef CONFIG_SMP
  238. extern int try_to_del_timer_sync(struct timer_list *timer);
  239. extern int del_timer_sync(struct timer_list *timer);
  240. #else
  241. # define try_to_del_timer_sync(t) del_timer(t)
  242. # define del_timer_sync(t) del_timer(t)
  243. #endif
  244. #define del_singleshot_timer_sync(t) del_timer_sync(t)
  245. extern void init_timers(void);
  246. extern void run_local_timers(void);
  247. struct hrtimer;
  248. extern enum hrtimer_restart it_real_fn(struct hrtimer *);
  249. unsigned long __round_jiffies(unsigned long j, int cpu);
  250. unsigned long __round_jiffies_relative(unsigned long j, int cpu);
  251. unsigned long round_jiffies(unsigned long j);
  252. unsigned long round_jiffies_relative(unsigned long j);
  253. unsigned long __round_jiffies_up(unsigned long j, int cpu);
  254. unsigned long __round_jiffies_up_relative(unsigned long j, int cpu);
  255. unsigned long round_jiffies_up(unsigned long j);
  256. unsigned long round_jiffies_up_relative(unsigned long j);
  257. #endif