blob: 916f5684d9572610c3408e0898509b444abdf894 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file timer_compat.h
2 * Compatibility header with some helpers
3 */
Sylvain Munaut07f11032011-10-21 21:55:29 +02004/*
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 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24/*! \defgroup timer Osmocom timers
25 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020026 * \file timer_compat.h */
Sylvain Munaut07f11032011-10-21 21:55:29 +020027
Sylvain Munaut12ba7782014-06-16 10:13:40 +020028#pragma once
Sylvain Munaut07f11032011-10-21 21:55:29 +020029
Pau Espin Pedrol2ca8ceb2018-12-09 01:26:55 +010030/* MacOS < 10.12 Sierra does not define clockid_t */
31#if defined(__APPLE__) && (!defined(__DARWIN_C_LEVEL) || __DARWIN_C_LEVEL < 199309L)
32typedef int clockid_t;
33#endif
Sylvain Munaut07f11032011-10-21 21:55:29 +020034
35/* Convenience macros for operations on timevals.
36 NOTE: `timercmp' does not work for >= or <=. */
37
38#ifndef timerisset
39# define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
40#endif
41
42#ifndef timerclear
43# define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
44#endif
45
46#ifndef timercmp
47# define timercmp(a, b, CMP) \
48 (((a)->tv_sec == (b)->tv_sec) ? \
49 ((a)->tv_usec CMP (b)->tv_usec) : \
50 ((a)->tv_sec CMP (b)->tv_sec))
51#endif
52
53#ifndef timeradd
54# define timeradd(a, b, result) \
55 do { \
56 (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
57 (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
58 if ((result)->tv_usec >= 1000000) \
59 { \
60 ++(result)->tv_sec; \
61 (result)->tv_usec -= 1000000; \
62 } \
63 } while (0)
64#endif
65
66#ifndef timersub
67# define timersub(a, b, result) \
68 do { \
69 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
70 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
71 if ((result)->tv_usec < 0) { \
72 --(result)->tv_sec; \
73 (result)->tv_usec += 1000000; \
74 } \
75 } while (0)
76#endif
77
Pau Espin Pedrol726ba362018-02-26 18:03:44 +010078/* Convenience macros for operations on timespecs.
79 NOTE: `timercmp' does not work for >= or <=. */
80
81#ifndef timespecisset
82# define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
83#endif
84
85#ifndef timespecclear
86# define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
87#endif
88
89#ifndef timespeccmp
90# define timespeccmp(a, b, CMP) \
91 (((a)->tv_sec == (b)->tv_sec) ? \
92 ((a)->tv_nsec CMP (b)->tv_nsec) : \
93 ((a)->tv_sec CMP (b)->tv_sec))
94#endif
95
96#ifndef timespecadd
97# define timespecadd(a, b, result) \
98 do { \
99 (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
100 (result)->tv_nsec = (a)->tv_nsec + (b)->tv_nsec; \
101 if ((result)->tv_nsec >= 1000000000) \
102 { \
103 ++(result)->tv_sec; \
104 (result)->tv_nsec -= 1000000000; \
105 } \
106 } while (0)
107#endif
108
109#ifndef timespecsub
110# define timespecsub(a, b, result) \
111 do { \
112 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
113 (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
114 if ((result)->tv_nsec < 0) { \
115 --(result)->tv_sec; \
116 (result)->tv_nsec += 1000000000; \
117 } \
118 } while (0)
119#endif
120
121
Sylvain Munaut07f11032011-10-21 21:55:29 +0200122
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200123/*! @} */