[<<][c][>>][..]
Sat May 9 02:25:24 CEST 2015

State machines with simple for loops

In C without too much ado.

Basic idea is to capture all the local variables in a struct, and have
entry points for all the "wait" statements.

These can be implemented as lables.

wait(COND) then boils down to:


label: if (!(COND)) { x->next = &label; return; }


That's just one macro:

#define WAIT(label,condition) \
  label: if (!(condition)) { state->next = &&label; return 0; }

Where we're assuming local context:
  - variable name "state"
  - field name "next"
  - 0 means task is blocked (other returns can have error conditions).

Use computed goto (addresses) but if that's not available, use a case
statement.

EDIT: going for it.  Also implementing sub-machine call (like
subroutine, but executing an encapsulated state machine until halt).




[Reply][About]
[<<][c][>>][..]