]> git.ozlabs.org Git - ccan/blob - ccan/timer/timer.h
65f849b3dccc22743ed958a6d36ddb04370c2b65
[ccan] / ccan / timer / timer.h
1 /* LGPL (v2.1 or any later version) - see LICENSE file for details */
2 #ifndef CCAN_TIMER_H
3 #define CCAN_TIMER_H
4 #include <ccan/time/time.h>
5 #include <ccan/list/list.h>
6 #include <stdint.h>
7
8 #ifndef TIMER_GRANULARITY
9 /* We divide all nsec values by 1000, reducing it to usec granularity. */
10 #define TIMER_GRANULARITY 1000
11 #endif
12
13 #ifndef TIMER_LEVEL_BITS
14 /* This gives 32 pointers per level, up to 13 levels deep. */
15 #define TIMER_LEVEL_BITS 5
16 #endif
17
18 struct timers;
19 struct timer;
20
21 /**
22  * timers_init - initialize a timers struct.
23  * @timers: the struct timers
24  * @start: the minimum time which will ever be added.
25  *
26  * This sets up a timers struct: any timers added before @start will be
27  * set to expire immediately.
28  */
29 void timers_init(struct timers *timers, struct timespec start);
30
31 /**
32  * timers_cleanup - free allocations within timers struct.
33  * @timers: the struct timers
34  *
35  * This frees any timer layers allocated during use.
36  */
37 void timers_cleanup(struct timers *timers);
38
39 /**
40  * timer_add - insert a timer.
41  * @timers: the struct timers
42  * @timer: the (uninitialized) timer to add
43  * @when: when @timer expires.
44  *
45  * This efficiently adds @timer to @timers, to expire @when (rounded to
46  * TIMER_GRANULARITY nanoseconds).
47  */
48 void timer_add(struct timers *timers, struct timer *timer,
49                struct timespec when);
50
51 /**
52  * timer_del - remove an unexpired timer.
53  * @timers: the struct timers
54  * @timer: the timer previously added with timer_add()
55  *
56  * This efficiently removes @timer from @timers.
57  */
58 void timer_del(struct timers *timers, struct timer *timer);
59
60 /**
61  * timer_earliest - find out the first time when a timer will expire
62  * @timers: the struct timers
63  * @first: the time, only set if there is a timer.
64  *
65  * This returns false, and doesn't alter @first if there are no
66  * timers.  Otherwise, it sets @first to the expiry time of the first
67  * timer (rounded to TIMER_GRANULARITY nanoseconds), and returns true.
68  */
69 bool timer_earliest(struct timers *timers, struct timespec *first);
70
71 /**
72  * timer_expire - update timers structure and remove expired timers.
73  * @timers: the struct timers
74  * @expire: the current time
75  * @list: the list for expired timers.
76  *
77  * @list will be initialized to the empty list, then all timers added
78  * with a @when arg less than or equal to @expire will be added to it in
79  * expiry order (within TIMER_GRANULARITY nanosecond precision).
80  *
81  * After this, @expire is considered the current time, and adding any
82  * timers with @when before this value will be silently changed to
83  * adding them with immediate expiration.
84  *
85  * You should not move @expire backwards, though it need not move
86  * forwards.
87  */
88 void timers_expire(struct timers *timers,
89                    struct timespec expire,
90                    struct list_head *list);
91
92 /**
93  * timers_check - check timer structure for consistency
94  * @t: the struct timers
95  * @abortstr: the location to print on aborting, or NULL.
96  *
97  * Because timers have redundant information, consistency checking can
98  * be done on the tree.  This is useful as a debugging check.  If
99  * @abortstr is non-NULL, that will be printed in a diagnostic if the
100  * timers structure is inconsistent, and the function will abort.
101  *
102  * Returns the timers struct if it is consistent, NULL if not (it can
103  * never return NULL if @abortstr is set).
104  */
105 struct timers *timers_check(const struct timers *t, const char *abortstr);
106
107 #ifdef CCAN_TIMER_DEBUG
108 #include <stdio.h>
109
110 /**
111  * timers_dump - dump the timers datastructure (for debugging it)
112  * @t: the struct timers
113  * @fp: the FILE to dump to (stderr if @fp is NULL)
114  */
115 void timers_dump(const struct timers *timers, FILE *fp);
116 #endif
117
118 /**
119  * struct timers - structure to hold a set of timers.
120  *
121  * Initialized using timers_init, the levels of the timer are
122  * allocated as necessary, using malloc.
123  *
124  * See Also:
125  *      timers_init(), timers_cleanup()
126  */
127 struct timers {
128         /* Far in the future. */
129         struct list_head far;
130         uint64_t base;
131         uint64_t first;
132
133         struct timer_level *level[(64 + TIMER_LEVEL_BITS-1) / TIMER_LEVEL_BITS];
134 };
135
136 /**
137  * struct timer - a single timer.
138  *
139  * Set up by timer_add(), this is usually contained within an
140  * application-specific structure.
141  *
142  * See Also:
143  *      ccan/container_of, timer_add(), timer_del()
144  */
145 struct timer {
146         struct list_node list;
147         uint64_t time;
148 };
149 #endif /* CCAN_TIMER_H */