| 1 | // This is a slimmed-down copy of includes/pytime.h. It removes all the |
| 2 | // PyObject interfaces and can be parsed by CTypeSpace |
| 3 | |
| 4 | #ifndef Py_PYTIME_H |
| 5 | #include <stdint.h> |
| 6 | #include <limits.h> |
| 7 | #ifdef HAVE_SYS_TIME_H |
| 8 | #include <sys/time.h> |
| 9 | #endif |
| 10 | #include <time.h> |
| 11 | #include <assert.h> |
| 12 | #include <math.h> |
| 13 | #include <errno.h> |
| 14 | #include "src/precommondefs.h" |
| 15 | |
| 16 | #define PyAPI_FUNC(x) x |
| 17 | #define PyErr_SetFromErrno(x) |
| 18 | #define Py_ABS(x) ((x) < 0 ? -(x) : (x)) |
| 19 | |
| 20 | |
| 21 | // Original pytime.h starts here |
| 22 | |
| 23 | // The _PyTime_t API is written to use timestamp and timeout values stored in |
| 24 | // various formats and to read clocks. |
| 25 | // |
| 26 | // The _PyTime_t type is an integer to support directly common arithmetic |
| 27 | // operations like t1 + t2. |
| 28 | // |
| 29 | // The _PyTime_t API supports a resolution of 1 nanosecond. The _PyTime_t type |
| 30 | // is signed to support negative timestamps. The supported range is around |
| 31 | // [-292.3 years; +292.3 years]. Using the Unix epoch (January 1st, 1970), the |
| 32 | // supported date range is around [1677-09-21; 2262-04-11]. |
| 33 | // |
| 34 | // Formats: |
| 35 | // |
| 36 | // * seconds |
| 37 | // * seconds as a floating pointer number (C double) |
| 38 | // * milliseconds (10^-3 seconds) |
| 39 | // * microseconds (10^-6 seconds) |
| 40 | // * 100 nanoseconds (10^-7 seconds) |
| 41 | // * nanoseconds (10^-9 seconds) |
| 42 | // * timeval structure, 1 microsecond resolution (10^-6 seconds) |
| 43 | // * timespec structure, 1 nanosecond resolution (10^-9 seconds) |
| 44 | // |
| 45 | // Integer overflows are detected and raise OverflowError. Conversion to a |
| 46 | // resolution worse than 1 nanosecond is rounded correctly with the requested |
| 47 | // rounding mode. There are 4 rounding modes: floor (towards -inf), ceiling |
| 48 | // (towards +inf), half even and up (away from zero). |
| 49 | // |
| 50 | // Some functions clamp the result in the range [_PyTime_MIN; _PyTime_MAX], so |
| 51 | // the caller doesn't have to handle errors and doesn't need to hold the GIL. |
| 52 | // For example, _PyTime_Add(t1, t2) computes t1+t2 and clamp the result on |
| 53 | // overflow. |
| 54 | // |
| 55 | // Clocks: |
| 56 | // |
| 57 | // * System clock |
| 58 | // * Monotonic clock |
| 59 | // * Performance counter |
| 60 | // |
| 61 | // Operations like (t * k / q) with integers are implemented in a way to reduce |
| 62 | // the risk of integer overflow. Such operation is used to convert a clock |
| 63 | // value expressed in ticks with a frequency to _PyTime_t, like |
| 64 | // QueryPerformanceCounter() with QueryPerformanceFrequency(). |
| 65 | |
| 66 | /* _PyTime_t: Python timestamp with subsecond precision. It can be used to |
| 67 | store a duration, and so indirectly a date (related to another date, like |
| 68 | UNIX epoch). */ |
| 69 | typedef int64_t _PyTime_t; |
| 70 | // _PyTime_MIN nanoseconds is around -292.3 years |
| 71 | #define _PyTime_MIN INT64_MIN |
| 72 | // _PyTime_MAX nanoseconds is around +292.3 years |
| 73 | #define _PyTime_MAX INT64_MAX |
| 74 | #define _SIZEOF_PYTIME_T 8 |
| 75 | |
| 76 | // parse from here |
| 77 | |
| 78 | typedef enum { |
| 79 | /* Round towards minus infinity (-inf). |
| 80 | For example, used to read a clock. */ |
| 81 | _PyTime_ROUND_FLOOR=0, |
| 82 | /* Round towards infinity (+inf). |
| 83 | For example, used for timeout to wait "at least" N seconds. */ |
| 84 | _PyTime_ROUND_CEILING=1, |
| 85 | /* Round to nearest with ties going to nearest even integer. |
| 86 | For example, used to round from a Python float. */ |
| 87 | _PyTime_ROUND_HALF_EVEN=2, |
| 88 | /* Round away from zero |
| 89 | For example, used for timeout. _PyTime_ROUND_CEILING rounds |
| 90 | -1e-9 to 0 milliseconds which causes bpo-31786 issue. |
| 91 | _PyTime_ROUND_UP rounds -1e-9 to -1 millisecond which keeps |
| 92 | the timeout sign as expected. select.poll(timeout) must block |
| 93 | for negative values." */ |
| 94 | _PyTime_ROUND_UP=3, |
| 95 | /* _PyTime_ROUND_TIMEOUT (an alias for _PyTime_ROUND_UP) should be |
| 96 | used for timeouts. */ |
| 97 | _PyTime_ROUND_TIMEOUT = _PyTime_ROUND_UP |
| 98 | } _PyTime_round_t; |
| 99 | |
| 100 | /* Structure used by time.get_clock_info() */ |
| 101 | typedef struct { |
| 102 | const char *implementation; |
| 103 | int monotonic; |
| 104 | int adjustable; |
| 105 | double resolution; |
| 106 | } _Py_clock_info_t; |
| 107 | |
| 108 | // stop parsing |
| 109 | |
| 110 | /* Create a timestamp from a number of seconds. */ |
| 111 | _PyTime_t _PyTime_FromSeconds(int seconds); |
| 112 | |
| 113 | /* Macro to create a timestamp from a number of seconds, no integer overflow. |
| 114 | Only use the macro for small values, prefer _PyTime_FromSeconds(). */ |
| 115 | #define _PYTIME_FROMSECONDS(seconds) \ |
| 116 | ((_PyTime_t)(seconds) * (1000 * 1000 * 1000)) |
| 117 | |
| 118 | /* Create a timestamp from a number of nanoseconds. */ |
| 119 | _PyTime_t _PyTime_FromNanoseconds(_PyTime_t ns); |
| 120 | |
| 121 | /* Convert a timestamp to a number of seconds as a C double. */ |
| 122 | RPY_EXTERN double |
| 123 | _PyTime_AsSecondsDouble(_PyTime_t t); |
| 124 | |
| 125 | /* Convert timestamp to a number of nanoseconds (10^-9 seconds). */ |
| 126 | _PyTime_t _PyTime_AsNanoseconds(_PyTime_t t); |
| 127 | |
| 128 | // Compute t1 + t2. Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow. |
| 129 | _PyTime_t _PyTime_Add(_PyTime_t t1, _PyTime_t t2); |
| 130 | |
| 131 | /* Compute ticks * mul / div. |
| 132 | Clamp to [_PyTime_MIN; _PyTime_MAX] on overflow. |
| 133 | The caller must ensure that ((div - 1) * mul) cannot overflow. */ |
| 134 | _PyTime_t _PyTime_MulDiv(_PyTime_t ticks, |
| 135 | _PyTime_t mul, |
| 136 | _PyTime_t div); |
| 137 | |
| 138 | /* Get the current time from the system clock. |
| 139 | |
| 140 | If the internal clock fails, silently ignore the error and return 0. |
| 141 | On integer overflow, silently ignore the overflow and clamp the clock to |
| 142 | [_PyTime_MIN; _PyTime_MAX]. |
| 143 | |
| 144 | Use _PyTime_GetSystemClockWithInfo() to check for failure. */ |
| 145 | _PyTime_t _PyTime_GetSystemClock(void); |
| 146 | |
| 147 | /* Get the current time from the system clock. |
| 148 | * On success, set *t and *info (if not NULL), and return 0. |
| 149 | * On error, raise an exception and return -1. |
| 150 | */ |
| 151 | RPY_EXTERN int |
| 152 | _PyTime_GetSystemClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info); |
| 153 | |
| 154 | /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards. |
| 155 | The clock is not affected by system clock updates. The reference point of |
| 156 | the returned value is undefined, so that only the difference between the |
| 157 | results of consecutive calls is valid. |
| 158 | |
| 159 | If the internal clock fails, silently ignore the error and return 0. |
| 160 | On integer overflow, silently ignore the overflow and clamp the clock to |
| 161 | [_PyTime_MIN; _PyTime_MAX]. |
| 162 | |
| 163 | Use _PyTime_GetMonotonicClockWithInfo() to check for failure. */ |
| 164 | _PyTime_t _PyTime_GetMonotonicClock(void); |
| 165 | |
| 166 | /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards. |
| 167 | The clock is not affected by system clock updates. The reference point of |
| 168 | the returned value is undefined, so that only the difference between the |
| 169 | results of consecutive calls is valid. |
| 170 | |
| 171 | Fill info (if set) with information of the function used to get the time. |
| 172 | |
| 173 | Return 0 on success, raise an exception and return -1 on error. */ |
| 174 | RPY_EXTERN int |
| 175 | _PyTime_GetMonotonicClockWithInfo(_PyTime_t *t, _Py_clock_info_t *info); |
| 176 | |
| 177 | /* Get the performance counter: clock with the highest available resolution to |
| 178 | measure a short duration. |
| 179 | |
| 180 | If the internal clock fails, silently ignore the error and return 0. |
| 181 | On integer overflow, silently ignore the overflow and clamp the clock to |
| 182 | [_PyTime_MIN; _PyTime_MAX]. |
| 183 | |
| 184 | Use _PyTime_GetPerfCounterWithInfo() to check for failure. */ |
| 185 | _PyTime_t _PyTime_GetPerfCounter(void); |
| 186 | |
| 187 | /* Get the performance counter: clock with the highest available resolution to |
| 188 | measure a short duration. |
| 189 | |
| 190 | Fill info (if set) with information of the function used to get the time. |
| 191 | |
| 192 | Return 0 on success, raise an exception and return -1 on error. */ |
| 193 | RPY_EXTERN int |
| 194 | _PyTime_GetPerfCounterWithInfo(_PyTime_t *t, _Py_clock_info_t *info); |
| 195 | |
| 196 | |
| 197 | // Create a deadline. |
| 198 | // Pseudo code: _PyTime_GetMonotonicClock() + timeout. |
| 199 | _PyTime_t _PyDeadline_Init(_PyTime_t timeout); |
| 200 | |
| 201 | // Get remaining time from a deadline. |
| 202 | // Pseudo code: deadline - _PyTime_GetMonotonicClock(). |
| 203 | _PyTime_t _PyDeadline_Get(_PyTime_t deadline); |
| 204 | |
| 205 | RPY_EXTERN int |
| 206 | _PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round); |
| 207 | |
| 208 | RPY_EXTERN int |
| 209 | _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts); |
| 210 | |
| 211 | #endif // Py_PYTIME_H |