#ifndef __DISPATCH_SHIMS_TIME__
#define __DISPATCH_SHIMS_TIME__
#ifndef __DISPATCH_INDIRECT__
#error "Please #include <dispatch/dispatch.h> instead of this file directly."
#endif
DISPATCH_ALWAYS_INLINE_NDEBUG
static inline void
_dispatch_contention_usleep(unsigned int us)
{
#if HAVE_MACH
#if defined(SWITCH_OPTION_DISPATCH_CONTENTION) && !(TARGET_IPHONE_SIMULATOR && \
IPHONE_SIMULATOR_HOST_MIN_VERSION_REQUIRED < 1090)
thread_switch(MACH_PORT_NULL, SWITCH_OPTION_DISPATCH_CONTENTION, us);
#else
thread_switch(MACH_PORT_NULL, SWITCH_OPTION_WAIT, ((us-1)/1000)+1);
#endif
#else
usleep(us);
#endif
}
#if TARGET_OS_WIN32
static inline unsigned int
sleep(unsigned int seconds)
{
Sleep(seconds * 1000); return 0;
}
#endif
uint64_t _dispatch_get_nanoseconds(void);
#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
static inline uint64_t
_dispatch_absolute_time(void)
{
#if HAVE_MACH_ABSOLUTE_TIME
return mach_absolute_time();
#elif TARGET_OS_WIN32
LARGE_INTEGER now;
return QueryPerformanceCounter(&now) ? now.QuadPart : 0;
#else
struct timespec ts;
int ret;
#if HAVE_DECL_CLOCK_UPTIME
ret = clock_gettime(CLOCK_UPTIME, &ts);
#elif HAVE_DECL_CLOCK_MONOTONIC
ret = clock_gettime(CLOCK_MONOTONIC, &ts);
#else
#error "clock_gettime: no supported absolute time clock"
#endif
(void)dispatch_assume_zero(ret);
return (ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec);
#endif // HAVE_MACH_ABSOLUTE_TIME
}
#endif // __DISPATCH_SHIMS_TIME__