]> git.ipfire.org Git - thirdparty/bird.git/blob - lib/timer.h
The MRT protocol
[thirdparty/bird.git] / lib / timer.h
1 /*
2 * BIRD -- Timers
3 *
4 * (c) 2013--2017 Ondrej Zajicek <santiago@crfreenet.org>
5 * (c) 2013--2017 CZ.NIC z.s.p.o.
6 *
7 * Can be freely distributed and used under the terms of the GNU GPL.
8 */
9
10 #ifndef _BIRD_TIMER_H_
11 #define _BIRD_TIMER_H_
12
13 #include "nest/bird.h"
14 #include "lib/buffer.h"
15 #include "lib/resource.h"
16
17
18 typedef struct timer
19 {
20 resource r;
21 void (*hook)(struct timer *);
22 void *data;
23
24 btime expires; /* 0=inactive */
25 uint randomize; /* Amount of randomization */
26 uint recurrent; /* Timer recurrence */
27
28 int index;
29 } timer;
30
31 struct timeloop
32 {
33 BUFFER_(timer *) timers;
34 btime last_time;
35 btime real_time;
36 };
37
38 static inline uint timers_count(struct timeloop *loop)
39 { return loop->timers.used - 1; }
40
41 static inline timer *timers_first(struct timeloop *loop)
42 { return (loop->timers.used > 1) ? loop->timers.data[1] : NULL; }
43
44 extern struct timeloop main_timeloop;
45
46 btime current_time(void);
47 btime current_real_time(void);
48
49 //#define now (current_time() TO_S)
50 //#define now_real (current_real_time() TO_S)
51 extern btime boot_time;
52
53 timer *tm_new(pool *p);
54 void tm_set(timer *t, btime when);
55 void tm_start(timer *t, btime after);
56 void tm_stop(timer *t);
57
58 static inline int
59 tm_active(timer *t)
60 {
61 return t->expires != 0;
62 }
63
64 static inline btime
65 tm_remains(timer *t)
66 {
67 btime now_ = current_time();
68 return (t->expires > now_) ? (t->expires - now_) : 0;
69 }
70
71 static inline timer *
72 tm_new_init(pool *p, void (*hook)(struct timer *), void *data, uint rec, uint rand)
73 {
74 timer *t = tm_new(p);
75 t->hook = hook;
76 t->data = data;
77 t->recurrent = rec;
78 t->randomize = rand;
79 return t;
80 }
81
82 static inline void
83 tm_set_max(timer *t, btime when)
84 {
85 if (when > t->expires)
86 tm_set(t, when);
87 }
88
89 static inline void
90 tm_start_max(timer *t, btime after)
91 {
92 btime rem = tm_remains(t);
93 tm_start(t, MAX_(rem, after));
94 }
95
96 /* In sysdep code */
97 void times_init(struct timeloop *loop);
98 void times_update(struct timeloop *loop);
99 void times_update_real_time(struct timeloop *loop);
100
101 /* For I/O loop */
102 void timers_init(struct timeloop *loop, pool *p);
103 void timers_fire(struct timeloop *loop);
104
105 void timer_init(void);
106
107
108 struct timeformat {
109 char *fmt1, *fmt2;
110 btime limit;
111 };
112
113 #define TM_ISO_SHORT_S (struct timeformat){"%T", "%F", (s64) (20*3600) S_}
114 #define TM_ISO_SHORT_MS (struct timeformat){"%T.%3f", "%F", (s64) (20*3600) S_}
115 #define TM_ISO_SHORT_US (struct timeformat){"%T.%6f", "%F", (s64) (20*3600) S_}
116
117 #define TM_ISO_LONG_S (struct timeformat){"%F %T", NULL, 0}
118 #define TM_ISO_LONG_MS (struct timeformat){"%F %T.%3f", NULL, 0}
119 #define TM_ISO_LONG_US (struct timeformat){"%F %T.%6f", NULL, 0}
120
121 #define TM_DATETIME_BUFFER_SIZE 32 /* Buffer size required by tm_format_time() */
122
123 btime tm_parse_time(char *x);
124 void tm_format_time(char *x, struct timeformat *fmt, btime t);
125 int tm_format_real_time(char *x, size_t max, const char *fmt, btime t);
126
127 #endif