Created
October 22, 2014 18:41
-
-
Save BoonsNaibot/390250793940803e7ff8 to your computer and use it in GitHub Desktop.
Line 17 & 20
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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