#ifndef __DISPATCH_SHIMS_TIME__
#define __DISPATCH_SHIMS_TIME__
#ifndef __DISPATCH_INDIRECT__
#error "Please #include <dispatch/dispatch.h> instead of this file directly."
#endif
#if TARGET_OS_WIN32
static inline unsigned int
sleep(unsigned int seconds)
{
Sleep(seconds * 1000); return 0;
}
#endif
typedef enum {
DISPATCH_CLOCK_WALL,
DISPATCH_CLOCK_MACH,
#define DISPATCH_CLOCK_COUNT (DISPATCH_CLOCK_MACH + 1)
} dispatch_clock_t;
#if defined(__i386__) || defined(__x86_64__) || !HAVE_MACH_ABSOLUTE_TIME
DISPATCH_ALWAYS_INLINE
static inline uint64_t
_dispatch_time_mach2nano(uint64_t machtime)
{
return machtime;
}
DISPATCH_ALWAYS_INLINE
static inline uint64_t
_dispatch_time_nano2mach(uint64_t nsec)
{
return nsec;
}
#else
typedef struct _dispatch_host_time_data_s {
dispatch_once_t pred;
long double frac;
bool ratio_1_to_1;
} _dispatch_host_time_data_s;
extern _dispatch_host_time_data_s _dispatch_host_time_data;
void _dispatch_get_host_time_init(void *context);
static inline uint64_t
_dispatch_time_mach2nano(uint64_t machtime)
{
_dispatch_host_time_data_s *const data = &_dispatch_host_time_data;
dispatch_once_f(&data->pred, NULL, _dispatch_get_host_time_init);
if (!machtime || slowpath(data->ratio_1_to_1)) {
return machtime;
}
if (machtime >= INT64_MAX) {
return INT64_MAX;
}
long double big_tmp = ((long double)machtime * data->frac) + .5;
if (slowpath(big_tmp >= INT64_MAX)) {
return INT64_MAX;
}
return (uint64_t)big_tmp;
}
static inline uint64_t
_dispatch_time_nano2mach(uint64_t nsec)
{
_dispatch_host_time_data_s *const data = &_dispatch_host_time_data;
dispatch_once_f(&data->pred, NULL, _dispatch_get_host_time_init);
if (!nsec || slowpath(data->ratio_1_to_1)) {
return nsec;
}
if (nsec >= INT64_MAX) {
return INT64_MAX;
}
long double big_tmp = ((long double)nsec / data->frac) + .5;
if (slowpath(big_tmp >= INT64_MAX)) {
return INT64_MAX;
}
return (uint64_t)big_tmp;
}
#endif
#define _dispatch_timespec_to_nano(ts) \
((uint64_t)(ts).tv_sec * NSEC_PER_SEC + (uint64_t)(ts).tv_nsec)
#define _dispatch_timeval_to_nano(tv) \
((uint64_t)(tv).tv_sec * NSEC_PER_SEC + \
(uint64_t)(tv).tv_usec * NSEC_PER_USEC)
static inline uint64_t
_dispatch_get_nanoseconds(void)
{
dispatch_static_assert(sizeof(NSEC_PER_SEC) == 8);
dispatch_static_assert(sizeof(USEC_PER_SEC) == 8);
#if TARGET_OS_MAC && DISPATCH_HOST_SUPPORTS_OSX(101200)
return clock_gettime_nsec_np(CLOCK_REALTIME);
#elif HAVE_DECL_CLOCK_REALTIME
struct timespec ts;
dispatch_assume_zero(clock_gettime(CLOCK_REALTIME, &ts));
return _dispatch_timespec_to_nano(ts);
#elif TARGET_OS_WIN32
FILETIME ft;
ULARGE_INTEGER li;
GetSystemTimeAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
return li.QuadPart * 100ull;
#else
struct timeval tv;
dispatch_assert_zero(gettimeofday(&tv, NULL));
return _dispatch_timeval_to_nano(tv);
#endif
}
static inline uint64_t
_dispatch_absolute_time(void)
{
#if HAVE_MACH_ABSOLUTE_TIME
return mach_absolute_time();
#elif HAVE_DECL_CLOCK_UPTIME && !defined(__linux__)
struct timespec ts;
dispatch_assume_zero(clock_gettime(CLOCK_UPTIME, &ts));
return _dispatch_timespec_to_nano(ts);
#elif HAVE_DECL_CLOCK_MONOTONIC
struct timespec ts;
dispatch_assume_zero(clock_gettime(CLOCK_MONOTONIC, &ts));
return _dispatch_timespec_to_nano(ts);
#elif TARGET_OS_WIN32
LARGE_INTEGER now;
return QueryPerformanceCounter(&now) ? now.QuadPart : 0;
#else
#error platform needs to implement _dispatch_absolute_time()
#endif
}
DISPATCH_ALWAYS_INLINE
static inline uint64_t
_dispatch_approximate_time(void)
{
return _dispatch_absolute_time();
}
DISPATCH_ALWAYS_INLINE
static inline uint64_t
_dispatch_time_now(dispatch_clock_t clock)
{
switch (clock) {
case DISPATCH_CLOCK_MACH:
return _dispatch_absolute_time();
case DISPATCH_CLOCK_WALL:
return _dispatch_get_nanoseconds();
}
__builtin_unreachable();
}
typedef struct {
uint64_t nows[DISPATCH_CLOCK_COUNT];
} dispatch_clock_now_cache_s, *dispatch_clock_now_cache_t;
DISPATCH_ALWAYS_INLINE
static inline uint64_t
_dispatch_time_now_cached(dispatch_clock_t clock,
dispatch_clock_now_cache_t cache)
{
if (likely(cache->nows[clock])) {
return cache->nows[clock];
}
return cache->nows[clock] = _dispatch_time_now(clock);
}
#endif // __DISPATCH_SHIMS_TIME__