Use mach_approximate_time() for coarse time where available.

This lets us have a coarse-time implementation with reasonable
performance characteristics on OSX and iOS.

Implements 24427.
This commit is contained in:
Nick Mathewson
2017-12-08 09:24:02 -05:00
parent afceb431ed
commit 021fdd39e4
4 changed files with 24 additions and 0 deletions
+15
View File
@@ -314,6 +314,21 @@ monotime_get(monotime_t *out)
out->abstime_ = mach_absolute_time();
}
#if defined(HAVE_MACH_APPROXIMATE_TIME)
void
monotime_coarse_get(monotime_coarse_t *out)
{
#ifdef TOR_UNIT_TESTS
if (monotime_mocking_enabled) {
out->abstime_ = (mock_time_nsec_coarse * mach_time_info.denom)
/ mach_time_info.numer;
return;
}
#endif /* defined(TOR_UNIT_TESTS) */
out->abstime_ = mach_approximate_time();
}
#endif
/**
* Return the number of nanoseconds between <b>start</b> and <b>end</b>.
*/
+3
View File
@@ -65,6 +65,9 @@ typedef struct monotime_t {
typedef struct monotime_coarse_t {
uint64_t tick_count_;
} monotime_coarse_t;
#elif defined(__APPLE__) && defined(HAVE_MACH_APPROXIMATE_TIME)
#define MONOTIME_COARSE_FN_IS_DIFFERENT
#define monotime_coarse_t monotime_t
#else
#define monotime_coarse_t monotime_t
#endif /* defined(CLOCK_MONOTONIC_COARSE) && ... || ... */