ocaml-multicore / picos

Interoperable effects based concurrency
https://ocaml-multicore.github.io/picos/doc/picos/index.html
ISC License
86 stars 3 forks source link

Redesign `Spawn` and introduce concept of fatal errors #210

Closed polytypic closed 2 months ago

polytypic commented 2 months ago

This PR essentially redesigns the Fiber.spawn mechanism to allow FLS to be populated before a fiber is spawned. Being able to populate the FLS before spawn can be used to reduce the amount of allocations required (i.e. this results in smaller main closure for the fiber) and also allows some use cases to be implemented more easily.

This also removes the early idea of giving a list of main functions, which allowed multiple fibers to be spawned with a single effect. It seems that moving the allocation of the fiber record to happen before spawn also speeds up a single spawn. This is nice, because, in most cases, one spawns one fiber at a time.

This also introduces the concept of fatal errors. Previously, if the main function of a fiber raised an exception, it was considered to be undefined behavior. This is a subtle way to say that main had to always handle all exceptions, because raising an exception had no behavior that one could rely upon. This is now changed such that raising an exception from the main function is considered a fatal error, which is guaranteed to stop the entire program.

On the other hand, these changes move the requirement that fiber records are unique to be maintained outside of the scheduler.