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 | * |
Sylvain Munaut | 07f1103 | 2011-10-21 21:55:29 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /*! \defgroup timer Osmocom timers |
| 21 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 22 | * \file timer_compat.h */ |
Sylvain Munaut | 07f1103 | 2011-10-21 21:55:29 +0200 | [diff] [blame] | 23 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 24 | #pragma once |
Sylvain Munaut | 07f1103 | 2011-10-21 21:55:29 +0200 | [diff] [blame] | 25 | |
Pau Espin Pedrol | 2ca8ceb | 2018-12-09 01:26:55 +0100 | [diff] [blame] | 26 | /* MacOS < 10.12 Sierra does not define clockid_t */ |
| 27 | #if defined(__APPLE__) && (!defined(__DARWIN_C_LEVEL) || __DARWIN_C_LEVEL < 199309L) |
| 28 | typedef int clockid_t; |
| 29 | #endif |
Sylvain Munaut | 07f1103 | 2011-10-21 21:55:29 +0200 | [diff] [blame] | 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 | |
Pau Espin Pedrol | 726ba36 | 2018-02-26 18:03:44 +0100 | [diff] [blame] | 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 | |
Sylvain Munaut | 07f1103 | 2011-10-21 21:55:29 +0200 | [diff] [blame] | 118 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 119 | /*! @} */ |