Skip to content

Instantly share code, notes, and snippets.

Created April 10, 2017 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e9a4f29a2e62452a36ba7488d7422242 to your computer and use it in GitHub Desktop.
Save anonymous/e9a4f29a2e62452a36ba7488d7422242 to your computer and use it in GitHub Desktop.
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