blob: e00fc4d3d74f2490301ef37fb0a233c72b140d9a [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
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080024struct hrtimer_clock_base;
25struct hrtimer_cpu_base;
26
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080027/*
28 * Mode arguments of xxx_hrtimer functions:
29 */
30enum hrtimer_mode {
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -080031 HRTIMER_MODE_ABS, /* Time value is absolute */
32 HRTIMER_MODE_REL, /* Time value is relative to now */
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080033};
34
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -080035/*
36 * Return values for the callback function
37 */
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080038enum hrtimer_restart {
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -080039 HRTIMER_NORESTART, /* Timer is not restarted */
40 HRTIMER_RESTART, /* Timer must be restarted */
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080041};
42
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080043/**
44 * struct hrtimer - the basic hrtimer structure
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080045 * @node: red black tree node for time ordered insertion
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080046 * @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.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080049 * @function: timer expiry callback function
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080050 * @base: pointer to the timer base (per cpu and per clock)
51 *
52 * The hrtimer structure must be initialized by init_hrtimer_#CLOCKTYPE()
53 */
54struct hrtimer {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080055 struct rb_node node;
56 ktime_t expires;
57 enum hrtimer_restart (*function)(struct hrtimer *);
58 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080059};
60
61/**
Thomas Gleixner00362e32006-03-31 02:31:17 -080062 * struct hrtimer_sleeper - simple sleeper structure
Thomas Gleixner00362e32006-03-31 02:31:17 -080063 * @timer: embedded timer structure
64 * @task: task to wake up
65 *
66 * task is set to NULL, when the timer expires.
67 */
68struct hrtimer_sleeper {
69 struct hrtimer timer;
70 struct task_struct *task;
71};
72
73/**
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080074 * struct hrtimer_base - the timer base for a specific clock
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080075 * @index: clock type index for per_cpu support when moving a
76 * timer to a base on another cpu.
Thomas Gleixner92127c72006-03-26 01:38:05 -080077 * @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
Martin Waitza5802902006-04-02 13:59:55 +020081 * @get_softirq_time: function to retrieve the current time from the softirq
Thomas Gleixner92127c72006-03-26 01:38:05 -080082 * @softirq_time: the time when running the hrtimer queue in the softirq
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080083 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080084struct hrtimer_clock_base {
85 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080086 clockid_t index;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080087 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 Gleixner92127c72006-03-26 01:38:05 -080092 ktime_t softirq_time;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080093};
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 */
105struct 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;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800110};
111
Thomas Gleixnerbecf8b52006-01-09 20:52:38 -0800112/*
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
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800119/* Exported timer functions: */
120
121/* Initialize timers: */
George Anzinger7978672c2006-02-01 03:05:11 -0800122extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
123 enum hrtimer_mode mode);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800124
125/* Basic timer operations: */
126extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
127 const enum hrtimer_mode mode);
128extern int hrtimer_cancel(struct hrtimer *timer);
129extern int hrtimer_try_to_cancel(struct hrtimer *timer);
130
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800131static inline int hrtimer_restart(struct hrtimer *timer)
132{
133 return hrtimer_start(timer, timer->expires, HRTIMER_MODE_ABS);
134}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800135
136/* Query timers: */
137extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
138extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
139
Tony Lindgren69239742006-03-06 15:42:45 -0800140#ifdef CONFIG_NO_IDLE_HZ
141extern ktime_t hrtimer_get_next_event(void);
142#endif
143
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800144static inline int hrtimer_active(const struct hrtimer *timer)
145{
David Woodhouseed198cb2006-04-22 02:38:50 +0100146 return rb_parent(&timer->node) != &timer->node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800147}
148
149/* Forward a hrtimer so it expires after now: */
Roman Zippel44f21472006-03-26 01:38:06 -0800150extern unsigned long
151hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800152
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800153/* Precise sleep: */
154extern long hrtimer_nanosleep(struct timespec *rqtp,
155 struct timespec __user *rmtp,
156 const enum hrtimer_mode mode,
157 const clockid_t clockid);
Toyo Abe1711ef32006-09-29 02:00:28 -0700158extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800159
Thomas Gleixner00362e32006-03-31 02:31:17 -0800160extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
161 struct task_struct *tsk);
162
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800163/* Soft interrupt function to run the hrtimer queues: */
164extern void hrtimer_run_queues(void);
165
John Stultz411187f2007-02-16 01:27:30 -0800166/* Resume notification */
167void hrtimer_notify_resume(void);
168
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800169/* Bootup initialization: */
170extern void __init hrtimers_init(void);
171
172#endif