timer.h 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 *	BIRD -- Timers
 *
 *	(c) 2013--2017 Ondrej Zajicek <santiago@crfreenet.org>
 *	(c) 2013--2017 CZ.NIC z.s.p.o.
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

10 11
#ifndef _BIRD_TIMER_H_
#define _BIRD_TIMER_H_
12 13 14 15 16 17

#include "nest/bird.h"
#include "lib/buffer.h"
#include "lib/resource.h"


18
typedef struct timer
19 20
{
  resource r;
21
  void (*hook)(struct timer *);
22 23 24 25 26 27 28
  void *data;

  btime expires;			/* 0=inactive */
  uint randomize;			/* Amount of randomization */
  uint recurrent;			/* Timer recurrence */

  int index;
29
} timer;
30 31 32

struct timeloop
{
33
  BUFFER_(timer *) timers;
34 35 36 37 38 39 40
  btime last_time;
  btime real_time;
};

static inline uint timers_count(struct timeloop *loop)
{ return loop->timers.used - 1; }

41
static inline timer *timers_first(struct timeloop *loop)
42 43 44 45 46
{ return (loop->timers.used > 1) ? loop->timers.data[1] : NULL; }

extern struct timeloop main_timeloop;

btime current_time(void);
47 48
btime current_real_time(void);

49 50
//#define now (current_time() TO_S)
//#define now_real (current_real_time() TO_S)
51
extern btime boot_time;
52

53 54 55 56
timer *tm_new(pool *p);
void tm_set(timer *t, btime when);
void tm_start(timer *t, btime after);
void tm_stop(timer *t);
57 58

static inline int
59
tm_active(timer *t)
60 61 62 63 64
{
  return t->expires != 0;
}

static inline btime
65
tm_remains(timer *t)
66
{
67 68
  btime now_ = current_time();
  return (t->expires > now_) ? (t->expires - now_) : 0;
69 70
}

71 72
static inline timer *
tm_new_init(pool *p, void (*hook)(struct timer *), void *data, uint rec, uint rand)
73
{
74
  timer *t = tm_new(p);
75 76 77 78 79 80 81 82
  t->hook = hook;
  t->data = data;
  t->recurrent = rec;
  t->randomize = rand;
  return t;
}

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 93
  btime rem = tm_remains(t);
  tm_start(t, MAX_(rem, after));
94 95 96 97 98
}

/* In sysdep code */
void times_init(struct timeloop *loop);
void times_update(struct timeloop *loop);
99
void times_update_real_time(struct timeloop *loop);
100 101 102 103 104 105 106 107

/* For I/O loop */
void timers_init(struct timeloop *loop, pool *p);
void timers_fire(struct timeloop *loop);

void timer_init(void);


108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
struct timeformat {
  char *fmt1, *fmt2;
  btime limit;
};

#define TM_ISO_SHORT_S	(struct timeformat){"%T",     "%F", (s64) (20*3600) S_}
#define TM_ISO_SHORT_MS	(struct timeformat){"%T.%3f", "%F", (s64) (20*3600) S_}
#define TM_ISO_SHORT_US	(struct timeformat){"%T.%6f", "%F", (s64) (20*3600) S_}

#define TM_ISO_LONG_S	(struct timeformat){"%F %T",     NULL, 0}
#define TM_ISO_LONG_MS	(struct timeformat){"%F %T.%3f", NULL, 0}
#define TM_ISO_LONG_US	(struct timeformat){"%F %T.%6f", NULL, 0}

#define TM_DATETIME_BUFFER_SIZE 32	/* Buffer size required by tm_format_time() */

btime tm_parse_time(char *x);
void tm_format_time(char *x, struct timeformat *fmt, btime t);
void tm_format_real_time(char *x, const char *fmt, btime t);

127
#endif