From 79937c9495d73539a4c6af20dc91d9cd098dd593 Mon Sep 17 00:00:00 2001 From: Yue Wang Date: Sun, 15 Jul 2018 13:22:53 -0700 Subject: [PATCH 1/2] Support timed_wait in microseconds level Some Audio plugin (such as ALSA, and soon CoreAudio) already support microsecond level buffer time. However, current interval less than 1000 microseconds will cause a bug that the code treated as 0 ms. --- src/thread/PosixCond.hxx | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/thread/PosixCond.hxx b/src/thread/PosixCond.hxx index e1c272071..c6a1b9096 100644 --- a/src/thread/PosixCond.hxx +++ b/src/thread/PosixCond.hxx @@ -75,13 +75,13 @@ public: } private: - bool timed_wait(PosixMutex &mutex, unsigned timeout_ms) noexcept { + bool timed_wait(PosixMutex &mutex, long timeout_us) noexcept { struct timeval now; gettimeofday(&now, nullptr); struct timespec ts; - ts.tv_sec = now.tv_sec + timeout_ms / 1000; - ts.tv_nsec = (now.tv_usec + (timeout_ms % 1000) * 1000) * 1000; + ts.tv_sec = now.tv_sec + timeout_us / 1000000; + ts.tv_nsec = (now.tv_usec + (timeout_us % 1000000)) * 1000; // Keep tv_nsec < 1E9 to prevent return of EINVAL if (ts.tv_nsec >= 1000000000) { ts.tv_nsec -= 1000000000; @@ -94,13 +94,13 @@ private: public: bool timed_wait(PosixMutex &mutex, std::chrono::steady_clock::duration timeout) noexcept { - auto timeout_ms = std::chrono::duration_cast(timeout).count(); - if (timeout_ms < 0) - timeout_ms = 0; - else if (timeout_ms > std::numeric_limits::max()) - timeout_ms = std::numeric_limits::max(); + auto timeout_us = std::chrono::duration_cast(timeout).count(); + if (timeout_us < 0) + timeout_us = 0; + else if (timeout_us > std::numeric_limits::max()) + timeout_us = std::numeric_limits::max(); - return timed_wait(mutex, timeout_ms); + return timed_wait(mutex, timeout_us); } }; From 866821765af714536fe60502cfab29a2a11d5c1c Mon Sep 17 00:00:00 2001 From: Yue Wang Date: Mon, 16 Jul 2018 09:42:04 -0700 Subject: [PATCH 2/2] use uint_least32_t instead of long for timeout --- src/thread/PosixCond.hxx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/thread/PosixCond.hxx b/src/thread/PosixCond.hxx index c6a1b9096..c28147257 100644 --- a/src/thread/PosixCond.hxx +++ b/src/thread/PosixCond.hxx @@ -75,7 +75,7 @@ public: } private: - bool timed_wait(PosixMutex &mutex, long timeout_us) noexcept { + bool timed_wait(PosixMutex &mutex, uint_least32_t timeout_us) noexcept { struct timeval now; gettimeofday(&now, nullptr); @@ -97,8 +97,8 @@ public: auto timeout_us = std::chrono::duration_cast(timeout).count(); if (timeout_us < 0) timeout_us = 0; - else if (timeout_us > std::numeric_limits::max()) - timeout_us = std::numeric_limits::max(); + else if (timeout_us > std::numeric_limits::max()) + timeout_us = std::numeric_limits::max(); return timed_wait(mutex, timeout_us); }