add Blocking_queue.size

This commit is contained in:
Simon Cruanes 2023-06-15 10:20:33 -04:00
parent debdc8fc31
commit f18ed688e9
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
3 changed files with 16 additions and 0 deletions

View file

@ -83,3 +83,9 @@ let try_push (self : _ t) x : bool =
true true
) else ) else
false false
let size (self : _ t) : int =
Mutex.lock self.mutex;
let n = Queue.length self.q in
Mutex.unlock self.mutex;
n

View file

@ -10,6 +10,10 @@ val push : 'a t -> 'a -> unit
(** [push q x] pushes [x] into [q], and returns [()]. (** [push q x] pushes [x] into [q], and returns [()].
@raise Closed if [close q] was previously called.*) @raise Closed if [close q] was previously called.*)
val size : _ t -> int
(** Number of items currently in the queue.
@since 0.2 *)
val pop : 'a t -> 'a val pop : 'a t -> 'a
(** [pop q] pops the next element in [q]. It might block until an element comes. (** [pop q] pops the next element in [q]. It might block until an element comes.
@raise Closed if the queue was closed before a new element was available. *) @raise Closed if the queue was closed before a new element was available. *)

View file

@ -40,6 +40,12 @@ module Blocking_queue : sig
val create : unit -> _ t val create : unit -> _ t
(** Create a new unbounded queue. *) (** Create a new unbounded queue. *)
val size : _ t -> int
(** Number of items currently in the queue. Note that [pop]
might still block if this returns a non-zero number, since another
thread might have consumed the items in the mean time.
@since 0.2 *)
exception Closed exception Closed
val push : 'a t -> 'a -> unit val push : 'a t -> 'a -> unit