ocaml-multicore / eio

Effects-based direct-style IO for multicore OCaml
Other
529 stars 67 forks source link

Switches should be domain-safe #669

Closed talex5 closed 5 months ago

talex5 commented 6 months ago

Currently, a switch can only be used from the domain in which it was created (and it rejects attempts to use it from another domain). It would be useful to remove this restriction. It's particularly important when implementing any kind of cache or pool that is shared between domains, since a resource allocated in one domain should be usable by the others.

Perhaps the fibers field could be made atomic, allowing any domain to prevent a switch from finishing while it's adding a resource to it. e.g. to allocate a resource:

  1. Atomically increment fibers from a positive value.
  2. Create the resource and attach it to the switch.
  3. Decrement fibers.

The on_release list would need to become thread-safe too.

If changing all operations to do this is too difficult or hurts performance, it might be possible instead to add just a thread-safe transfer operation. Then other domains could open resources in their own domain and then transfer them to the cache's switch afterwards. A transfer operation would be useful even with a single domain.

Requested by @art-w and @clecat.