I use protothreads on my embedded device (STM32, GCC) as it's lightweight, and saves me to handle a complex state machine by myself.
As my main program grows, a PT_* function is called on a line number greater than 256
struct pt main_pt;
int main(void)
{
PT_BEGIN(pt);
while(true)
{
/*... 256 lines further*/
PT_WAIT_UNTIL(pt, flag);
PT_YIELD(pt);
}
PT_END(pt);
}
And at compile, it throws this error:
main.c: In function 'main':
main.c:256:4: error: case label value exceeds maximum value for type [-Werror]
PT_WAIT_UNTIL(pt, flag);
main.c:257:4: error: case label value exceeds maximum value for type [-Werror]
PT_YIELD(pt);
If PT_YIELD or PT_WAIT_UNTIL is placed on a line number lower than 256 it does compile.
Here is snippet for PT_YIELD, PT_WAIT_UNTIL, LC_SET and struct pt:
typedef uint32_t lc_t;
struct pt {
lc_t lc;
};
#define LC_RESUME(s) switch(s) { case 0:
#define LC_SET(s) s = __LINE__; case __LINE__:
#define PT_BEGIN(pt) { unsigned int PT_YIELD_FLAG = 1; LC_RESUME((pt)->lc)
#define PT_WAIT_UNTIL(pt, condition) \
do { \
LC_SET((pt)->lc); \
if(!(condition)) { \
return PT_WAITING; \
} \
} while(0)
#define PT_YIELD(pt) \
do { \
PT_YIELD_FLAG = 0; \
LC_SET((pt)->lc); \
if(PT_YIELD_FLAG == 0) { \
return PT_YIELDED; \
} \
} while(0)
Why can't "case __LINE__:" exceed 256 as it's store on uint32_t?