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

Add add_nonblocking, capacity and is_full for streams #790

Open
wants to merge 1 commit 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
35 changes: 35 additions & 0 deletions lib_eio/stream.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
type drop_priority = Newest | Oldest

module Locking = struct
type 'a t = {
mutex : Mutex.t;
Expand Down Expand Up @@ -64,6 +66,26 @@ module Locking = struct
)
)

let add_nonblocking ~drop_priority t item =
Mutex.lock t.mutex;
match Waiters.wake_one t.readers item with
| `Ok -> Mutex.unlock t.mutex; None
| `Queue_empty ->
(* No-one is waiting for an item. Queue it. *)
if Queue.length t.items < t.capacity then (
Queue.add item t.items;
Mutex.unlock t.mutex;
None
) else (
match drop_priority with
| Newest -> Mutex.unlock t.mutex; Some item
| Oldest ->
let dropped_item = Queue.take t.items in
Queue.add item t.items;
Mutex.unlock t.mutex;
Some dropped_item
)

let take t =
Mutex.lock t.mutex;
match Queue.take_opt t.items with
Expand Down Expand Up @@ -101,6 +123,8 @@ module Locking = struct
let len = Queue.length t.items in
Mutex.unlock t.mutex;
len

let capacity t = t.capacity

let dump f t =
Fmt.pf f "<Locking stream: %d/%d items>" (length t) t.capacity
Expand All @@ -123,6 +147,11 @@ let take = function
| Sync x -> Sync.take x |> Result.get_ok (* todo: allow closing streams *)
| Locking x -> Locking.take x

let add_nonblocking ~drop_priority t v =
match t with
| Sync _ -> Some v
| Locking x -> Locking.add_nonblocking ~drop_priority x v

let take_nonblocking = function
| Locking x -> Locking.take_nonblocking x
| Sync x ->
Expand All @@ -134,8 +163,14 @@ let length = function
| Sync _ -> 0
| Locking x -> Locking.length x

let capacity = function
| Sync _ -> 0
| Locking x -> Locking.capacity x

let is_empty t = (length t = 0)

let is_full t = (length t = capacity t)

let dump f = function
| Sync x -> Sync.dump f x
| Locking x -> Locking.dump f x
23 changes: 23 additions & 0 deletions lib_eio/stream.mli
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ val take : 'a t -> 'a

If no items are available, it waits until one becomes available. *)

type drop_priority = Newest | Oldest

val add_nonblocking : drop_priority: drop_priority -> 'a t -> 'a -> 'a option
(** [add_nonblocking ~drop_priority t item] is like [(add t item); None] except that
it returns [Some dropped_item] if the stream is full rather than waiting, where
[dropped_item] is [item] if [drop_priority = Newest], and the first element of the
stream if [drop_priority = Oldest].

In other words, if the stream is full then:
- [add_nonblocking ~drop_priority:Newest t item] is like [Some item]; and
- [add_nonblocking ~drop_priority:Oldest t item] is like
[let dropped_item = take t in add t item; Some dropped_item]
except that no other stream operation can happen (even in other threads)
between the [take] and the [add].

On streams of capacity [0], this always returns [Some item], even if a reader is waiting. *)

val take_nonblocking : 'a t -> 'a option
(** [take_nonblocking t] is like [Some (take t)] except that
it returns [None] if the stream is empty rather than waiting.
Expand All @@ -43,8 +60,14 @@ val take_nonblocking : 'a t -> 'a option
val length : 'a t -> int
(** [length t] returns the number of items currently in [t]. *)

val capacity : 'a t -> int
(** [capacity t] returns the number of items [t] can hold without blocking writers. *)

val is_empty : 'a t -> bool
(** [is_empty t] is [length t = 0]. *)

val is_full : 'a t -> bool
(** [is_full t] is [length t = capacity t]. *)

val dump : 'a t Fmt.t
(** For debugging. *)
27 changes: 27 additions & 0 deletions tests/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ let add t v =
S.add t v;
traceln "Added %d to stream" v

let add_nonblocking ~drop_priority t v =
traceln "Adding %d to stream" v;
match S.add_nonblocking ~drop_priority t v with
| None -> traceln "Added %d to stream" v
| Some d ->
match drop_priority, S.capacity t with
| Newest, _ | _, 0 -> assert (d = v); traceln "Dropped %i instead of adding it to stream" v
| Oldest, _ -> traceln "Dropped %i from stream and added %i to stream" d v


let take t =
traceln "Reading from stream";
traceln "Got %d from stream" (S.take t)
Expand Down Expand Up @@ -320,6 +330,23 @@ Cancelling writing to a stream:
- : unit = ()
```

Non-blocking add:

```ocaml
# run @@ fun () ->
let t = S.create 1 in
add t 0;
add_nonblocking ~drop_priority:Newest t 1;
add_nonblocking ~drop_priority:Oldest t 2;;
+Adding 0 to stream
+Added 0 to stream
+Adding 1 to stream
+Dropped 1 instead of adding it to stream
+Adding 2 to stream
+Dropped 0 from stream and added 2 to stream
- : unit = ()
```

Non-blocking take:

```ocaml
Expand Down