Skip to content

Commit fa4c93c

Browse files
committed
[timer] Fix Timeout::wait implementation
1 parent f62ebae commit fa4c93c

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

src/modm/processing/fiber/functions.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ yield();
5353
modm::fiber::id
5454
get_id();
5555

56-
/// Yields the current fiber while `bool condition()` returns true.
56+
/// Yields the current fiber until `bool condition()` returns true.
5757
/// @warning If `bool condition()` is true on first call, no yield is performed!
5858
template< class Function >
5959
requires requires { std::is_invocable_r_v<bool, Function, void>; }

src/modm/processing/timer/timeout_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ template< class Clock, class Duration >
130130
void
131131
modm::GenericTimeout<Clock, Duration>::wait()
132132
{
133-
modm::this_fiber::poll([this]{ return not execute(); });
133+
modm::this_fiber::poll([this]{ return execute(); });
134134
}
135135

136136
// ----------------------------------------------------------------------------

test/modm/processing/fiber/fiber_test.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "fiber_test.hpp"
1414
#include "shared.hpp"
1515

16+
#include <modm/processing/timer.hpp>
1617
#include <modm-test/mock/clock.hpp>
1718

1819
using namespace std::chrono_literals;
@@ -261,3 +262,79 @@ FiberTest::testStopToken()
261262
});
262263
modm::fiber::Scheduler::run();
263264
}
265+
266+
void
267+
FiberTest::testTimeoutWait()
268+
{
269+
modm::ShortTimeout timeout(20ms);
270+
modm::fiber::Task fiber1(stack1, [&]
271+
{
272+
TEST_ASSERT_EQUALS(state++, 0u);
273+
274+
timeout.wait();
275+
TEST_ASSERT_EQUALS(state++, 5u);
276+
277+
});
278+
modm::fiber::Task fiber2(stack2, [&]
279+
{
280+
TEST_ASSERT_EQUALS(state++, 1u);
281+
modm::this_fiber::yield();
282+
TEST_ASSERT_EQUALS(state++, 2u);
283+
test_clock_ms::increment(1);
284+
modm::this_fiber::yield();
285+
TEST_ASSERT_EQUALS(state++, 3u);
286+
test_clock_ms::increment(10);
287+
modm::this_fiber::yield();
288+
TEST_ASSERT_EQUALS(state++, 4u);
289+
test_clock_ms::increment(30);
290+
modm::this_fiber::yield(); // goto 3
291+
292+
TEST_ASSERT_EQUALS(state++, 6u);
293+
});
294+
modm::fiber::Scheduler::run();
295+
}
296+
297+
void
298+
FiberTest::testPeriodicTimerWait()
299+
{
300+
modm::ShortPeriodicTimer timer(20ms);
301+
modm::fiber::Task fiber1(stack1, [&]
302+
{
303+
TEST_ASSERT_EQUALS(state++, 0u);
304+
305+
TEST_ASSERT_EQUALS(timer.wait(), 1u);
306+
TEST_ASSERT_EQUALS(state++, 3u);
307+
308+
TEST_ASSERT_EQUALS(timer.wait(), 1u); // goto 4
309+
TEST_ASSERT_EQUALS(state++, 6u);
310+
311+
TEST_ASSERT_EQUALS(timer.wait(), 3u); // goto 7
312+
TEST_ASSERT_EQUALS(state++, 9u);
313+
314+
});
315+
modm::fiber::Task fiber2(stack2, [&]
316+
{
317+
TEST_ASSERT_EQUALS(state++, 1u);
318+
modm::this_fiber::yield();
319+
TEST_ASSERT_EQUALS(state++, 2u);
320+
test_clock_ms::increment(30);
321+
modm::this_fiber::yield(); // goto 3
322+
323+
TEST_ASSERT_EQUALS(state++, 4u);
324+
test_clock_ms::increment(9);
325+
modm::this_fiber::yield();
326+
TEST_ASSERT_EQUALS(state++, 5u);
327+
test_clock_ms::increment(1);
328+
modm::this_fiber::yield(); // goto 6
329+
330+
TEST_ASSERT_EQUALS(state++, 7u);
331+
test_clock_ms::increment(15);
332+
modm::this_fiber::yield();
333+
TEST_ASSERT_EQUALS(state++, 8u);
334+
test_clock_ms::increment(50);
335+
modm::this_fiber::yield(); // goto 9
336+
337+
TEST_ASSERT_EQUALS(state++, 10u);
338+
});
339+
modm::fiber::Scheduler::run();
340+
}

test/modm/processing/fiber/fiber_test.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,10 @@ class FiberTest : public unittest::TestSuite
4343

4444
void
4545
testStopToken();
46+
47+
void
48+
testTimeoutWait();
49+
50+
void
51+
testPeriodicTimerWait();
4652
};

0 commit comments

Comments
 (0)