From acfd220920ed78676b557eb7a200830139091037 Mon Sep 17 00:00:00 2001 From: Niklas Hauser Date: Fri, 9 Aug 2024 22:46:28 +0200 Subject: [PATCH] =?UTF-8?q?[fiber]=20Guarantee=20minimum=20for=20durations?= =?UTF-8?q?=20<=201=C2=B5s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modm/processing/fiber/functions.hpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/modm/processing/fiber/functions.hpp b/src/modm/processing/fiber/functions.hpp index abfb1482db..4f1c11d46b 100644 --- a/src/modm/processing/fiber/functions.hpp +++ b/src/modm/processing/fiber/functions.hpp @@ -87,12 +87,20 @@ poll_for(std::chrono::duration sleep_duration, Function &&condition std::chrono::duration>, modm::chrono::milli_clock, modm::chrono::micro_clock>; + // Ensure the sleep duration is at least 1 clock tick + using Duration = typename Clock::duration; + Duration clock_sleep_duration(std::chrono::duration_cast(sleep_duration)); + if constexpr (std::ratio_less_v) { + if (clock_sleep_duration == Duration(0)) + clock_sleep_duration = Duration(1); + } + const auto start = Clock::now(); do { modm::this_fiber::yield(); if (std::forward(condition)()) return true; } - while((Clock::now() - start) <= sleep_duration); + while((Clock::now() - start) <= clock_sleep_duration); return false; }