lindig / polly

OCaml bindings for Linux epoll(2)
MIT License
9 stars 4 forks source link

Add wait_fold for iterating over FDs #1

Closed lindig closed 1 year ago

lindig commented 1 year ago

The current Polly.wait invokes a function f with every file descriptor that is ready to be processed. It assumes that these involcations are independent. To faciliate passing results from f back to the caller, introduce wait_fold:

val wait_fold : t ( epoll *) -> int (* max fds to handle ) -> int ( timeout in milliseconds: -1 = wait forever ) -> 'a ( initial value passed to f below *) -> (t -> Unix.file_descr -> Events.t -> 'a -> 'a) -> 'a

Function f receives as last argument a value and returns a value of the same type, which is passed to the next invocation of f. The value of the last invocation is returned by wait_fold. The first invocation of f receives a value provided as 4th argument to wait_fold by the caller.

To avoid allocating OCaml values, wait_fold does not return the number of file descriptors that were processed. But it would be possible to implement it using wait_fold.

Signed-off-by: Christian Lindig christian.lindig@citrix.com

lindig commented 1 year ago

Discussion at https://discuss.ocaml.org/t/camllocal-for-caml-callbackn-arg-array-required/11012/1 about registering the arg values as roots.