ocaml-multicore / eio

Effects-based direct-style IO for multicore OCaml
Other
540 stars 66 forks source link

Add Eio.Pool #602

Closed talex5 closed 1 year ago

talex5 commented 1 year ago

This is similar to Lwt_pool.

It provides a similar API to @darrenldl's draft in #566, but:

I removed the clear function for now. According to https://sherlocode.com/?q=Lwt_pool.clear, the only user of Lwt_pool.clear is Lwt's own tests!

The docs note some complications regarding switches. We should probably try to make this simpler in future. e.g.

Instead of providing validate, dispose and alloc functions, we could just expose the slot and let the user deal with it. That would give maximum flexibility.

avsm commented 1 year ago

I am curious to see what the version that exposes the slot looks like, to avoid all the callbacks for the lifetime management...

talex5 commented 1 year ago

I am curious to see what the version that exposes the slot looks like, to avoid all the callbacks for the lifetime management...

The simplest case would be just:

type 'a t
val create : int -> 'a t
val use : 'a t -> ('a ref -> 'b) -> 'b

When using the pool you get a slot (initially empty) and it's up to you to create a new resource and store it in the slot. That would mostly force people to create their own wrappers around pool.

Alternatively, the wrapper could be passed to create:

type 'a t

module type RESOURCE = sig
  type t
  val use_slot : t ref -> (t -> 'b) -> 'b
end

val create : (module RESOURCE) -> int -> 'a t

val use : 'a t -> ('a -> 'b) -> 'b