3 ms·
Thanks for submitting this article and thank you to the author. This is exactly the kind of technical post I love to see. I recently wrote a simple barebones
by samsquire 4y ago
Thanks for submitting this article and thank you to the author.
This is exactly the kind of technical post I love to see.
I recently wrote a simple barebones toy amd64 x86_64 compiler and coroutines would be a beautiful thing to add to the compiler. I think I can learn from this and the Inko programming language which refers to Tina a coroutine library
https://github.com/samsquire/compiler https://github.com/samsquire/compiler
https://slembcke.github.io/Tina https://slembcke.github.io/Tina
This area "coroutines" is an area I am particularly interested in. I recently wrote an unrolled switch based state machine async/await implementation in Java which can run tasks on a background thread. When I say unrolled I mean that this work would usually be done by a transpiler or macros but I wrote the switch by hand to see if it would work.
This is similar to protothreads for C.
My async/await unrolled switch statement could in theory be implemented in any language that has switch statements and without threading. See the Java code here. The switch statement represents a state machine that decides whether to fork or yield or execute statements of the following virtual program:
task1:
while True:
handle = async task2()
handle2 = async task3() # if threading were enabled, this would eagerly run in parallel to task2, simultaneously
rvalue1 = await handle
rvalue2 = await handle2
task2:
while True:
yield value++
task3:
while True:
yield value2++
https://github.com/samsquire/multiversion-concurrency-control/blob/main/src/main/java/main/TokenRingTimer2AsyncAwait3.java https://github.com/samsquire/multiversion-concurrency-contro...
I am still working on allowing multiple coroutines to be in flight in parallel at the same time. At the moment the tasks share the same background thread.
I asked this stackoverflow question regarding C++ coroutines, as I wanted to use coroutines with a thread pool.
https://stackoverflow.com/questions/74520133/how-can-i-pass-things-to-a-c20-coroutine-co-await-await-suspend-operator-funct https://stackoverflow.com/questions/74520133/how-can-i-pass-...
Another avenue of my research into parallelism is preempting threads from other threads by setting other thread's looping variable to the limit. I feel this could be an alternative to doing a check inside a loop that the author does.
Code here in Rust, Java and C shows this userspace 1:M:N scheduler where N lightweight threads are multiplexed over M kernel threads and 1 scheduler thread. This is similar to a golang runtime.
https://github.com/samsquire/preemptible-thread https://github.com/samsquire/preemptible-thread