blob: 1794674c1a525328fcd5f1c279c006265100729f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_TIMER_H
2#define _LINUX_TIMER_H
3
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/list.h>
Ingo Molnar82f67cd2007-02-16 01:28:13 -08005#include <linux/ktime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/stddef.h>
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07007#include <linux/debugobjects.h>
Johannes Berg6f2b9b92009-01-29 16:03:20 +01008#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Pavel Macheka6fa8e52008-01-30 13:30:00 +010010struct tvec_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12struct timer_list {
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -080013 /*
14 * All fields that change during normal runtime grouped to the
15 * same cacheline
16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 struct list_head entry;
18 unsigned long expires;
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -080019 struct tvec_base *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 void (*function)(unsigned long);
22 unsigned long data;
23
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -080024 int slack;
25
Ingo Molnar82f67cd2007-02-16 01:28:13 -080026#ifdef CONFIG_TIMER_STATS
Richard Kennedyd0959022010-10-20 15:57:30 -070027 int start_pid;
Ingo Molnar82f67cd2007-02-16 01:28:13 -080028 void *start_site;
29 char start_comm[16];
Ingo Molnar82f67cd2007-02-16 01:28:13 -080030#endif
Johannes Berg6f2b9b92009-01-29 16:03:20 +010031#ifdef CONFIG_LOCKDEP
32 struct lockdep_map lockdep_map;
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
Pavel Macheka6fa8e52008-01-30 13:30:00 +010036extern struct tvec_base boot_tvec_bases;
Oleg Nesterov55c888d2005-06-23 00:08:56 -070037
Johannes Berg6f2b9b92009-01-29 16:03:20 +010038#ifdef CONFIG_LOCKDEP
39/*
40 * NB: because we have to copy the lockdep_map, setting the lockdep_map key
41 * (second argument) here is required, otherwise it could be initialised to
42 * the copy of the lockdep_map later! We use the pointer to and the string
43 * "<file>:<line>" as the key resp. the name of the lockdep_map.
44 */
45#define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn) \
46 .lockdep_map = STATIC_LOCKDEP_MAP_INIT(_kn, &_kn),
47#else
48#define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)
49#endif
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define TIMER_INITIALIZER(_function, _expires, _data) { \
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -070052 .entry = { .prev = TIMER_ENTRY_STATIC }, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .function = (_function), \
54 .expires = (_expires), \
55 .data = (_data), \
Oleg Nesterov3691c512006-03-31 02:30:30 -080056 .base = &boot_tvec_bases, \
Changli Gaoaaabe312010-10-20 15:57:30 -070057 .slack = -1, \
Johannes Berg6f2b9b92009-01-29 16:03:20 +010058 __TIMER_LOCKDEP_MAP_INITIALIZER( \
59 __FILE__ ":" __stringify(__LINE__)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
61
Ingo Molnar8d06afa2005-09-09 13:10:40 -070062#define DEFINE_TIMER(_name, _function, _expires, _data) \
63 struct timer_list _name = \
64 TIMER_INITIALIZER(_function, _expires, _data)
65
Johannes Berg6f2b9b92009-01-29 16:03:20 +010066void init_timer_key(struct timer_list *timer,
67 const char *name,
68 struct lock_class_key *key);
69void init_timer_deferrable_key(struct timer_list *timer,
70 const char *name,
71 struct lock_class_key *key);
72
73#ifdef CONFIG_LOCKDEP
74#define init_timer(timer) \
75 do { \
76 static struct lock_class_key __key; \
77 init_timer_key((timer), #timer, &__key); \
78 } while (0)
79
80#define init_timer_deferrable(timer) \
81 do { \
82 static struct lock_class_key __key; \
83 init_timer_deferrable_key((timer), #timer, &__key); \
84 } while (0)
85
86#define init_timer_on_stack(timer) \
87 do { \
88 static struct lock_class_key __key; \
89 init_timer_on_stack_key((timer), #timer, &__key); \
90 } while (0)
91
92#define setup_timer(timer, fn, data) \
93 do { \
94 static struct lock_class_key __key; \
95 setup_timer_key((timer), #timer, &__key, (fn), (data));\
96 } while (0)
97
98#define setup_timer_on_stack(timer, fn, data) \
99 do { \
100 static struct lock_class_key __key; \
101 setup_timer_on_stack_key((timer), #timer, &__key, \
102 (fn), (data)); \
103 } while (0)
Jesse Barnes8cadd2832010-05-10 14:26:20 -0700104#define setup_deferrable_timer_on_stack(timer, fn, data) \
105 do { \
106 static struct lock_class_key __key; \
107 setup_deferrable_timer_on_stack_key((timer), #timer, \
108 &__key, (fn), \
109 (data)); \
110 } while (0)
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100111#else
112#define init_timer(timer)\
113 init_timer_key((timer), NULL, NULL)
114#define init_timer_deferrable(timer)\
115 init_timer_deferrable_key((timer), NULL, NULL)
116#define init_timer_on_stack(timer)\
117 init_timer_on_stack_key((timer), NULL, NULL)
118#define setup_timer(timer, fn, data)\
119 setup_timer_key((timer), NULL, NULL, (fn), (data))
120#define setup_timer_on_stack(timer, fn, data)\
121 setup_timer_on_stack_key((timer), NULL, NULL, (fn), (data))
Jesse Barnes8cadd2832010-05-10 14:26:20 -0700122#define setup_deferrable_timer_on_stack(timer, fn, data)\
123 setup_deferrable_timer_on_stack_key((timer), NULL, NULL, (fn), (data))
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100124#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700126#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100127extern void init_timer_on_stack_key(struct timer_list *timer,
128 const char *name,
129 struct lock_class_key *key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700130extern void destroy_timer_on_stack(struct timer_list *timer);
131#else
132static inline void destroy_timer_on_stack(struct timer_list *timer) { }
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100133static inline void init_timer_on_stack_key(struct timer_list *timer,
134 const char *name,
135 struct lock_class_key *key)
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700136{
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100137 init_timer_key(timer, name, key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700138}
139#endif
140
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100141static inline void setup_timer_key(struct timer_list * timer,
142 const char *name,
143 struct lock_class_key *key,
Oleg Nesterova8db2db2005-10-30 15:01:38 -0800144 void (*function)(unsigned long),
145 unsigned long data)
146{
147 timer->function = function;
148 timer->data = data;
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100149 init_timer_key(timer, name, key);
Oleg Nesterova8db2db2005-10-30 15:01:38 -0800150}
151
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100152static inline void setup_timer_on_stack_key(struct timer_list *timer,
153 const char *name,
154 struct lock_class_key *key,
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700155 void (*function)(unsigned long),
156 unsigned long data)
157{
158 timer->function = function;
159 timer->data = data;
Johannes Berg6f2b9b92009-01-29 16:03:20 +0100160 init_timer_on_stack_key(timer, name, key);
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -0700161}
162
Jesse Barnes8cadd2832010-05-10 14:26:20 -0700163extern void setup_deferrable_timer_on_stack_key(struct timer_list *timer,
164 const char *name,
165 struct lock_class_key *key,
166 void (*function)(unsigned long),
167 unsigned long data);
168
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800169/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * timer_pending - is a timer pending?
171 * @timer: the timer in question
172 *
173 * timer_pending will tell whether a given timer is currently pending,
174 * or not. Callers must ensure serialization wrt. other operations done
175 * to this timer, eg. interrupt contexts, or other CPUs on SMP.
176 *
177 * return value: 1 if the timer is pending, 0 if not.
178 */
179static inline int timer_pending(const struct timer_list * timer)
180{
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700181 return timer->entry.next != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184extern void add_timer_on(struct timer_list *timer, int cpu);
185extern int del_timer(struct timer_list * timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186extern int mod_timer(struct timer_list *timer, unsigned long expires);
Ingo Molnar74019222009-02-18 12:23:29 +0100187extern int mod_timer_pending(struct timer_list *timer, unsigned long expires);
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530188extern int mod_timer_pinned(struct timer_list *timer, unsigned long expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Arjan van de Ven3bbb9ec2010-03-11 14:04:36 -0800190extern void set_timer_slack(struct timer_list *time, int slack_hz);
191
Arun R Bharadwaj597d0272009-04-16 12:13:26 +0530192#define TIMER_NOT_PINNED 0
193#define TIMER_PINNED 1
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800194/*
Thomas Gleixnereaad0842007-05-29 23:47:39 +0200195 * The jiffies value which is added to now, when there is no timer
196 * in the timer wheel:
197 */
198#define NEXT_TIMER_MAX_DELTA ((1UL << 30) - 1)
199
200/*
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800201 * Return when the next timer-wheel timeout occurs (in absolute jiffies),
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800202 * locks the timer base and does the comparison against the given
203 * jiffie.
204 */
205extern unsigned long get_next_timer_interrupt(unsigned long now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800207/*
208 * Timer-statistics info:
209 */
210#ifdef CONFIG_TIMER_STATS
211
Heiko Carstens507e1232009-06-23 17:38:15 +0200212extern int timer_stats_active;
213
Venki Pallipadic5c061b82007-07-15 23:40:30 -0700214#define TIMER_STATS_FLAG_DEFERRABLE 0x1
215
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800216extern void init_timer_stats(void);
217
218extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
Venki Pallipadic5c061b82007-07-15 23:40:30 -0700219 void *timerf, char *comm,
220 unsigned int timer_flag);
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800221
222extern void __timer_stats_timer_set_start_info(struct timer_list *timer,
223 void *addr);
224
225static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
226{
Heiko Carstens507e1232009-06-23 17:38:15 +0200227 if (likely(!timer_stats_active))
228 return;
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800229 __timer_stats_timer_set_start_info(timer, __builtin_return_address(0));
230}
231
232static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
233{
234 timer->start_site = NULL;
235}
236#else
237static inline void init_timer_stats(void)
238{
239}
240
Ingo Molnar82f67cd2007-02-16 01:28:13 -0800241static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
242{
243}
244
245static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
246{
247}
248#endif
249
Ingo Molnar74019222009-02-18 12:23:29 +0100250extern void add_timer(struct timer_list *timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252#ifdef CONFIG_SMP
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700253 extern int try_to_del_timer_sync(struct timer_list *timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 extern int del_timer_sync(struct timer_list *timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#else
Oleg Nesterovfd450b72005-06-23 00:08:59 -0700256# define try_to_del_timer_sync(t) del_timer(t)
257# define del_timer_sync(t) del_timer(t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258#endif
259
Oleg Nesterov55c888d2005-06-23 00:08:56 -0700260#define del_singleshot_timer_sync(t) del_timer_sync(t)
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262extern void init_timers(void);
263extern void run_local_timers(void);
Roman Zippel05cfb612006-03-26 01:38:12 -0800264struct hrtimer;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800265extern enum hrtimer_restart it_real_fn(struct hrtimer *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Arjan van de Ven4c36a5d2006-12-10 02:21:24 -0800267unsigned long __round_jiffies(unsigned long j, int cpu);
268unsigned long __round_jiffies_relative(unsigned long j, int cpu);
269unsigned long round_jiffies(unsigned long j);
270unsigned long round_jiffies_relative(unsigned long j);
271
Alan Stern9c133c42008-11-06 08:42:48 +0100272unsigned long __round_jiffies_up(unsigned long j, int cpu);
273unsigned long __round_jiffies_up_relative(unsigned long j, int cpu);
274unsigned long round_jiffies_up(unsigned long j);
275unsigned long round_jiffies_up_relative(unsigned long j);
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277#endif