X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=common%2Fos_time.hpp;h=13cc733ae05488312b4fb5554d08fcb8fd49bc2d;hb=bb23f3a0bad625e3c427b9de991d4a42fa22e5f0;hp=a4f52093c94c125e7874a8e77f1597f056c0ab7b;hpb=48412ffde3dd4710c96d5e8d9cfdf1789e4d703a;p=apitrace diff --git a/common/os_time.hpp b/common/os_time.hpp index a4f5209..13cc733 100644 --- a/common/os_time.hpp +++ b/common/os_time.hpp @@ -32,26 +32,31 @@ #if defined(_WIN32) -#include -#elif defined(__linux__) -#include +# include #else -#include +# if defined(__linux__) +# include +# elif defined(__APPLE__) +# include +# else +# include +# endif +# include #endif namespace os { // OS dependent time frequency -#if defined(_WIN32) - // runtime variable on Windows +#if defined(_WIN32) || defined(__APPLE__) + // runtime variable on Windows and MacOSX extern long long timeFrequency; #elif defined(__linux__) - // nanoseconds - static const long long timeFrequency = 1000000000; + // nanoseconds on Linux + static const long long timeFrequency = 1000000000LL; #else - // microseconds on - static const long long timeFrequency = 1000000; + // microseconds on Unices + static const long long timeFrequency = 1000000LL; #endif // Time from an unknown base in a unit determined by timeFrequency @@ -72,6 +77,13 @@ namespace os { return 0; } return tp.tv_sec * 1000000000LL + tp.tv_nsec; +#elif defined(__APPLE__) + if (!timeFrequency) { + mach_timebase_info_data_t timebaseInfo; + mach_timebase_info(&timebaseInfo); + timeFrequency = 1000000000LL * timebaseInfo.denom / timebaseInfo.numer; + } + return mach_absolute_time(); #else struct timeval tv; gettimeofday(&tv, NULL); @@ -79,6 +91,15 @@ namespace os { #endif } + // Suspend execution + inline void + sleep(unsigned long usecs) { +#if defined(_WIN32) + Sleep((usecs + 999) / 1000); +#else + usleep(usecs); +#endif + } } /* namespace os */