Merge pull request #141 from bikallem/stream-is_empty

lib_eio: implement Stream.is_empty
This commit is contained in:
Thomas Leonard 2022-01-13 13:22:44 +00:00 committed by GitHub
commit ccc62f5530
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -308,6 +308,12 @@ module Stream : sig
it returns [None] if the stream is empty rather than waiting.
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 length : 'a t -> int
(** [length t] returns the number of items currently in [t]. *)
val is_empty : 'a t -> bool
(** [is_empty t] is [length t = 0]. *)
end
(** Cancelling other fibres when an exception occurs. *)

View File

@ -118,3 +118,11 @@ let take_nonblocking t =
end;
Mutex.unlock t.mutex;
Some v
let length t =
Mutex.lock t.mutex;
let len = Queue.length t.items in
Mutex.unlock t.mutex;
len
let is_empty t = (length t = 0)