robur-coop / miou

A simple scheduler for OCaml 5
https://docs.osau.re/miou/index.html
MIT License
93 stars 7 forks source link

Match the type of Miou_unix.write with Unix.write and Lwt_unix.write #9

Closed kit-ty-kate closed 1 year ago

kit-ty-kate commented 1 year ago

Both Lwt_unix.write and Unix take bytes as input instead of string. I think keeping it this way would make it easier to miou-ify existing projects.

Also, as a side note: one of the example makes it clear that people usually already have a bytes type and not a string type when using this function.

dinosaure commented 1 year ago

I'm a bit reluctant to make such a change as I've already had quite a few ownerships problems due to the use of bytes (this brings back bad memories of mirage-tcpip).

I'd be more in favour of proposing unsafe_write with good documentation explaining that the buffer should not be modified until the system call is complete. An bad example will be:

let buf = Bytes.create 0x100 in
let prm0 = Miou.call ~give:[ Miou_unix.owner fd ] (fun () -> Miou_unix.read fd buf ~off:0 ~len:(Bytes.length buf)) in
let prm1 = Miou.call ~give:[ Miou_unix.owner fd ] (fun () -> Miou_unix.write fd buf ~off:0 ~len:(Bytes.length buf)) in
Miou.await_all [ prm0; prm1 ]

It should be noted that with the use of Miou.call_cc (concurrency), this code could work as long as a certain order is expected in the execution of the tasks (which should never be the case). The example may seem dumb, but we've already found ourselves in this type of situation when: 1) we generally want to have just one buffer (and avoid allocations) throughout the process 2) when you forget about the notion of ownership on other parts (deep in the stack), which is not explained anywhere

I'd definitely prefer an unsafe_write :+1:.

kit-ty-kate commented 1 year ago

ok that's fair. I'll close this then. Thanks!