-
-
Save Meai1/1dce36bdfdf250467a7989efb4f75bc5 to your computer and use it in GitHub Desktop.
| //coroutine pseudo code explanation | |
| <hio> Okay I think I understand now why it's so hard for me to grok this, there is no regular programming API for this. Right? Who is supposed to offer an API for this, is this supposed to come from the compiler, like GCC? | |
| <hio> i think not, many people do create their own coroutine libraries so it's clearly not necessary to deal with the compiler | |
| <hio> but there is a lack of programming api surface, like.. C doesnt expose this stuff right? | |
| <hio> or is it the OS that doesnt expose it properly? | |
| <hio> I think it is probably the OS in combination with the system C library that should be offering an API to interact with the current stackframes etc. right? | |
| <hio> I feel like this isnt even a "low level" problem necessarily, you just need to get some reflection type access in your language and C/c++ dont have that so it's awkward and weirdly complex, like here c# actually allows access to stackframes: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stackframe?redirectedfrom=MSDN&view=netframework-4.7.2 | |
| List<Stack> saved_execution; | |
| void current(){ | |
| x = bla; | |
| y = bla; | |
| List<int> list = myfunc(); | |
| { // does this in the background: | |
| saved_execution.add(new Stack..save(x,y)); | |
| myfunc(); | |
| } | |
| bla x, y; | |
| } | |
| List<int> myfunc(){ | |
| for (int i=0; i < 10; ++i) | |
| { | |
| yield i; | |
| { | |
| // 1. suspend current execution | |
| // 2. save current | |
| // 3. resume whatever execution the current execution was called from | |
| saved_execution.add(new Stack()); | |
| saved_execution. | |
| } | |
| } | |
| } | |
| // lets say that at thet await bla; above we can finally free up some time on the current thread to run this: | |
| function otherFunction(){ | |
| while(true); | |
| } | |
| // now when do we switch back to current() ? |
the issue just gets so overcomplicated because language designers bake this stuff syntactically into the language
so basically it seems that coroutines are a special new way of jumping around while you execute stuff and keeping the stack sane and useful/intact while doing that.
that jumping and saving of the stack variables and information can be done with assembly instructions but some basic coroutine like logic can also be done with longjmp and setjmp.
so why even use coroutines? Exactly because especially in event driven applications it's a pain in the ass to code your functions in a way that makes them work properly when they get called with different state that signifies different kind of progress.
so instead the coroutine way of doing things lets the program "magically" resume execution in the code text position where we were before instead of having to call the entire function again when our event fires. Instead we just call the coroutine magic that will let us jump straight back into where we are.
like await Httprequest.body() might be behind a complex multithreaded event driven architecture but we dont need to know anything about it if we have await. We just call await and after it's completely finished accumulating the body, we get execution resumed.
I feel very confused about the threading, like what happens if I do an expensive database call on 4 requests on my quad core that each takes 10 seconds to complete? then my entire http server just stops responding to other cheaper requests...that seems bad, shouldnt there always be a few threads that accept requests from the socket and then offloading to a pool of workers so that request receival will never stall at least?
I still think that it's insane to use coroutines. It adds a shitload of complexity and magic to your code... for freaking what. It's so much easier to understand an event based application if you actually use the events and callbacks.
Yes it's WEIRD to code around an event based application but that's what your app is like so... I mean at some point you have to stop abstracting away reality and just accept it and make it more palatable instead of hiding it.
so ironically it would probably be easier to implement coroutines in c# userspace than in c