Skip to content

Instantly share code, notes, and snippets.

@aprell
Created May 19, 2019 17:55
Show Gist options
  • Select an option

  • Save aprell/b9dccaf05bf53b3b6a76d50dc7f1da15 to your computer and use it in GitHub Desktop.

Select an option

Save aprell/b9dccaf05bf53b3b6a76d50dc7f1da15 to your computer and use it in GitHub Desktop.
Slightly different take on http://dunkels.com/adam/pt/expansion.html (1)
#include <stdbool.h>
#include <stdio.h>
struct coro {
unsigned int lc;
bool done;
};
#define CORO_INIT(co) \
(co)->lc = 0; \
(co)->done = false
#define CORO_INITIALIZER \
(struct coro){ .lc = 0, .done = false }
#define CORO_BEGIN(co) \
switch ((co)->lc) { case 0:
#define CORO_END(co, res) \
} (co)->lc = 0; (co)->done = true; return res
#define CORO_YIELD(co, res) \
(co)->lc = __LINE__; return res; case __LINE__:
static int test(struct coro *co, int n)
{
static int i;
CORO_BEGIN(co);
for (i = 0; i < 5; i++) {
CORO_YIELD(co, n + i);
}
CORO_END(co, n + 5);
}
int main(void)
{
struct coro co = CORO_INITIALIZER;
int n = 100;
while (!co.done) {
n = test(&co, n);
printf("Back in main with n = %d\n", n);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment