Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 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 | /* |
| 25 | * Mode arguments of xxx_hrtimer functions: |
| 26 | */ |
| 27 | enum hrtimer_mode { |
| 28 | HRTIMER_ABS, /* Time value is absolute */ |
| 29 | HRTIMER_REL, /* Time value is relative to now */ |
| 30 | }; |
| 31 | |
| 32 | enum hrtimer_restart { |
| 33 | HRTIMER_NORESTART, |
| 34 | HRTIMER_RESTART, |
| 35 | }; |
| 36 | |
| 37 | /* |
| 38 | * Timer states: |
| 39 | */ |
| 40 | enum hrtimer_state { |
| 41 | HRTIMER_INACTIVE, /* Timer is inactive */ |
| 42 | HRTIMER_EXPIRED, /* Timer is expired */ |
akpm@osdl.org | ff60a5d | 2006-02-01 03:05:10 -0800 | [diff] [blame] | 43 | HRTIMER_RUNNING, /* Timer is running the callback function */ |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 44 | HRTIMER_PENDING, /* Timer is pending */ |
| 45 | }; |
| 46 | |
| 47 | struct hrtimer_base; |
| 48 | |
| 49 | /** |
| 50 | * struct hrtimer - the basic hrtimer structure |
| 51 | * |
| 52 | * @node: red black tree node for time ordered insertion |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 53 | * @expires: the absolute expiry time in the hrtimers internal |
| 54 | * representation. The time is related to the clock on |
| 55 | * which the timer is based. |
| 56 | * @state: state of the timer |
| 57 | * @function: timer expiry callback function |
| 58 | * @data: argument for the callback function |
| 59 | * @base: pointer to the timer base (per cpu and per clock) |
| 60 | * |
| 61 | * The hrtimer structure must be initialized by init_hrtimer_#CLOCKTYPE() |
| 62 | */ |
| 63 | struct hrtimer { |
| 64 | struct rb_node node; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 65 | ktime_t expires; |
| 66 | enum hrtimer_state state; |
| 67 | int (*function)(void *); |
| 68 | void *data; |
| 69 | struct hrtimer_base *base; |
| 70 | }; |
| 71 | |
| 72 | /** |
| 73 | * struct hrtimer_base - the timer base for a specific clock |
| 74 | * |
| 75 | * @index: clock type index for per_cpu support when moving a timer |
| 76 | * to a base on another cpu. |
| 77 | * @lock: lock protecting the base and associated timers |
| 78 | * @active: red black tree root node for the active timers |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 79 | * @first: pointer to the timer node which expires first |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 80 | * @resolution: the resolution of the clock, in nanoseconds |
| 81 | * @get_time: function to retrieve the current time of the clock |
| 82 | * @curr_timer: the timer which is executing a callback right now |
| 83 | */ |
| 84 | struct hrtimer_base { |
| 85 | clockid_t index; |
| 86 | spinlock_t lock; |
| 87 | struct rb_root active; |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 88 | struct rb_node *first; |
Thomas Gleixner | e278763 | 2006-01-12 11:36:14 +0100 | [diff] [blame] | 89 | ktime_t resolution; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 90 | ktime_t (*get_time)(void); |
| 91 | struct hrtimer *curr_timer; |
| 92 | }; |
| 93 | |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 94 | /* |
| 95 | * clock_was_set() is a NOP for non- high-resolution systems. The |
| 96 | * time-sorted order guarantees that a timer does not expire early and |
| 97 | * is expired in the next softirq when the clock was advanced. |
| 98 | */ |
| 99 | #define clock_was_set() do { } while (0) |
| 100 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 101 | /* Exported timer functions: */ |
| 102 | |
| 103 | /* Initialize timers: */ |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame^] | 104 | extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock, |
| 105 | enum hrtimer_mode mode); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 106 | |
| 107 | /* Basic timer operations: */ |
| 108 | extern int hrtimer_start(struct hrtimer *timer, ktime_t tim, |
| 109 | const enum hrtimer_mode mode); |
| 110 | extern int hrtimer_cancel(struct hrtimer *timer); |
| 111 | extern int hrtimer_try_to_cancel(struct hrtimer *timer); |
| 112 | |
| 113 | #define hrtimer_restart(timer) hrtimer_start((timer), (timer)->expires, HRTIMER_ABS) |
| 114 | |
| 115 | /* Query timers: */ |
| 116 | extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer); |
| 117 | extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp); |
| 118 | |
| 119 | static inline int hrtimer_active(const struct hrtimer *timer) |
| 120 | { |
| 121 | return timer->state == HRTIMER_PENDING; |
| 122 | } |
| 123 | |
| 124 | /* Forward a hrtimer so it expires after now: */ |
Thomas Gleixner | c9db4fa | 2006-01-12 11:47:34 +0100 | [diff] [blame] | 125 | extern unsigned long hrtimer_forward(struct hrtimer *timer, ktime_t interval); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 126 | |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 127 | /* Precise sleep: */ |
| 128 | extern long hrtimer_nanosleep(struct timespec *rqtp, |
| 129 | struct timespec __user *rmtp, |
| 130 | const enum hrtimer_mode mode, |
| 131 | const clockid_t clockid); |
| 132 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 133 | /* Soft interrupt function to run the hrtimer queues: */ |
| 134 | extern void hrtimer_run_queues(void); |
| 135 | |
| 136 | /* Bootup initialization: */ |
| 137 | extern void __init hrtimers_init(void); |
| 138 | |
| 139 | #endif |