diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2015-01-23 11:25:56 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2015-01-23 11:25:56 +0100 |
commit | b7cdca615db89454b0f742fe603f723ce5ed7c29 (patch) | |
tree | c6d66a80b4552c30bff7c13d4623e3bc63a08320 | |
parent | 86a147ba46aad4516913ba7b5ad7d37d79422a04 (diff) | |
download | dabmux-b7cdca615db89454b0f742fe603f723ce5ed7c29.tar.gz dabmux-b7cdca615db89454b0f742fe603f723ce5ed7c29.tar.bz2 dabmux-b7cdca615db89454b0f742fe603f723ce5ed7c29.zip |
Use CLOCK_MONOTONIC instead of gettimeofday
gettimeofday is not a suitable clock for measuring intervals,
because it can jump.
-rw-r--r-- | src/dabOutput/dabOutput.h | 2 | ||||
-rw-r--r-- | 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 |