libosmocore 1.9.0.196-9975
Osmocom core library
timer_compat.h
Go to the documentation of this file.
1
4/*
5 * (C) 2011 Sylvain Munaut <tnt@246tNt.com>
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 */
19
24#pragma once
25
26/* MacOS < 10.12 Sierra does not define clockid_t */
27#if defined(__APPLE__) && (!defined(__DARWIN_C_LEVEL) || __DARWIN_C_LEVEL < 199309L)
28typedef int clockid_t;
29#endif
30
31/* Convenience macros for operations on timevals.
32 NOTE: `timercmp' does not work for >= or <=. */
33
34#ifndef timerisset
35# define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
36#endif
37
38#ifndef timerclear
39# define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
40#endif
41
42#ifndef timercmp
43# define timercmp(a, b, CMP) \
44 (((a)->tv_sec == (b)->tv_sec) ? \
45 ((a)->tv_usec CMP (b)->tv_usec) : \
46 ((a)->tv_sec CMP (b)->tv_sec))
47#endif
48
49#ifndef timeradd
50# define timeradd(a, b, result) \
51 do { \
52 (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
53 (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
54 if ((result)->tv_usec >= 1000000) \
55 { \
56 ++(result)->tv_sec; \
57 (result)->tv_usec -= 1000000; \
58 } \
59 } while (0)
60#endif
61
62#ifndef timersub
63# define timersub(a, b, result) \
64 do { \
65 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
66 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
67 if ((result)->tv_usec < 0) { \
68 --(result)->tv_sec; \
69 (result)->tv_usec += 1000000; \
70 } \
71 } while (0)
72#endif
73
74/* Convenience macros for operations on timespecs.
75 NOTE: `timercmp' does not work for >= or <=. */
76
77#ifndef timespecisset
78# define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
79#endif
80
81#ifndef timespecclear
82# define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
83#endif
84
85#ifndef timespeccmp
86# define timespeccmp(a, b, CMP) \
87 (((a)->tv_sec == (b)->tv_sec) ? \
88 ((a)->tv_nsec CMP (b)->tv_nsec) : \
89 ((a)->tv_sec CMP (b)->tv_sec))
90#endif
91
92#ifndef timespecadd
93# define timespecadd(a, b, result) \
94 do { \
95 (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
96 (result)->tv_nsec = (a)->tv_nsec + (b)->tv_nsec; \
97 if ((result)->tv_nsec >= 1000000000) \
98 { \
99 ++(result)->tv_sec; \
100 (result)->tv_nsec -= 1000000000; \
101 } \
102 } while (0)
103#endif
104
105#ifndef timespecsub
106# define timespecsub(a, b, result) \
107 do { \
108 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
109 (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
110 if ((result)->tv_nsec < 0) { \
111 --(result)->tv_sec; \
112 (result)->tv_nsec += 1000000000; \
113 } \
114 } while (0)
115#endif
116
117
118