Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file timer_compat.h |
| 2 | * Compatibility header with some helpers |
| 3 | */ |
Sylvain Munaut | 07f1103 | 2011-10-21 21:55:29 +0200 | [diff] [blame] | 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 | * 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 Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 26 | * \file timer_compat.h */ |
Sylvain Munaut | 07f1103 | 2011-10-21 21:55:29 +0200 | [diff] [blame] | 27 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 28 | #pragma once |
Sylvain Munaut | 07f1103 | 2011-10-21 21:55:29 +0200 | [diff] [blame] | 29 | |
Pau Espin Pedrol | 2ca8ceb | 2018-12-09 01:26:55 +0100 | [diff] [blame] | 30 | /* MacOS < 10.12 Sierra does not define clockid_t */ |
| 31 | #if defined(__APPLE__) && (!defined(__DARWIN_C_LEVEL) || __DARWIN_C_LEVEL < 199309L) |
| 32 | typedef int clockid_t; |
| 33 | #endif |
Sylvain Munaut | 07f1103 | 2011-10-21 21:55:29 +0200 | [diff] [blame] | 34 | |
| 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 Pedrol | 726ba36 | 2018-02-26 18:03:44 +0100 | [diff] [blame] | 78 | /* 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 Munaut | 07f1103 | 2011-10-21 21:55:29 +0200 | [diff] [blame] | 122 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 123 | /*! @} */ |