| 1 | // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.cxx . . $ `<file>/librabbitmq/amqp_time.c`/ |
| 2 | // SPDX-License-Identifier: mit |
| 3 | |
| 4 | #include "amqp_time.h"cxx . . $ `<file>/librabbitmq/amqp_time.h`/ |
| 5 | #include "rabbitmq-c/amqp.h"cxx . . $ `<file>/include/rabbitmq-c/amqp.h`/ |
| 6 | #include <assert.h> |
| 7 | #include <limits.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | #if (defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || \ |
| 11 | defined(__MINGW32__) || defined(__MINGW64__)) |
| 12 | #define AMQP_WIN_TIMER_API |
| 13 | #elif (defined(machintosh) || defined(__APPLE__) || defined(__APPLE_CC__)) |
| 14 | #define AMQP_MAC_TIMER_API |
| 15 | #else |
| 16 | #define AMQP_POSIX_TIMER_APIcxx . . $ `librabbitmq/amqp_time.c:16:9`! |
| 17 | #endif |
| 18 | |
| 19 | #ifdef AMQP_WIN_TIMER_API |
| 20 | #ifndef WIN32_LEAN_AND_MEAN |
| 21 | #define WIN32_LEAN_AND_MEAN |
| 22 | #endif |
| 23 | #include <windows.h> |
| 24 | |
| 25 | uint64_t amqp_get_monotonic_timestamp(void) { |
| 26 | static double NS_PER_COUNT = 0; |
| 27 | LARGE_INTEGER perf_count; |
| 28 | |
| 29 | if (0 == NS_PER_COUNT) { |
| 30 | LARGE_INTEGER perf_frequency; |
| 31 | if (!QueryPerformanceFrequency(&perf_frequency)) { |
| 32 | return 0; |
| 33 | } |
| 34 | NS_PER_COUNT = (double)AMQP_NS_PER_S / perf_frequency.QuadPart; |
| 35 | } |
| 36 | |
| 37 | if (!QueryPerformanceCounter(&perf_count)) { |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | return (uint64_t)(perf_count.QuadPart * NS_PER_COUNT); |
| 42 | } |
| 43 | #endif /* AMQP_WIN_TIMER_API */ |
| 44 | |
| 45 | #ifdef AMQP_MAC_TIMER_API |
| 46 | #include <mach/mach_time.h> |
| 47 | |
| 48 | uint64_t amqp_get_monotonic_timestamp(void) { |
| 49 | static mach_timebase_info_data_t s_timebase = {0, 0}; |
| 50 | uint64_t timestamp; |
| 51 | |
| 52 | timestamp = mach_absolute_time(); |
| 53 | |
| 54 | if (s_timebase.denom == 0) { |
| 55 | mach_timebase_info(&s_timebase); |
| 56 | if (0 == s_timebase.denom) { |
| 57 | return 0; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | timestamp *= (uint64_t)s_timebase.numer; |
| 62 | timestamp /= (uint64_t)s_timebase.denom; |
| 63 | |
| 64 | return timestamp; |
| 65 | } |
| 66 | #endif /* AMQP_MAC_TIMER_API */ |
| 67 | |
| 68 | #ifdef AMQP_POSIX_TIMER_APIcxx . . $ `librabbitmq/amqp_time.c:16:9`! |
| 69 | #include <time.h> |
| 70 | |
| 71 | uint64_tcxx . . $ uint64_t# amqp_get_monotonic_timestampcxx . . $ amqp_get_monotonic_timestamp(f708dce9a8856c70).(void) { |
| 72 | #ifdef __hpux |
| 73 | return (uint64_t)gethrtime(); |
| 74 | #else |
| 75 | struct timespeccxx . . $ timespec# tp; |
| 76 | if (-1 == clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(CLOCK_MONOTONICcxx . . $ `<external>/89459239771508533/time.h:48:10`!, &tp)) { |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | return ((uint64_tcxx . . $ uint64_t#)tp.tv_seccxx . . $ timespec#tv_sec. * AMQP_NS_PER_Scxx . . $ `librabbitmq/amqp_time.h:23:9`! + (uint64_tcxx . . $ uint64_t#)tp.tv_nseccxx . . $ timespec#tv_nsec.); |
| 81 | #endif |
| 82 | } |
| 83 | #endif /* AMQP_POSIX_TIMER_API */ |
| 84 | |
| 85 | int amqp_time_from_nowcxx . . $ amqp_time_from_now(64a1f94b3ba1ec68).(amqp_time_tcxx . . $ amqp_time_t# *time, const struct timevalcxx . . $ timeval# *timeout) { |
| 86 | uint64_tcxx . . $ uint64_t# now_ns; |
| 87 | uint64_tcxx . . $ uint64_t# delta_ns; |
| 88 | |
| 89 | assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(NULL != time); |
| 90 | |
| 91 | if (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! == timeout) { |
| 92 | *time = amqp_time_infinitecxx . . $ amqp_time_infinite(6bdd969bb3f20527).(); |
| 93 | return AMQP_STATUS_OKcxx . . $ AMQP_STATUS_OK.; |
| 94 | } |
| 95 | |
| 96 | if (timeout->tv_seccxx . . $ timeval#tv_sec. < 0 || timeout->tv_useccxx . . $ timeval#tv_usec. < 0) { |
| 97 | return AMQP_STATUS_INVALID_PARAMETERcxx . . $ AMQP_STATUS_INVALID_PARAMETER.; |
| 98 | } |
| 99 | |
| 100 | delta_ns = (uint64_tcxx . . $ uint64_t#)timeout->tv_seccxx . . $ timeval#tv_sec. * AMQP_NS_PER_Scxx . . $ `librabbitmq/amqp_time.h:23:9`! + |
| 101 | (uint64_tcxx . . $ uint64_t#)timeout->tv_useccxx . . $ timeval#tv_usec. * AMQP_NS_PER_UScxx . . $ `librabbitmq/amqp_time.h:25:9`!; |
| 102 | |
| 103 | now_ns = amqp_get_monotonic_timestampcxx . . $ amqp_get_monotonic_timestamp(f708dce9a8856c70).(); |
| 104 | if (0 == now_ns) { |
| 105 | return AMQP_STATUS_TIMER_FAILUREcxx . . $ AMQP_STATUS_TIMER_FAILURE.; |
| 106 | } |
| 107 | |
| 108 | time->time_point_nscxx . . $ amqp_time_t_#time_point_ns. = now_ns + delta_ns; |
| 109 | if (now_ns > time->time_point_nscxx . . $ amqp_time_t_#time_point_ns. || delta_ns > time->time_point_nscxx . . $ amqp_time_t_#time_point_ns.) { |
| 110 | return AMQP_STATUS_INVALID_PARAMETERcxx . . $ AMQP_STATUS_INVALID_PARAMETER.; |
| 111 | } |
| 112 | |
| 113 | return AMQP_STATUS_OKcxx . . $ AMQP_STATUS_OK.; |
| 114 | } |
| 115 | |
| 116 | int amqp_time_s_from_nowcxx . . $ amqp_time_s_from_now(ddc3f1449d2fd0c6).(amqp_time_tcxx . . $ amqp_time_t# *time, int seconds) { |
| 117 | uint64_tcxx . . $ uint64_t# now_ns; |
| 118 | uint64_tcxx . . $ uint64_t# delta_ns; |
| 119 | assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(NULL != time); |
| 120 | |
| 121 | if (0 >= seconds) { |
| 122 | *time = amqp_time_infinitecxx . . $ amqp_time_infinite(6bdd969bb3f20527).(); |
| 123 | return AMQP_STATUS_OKcxx . . $ AMQP_STATUS_OK.; |
| 124 | } |
| 125 | |
| 126 | now_ns = amqp_get_monotonic_timestampcxx . . $ amqp_get_monotonic_timestamp(f708dce9a8856c70).(); |
| 127 | if (0 == now_ns) { |
| 128 | return AMQP_STATUS_TIMER_FAILUREcxx . . $ AMQP_STATUS_TIMER_FAILURE.; |
| 129 | } |
| 130 | |
| 131 | delta_ns = (uint64_tcxx . . $ uint64_t#)seconds * AMQP_NS_PER_Scxx . . $ `librabbitmq/amqp_time.h:23:9`!; |
| 132 | time->time_point_nscxx . . $ amqp_time_t_#time_point_ns. = now_ns + delta_ns; |
| 133 | if (now_ns > time->time_point_nscxx . . $ amqp_time_t_#time_point_ns. || delta_ns > time->time_point_nscxx . . $ amqp_time_t_#time_point_ns.) { |
| 134 | return AMQP_STATUS_INVALID_PARAMETERcxx . . $ AMQP_STATUS_INVALID_PARAMETER.; |
| 135 | } |
| 136 | |
| 137 | return AMQP_STATUS_OKcxx . . $ AMQP_STATUS_OK.; |
| 138 | } |
| 139 | |
| 140 | amqp_time_tcxx . . $ amqp_time_t# amqp_time_infinitecxx . . $ amqp_time_infinite(6bdd969bb3f20527).(void) { |
| 141 | amqp_time_tcxx . . $ amqp_time_t# time; |
| 142 | time.time_point_nscxx . . $ amqp_time_t_#time_point_ns. = UINT64_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:119:10`!; |
| 143 | return time; |
| 144 | } |
| 145 | |
| 146 | int amqp_time_ms_untilcxx . . $ amqp_time_ms_until(14b75da8f64c9fb6).(amqp_time_tcxx . . $ amqp_time_t# time) { |
| 147 | uint64_tcxx . . $ uint64_t# now_ns; |
| 148 | uint64_tcxx . . $ uint64_t# delta_ns; |
| 149 | int left_ms; |
| 150 | |
| 151 | if (UINT64_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:119:10`! == time.time_point_nscxx . . $ amqp_time_t_#time_point_ns.) { |
| 152 | return -1; |
| 153 | } |
| 154 | if (0 == time.time_point_nscxx . . $ amqp_time_t_#time_point_ns.) { |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | now_ns = amqp_get_monotonic_timestampcxx . . $ amqp_get_monotonic_timestamp(f708dce9a8856c70).(); |
| 159 | if (0 == now_ns) { |
| 160 | return AMQP_STATUS_TIMER_FAILUREcxx . . $ AMQP_STATUS_TIMER_FAILURE.; |
| 161 | } |
| 162 | |
| 163 | if (now_ns >= time.time_point_nscxx . . $ amqp_time_t_#time_point_ns.) { |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | delta_ns = time.time_point_nscxx . . $ amqp_time_t_#time_point_ns. - now_ns; |
| 168 | left_ms = (int)(delta_ns / AMQP_NS_PER_MScxx . . $ `librabbitmq/amqp_time.h:24:9`!); |
| 169 | |
| 170 | return left_ms; |
| 171 | } |
| 172 | |
| 173 | int amqp_time_tv_untilcxx . . $ amqp_time_tv_until(b2bb6e057a3fdcac).(amqp_time_tcxx . . $ amqp_time_t# time, struct timevalcxx . . $ timeval# *in, |
| 174 | struct timevalcxx . . $ timeval# **out) { |
| 175 | uint64_tcxx . . $ uint64_t# now_ns; |
| 176 | uint64_tcxx . . $ uint64_t# delta_ns; |
| 177 | |
| 178 | assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(in != NULL); |
| 179 | if (UINT64_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:119:10`! == time.time_point_nscxx . . $ amqp_time_t_#time_point_ns.) { |
| 180 | *out = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 181 | return AMQP_STATUS_OKcxx . . $ AMQP_STATUS_OK.; |
| 182 | } |
| 183 | if (0 == time.time_point_nscxx . . $ amqp_time_t_#time_point_ns.) { |
| 184 | in->tv_seccxx . . $ timeval#tv_sec. = 0; |
| 185 | in->tv_useccxx . . $ timeval#tv_usec. = 0; |
| 186 | *out = in; |
| 187 | return AMQP_STATUS_OKcxx . . $ AMQP_STATUS_OK.; |
| 188 | } |
| 189 | |
| 190 | now_ns = amqp_get_monotonic_timestampcxx . . $ amqp_get_monotonic_timestamp(f708dce9a8856c70).(); |
| 191 | if (0 == now_ns) { |
| 192 | return AMQP_STATUS_TIMER_FAILUREcxx . . $ AMQP_STATUS_TIMER_FAILURE.; |
| 193 | } |
| 194 | |
| 195 | if (now_ns >= time.time_point_nscxx . . $ amqp_time_t_#time_point_ns.) { |
| 196 | in->tv_seccxx . . $ timeval#tv_sec. = 0; |
| 197 | in->tv_useccxx . . $ timeval#tv_usec. = 0; |
| 198 | *out = in; |
| 199 | return AMQP_STATUS_OKcxx . . $ AMQP_STATUS_OK.; |
| 200 | } |
| 201 | |
| 202 | delta_ns = time.time_point_nscxx . . $ amqp_time_t_#time_point_ns. - now_ns; |
| 203 | in->tv_seccxx . . $ timeval#tv_sec. = (int)(delta_ns / AMQP_NS_PER_Scxx . . $ `librabbitmq/amqp_time.h:23:9`!); |
| 204 | in->tv_useccxx . . $ timeval#tv_usec. = (int)((delta_ns % AMQP_NS_PER_Scxx . . $ `librabbitmq/amqp_time.h:23:9`!) / AMQP_NS_PER_UScxx . . $ `librabbitmq/amqp_time.h:25:9`!); |
| 205 | *out = in; |
| 206 | |
| 207 | return AMQP_STATUS_OKcxx . . $ AMQP_STATUS_OK.; |
| 208 | } |
| 209 | |
| 210 | int amqp_time_has_pastcxx . . $ amqp_time_has_past(14b75da8f64c9fb6).(amqp_time_tcxx . . $ amqp_time_t# time) { |
| 211 | uint64_tcxx . . $ uint64_t# now_ns; |
| 212 | if (UINT64_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:119:10`! == time.time_point_nscxx . . $ amqp_time_t_#time_point_ns.) { |
| 213 | return AMQP_STATUS_OKcxx . . $ AMQP_STATUS_OK.; |
| 214 | } |
| 215 | |
| 216 | now_ns = amqp_get_monotonic_timestampcxx . . $ amqp_get_monotonic_timestamp(f708dce9a8856c70).(); |
| 217 | if (0 == now_ns) { |
| 218 | return AMQP_STATUS_TIMER_FAILUREcxx . . $ AMQP_STATUS_TIMER_FAILURE.; |
| 219 | } |
| 220 | |
| 221 | if (now_ns > time.time_point_nscxx . . $ amqp_time_t_#time_point_ns.) { |
| 222 | return AMQP_STATUS_TIMEOUTcxx . . $ AMQP_STATUS_TIMEOUT.; |
| 223 | } |
| 224 | return AMQP_STATUS_OKcxx . . $ AMQP_STATUS_OK.; |
| 225 | } |
| 226 | |
| 227 | amqp_time_tcxx . . $ amqp_time_t# amqp_time_firstcxx . . $ amqp_time_first(2e3e50d7e7e70d5c).(amqp_time_tcxx . . $ amqp_time_t# l, amqp_time_tcxx . . $ amqp_time_t# r) { |
| 228 | if (l.time_point_nscxx . . $ amqp_time_t_#time_point_ns. < r.time_point_nscxx . . $ amqp_time_t_#time_point_ns.) { |
| 229 | return l; |
| 230 | } |
| 231 | return r; |
| 232 | } |
| 233 | |
| 234 | int amqp_time_equalcxx . . $ amqp_time_equal(e91b13531b73e403).(amqp_time_tcxx . . $ amqp_time_t# l, amqp_time_tcxx . . $ amqp_time_t# r) { |
| 235 | return l.time_point_nscxx . . $ amqp_time_t_#time_point_ns. == r.time_point_nscxx . . $ amqp_time_t_#time_point_ns.; |
| 236 | } |