-
Notifications
You must be signed in to change notification settings - Fork 2
/
timer.c
47 lines (42 loc) · 1.16 KB
/
timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
*
* Authors:
* Pedro Roque <[email protected]>
* Lars Fenneberg <[email protected]>
*
* This software is Copyright 1996-2000 by the above mentioned author(s),
* All Rights Reserved.
*
* The license which is distributed with this software in the file COPYRIGHT
* applies to this software. If your distribution is missing this file, you
* may request it from <[email protected]>.
*
*/
#include "config.h"
#include "radvd.h"
struct timespec next_timespec(double next)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
ts.tv_sec += (int)next;
ts.tv_nsec += 1000000000ULL * (next - (int)next);
return ts;
}
int64_t timespecdiff(struct timespec const *a, struct timespec const *b)
{
int64_t msec;
msec = ((int64_t)a->tv_sec - b->tv_sec) * 1000;
msec += ((int64_t)a->tv_nsec - b->tv_nsec) / (1000 * 1000);
return msec;
}
/* Returns when the next time should expire in milliseconds. */
uint64_t next_time_msec(struct Interface const *iface)
{
struct timespec ts;
int64_t diff_ms;
clock_gettime(CLOCK_MONOTONIC, &ts);
diff_ms = timespecdiff(&iface->times.next_multicast, &ts);
if (diff_ms <= 0)
return 0;
return (uint64_t)diff_ms;
}