Skip to content

Commit c0a3132

Browse files
KAGA-KOKOLinus Torvalds
authored andcommitted
[PATCH] hrtimer: hrtimer core code
hrtimer subsystem core. It is initialized at bootup and expired by the timer interrupt, but is otherwise not utilized by any other subsystem yet. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 97fc79f commit c0a3132

6 files changed

Lines changed: 828 additions & 1 deletion

File tree

‎include/linux/hrtimer.h‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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 */
43+
HRTIMER_PENDING, /* Timer is pending */
44+
};
45+
46+
struct hrtimer_base;
47+
48+
/**
49+
* struct hrtimer - the basic hrtimer structure
50+
*
51+
* @node: red black tree node for time ordered insertion
52+
* @list: list head for easier access to the time ordered list,
53+
* without walking the red black tree.
54+
* @expires: the absolute expiry time in the hrtimers internal
55+
* representation. The time is related to the clock on
56+
* which the timer is based.
57+
* @state: state of the timer
58+
* @function: timer expiry callback function
59+
* @data: argument for the callback function
60+
* @base: pointer to the timer base (per cpu and per clock)
61+
*
62+
* The hrtimer structure must be initialized by init_hrtimer_#CLOCKTYPE()
63+
*/
64+
struct hrtimer {
65+
struct rb_node node;
66+
struct list_head list;
67+
ktime_t expires;
68+
enum hrtimer_state state;
69+
int (*function)(void *);
70+
void *data;
71+
struct hrtimer_base *base;
72+
};
73+
74+
/**
75+
* struct hrtimer_base - the timer base for a specific clock
76+
*
77+
* @index: clock type index for per_cpu support when moving a timer
78+
* to a base on another cpu.
79+
* @lock: lock protecting the base and associated timers
80+
* @active: red black tree root node for the active timers
81+
* @pending: list of pending timers for simple time ordered access
82+
* @resolution: the resolution of the clock, in nanoseconds
83+
* @get_time: function to retrieve the current time of the clock
84+
* @curr_timer: the timer which is executing a callback right now
85+
*/
86+
struct hrtimer_base {
87+
clockid_t index;
88+
spinlock_t lock;
89+
struct rb_root active;
90+
struct list_head pending;
91+
unsigned long resolution;
92+
ktime_t (*get_time)(void);
93+
struct hrtimer *curr_timer;
94+
};
95+
96+
/* Exported timer functions: */
97+
98+
/* Initialize timers: */
99+
extern void hrtimer_init(struct hrtimer *timer, const clockid_t which_clock);
100+
extern void hrtimer_rebase(struct hrtimer *timer, const clockid_t which_clock);
101+
102+
103+
/* Basic timer operations: */
104+
extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
105+
const enum hrtimer_mode mode);
106+
extern int hrtimer_cancel(struct hrtimer *timer);
107+
extern int hrtimer_try_to_cancel(struct hrtimer *timer);
108+
109+
#define hrtimer_restart(timer) hrtimer_start((timer), (timer)->expires, HRTIMER_ABS)
110+
111+
/* Query timers: */
112+
extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
113+
extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
114+
115+
static inline int hrtimer_active(const struct hrtimer *timer)
116+
{
117+
return timer->state == HRTIMER_PENDING;
118+
}
119+
120+
/* Forward a hrtimer so it expires after now: */
121+
extern unsigned long hrtimer_forward(struct hrtimer *timer,
122+
const ktime_t interval);
123+
124+
/* Soft interrupt function to run the hrtimer queues: */
125+
extern void hrtimer_run_queues(void);
126+
127+
/* Bootup initialization: */
128+
extern void __init hrtimers_init(void);
129+
130+
#endif

‎include/linux/ktime.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,19 @@ static inline u64 ktime_to_ns(const ktime_t kt)
266266

267267
#endif
268268

269+
/*
270+
* The resolution of the clocks. The resolution value is returned in
271+
* the clock_getres() system call to give application programmers an
272+
* idea of the (in)accuracy of timers. Timer values are rounded up to
273+
* this resolution values.
274+
*/
275+
#define KTIME_REALTIME_RES (NSEC_PER_SEC/HZ)
276+
#define KTIME_MONOTONIC_RES (NSEC_PER_SEC/HZ)
277+
278+
/* Get the monotonic time in timespec format: */
279+
extern void ktime_get_ts(struct timespec *ts);
280+
281+
/* Get the real (wall-) time in timespec format: */
282+
#define ktime_get_real_ts(ts) getnstimeofday(ts)
283+
269284
#endif

‎init/main.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ asmlinkage void __init start_kernel(void)
485485
init_IRQ();
486486
pidhash_init();
487487
init_timers();
488+
hrtimers_init();
488489
softirq_init();
489490
time_init();
490491

‎kernel/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ obj-y = sched.o fork.o exec_domain.o panic.o printk.o profile.o \
77
sysctl.o capability.o ptrace.o timer.o user.o \
88
signal.o sys.o kmod.o workqueue.o pid.o \
99
rcupdate.o intermodule.o extable.o params.o posix-timers.o \
10-
kthread.o wait.o kfifo.o sys_ni.o posix-cpu-timers.o mutex.o
10+
kthread.o wait.o kfifo.o sys_ni.o posix-cpu-timers.o mutex.o \
11+
hrtimer.o
1112

1213
obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o
1314
obj-$(CONFIG_FUTEX) += futex.o

0 commit comments

Comments
 (0)