aboutsummaryrefslogtreecommitdiff
path: root/include/apos/timer.h
blob: 85cd5bc0ae29814baa92d1ae5c44dca660fa37cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef APOS_TIMER_H
#define APOS_TIMER_H

/**
 * @file timer.h
 * Timer handling.
 */

#include <apos/types.h>

/* GCC will compile uint64_t even on 32bit platforms, just with some runtime
 * overhead, should be fine. This will allow us to have a reasonable time range
 * even with nanosecond clocks. (138 years with ~4.2 Hz clock) */
typedef uint64_t ticks_t;
/* whichever time unit we're dealing with */
typedef size_t tunit_t;

struct timer {
	id_t tid;
	id_t cid;

	ticks_t ticks;
};

/**
 * Initialize timers.
 *
 * @param fdt Pointer to global FDT
 */
void init_timer(const void *fdt);

/**
 * Set up timer interrupt ticks from now.
 *
 * @param tid Thread id for callback.
 * @param ticks Ticks from \ref current_ticks().
 * @return Id of created timer.
 */
id_t new_rel_timer(id_t tid, ticks_t ticks);

/**
 * Set up timer interrupt at ticks.
 *
 * @param tid Thread id for callback.
 * @param ticks Ticks from \ref current_ticks().
 * @return Id of created timer.
 */
id_t new_abs_timer(id_t tid, ticks_t ticks);

struct timer *newest_timer();
struct timer *find_timer(id_t cid);
void remove_timer(struct timer *);

ticks_t nsecs_to_ticks(tunit_t nsecs);

static inline ticks_t usecs_to_ticks(tunit_t usecs)
{
	return nsecs_to_ticks(usecs * 1000);
}

static inline ticks_t msecs_to_ticks(tunit_t msecs)
{
	return usecs_to_ticks(msecs * 1000);
}

/* TODO: likely not a problem on 64bit systems, not sure how to handle situation on
 * 32bit */
static inline ticks_t secs_to_ticks(tunit_t secs)
{
	return msecs_to_ticks(secs * 1000);
}

#endif /* APOS_TIMER_H */