iof-bird-daemon / lib / timer.h @ 574b2324
History | View | Annotate | Download (2.83 KB)
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_TIMER2_H_
|
11 |
#define _BIRD_TIMER2_H_
|
12 |
|
13 |
#include "nest/bird.h" |
14 |
#include "lib/buffer.h" |
15 |
#include "lib/resource.h" |
16 |
|
17 |
|
18 |
typedef struct timer2 |
19 |
{ |
20 |
resource r; |
21 |
void (*hook)(struct timer2 *); |
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 |
} timer2; |
30 |
|
31 |
struct timeloop
|
32 |
{ |
33 |
BUFFER(timer2 *) 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 timer2 *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 |
timer2 *tm2_new(pool *p); |
54 |
void tm2_set(timer2 *t, btime when);
|
55 |
void tm2_start(timer2 *t, btime after);
|
56 |
void tm2_stop(timer2 *t);
|
57 |
|
58 |
static inline int |
59 |
tm2_active(timer2 *t) |
60 |
{ |
61 |
return t->expires != 0; |
62 |
} |
63 |
|
64 |
static inline btime |
65 |
tm2_remains(timer2 *t) |
66 |
{ |
67 |
btime now_ = current_time(); |
68 |
return (t->expires > now_) ? (t->expires - now_) : 0; |
69 |
} |
70 |
|
71 |
static inline timer2 * |
72 |
tm2_new_init(pool *p, void (*hook)(struct timer2 *), void *data, uint rec, uint rand) |
73 |
{ |
74 |
timer2 *t = tm2_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 |
tm2_set_max(timer2 *t, btime when) |
84 |
{ |
85 |
if (when > t->expires)
|
86 |
tm2_set(t, when); |
87 |
} |
88 |
|
89 |
static inline void |
90 |
tm2_start_max(timer2 *t, btime after) |
91 |
{ |
92 |
btime rem = tm2_remains(t); |
93 |
tm2_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 |
void tm_format_real_time(char *x, const char *fmt, btime t); |
126 |
|
127 |
#endif
|