From a0290f8c5164b0a46e82433fced5ffd3c7cf205a Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Tue, 12 Mar 2024 13:44:54 +0100 Subject: [PATCH] base-hw: preserve usec accuracy in us_to_ticks The kernel timer used to truncated timeouts to the next lower millisecond, which not only limits the wakeup accuracy but also results in situations where a user-level timeout is triggered earlier than expected. The latter effect results in the observation of a spurious timeouts and the subsequent programming of another timeout. The patch solves the problem by preserving the sub-milliseconds bits in the 'us_to_ticks' implementation(s). Issue #5142 --- repos/base-hw/src/core/spec/arm/cortex_a9_global_timer.cc | 2 +- repos/base-hw/src/core/spec/arm/generic_timer.cc | 2 +- repos/base-hw/src/core/spec/arm/imx_epit.cc | 2 +- repos/base-hw/src/core/spec/x86_64/pit.cc | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/repos/base-hw/src/core/spec/arm/cortex_a9_global_timer.cc b/repos/base-hw/src/core/spec/arm/cortex_a9_global_timer.cc index 62b9602991..242b0fe235 100644 --- a/repos/base-hw/src/core/spec/arm/cortex_a9_global_timer.cc +++ b/repos/base-hw/src/core/spec/arm/cortex_a9_global_timer.cc @@ -102,7 +102,7 @@ unsigned Timer::interrupt_id() const { time_t Timer::us_to_ticks(time_t const us) const { - return (us / 1000) * TICS_PER_MS; } + return (us * TICS_PER_MS) / 1000; } time_t Timer::_duration() const { diff --git a/repos/base-hw/src/core/spec/arm/generic_timer.cc b/repos/base-hw/src/core/spec/arm/generic_timer.cc index efdc010751..0d702cecd3 100644 --- a/repos/base-hw/src/core/spec/arm/generic_timer.cc +++ b/repos/base-hw/src/core/spec/arm/generic_timer.cc @@ -58,7 +58,7 @@ time_t Timer::ticks_to_us(time_t const ticks) const { time_t Timer::us_to_ticks(time_t const us) const { - return (us / 1000) * _device.ticks_per_ms; } + return (us * _device.ticks_per_ms) / 1000; } time_t Timer::_max_value() const { diff --git a/repos/base-hw/src/core/spec/arm/imx_epit.cc b/repos/base-hw/src/core/spec/arm/imx_epit.cc index 5145c265c5..6854f90c94 100644 --- a/repos/base-hw/src/core/spec/arm/imx_epit.cc +++ b/repos/base-hw/src/core/spec/arm/imx_epit.cc @@ -84,7 +84,7 @@ time_t Timer::ticks_to_us(time_t const ticks) const { time_t Timer::us_to_ticks(time_t const us) const { - return (us / 1000UL) * Board::Timer::TICS_PER_MS; } + return (us * Board::Timer::TICS_PER_MS) / 1000; } time_t Timer::_max_value() const { diff --git a/repos/base-hw/src/core/spec/x86_64/pit.cc b/repos/base-hw/src/core/spec/x86_64/pit.cc index 60bdb5662d..bb895b9084 100644 --- a/repos/base-hw/src/core/spec/x86_64/pit.cc +++ b/repos/base-hw/src/core/spec/x86_64/pit.cc @@ -109,7 +109,7 @@ time_t Timer::ticks_to_us(time_t const ticks) const { time_t Timer::us_to_ticks(time_t const us) const { - return (us / 1000) * _device.ticks_per_ms; } + return (us * _device.ticks_per_ms) / 1000; } time_t Timer::_max_value() const {