blob: 0e8f4762f6f8e8cc52cbedb920fa066523de0517 [file] [log] [blame]
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001/*
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 */
27enum hrtimer_mode {
28 HRTIMER_ABS, /* Time value is absolute */
29 HRTIMER_REL, /* Time value is relative to now */
30};
31
32enum hrtimer_restart {
33 HRTIMER_NORESTART,
34 HRTIMER_RESTART,
35};
36
37/*
38 * Timer states:
39 */
40enum hrtimer_state {
Roman Zippel432569b2006-03-26 01:38:08 -080041 HRTIMER_INACTIVE, /* Timer is inactive */
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080042 HRTIMER_PENDING, /* Timer is pending */
43};
44
45struct hrtimer_base;
46
47/**
48 * struct hrtimer - the basic hrtimer structure
49 *
50 * @node: red black tree node for time ordered insertion
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080051 * @expires: the absolute expiry time in the hrtimers internal
52 * representation. The time is related to the clock on
53 * which the timer is based.
54 * @state: state of the timer
55 * @function: timer expiry callback function
56 * @data: argument for the callback function
57 * @base: pointer to the timer base (per cpu and per clock)
58 *
59 * The hrtimer structure must be initialized by init_hrtimer_#CLOCKTYPE()
60 */
61struct hrtimer {
62 struct rb_node node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080063 ktime_t expires;
64 enum hrtimer_state state;
65 int (*function)(void *);
66 void *data;
67 struct hrtimer_base *base;
68};
69
70/**
71 * struct hrtimer_base - the timer base for a specific clock
72 *
Thomas Gleixner92127c72006-03-26 01:38:05 -080073 * @index: clock type index for per_cpu support when moving a timer
74 * to a base on another cpu.
75 * @lock: lock protecting the base and associated timers
76 * @active: red black tree root node for the active timers
77 * @first: pointer to the timer node which expires first
78 * @resolution: the resolution of the clock, in nanoseconds
79 * @get_time: function to retrieve the current time of the clock
80 * @get_sofirq_time: function to retrieve the current time from the softirq
81 * @curr_timer: the timer which is executing a callback right now
82 * @softirq_time: the time when running the hrtimer queue in the softirq
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080083 */
84struct hrtimer_base {
85 clockid_t index;
86 spinlock_t lock;
87 struct rb_root active;
Thomas Gleixner288867e2006-01-12 11:25:54 +010088 struct rb_node *first;
Thomas Gleixnere2787632006-01-12 11:36:14 +010089 ktime_t resolution;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080090 ktime_t (*get_time)(void);
Thomas Gleixner92127c72006-03-26 01:38:05 -080091 ktime_t (*get_softirq_time)(void);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080092 struct hrtimer *curr_timer;
Thomas Gleixner92127c72006-03-26 01:38:05 -080093 ktime_t softirq_time;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080094};
95
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -080096/*
97 * clock_was_set() is a NOP for non- high-resolution systems. The
98 * time-sorted order guarantees that a timer does not expire early and
99 * is expired in the next softirq when the clock was advanced.
100 */
101#define clock_was_set() do { } while (0)
102
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800103/* Exported timer functions: */
104
105/* Initialize timers: */
George Anzinger79786722006-02-01 03:05:11 -0800106extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
107 enum hrtimer_mode mode);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800108
109/* Basic timer operations: */
110extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
111 const enum hrtimer_mode mode);
112extern int hrtimer_cancel(struct hrtimer *timer);
113extern int hrtimer_try_to_cancel(struct hrtimer *timer);
114
115#define hrtimer_restart(timer) hrtimer_start((timer), (timer)->expires, HRTIMER_ABS)
116
117/* Query timers: */
118extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
119extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
120
Tony Lindgren69239742006-03-06 15:42:45 -0800121#ifdef CONFIG_NO_IDLE_HZ
122extern ktime_t hrtimer_get_next_event(void);
123#endif
124
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800125static inline int hrtimer_active(const struct hrtimer *timer)
126{
127 return timer->state == HRTIMER_PENDING;
128}
129
130/* Forward a hrtimer so it expires after now: */
Roman Zippel44f21472006-03-26 01:38:06 -0800131extern unsigned long
132hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800133
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800134/* Precise sleep: */
135extern long hrtimer_nanosleep(struct timespec *rqtp,
136 struct timespec __user *rmtp,
137 const enum hrtimer_mode mode,
138 const clockid_t clockid);
139
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800140/* Soft interrupt function to run the hrtimer queues: */
141extern void hrtimer_run_queues(void);
142
143/* Bootup initialization: */
144extern void __init hrtimers_init(void);
145
146#endif