Skip to content

Commit

Permalink
[fiber] Guarantee minimum for durations < 1µs
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Aug 9, 2024
1 parent 6e03e09 commit acfd220
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/modm/processing/fiber/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,20 @@ poll_for(std::chrono::duration<Rep, Period> sleep_duration, Function &&condition
std::chrono::duration<Rep, std::milli>>,
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<Duration>(sleep_duration));
if constexpr (std::ratio_less_v<Period, typename Clock::period>) {
if (clock_sleep_duration == Duration(0))
clock_sleep_duration = Duration(1);
}

const auto start = Clock::now();
do {
modm::this_fiber::yield();
if (std::forward<Function>(condition)()) return true;
}
while((Clock::now() - start) <= sleep_duration);
while((Clock::now() - start) <= clock_sleep_duration);
return false;
}

Expand Down

0 comments on commit acfd220

Please sign in to comment.