#Any work-arounds for creating coroutines (async) in stage2?
1 messages · Page 1 of 1 (latest)
getcontext / setcontext, and setjmp longjmp all work
also works with minor modification to zig translate-c output
i'm working on a pure zig port for the same reason, tired of waiting for async in stage 2
Nice! I'll take a look, thanks!
I'd be interested in seeing your port when you get far enough to post it!
sure thing
You can not use async
async after all is just a state machine
you can create a generator of state machines, and contexts
Generator of state machines and contexts... would you have an example, or resource to point me to?
Thanks!!
Better than putting it off for a year 😃
another option is using callback based control flow: https://github.com/tigerbeetledb/tigerbeetle/blob/main/src/io/test.zig#L10-L82 accomplishes yielding but done explicitely
I appreciate the link, I'll take a look!
I don't understand this suggestion. Not only is longjmp/setcontext undefined behaviour in Zig, but also it doesn't let you implement coroutines
That minicoro library might work but again it's very much undefined behaviour
weird considering I’ve tested everything I just suggested and it does work as expected
UB yes definitely
though if you have true C compatibility and the ability to run assembly is that real UB
Yes, returning from a function multiple times is UB in Zig
he specifically wanted a hack/workaround
Sure, I just wanted to be clear about how it's very much undefined :)
Though I still don't understand the longjmp/setcontext suggestion
you can use the combo to create cooperative multitasking
Ah right yes I forgot setcontext can switch stacks
and it has the bonus of having the right assembly for a wide set of architectures
