Skip to content

Instantly share code, notes, and snippets.

@BoonsNaibot
Created October 22, 2014 18:41
Show Gist options
  • Save BoonsNaibot/390250793940803e7ff8 to your computer and use it in GitHub Desktop.
Save BoonsNaibot/390250793940803e7ff8 to your computer and use it in GitHub Desktop.
Line 17 & 20
static struct coro {
struct coro *next;
jmp_buf state;
} first, *running = &first, *idle;
static void *pass(coro me, void *arg) {
static void *saved;
saved = arg;
if(!setjmp(me->state))
longjmp(running->state, 1);
return(saved);
}
void coroutine_main(void *ret) {
void *(*fun)(void *arg); // Function Pointer, ok...
struct coro me;
push(&idle, &me);
fun = pass(&me, ret); // Wait...what?
if(!setjmp(running->state))
coroutine_start();
for(;;) {
ret = fun(yield(&me));
push(&idle, pop(&running));
fun = pass(&me, ret);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment