xref: /linux-6.15/include/linux/hrtimer.h (revision ccea15f4)
1 /*
2  *  include/linux/hrtimer.h
3  *
4  *  hrtimers - High-resolution kernel timers
5  *
6  *   Copyright(C) 2005, Thomas Gleixner <[email protected]>
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 #define HRTIMER_INACTIVE	((void *)1UL)
38 
39 struct hrtimer_base;
40 
41 /**
42  * struct hrtimer - the basic hrtimer structure
43  *
44  * @node:	red black tree node for time ordered insertion
45  * @expires:	the absolute expiry time in the hrtimers internal
46  *		representation. The time is related to the clock on
47  *		which the timer is based.
48  * @function:	timer expiry callback function
49  * @base:	pointer to the timer base (per cpu and per clock)
50  *
51  * The hrtimer structure must be initialized by init_hrtimer_#CLOCKTYPE()
52  */
53 struct hrtimer {
54 	struct rb_node		node;
55 	ktime_t			expires;
56 	int			(*function)(struct hrtimer *);
57 	struct hrtimer_base	*base;
58 };
59 
60 /**
61  * struct hrtimer_sleeper - simple sleeper structure
62  *
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  *
76  * @index:		clock type index for per_cpu support when moving a timer
77  *			to a base on another cpu.
78  * @lock:		lock protecting the base and associated timers
79  * @active:		red black tree root node for the active timers
80  * @first:		pointer to the timer node which expires first
81  * @resolution:		the resolution of the clock, in nanoseconds
82  * @get_time:		function to retrieve the current time of the clock
83  * @get_softirq_time:	function to retrieve the current time from the softirq
84  * @curr_timer:		the timer which is executing a callback right now
85  * @softirq_time:	the time when running the hrtimer queue in the softirq
86  */
87 struct hrtimer_base {
88 	clockid_t		index;
89 	spinlock_t		lock;
90 	struct rb_root		active;
91 	struct rb_node		*first;
92 	ktime_t			resolution;
93 	ktime_t			(*get_time)(void);
94 	ktime_t			(*get_softirq_time)(void);
95 	struct hrtimer		*curr_timer;
96 	ktime_t			softirq_time;
97 };
98 
99 /*
100  * clock_was_set() is a NOP for non- high-resolution systems. The
101  * time-sorted order guarantees that a timer does not expire early and
102  * is expired in the next softirq when the clock was advanced.
103  */
104 #define clock_was_set()		do { } while (0)
105 
106 /* Exported timer functions: */
107 
108 /* Initialize timers: */
109 extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
110 			 enum hrtimer_mode mode);
111 
112 /* Basic timer operations: */
113 extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
114 			 const enum hrtimer_mode mode);
115 extern int hrtimer_cancel(struct hrtimer *timer);
116 extern int hrtimer_try_to_cancel(struct hrtimer *timer);
117 
118 #define hrtimer_restart(timer) hrtimer_start((timer), (timer)->expires, HRTIMER_ABS)
119 
120 /* Query timers: */
121 extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
122 extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
123 
124 #ifdef CONFIG_NO_IDLE_HZ
125 extern ktime_t hrtimer_get_next_event(void);
126 #endif
127 
128 static inline int hrtimer_active(const struct hrtimer *timer)
129 {
130 	return timer->node.rb_parent != HRTIMER_INACTIVE;
131 }
132 
133 /* Forward a hrtimer so it expires after now: */
134 extern unsigned long
135 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
136 
137 /* Precise sleep: */
138 extern long hrtimer_nanosleep(struct timespec *rqtp,
139 			      struct timespec __user *rmtp,
140 			      const enum hrtimer_mode mode,
141 			      const clockid_t clockid);
142 
143 extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
144 				 struct task_struct *tsk);
145 
146 /* Soft interrupt function to run the hrtimer queues: */
147 extern void hrtimer_run_queues(void);
148 
149 /* Bootup initialization: */
150 extern void __init hrtimers_init(void);
151 
152 #endif
153