From b7cdca615db89454b0f742fe603f723ce5ed7c29 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 23 Jan 2015 11:25:56 +0100 Subject: Use CLOCK_MONOTONIC instead of gettimeofday gettimeofday is not a suitable clock for measuring intervals, because it can jump. --- src/dabOutput/dabOutput.h | 2 +- src/dabOutput/dabOutputSimul.cpp | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/dabOutput/dabOutput.h b/src/dabOutput/dabOutput.h index 6048d51..108fd3d 100644 --- a/src/dabOutput/dabOutput.h +++ b/src/dabOutput/dabOutput.h @@ -244,7 +244,7 @@ class DabOutputSimul : public DabOutput #ifdef _WIN32 DWORD startTime_; #else - timeval startTime_; + struct timespec startTime_; #endif }; diff --git a/src/dabOutput/dabOutputSimul.cpp b/src/dabOutput/dabOutputSimul.cpp index b40a757..2317750 100644 --- a/src/dabOutput/dabOutputSimul.cpp +++ b/src/dabOutput/dabOutputSimul.cpp @@ -49,7 +49,7 @@ int DabOutputSimul::Open(const char* name) #ifdef _WIN32 startTime_ = GetTickCount(); #else - gettimeofday(&startTime_, NULL); + clock_gettime(CLOCK_MONOTONIC, &startTime_); #endif return 0; @@ -75,18 +75,18 @@ int DabOutputSimul::Write(void* buffer, int size) } this->startTime_ += 24; #else - timeval curTime; - gettimeofday(&curTime, NULL); - current = (1000000ul * curTime.tv_sec) + curTime.tv_usec; - start = (1000000ul * this->startTime_.tv_sec) + this->startTime_.tv_usec; - waiting = 24000ul - (current - start); - if ((current - start) < 24000ul) { - usleep(waiting); + 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); } - this->startTime_.tv_usec += 24000; - if (this->startTime_.tv_usec >= 1000000) { - this->startTime_.tv_usec -= 1000000; + this->startTime_.tv_nsec += 24000000; + if (this->startTime_.tv_nsec >= 1000000000) { + this->startTime_.tv_nsec -= 1000000000; ++this->startTime_.tv_sec; } #endif -- cgit v1.2.3