more accurate getTickCount for macOS

This commit is contained in:
Johannes Pohl 2016-10-19 09:22:37 +02:00
parent ef6aa89222
commit e91eb74f3e

View file

@ -21,6 +21,10 @@
#include <chrono> #include <chrono>
#include <sys/time.h> #include <sys/time.h>
#ifdef MACOS
#include <mach/clock.h>
#include <mach/mach.h>
#endif
namespace chronos namespace chronos
{ {
@ -49,9 +53,12 @@ namespace chronos
inline static long getTickCount() inline static long getTickCount()
{ {
#ifdef MACOS #ifdef MACOS
struct timeval now; clock_serv_t cclock;
gettimeofday(&now, NULL); mach_timespec_t mts;
return now.tv_sec*1000 + now.tv_usec / 1000; host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
return mts.tv_sec*1000 + mts.tv_nsec / 1000000;
#else #else
struct timespec now; struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);