From b86f6631d7fd7a332e33496e1e89614e2644f60e Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Tue, 14 Sep 2021 16:32:53 +0200 Subject: Timer API implementation for Linux --- zencore/timer.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'zencore/timer.cpp') diff --git a/zencore/timer.cpp b/zencore/timer.cpp index ee8e1cf9c..f67822e73 100644 --- a/zencore/timer.cpp +++ b/zencore/timer.cpp @@ -3,26 +3,41 @@ #include #include #include -#include +#if ZEN_PLATFORM_WINDOWS +# include +#elif ZEN_PLATFORM_LINUX +# include +# include +#endif namespace zen { uint64_t GetHifreqTimerValue() { +#if ZEN_PLATFORM_WINDOWS LARGE_INTEGER li; QueryPerformanceCounter(&li); return li.QuadPart; +#else + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t(ts.tv_sec) * 1000000ull) + (uint64_t(ts.tv_nsec) / 1000ull); +#endif } uint64_t internalGetHifreqTimerFrequency() { +#if ZEN_PLATFORM_WINDOWS LARGE_INTEGER li; QueryPerformanceFrequency(&li); return li.QuadPart; +#else + return 1000000ull; +#endif } static uint64_t qpcFreq = internalGetHifreqTimerFrequency(); @@ -42,6 +57,15 @@ GetHifreqTimerFrequencySafe() return qpcFreq; } +////////////////////////////////////////////////////////////////////////// + +#if !ZEN_PLATFORM_WINDOWS +void Sleep(uint32_t ms) +{ + usleep(ms * 1000U); +} +#endif + ////////////////////////////////////////////////////////////////////////// // // Testing related code follows... -- cgit v1.2.3 From 1138c44843ee77460199fde01f1e1b52c02b7f10 Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Wed, 15 Sep 2021 09:05:40 +0200 Subject: Use zen::Sleep() in timer.cpp's tests --- zencore/timer.cpp | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) (limited to 'zencore/timer.cpp') diff --git a/zencore/timer.cpp b/zencore/timer.cpp index f67822e73..08b5e06d2 100644 --- a/zencore/timer.cpp +++ b/zencore/timer.cpp @@ -57,15 +57,6 @@ GetHifreqTimerFrequencySafe() return qpcFreq; } -////////////////////////////////////////////////////////////////////////// - -#if !ZEN_PLATFORM_WINDOWS -void Sleep(uint32_t ms) -{ - usleep(ms * 1000U); -} -#endif - ////////////////////////////////////////////////////////////////////////// // // Testing related code follows... @@ -80,7 +71,7 @@ TEST_CASE("Timer") { uint64_t s0 = GetHifreqTimerValue(); uint64_t t0 = GetCpuTimerValue(); - Sleep(1000); + zen::Sleep(1000); uint64_t s1 = GetHifreqTimerValue(); uint64_t t1 = GetCpuTimerValue(); // double r = double(t1 - t0) / (s1 - s0); -- cgit v1.2.3