Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream select #578

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib_eio/stream.ml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ let take_nonblocking = function
| Sync x -> Sync.take_nonblocking x
| Locking x -> Locking.take_nonblocking x

let select streams =
let f_of (stream, f) () = f (take stream) in
let fs = List.map f_of streams in
Fiber.any fs

let length = function
| Sync _ -> 0
| Locking x -> Locking.length x
Expand Down
9 changes: 9 additions & 0 deletions lib_eio/stream.mli
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ val take_nonblocking : 'a t -> 'a option
Note that if another domain may add to the stream then a [None]
result may already be out-of-date by the time this returns. *)

val select: ('a t * ('a -> 'b)) list -> 'b
(** [select] waits for any stream to have an item available. The item
is mapped by the provided function and returned. Example:
[select [(s1, fun x -> x+1); (s2, fun x -> x+2)]

Warning: as with `Fiber.first`, it is possible that two or more streams
yield an item simultaneously, in which case only one item will be
returned, and the other items are discarded.*)
Comment on lines +48 to +50
Copy link
Collaborator

@SGrondin SGrondin Jul 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, this caveat is too much at the moment.
I would love to see Eio.Stream.select added, but I think it needs to be done using something like #558


val length : 'a t -> int
(** [length t] returns the number of items currently in [t]. *)

Expand Down
15 changes: 15 additions & 0 deletions tests/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,21 @@ Cancelling writing to a stream:
- : unit = ()
```

Selecting from multiple channels:

```ocaml
# run @@ fun () -> Switch.run (fun sw ->
let t1, t2 = (S.create 2, S.create 2) in
let selector = [(t1, fun s -> traceln "Stream 1: %s" s); (t2, fun s -> traceln "Stream 2: %s" s)] in
Fiber.fork ~sw (fun () -> S.add t2 "Hello");
Fiber.fork ~sw (fun () -> S.select selector);
Fiber.fork ~sw (fun () -> S.add t1 "Goodbye");
Fiber.fork ~sw (fun () -> S.select selector));;
+Stream 2: Hello
+Stream 1: Goodbye
- : unit = ()
```

Non-blocking take:

```ocaml
Expand Down