From 18b3c71d48c131d34c15dd9e81692ff2622bff37 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 8 Dec 2016 19:28:16 +0100 Subject: Alternative approach to semaphore timed wait. --- src/semaphore.cc | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/semaphore.cc b/src/semaphore.cc index 42c9aec..51014b3 100644 --- a/src/semaphore.cc +++ b/src/semaphore.cc @@ -119,18 +119,12 @@ bool Semaphore::wait(const std::chrono::milliseconds& timeout) } #else struct timespec ts; - - struct timeval now; - int rv = gettimeofday(&now, nullptr); - assert(rv == 0); - - ts.tv_sec = now.tv_sec; - ts.tv_nsec = now.tv_usec * 1000; + clock_gettime(CLOCK_REALTIME, &ts); // Add timeout time_t seconds = (time_t)(timeout.count() / 1000); ts.tv_sec += seconds; - ts.tv_nsec += (long)((timeout.count() - (seconds * 1000)) * 1000000); + ts.tv_nsec += (long)((timeout.count() % 1000) * 1000000); // Make sure we don't overflow the nanoseconds. constexpr long nsec = 1000000000LL; -- cgit v1.2.3