ocaml-multicore / eio

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

Introduce HOFs that wrap functions to run with a locked mutex #716

Open yawaramin opened 3 months ago

yawaramin commented 3 months ago

Introduce wrappers that can turn any function f : a -> b into a new function that runs with a wrapped mutex. a can even be a mutable value that can then be modified only while the mutex is held.

Fix #336

talex5 commented 3 months ago

This seems not quite the same as your #336 proposal - in fact, it seems incompatible with that idea (of storing the protected resource inside the mutex). Here it's passed as an argument instead.

This "wrap" is effectively the same as "use", except using the alternative API style of taking an argument for the function.

e.g. it allows writing wrap_ro m foo x instead of use_ro m (fun () -> foo x).

The problem usually with this form is that it's really easy to mess it up when you have more than one argument:

wrap_ro m foo x y

Tthis takes m while creating the partially-applied foo x, but then runs foo x y without the lock, which was probably not what was wanted.