If you've done any amount of deep diving in C, you're probably already familiar withDuff's Device
. For me, it was one of the keys to breaking the syntax spell; not because I use it very often, but because of the way it changed my perspective. These days, I view programming languages as sets of tools; and I'll use the best combination of tools that I can envision for a task, every time; public opinion be damned.
Coroutines are functions that may be paused and resumed.libc4life
provides coroutines in the form of a pair of macros inspired by Duff's Device. A coroutine is defined as a regular function with user specified result type and parameters by calling 'C4CORO'; except for the extra context parameter, calling is the same as any other function. Memory allocated from the stack is automatically backed up and restored on reentry. Calling 'return' aborts the current call with specified result, 'break' jumps to the end and resets the routine for a new series of calls.
And here is the implementation: