3 ms·
How do switches simulate coroutines? I'm reading thru the article they linked https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html https://www.chiark.gr
by TYMorningCoffee 6y ago
How do switches simulate coroutines? I'm reading thru the article they linked https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
But don't follow how they achieve independent stacks so a caller can continue where it left off in the callee.
- xaedes 6y agoIt is using static variables for coroutine state.
- masklinn 6y ago> But don't follow how they achieve independent stacks so a caller can continue where it left off in the callee. They're "stackless" coroutine, you can only "yield" at the toplevel (where the switch can resume), and each coroutine function can only animate one coroutine because the state is global (it's a static).
- simias 6y agoThe mednafen PSX emulator uses this trick to implement some of the modules (the MDEC and SPU use it IIRC). I always found it hard to follow and personally prefer simpler, more verbose code with explicit state management.
- swiley 6y agoThere's no way to actually create something like call/cc in c so the coroutines can't yield while in a subroutine. It's more like syntactic sugar for for(;;)switch(task){...}