FixScript

Forum: General Discussion: Coroutines


unevenprankster
2025/08/02 15:00
Can FixScript handle cooperative multitasking (yielding)? I haven't been able to find that implemented in one of the libraries, and the Task one is very daunting and likely not what I'm looking for.
jezek2
2025/08/02 15:49
It is in the development version which is not released yet. It needs some more work (mostly to add support for other than x86 architectures and WebAssembly).

It provides stackful coroutines in two modes:

a) using a shared big stack where only the used portion is copied out when suspending (and copied back when resuming), this is the default mode
b) using a dedicated stack of chosen size, this has about 10x faster switch time, but when you use it with too small stack you can get a hard crash

The stackful coroutines do not have any special syntax and you can call functions (including native ones) normally. The performance is best suited for buffered I/O and not for implementing things like iterators where switch time dominates.

There is also a plan to support stackless coroutines in the form of generators (mostly to support iterators), a function that is converted to a state machine under the hood and allow to return any number of values when yielding. But no actual work has been done on it yet. The implementation will be a token processor without any need for native support.

In the meantime the Task together with Channels should be comfortable for multithreading. Can you elaborate which part do you find daunting and what use case do you have? Maybe things could be improved or better documented.

Another alternative is to use async I/O which is using callbacks, or you can use a simple "polling" approach to I/O where you use non-blocking reads in a loop with a small sleep (like 10ms). This is enough to not take a lot of CPU but still going through the loop continuously.
unevenprankster
2025/08/02 16:13
Thanks for the reply! My use case is implementing state machines for actors in a game, as well as animations for things like cutscenes and UI where it's significantly simpler scripting them as a sequence of steps that gets paused and resumed.
jezek2
2025/08/04 13:42
I've created a first version of stackless coroutines, you can download it here:

https://www.fixscript.org/tmp/fixscript-coroutines-20250804.zip

There is a short documentation at the start of the "coroutines.fix" file. The "coroutine.fix" (without 's') is automatically imported by the token processor.

I think for your use case the stackless coroutines are probably the best option. But you'll be able to compare it with the stackful implementation once it's released.

Let me know if it suits your needs or something needs to be improved/changed or if you find any bugs.
unevenprankster
2025/08/04 19:29
This is exactly what I was looking for, thanks! I'll be trying it out as well exploring more of FixScript for my project.

Reply

Name:
Content:
Confirmation code:
 
  (type the letters shown in the picture)