Skip to content

Commit

Permalink
Merge pull request #5 from ChrisPardy/addFsmTimeoutDetection
Browse files Browse the repository at this point in the history
add functionality to tell if a wait on a signal timed out
  • Loading branch information
alexhgreen07 authored Oct 22, 2021
2 parents e14d061 + f27d3da commit 65fd2cb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions emb/fsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ void Fsm::sleep(unsigned long milliseconds) {
scheduler->sleepCurrentExecutor(milliseconds);
}

bool Fsm::expired() { return lastSignal == NULL; }

void Fsm::wait(Signal &signal, unsigned long milliseconds) {
assert(scheduler != NULL);
signal.waitCurrentExecutor(*scheduler, milliseconds);
Expand Down Expand Up @@ -127,6 +129,7 @@ void Scheduler::wakeupExpired(list<Entry> &pendingList) {
if (clock.millis() >= current->value.expiry) {
pendingList.remove(0);
current->value.signal = NULL;
current->value.fsm->lastSignal = NULL;
ready.insert(*current);
} else {
break;
Expand Down Expand Up @@ -164,6 +167,7 @@ void Scheduler::wakeup(list<Entry> &pendingList, Signal &signal, bool all,
if (current->value.signal == &signal) {
pendingList.remove(*current);
current->value.signal = NULL;
current->value.fsm->lastSignal = &signal;
pending.insert(*current);
if (!all)
break;
Expand Down
3 changes: 3 additions & 0 deletions emb/fsm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Fsm {
void wait(Signal &signal, unsigned long milliseconds = (unsigned long)-1);
void sleep(unsigned long milliseconds);

bool expired();

Scheduler *scheduler;

private:
Expand All @@ -50,6 +52,7 @@ class Fsm {
void execute();
State next;
Fsm *parent;
Signal *lastSignal;
};

class Scheduler {
Expand Down
4 changes: 4 additions & 0 deletions test/test_fsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using emb::array;
using namespace FsmFramework;

bool TestFsm::expired() { return Fsm::expired(); }

void TestFsm::initial() {}

void TestFsm::increment() { counter++; }
Expand Down Expand Up @@ -169,6 +171,7 @@ TEST(Fsm, WaitsForSignal) {
for (auto &fsm : otherFsms) {
LONGS_EQUAL(totalExecutionCount, fsm.counter);
}
CHECK_FALSE(testFsm.expired());
}

TEST(Fsm, WaitsForBroadcast) {
Expand Down Expand Up @@ -213,6 +216,7 @@ TEST(Fsm, WaitsForSignalTimeout) {
for (auto &fsm : otherFsms) {
LONGS_EQUAL(totalExecutionCount, fsm.counter);
}
CHECK(testFsm.expired());
}

TEST(Fsm, WaitsForMultipleSignals) {
Expand Down
2 changes: 2 additions & 0 deletions test/test_fsm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ struct FsmTestGroupBase : public Utest {

struct TestFsm : public Fsm {

bool expired();

virtual void initial() override;
void increment();
void ping();
Expand Down

0 comments on commit 65fd2cb

Please sign in to comment.