Skip to content

Instantly share code, notes, and snippets.

/coroutine.c
Created Apr 10, 2017

Embed
What would you like to do?
void yield(coroutine* c, ...)
{
va_list ap;
void *arg;
c->arg_ptr = (void **)(c->arg_buf);
va_start(ap, c);
do {
arg = va_arg(ap, void *);
*c->arg_ptr++ = arg;
} while(arg != NULL);
va_end(ap);
if(!setjmp(c->callee_context)) {
longjmp(c->caller_context, WORKING);
}
}
int next(coroutine *c, void (*on_next)(coroutine *c, va_list *ap), ...)
{
int ret = setjmp(c->caller_context);
if(!ret) {
longjmp(c->callee_context, WORKING);
} else {
va_list ap;
va_start(ap, on_next);
on_next(c, &ap);
va_end(ap);
return ret == WORKING;
}
}
void on_next(coroutine *c, va_list *ap)
{
int *x, *y;
x = va_arg(*ap, int *);
y = va_arg(*ap, int *);
*x = *(int *)c->arg_buf[0];
*y = *(int *)c->arg_buf[1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.