Skip to content

Commit

Permalink
[fibers] Query condition before and after yield
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Aug 9, 2024
1 parent 8ca0de1 commit 0166e08
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/modm/processing/fiber/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ requires requires { std::is_invocable_r_v<bool, Function, void>; }
bool
poll_for(std::chrono::duration<Rep, Period> sleep_duration, Function &&condition)
{
if (std::forward<Function>(condition)()) return true;

// Only choose the microsecond clock if necessary
using Clock = std::conditional_t<
std::is_convertible_v<std::chrono::duration<Rep, Period>,
Expand All @@ -91,8 +93,8 @@ poll_for(std::chrono::duration<Rep, Period> sleep_duration, Function &&condition

const auto start = Clock::now();
do {
if (std::forward<Function>(condition)()) return true;
modm::this_fiber::yield();
if (std::forward<Function>(condition)()) return true;
}
while((Clock::now() - start) <= clock_sleep_duration);
return false;
Expand All @@ -114,11 +116,13 @@ requires requires { std::is_invocable_r_v<bool, Function, void>; }
bool
poll_until(std::chrono::time_point<Clock, Duration> sleep_time, Function &&condition)
{
if (std::forward<Function>(condition)()) return true;

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

0 comments on commit 0166e08

Please sign in to comment.