[PATCH] hrtimers: cleanup locking
[safe/jmp/linux-2.6] / include / linux / hrtimer.h
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
18 #include <linux/rbtree.h>
19 #include <linux/ktime.h>
20 #include <linux/init.h>
21 #include <linux/list.h>
22 #include <linux/wait.h>
23
24 struct hrtimer_clock_base;
25 struct hrtimer_cpu_base;
26
27 /*
28  * Mode arguments of xxx_hrtimer functions:
29  */
30 enum hrtimer_mode {
31         HRTIMER_MODE_ABS,       /* Time value is absolute */
32         HRTIMER_MODE_REL,       /* Time value is relative to now */
33 };
34
35 /*
36  * Return values for the callback function
37  */
38 enum hrtimer_restart {
39         HRTIMER_NORESTART,      /* Timer is not restarted */
40         HRTIMER_RESTART,        /* Timer must be restarted */
41 };
42
43 /**
44  * struct hrtimer - the basic hrtimer structure
45  * @node:       red black tree node for time ordered insertion
46  * @expires:    the absolute expiry time in the hrtimers internal
47  *              representation. The time is related to the clock on
48  *              which the timer is based.
49  * @function:   timer expiry callback function
50  * @base:       pointer to the timer base (per cpu and per clock)
51  *
52  * The hrtimer structure must be initialized by init_hrtimer_#CLOCKTYPE()
53  */
54 struct hrtimer {
55         struct rb_node                  node;
56         ktime_t                         expires;
57         enum hrtimer_restart            (*function)(struct hrtimer *);
58         struct hrtimer_clock_base       *base;
59 };
60
61 /**
62  * struct hrtimer_sleeper - simple sleeper structure
63  * @timer:      embedded timer structure
64  * @task:       task to wake up
65  *
66  * task is set to NULL, when the timer expires.
67  */
68 struct hrtimer_sleeper {
69         struct hrtimer timer;
70         struct task_struct *task;
71 };
72
73 /**
74  * struct hrtimer_base - the timer base for a specific clock
75  * @index:              clock type index for per_cpu support when moving a
76  *                      timer to a base on another cpu.
77  * @active:             red black tree root node for the active timers
78  * @first:              pointer to the timer node which expires first
79  * @resolution:         the resolution of the clock, in nanoseconds
80  * @get_time:           function to retrieve the current time of the clock
81  * @get_softirq_time:   function to retrieve the current time from the softirq
82  * @softirq_time:       the time when running the hrtimer queue in the softirq
83  */
84 struct hrtimer_clock_base {
85         struct hrtimer_cpu_base *cpu_base;
86         clockid_t               index;
87         struct rb_root          active;
88         struct rb_node          *first;
89         ktime_t                 resolution;
90         ktime_t                 (*get_time)(void);
91         ktime_t                 (*get_softirq_time)(void);
92         ktime_t                 softirq_time;
93 };
94
95 #define HRTIMER_MAX_CLOCK_BASES 2
96
97 /*
98  * struct hrtimer_cpu_base - the per cpu clock bases
99  * @lock:               lock protecting the base and associated clock bases
100  *                      and timers
101  * @lock_key:           the lock_class_key for use with lockdep
102  * @clock_base:         array of clock bases for this cpu
103  * @curr_timer:         the timer which is executing a callback right now
104  */
105 struct hrtimer_cpu_base {
106         spinlock_t                      lock;
107         struct lock_class_key           lock_key;
108         struct hrtimer_clock_base       clock_base[HRTIMER_MAX_CLOCK_BASES];
109         struct hrtimer                  *curr_timer;
110 };
111
112 /*
113  * clock_was_set() is a NOP for non- high-resolution systems. The
114  * time-sorted order guarantees that a timer does not expire early and
115  * is expired in the next softirq when the clock was advanced.
116  */
117 #define clock_was_set()         do { } while (0)
118
119 /* Exported timer functions: */
120
121 /* Initialize timers: */
122 extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
123                          enum hrtimer_mode mode);
124
125 /* Basic timer operations: */
126 extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
127                          const enum hrtimer_mode mode);
128 extern int hrtimer_cancel(struct hrtimer *timer);
129 extern int hrtimer_try_to_cancel(struct hrtimer *timer);
130
131 static inline int hrtimer_restart(struct hrtimer *timer)
132 {
133         return hrtimer_start(timer, timer->expires, HRTIMER_MODE_ABS);
134 }
135
136 /* Query timers: */
137 extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
138 extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
139
140 #ifdef CONFIG_NO_IDLE_HZ
141 extern ktime_t hrtimer_get_next_event(void);
142 #endif
143
144 static inline int hrtimer_active(const struct hrtimer *timer)
145 {
146         return rb_parent(&timer->node) != &timer->node;
147 }
148
149 /* Forward a hrtimer so it expires after now: */
150 extern unsigned long
151 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
152
153 /* Precise sleep: */
154 extern long hrtimer_nanosleep(struct timespec *rqtp,
155                               struct timespec __user *rmtp,
156                               const enum hrtimer_mode mode,
157                               const clockid_t clockid);
158 extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
159
160 extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
161                                  struct task_struct *tsk);
162
163 /* Soft interrupt function to run the hrtimer queues: */
164 extern void hrtimer_run_queues(void);
165
166 /* Resume notification */
167 void hrtimer_notify_resume(void);
168
169 /* Bootup initialization: */
170 extern void __init hrtimers_init(void);
171
172 #endif