5 ms·
Wikipedia entry on protothreads: https://en.wikipedia.org/wiki/Coroutine#Implementations_for_C https://en.wikipedia.org/wiki/Coroutine#Implementations_for_...
by sillysaurusx 6y ago
Wikipedia entry on protothreads: https://en.wikipedia.org/wiki/Coroutine#Implementations_for_C https://en.wikipedia.org/wiki/Coroutine#Implementations_for_...
The source code is interesting. LC_SET is particularly neat:
/* WARNING! lc implementation using switch() does not work if an
LC_SET() is done within another switch() statement! */
/** \hideinitializer */
typedef unsigned short lc_t;
#define LC_INIT(s) s = 0;
#define LC_RESUME(s) switch(s) { case 0:
#define LC_SET(s) s = __LINE__; case __LINE__:
#define LC_END(s) }
- edflsafoiewq 6y agoOne cool thing you can do is to manage the numbers in the case manually, eg. #define LC_SET(s,num) s = num; case num: This will let you serialize the state s, modify the program, recompile and reload s, and have it resume at the desired point.
- exDM69 6y agoYou could take this one step further by using the __COUNTER__ macro to generate unique numbers in the preprocessor. Alas, this isn't a part of the standard (iirc), so you need to depend on compiler-specific features.
- edflsafoiewq 6y agoWhat I'm thinking of is modifying it like this LC_SET(s,0) A + LC_SET(s,2) + C LC_SET(s,1) B When the number for each resume point is explicit instead of automatic, you can edit the source code while controling where any existing suspended coroutines will resume at.