132 #ifndef TOR_COMPAT_TIME_H
133 #define TOR_COMPAT_TIME_H
135 #include "orconfig.h"
141 #undef HAVE_CLOCK_GETTIME
144 #if defined(HAVE_CLOCK_GETTIME)
149 #if !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
153 unsigned int tv_usec;
162 #elif defined(HAVE_CLOCK_GETTIME)
165 #elif defined (_WIN32)
169 #define MONOTIME_USING_GETTIMEOFDAY
175 #if defined(CLOCK_MONOTONIC_COARSE) && \
176 defined(HAVE_CLOCK_GETTIME)
177 #define MONOTIME_COARSE_FN_IS_DIFFERENT
178 #define monotime_coarse_t monotime_t
179 #elif defined(_WIN32)
180 #define MONOTIME_COARSE_FN_IS_DIFFERENT
181 #define MONOTIME_COARSE_TYPE_IS_DIFFERENT
183 typedef struct monotime_coarse_t {
184 uint64_t tick_count_;
186 #elif defined(__APPLE__) && defined(HAVE_MACH_APPROXIMATE_TIME)
187 #define MONOTIME_COARSE_FN_IS_DIFFERENT
188 #define monotime_coarse_t monotime_t
190 #define monotime_coarse_t monotime_t
251 #if defined(MONOTIME_COARSE_FN_IS_DIFFERENT)
255 void monotime_coarse_get(monotime_coarse_t *out);
259 uint64_t monotime_coarse_absolute_nsec(
void);
260 uint64_t monotime_coarse_absolute_usec(
void);
261 uint64_t monotime_coarse_absolute_msec(
void);
263 #define monotime_coarse_get monotime_get
264 #define monotime_coarse_absolute_nsec monotime_absolute_nsec
265 #define monotime_coarse_absolute_usec monotime_absolute_usec
266 #define monotime_coarse_absolute_msec monotime_absolute_msec
287 uint64_t monotime_msec_to_approx_coarse_stamp_units(uint64_t msec);
290 #if defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT)
294 int64_t monotime_coarse_diff_nsec(
const monotime_coarse_t *start,
295 const monotime_coarse_t *end);
296 int64_t monotime_coarse_diff_usec(
const monotime_coarse_t *start,
297 const monotime_coarse_t *end);
298 int64_t monotime_coarse_diff_msec(
const monotime_coarse_t *start,
299 const monotime_coarse_t *end);
303 void monotime_coarse_zero(monotime_coarse_t *out);
304 int monotime_coarse_is_zero(
const monotime_coarse_t *val);
305 void monotime_coarse_add_msec(monotime_coarse_t *out,
306 const monotime_coarse_t *val, uint32_t msec);
308 #define monotime_coarse_diff_nsec monotime_diff_nsec
309 #define monotime_coarse_diff_usec monotime_diff_usec
310 #define monotime_coarse_diff_msec monotime_diff_msec
311 #define monotime_coarse_zero monotime_zero
312 #define monotime_coarse_is_zero monotime_is_zero
313 #define monotime_coarse_add_msec monotime_add_msec
326 const monotime_coarse_t *end);
337 static inline int32_t
339 const monotime_coarse_t *end)
341 #if SIZEOF_VOID_P == 8
343 return (int32_t) monotime_coarse_diff_msec(start, end);
345 #define USING_32BIT_MSEC_HACK
350 #ifdef TOR_UNIT_TESTS
351 void tor_sleep_msec(
int msec);
353 void monotime_enable_test_mocking(
void);
354 void monotime_disable_test_mocking(
void);
355 void monotime_set_mock_time_nsec(int64_t);
356 #if defined(MONOTIME_COARSE_FN_IS_DIFFERENT)
357 void monotime_coarse_set_mock_time_nsec(int64_t);
359 #define monotime_coarse_set_mock_time_nsec monotime_set_mock_time_nsec
363 #ifdef COMPAT_TIME_PRIVATE
364 #if defined(_WIN32) || defined(TOR_UNIT_TESTS)
365 STATIC int64_t ratchet_performance_counter(int64_t count_raw);
366 STATIC int64_t ratchet_coarse_performance_counter(int64_t count_raw);
368 #if defined(MONOTIME_USING_GETTIMEOFDAY) || defined(TOR_UNIT_TESTS)
369 STATIC void ratchet_timeval(
const struct timeval *timeval_raw,
372 #ifdef TOR_UNIT_TESTS
373 void monotime_reset_ratchets_for_testing(
void);
void monotime_get(monotime_t *out)
int monotime_is_zero(const monotime_t *out)
static int32_t monotime_coarse_diff_msec32(const monotime_coarse_t *start, const monotime_coarse_t *end)
int64_t monotime_diff_msec(const monotime_t *start, const monotime_t *end)
uint32_t monotime_coarse_to_stamp(const monotime_coarse_t *t)
uint64_t monotime_coarse_stamp_units_to_approx_msec(uint64_t units)
void monotime_zero(monotime_t *out)
void monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec)
uint64_t monotime_absolute_nsec(void)
int64_t monotime_diff_nsec(const monotime_t *start, const monotime_t *end)
uint32_t monotime_coarse_get_stamp(void)
int64_t monotime_diff_usec(const monotime_t *start, const monotime_t *end)
int32_t monotime_coarse_diff_msec32_(const monotime_coarse_t *start, const monotime_coarse_t *end)
uint64_t monotime_absolute_msec(void)
uint64_t monotime_absolute_usec(void)
#define MOCK_DECL(rv, funcname, arglist)
Definitions for timing-related constants.
Header for tor_gettimeofday.c.
Integer definitions used throughout Tor.