]>
Commit | Line | Data |
---|---|---|
534215a1 OZ |
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 | ||
a6f79ca5 OZ |
10 | #ifndef _BIRD_TIMER_H_ |
11 | #define _BIRD_TIMER_H_ | |
534215a1 OZ |
12 | |
13 | #include "nest/bird.h" | |
14 | #include "lib/buffer.h" | |
15 | #include "lib/resource.h" | |
16 | ||
17 | ||
a6f79ca5 | 18 | typedef struct timer |
534215a1 OZ |
19 | { |
20 | resource r; | |
a6f79ca5 | 21 | void (*hook)(struct timer *); |
534215a1 OZ |
22 | void *data; |
23 | ||
24 | btime expires; /* 0=inactive */ | |
25 | uint randomize; /* Amount of randomization */ | |
26 | uint recurrent; /* Timer recurrence */ | |
27 | ||
28 | int index; | |
a6f79ca5 | 29 | } timer; |
534215a1 OZ |
30 | |
31 | struct timeloop | |
32 | { | |
a32a7b58 | 33 | BUFFER_(timer *) timers; |
534215a1 OZ |
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 | ||
a6f79ca5 | 41 | static inline timer *timers_first(struct timeloop *loop) |
534215a1 OZ |
42 | { return (loop->timers.used > 1) ? loop->timers.data[1] : NULL; } |
43 | ||
44 | extern struct timeloop main_timeloop; | |
45 | ||
46 | btime current_time(void); | |
02552526 OZ |
47 | btime current_real_time(void); |
48 | ||
574b2324 OZ |
49 | //#define now (current_time() TO_S) |
50 | //#define now_real (current_real_time() TO_S) | |
02552526 | 51 | extern btime boot_time; |
534215a1 | 52 | |
a6f79ca5 OZ |
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); | |
534215a1 OZ |
57 | |
58 | static inline int | |
a6f79ca5 | 59 | tm_active(timer *t) |
534215a1 OZ |
60 | { |
61 | return t->expires != 0; | |
62 | } | |
63 | ||
64 | static inline btime | |
a6f79ca5 | 65 | tm_remains(timer *t) |
534215a1 | 66 | { |
02552526 OZ |
67 | btime now_ = current_time(); |
68 | return (t->expires > now_) ? (t->expires - now_) : 0; | |
534215a1 OZ |
69 | } |
70 | ||
a6f79ca5 OZ |
71 | static inline timer * |
72 | tm_new_init(pool *p, void (*hook)(struct timer *), void *data, uint rec, uint rand) | |
534215a1 | 73 | { |
a6f79ca5 | 74 | timer *t = tm_new(p); |
534215a1 OZ |
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 | |
a6f79ca5 | 83 | tm_set_max(timer *t, btime when) |
534215a1 OZ |
84 | { |
85 | if (when > t->expires) | |
a6f79ca5 | 86 | tm_set(t, when); |
534215a1 OZ |
87 | } |
88 | ||
534215a1 | 89 | static inline void |
a6f79ca5 | 90 | tm_start_max(timer *t, btime after) |
534215a1 | 91 | { |
a6f79ca5 OZ |
92 | btime rem = tm_remains(t); |
93 | tm_start(t, MAX_(rem, after)); | |
534215a1 | 94 | } |
534215a1 OZ |
95 | |
96 | /* In sysdep code */ | |
97 | void times_init(struct timeloop *loop); | |
98 | void times_update(struct timeloop *loop); | |
02552526 | 99 | void times_update_real_time(struct timeloop *loop); |
534215a1 OZ |
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 | ||
f047271c OZ |
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); | |
863ecfc7 | 125 | int tm_format_real_time(char *x, size_t max, const char *fmt, btime t); |
f047271c | 126 | |
534215a1 | 127 | #endif |