blob: 77d4ce0ad855ceb2e3f989f09fe6cd3026ab5dd1 [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
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
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020075/*! @} */