]>
Commit | Line | Data |
---|---|---|
7790a2d6 DL |
1 | /* |
2 | * Copyright (c) 2017 David Lamparter, for NetDEF, Inc. | |
3 | * | |
4 | * Permission to use, copy, modify, and distribute this software for any | |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice appear in all copies. | |
7 | * | |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
15 | */ | |
16 | ||
17 | #ifndef _FRR_MONOTIME_H | |
18 | #define _FRR_MONOTIME_H | |
19 | ||
20 | #include <stdint.h> | |
21 | #include <time.h> | |
22 | #include <sys/time.h> | |
23 | ||
24 | #ifndef TIMESPEC_TO_TIMEVAL | |
25 | /* should be in sys/time.h on BSD & Linux libcs */ | |
0ee70925 QY |
26 | #define TIMESPEC_TO_TIMEVAL(tv, ts) do { \ |
27 | (tv)->tv_sec = (ts)->tv_sec; \ | |
28 | (tv)->tv_usec = (ts)->tv_nsec / 1000; \ | |
29 | } while (0) | |
7790a2d6 | 30 | #endif |
39cea822 DL |
31 | #ifndef TIMEVAL_TO_TIMESPEC |
32 | /* should be in sys/time.h on BSD & Linux libcs */ | |
0ee70925 QY |
33 | #define TIMEVAL_TO_TIMESPEC(tv, ts) do { \ |
34 | (ts)->tv_sec = (tv)->tv_sec; \ | |
35 | (ts)->tv_nsec = (tv)->tv_usec * 1000; \ | |
36 | } while (0) | |
39cea822 | 37 | #endif |
7790a2d6 DL |
38 | |
39 | static inline time_t monotime(struct timeval *tvo) | |
40 | { | |
0ee70925 | 41 | struct timespec ts; |
7790a2d6 | 42 | |
0ee70925 QY |
43 | clock_gettime(CLOCK_MONOTONIC, &ts); |
44 | if (tvo) { | |
45 | TIMESPEC_TO_TIMEVAL(tvo, &ts); | |
46 | } | |
47 | return ts.tv_sec; | |
7790a2d6 DL |
48 | } |
49 | ||
50 | /* the following two return microseconds, not time_t! | |
51 | * | |
52 | * also, they're negative forms of each other, but having both makes the | |
53 | * code more readable | |
54 | */ | |
55 | static inline int64_t monotime_since(const struct timeval *ref, | |
0ee70925 | 56 | struct timeval *out) |
7790a2d6 | 57 | { |
0ee70925 QY |
58 | struct timeval tv; |
59 | monotime(&tv); | |
60 | timersub(&tv, ref, &tv); | |
61 | if (out) | |
62 | *out = tv; | |
63 | return (int64_t)tv.tv_sec * 1000000LL + tv.tv_usec; | |
7790a2d6 DL |
64 | } |
65 | ||
66 | static inline int64_t monotime_until(const struct timeval *ref, | |
0ee70925 | 67 | struct timeval *out) |
7790a2d6 | 68 | { |
0ee70925 QY |
69 | struct timeval tv; |
70 | monotime(&tv); | |
71 | timersub(ref, &tv, &tv); | |
72 | if (out) | |
73 | *out = tv; | |
74 | return (int64_t)tv.tv_sec * 1000000LL + tv.tv_usec; | |
7790a2d6 DL |
75 | } |
76 | ||
77 | #endif /* _FRR_MONOTIME_H */ |