]> git.ozlabs.org Git - ccan/blob - ccan/timer/timer.h
7a9fb07518a880c3097c5e99d388bddc14d82346
[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  * Example:
30  *      struct timers timeouts;
31  *
32  *      timers_init(&timeouts, time_now());
33  */
34 void timers_init(struct timers *timers, struct timeabs start);
35
36 /**
37  * timers_cleanup - free allocations within timers struct.
38  * @timers: the struct timers
39  *
40  * This frees any timer layers allocated during use.
41  *
42  * Example:
43  *      timers_cleanup(&timeouts);
44  */
45 void timers_cleanup(struct timers *timers);
46
47 /**
48  * timer_init - initialize a timer.
49  * @timer: the timer to initialize
50  *
51  * Example:
52  *      struct timer t;
53  *
54  *      timer_init(&t);
55  */
56 void timer_init(struct timer *t);
57
58 /**
59  * timer_add - insert a timer.
60  * @timers: the struct timers
61  * @timer: the (initialized or timer_del'd) timer to add
62  * @when: when @timer expires.
63  *
64  * This efficiently adds @timer to @timers, to expire @when (rounded to
65  * TIMER_GRANULARITY nanoseconds).
66  *
67  * Example:
68  *      // Timeout in 100ms.
69  *      timer_add(&timeouts, &t, timeabs_add(time_now(), time_from_msec(100)));
70  */
71 void timer_add(struct timers *timers, struct timer *timer, struct timeabs when);
72
73 /**
74  * timer_del - remove a timer.
75  * @timers: the struct timers
76  * @timer: the timer
77  *
78  * This efficiently removes @timer from @timers, if timer_add() was
79  * called.  It can be called multiple times without bad effect, and
80  * can be called any time after timer_init().
81  *
82  * Example:
83  *      timer_del(&timeouts, &t);
84  */
85 void timer_del(struct timers *timers, struct timer *timer);
86
87 /**
88  * timer_earliest - find out the first time when a timer will expire
89  * @timers: the struct timers
90  * @first: the expiry time, only set if there is a timer.
91  *
92  * This returns false, and doesn't alter @first if there are no
93  * timers.  Otherwise, it sets @first to the expiry time of the first
94  * timer (rounded to TIMER_GRANULARITY nanoseconds), and returns true.
95  *
96  * Example:
97  *      struct timeabs next = { { (time_t)-1ULL, -1UL } };
98  *      timer_earliest(&timeouts, &next);
99  */
100 bool timer_earliest(struct timers *timers, struct timeabs *first);
101
102 /**
103  * timers_expire - update timers structure and remove one expire timer.
104  * @timers: the struct timers
105  * @expire: the current time
106  *
107  * A timers added with a @when arg less than or equal to @expire will be
108  * returned (within TIMER_GRANULARITY nanosecond precision).  If
109  * there are no timers due to expire, NULL is returned.
110  *
111  * After this returns NULL, @expire is considered the current time,
112  * and adding any timers with @when before this value will be silently
113  * changed to adding them with immediate expiration.
114  *
115  * You should not move @expire backwards, though it need not move
116  * forwards.
117  *
118  * Example:
119  *      struct timer *expired;
120  *
121  *      while ((expired = timers_expire(&timeouts, time_now())) != NULL)
122  *              printf("Timer expired!\n");
123  *
124  */
125 struct timer *timers_expire(struct timers *timers, struct timeabs expire);
126
127 /**
128  * timers_check - check timer structure for consistency
129  * @t: the struct timers
130  * @abortstr: the location to print on aborting, or NULL.
131  *
132  * Because timers have redundant information, consistency checking can
133  * be done on the tree.  This is useful as a debugging check.  If
134  * @abortstr is non-NULL, that will be printed in a diagnostic if the
135  * timers structure is inconsistent, and the function will abort.
136  *
137  * Returns the timers struct if it is consistent, NULL if not (it can
138  * never return NULL if @abortstr is set).
139  *
140  * Example:
141  *      timers_check(&timeouts, "After timer_expire");
142  */
143 struct timers *timers_check(const struct timers *t, const char *abortstr);
144
145 #ifdef CCAN_TIMER_DEBUG
146 #include <stdio.h>
147
148 /**
149  * timers_dump - dump the timers datastructure (for debugging it)
150  * @t: the struct timers
151  * @fp: the FILE to dump to (stderr if @fp is NULL)
152  */
153 void timers_dump(const struct timers *timers, FILE *fp);
154 #endif
155
156 /**
157  * struct timers - structure to hold a set of timers.
158  *
159  * Initialized using timers_init, the levels of the timer are
160  * allocated as necessary, using malloc.
161  *
162  * See Also:
163  *      timers_init(), timers_cleanup()
164  */
165 struct timers {
166         /* Far in the future. */
167         struct list_head far;
168         uint64_t base;
169         uint64_t first;
170
171         struct timer_level *level[(64 + TIMER_LEVEL_BITS-1) / TIMER_LEVEL_BITS];
172 };
173
174 /**
175  * struct timer - a single timer.
176  *
177  * Set up by timer_add(), this is usually contained within an
178  * application-specific structure.
179  *
180  * See Also:
181  *      ccan/container_of, timer_add(), timer_del()
182  */
183 struct timer {
184         struct list_node list;
185         uint64_t time;
186 };
187 #endif /* CCAN_TIMER_H */