b_queue: add transfer_into for moving batches into the queue

This commit is contained in:
Simon Cruanes 2023-10-29 17:42:02 -04:00
parent f3ae3397de
commit 157957530a
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 18 additions and 0 deletions

View file

@ -71,3 +71,18 @@ let transfer (self : 'a t) q2 : unit =
do
()
done
let transfer_into q (self : _ t) : unit =
if not (Queue.is_empty q) then (
Mutex.lock self.mutex;
if self.closed then (
Mutex.unlock self.mutex;
raise Closed
);
let was_empty = Queue.is_empty self.q in
Queue.transfer q self.q;
if was_empty then Condition.broadcast self.cond;
Mutex.unlock self.mutex
)

View file

@ -19,5 +19,8 @@ val transfer : 'a t -> 'a Queue.t -> unit
in [bq] into [q2], and clears [bq].
It blocks if no element is in [bq]. *)
val transfer_into : 'a Queue.t -> 'a t -> unit
(** [transfer q bq] transfers all items from [q] to [bq]. *)
val close : _ t -> unit
(** Close the queue, meaning there won't be any more [push] allowed. *)