Skip to content

Conversation

@yinamy
Copy link

@yinamy yinamy commented Dec 15, 2025

This patch:

  1. Adds a new interface fiber-switch.h with a new fiber operation fiber_switch.
  2. Adds a test program hello_switch.c that uses fiber_switch.
  3. Adds an asyncify implementation for fiber_switch which works with the test program above.

yinamy and others added 2 commits December 16, 2025 14:10
Co-authored-by: Daniel Hillerström <1827113+dhil@users.noreply.github.com>
…estion, updated the hello world test program accordingly
@yinamy
Copy link
Author

yinamy commented Dec 19, 2025

@dhil The new commit tries to do everything you mentioned above, it works with my updated version of hello_switch.c (I'll make some new examples later). I have a few uncertainties about it:

  1. In your example code for fiber_main and the main from the user program, you allocate a fiber running the main user program twice. I resolve this by only allocating it in fiber_main, exporting a new function get_active_fiber() in fiber_switch.h so that it can be referenced by the user program, and having it as a global variable in the user program. Not sure if this is the best solution because I feel like the user program shouldn't touch main_fiber this much, but I can't think of anything else for now.
  2. What should fibers do when they return? It makes sense to track the previously-running fiber with an extra field in the fiber_t struct (which I added), so that we can just resume the previous fiber when the current one returns. However, my example program keeps switching between the same two fibers hello_fiber and world_fiber as soon as the first switch from main_fiber happens, so main_fiber gets lost. I assume main_fiber should always finish running, so in this commit the user program explicitly tells the last-running user fiber to switch back to main_fiber, and the fiber_main checks if main_fiber is done. Also not sure if this is ideal!

Copy link
Member

@dhil dhil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's looking good. Just a few comments.

Semantically, I think the canonical reduction rule for switch is something like:

[switch] fiber_main(E[fiber_switch(target_E', arg, out)]) ~> fiber_main(E'[(arg, E)])

where E denotes the current evaluation context, and E' subscript denotes the evaluation context of the target, and the notation (arg, E) means substitute arg as the return value in E' and substitute E for out in E'.

Comment on lines 48 to 50
/** trying stuff */
export("get_active_fiber")
fiber_t get_active_fiber(void);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this is needed -- I would be hesitant to add it, because I think it will be a bit problematic to implement in the WasmFX backend, as we would need to track which fiber is currently executing there too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would if be better if I changed it to something like get_main_fiber, where it returns fiber_main specifically? (where fiber_main is the fiber running the main function of the user program)

yinamy and others added 9 commits December 21, 2025 15:41
Co-authored-by: Daniel Hillerström <1827113+dhil@users.noreply.github.com>
Co-authored-by: Daniel Hillerström <1827113+dhil@users.noreply.github.com>
Co-authored-by: Daniel Hillerström <1827113+dhil@users.noreply.github.com>
Co-authored-by: Daniel Hillerström <1827113+dhil@users.noreply.github.com>
…r_main() to make it work after Daniel's changes
…h switches to a target fiber and destroys the existing one.

Added test file racecar.c for `fiber_return_switch`.
Removed `get_main_fiber` function from `fiber_switch.h`
Updated test files `hello_switch` and `racecar` to make them work again
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants