From 126cc8829c003d483839fd230f979e47dcf856ae Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sat, 5 Mar 2016 17:28:24 +0100 Subject: Replace platform specific simul:// by C++11 chrono --- src/dabOutput/dabOutput.h | 4 +++- src/dabOutput/dabOutputSimul.cpp | 27 ++++++++++++--------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/dabOutput/dabOutput.h b/src/dabOutput/dabOutput.h index 2c4d1ef..6756027 100644 --- a/src/dabOutput/dabOutput.h +++ b/src/dabOutput/dabOutput.h @@ -36,7 +36,9 @@ #include #include #include +#include #include + #ifdef _WIN32 # include # ifdef __MINGW32__ @@ -305,7 +307,7 @@ class DabOutputSimul : public DabOutput #ifdef _WIN32 DWORD startTime_; #else - struct timespec startTime_; + std::chrono::steady_clock::time_point startTime_; #endif }; diff --git a/src/dabOutput/dabOutputSimul.cpp b/src/dabOutput/dabOutputSimul.cpp index 2317750..04ce536 100644 --- a/src/dabOutput/dabOutputSimul.cpp +++ b/src/dabOutput/dabOutputSimul.cpp @@ -28,6 +28,8 @@ #include #include #include +#include +#include #ifdef _WIN32 # include # ifdef __MINGW32__ @@ -49,7 +51,7 @@ int DabOutputSimul::Open(const char* name) #ifdef _WIN32 startTime_ = GetTickCount(); #else - clock_gettime(CLOCK_MONOTONIC, &startTime_); + startTime_ = std::chrono::steady_clock::now(); #endif return 0; @@ -57,11 +59,10 @@ int DabOutputSimul::Open(const char* name) int DabOutputSimul::Write(void* buffer, int size) { +#ifdef _WIN32 unsigned long current; unsigned long start; unsigned long waiting; - -#ifdef _WIN32 current = GetTickCount(); start = this->startTime_; if (current < start) { @@ -75,20 +76,16 @@ int DabOutputSimul::Write(void* buffer, int size) } this->startTime_ += 24; #else - struct timespec curTime; - clock_gettime(CLOCK_MONOTONIC, &curTime); - current = (1000000000ul * curTime.tv_sec) + curTime.tv_nsec; - start = (1000000000ul * this->startTime_.tv_sec) + this->startTime_.tv_nsec; - waiting = 24000000ul - (current - start); - if ((current - start) < 24000000ul) { - usleep(waiting / 1000); - } + auto curTime = std::chrono::steady_clock::now(); + + auto diff = curTime - startTime_; + auto waiting = std::chrono::milliseconds(24) - diff; - this->startTime_.tv_nsec += 24000000; - if (this->startTime_.tv_nsec >= 1000000000) { - this->startTime_.tv_nsec -= 1000000000; - ++this->startTime_.tv_sec; + if (diff < std::chrono::milliseconds(24)) { + std::this_thread::sleep_for(waiting); } + + startTime_ += std::chrono::milliseconds(24); #endif return size; -- cgit v1.2.3