Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / unix / timer.h @ 153f02da

History | View | Annotate | Download (2 KB)

1
/*
2
 *        BIRD -- Unix Timers
3
 *
4
 *        (c) 1998 Martin Mares <mj@ucw.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#ifndef _BIRD_TIMER_H_
10
#define _BIRD_TIMER_H_
11

    
12
#include <time.h>
13

    
14
#include "lib/resource.h"
15

    
16
typedef time_t bird_clock_t;                /* Use instead of time_t */
17

    
18
typedef struct timer {
19
  resource r;
20
  void (*hook)(struct timer *);
21
  void *data;
22
  uint randomize;                        /* Amount of randomization */
23
  uint recurrent;                        /* Timer recurrence */
24
  node n;                                /* Internal link */
25
  bird_clock_t expires;                        /* 0=inactive */
26
} timer;
27

    
28
timer *tm_new(pool *);
29
void tm_start(timer *, uint after);
30
void tm_stop(timer *);
31
void tm_dump_all(void);
32

    
33
extern bird_clock_t now;                 /* Relative, monotonic time in seconds */
34
extern bird_clock_t now_real;                /* Time in seconds since fixed known epoch */
35
extern bird_clock_t boot_time;
36

    
37
static inline int
38
tm_active(timer *t)
39
{
40
  return t->expires != 0;
41
}
42

    
43
static inline bird_clock_t
44
tm_remains(timer *t)
45
{
46
  return t->expires ? t->expires - now : 0;
47
}
48

    
49
static inline void
50
tm_start_max(timer *t, bird_clock_t after)
51
{
52
  bird_clock_t rem = tm_remains(t);
53
  tm_start(t, (rem > after) ? rem : after);
54
}
55

    
56
static inline timer *
57
tm_new_set(pool *p, void (*hook)(struct timer *), void *data, uint rand, uint rec)
58
{
59
  timer *t = tm_new(p);
60
  t->hook = hook;
61
  t->data = data;
62
  t->randomize = rand;
63
  t->recurrent = rec;
64
  return t;
65
}
66

    
67

    
68
struct timeformat {
69
  char *fmt1, *fmt2;
70
  bird_clock_t limit;
71
};
72

    
73
bird_clock_t tm_parse_date(char *);        /* Convert date to bird_clock_t */
74
bird_clock_t tm_parse_datetime(char *);        /* Convert date to bird_clock_t */
75

    
76
#define TM_DATETIME_BUFFER_SIZE 32        /* Buffer size required by tm_format_datetime */
77
void
78
tm_format_datetime(char *x, struct timeformat *fmt_spec, bird_clock_t t);
79

    
80
#define TIME_T_IS_64BIT (sizeof(time_t) == 8)
81
#define TIME_T_IS_SIGNED ((time_t) -1 < 0)
82

    
83
#define TIME_INFINITY                                                        \
84
  ((time_t) (TIME_T_IS_SIGNED ?                                                \
85
             (TIME_T_IS_64BIT ? 0x7fffffffffffffff : 0x7fffffff):        \
86
             (TIME_T_IS_64BIT ? 0xffffffffffffffff : 0xffffffff)))
87

    
88
#endif