#Any work-arounds for creating coroutines (async) in stage2?

1 messages · Page 1 of 1 (latest)

noble narwhal
#

I'm weighing my options between going back to stage1 (and facing old bugs) or delaying parts of projects until 0.11.

I'm curious whether anyone has come up with work-arounds (or hacks) to get coroutine-like behavior in stage2, to get the appearance of calling a function, yielding a result, and resuming the function later.

lilac jasper
#

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

noble narwhal
#

Nice! I'll take a look, thanks!

#

I'd be interested in seeing your port when you get far enough to post it!

lilac jasper
#

sure thing

pseudo matrix
#

You can not use async

#

async after all is just a state machine

#

you can create a generator of state machines, and contexts

noble narwhal
#

Generator of state machines and contexts... would you have an example, or resource to point me to?

pseudo matrix
noble narwhal
#

Thanks!!

pseudo matrix
#

it will be cumbersome

#

but such is life

noble narwhal
#

Better than putting it off for a year 😃

pseudo matrix
#

yea

#

@noble narwhal if you need further help on zigfsm, I use it catlaugh

thin pond
noble narwhal
#

I appreciate the link, I'll take a look!

late anvil
#

That minicoro library might work but again it's very much undefined behaviour

lilac jasper
#

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

late anvil
#

Yes, returning from a function multiple times is UB in Zig

lilac jasper
#

he specifically wanted a hack/workaround

late anvil
#

Sure, I just wanted to be clear about how it's very much undefined :)

#

Though I still don't understand the longjmp/setcontext suggestion

lilac jasper
#

you can use the combo to create cooperative multitasking

late anvil
#

Ah right yes I forgot setcontext can switch stacks

lilac jasper
#

and it has the bonus of having the right assembly for a wide set of architectures

lilac jasper