mirror of
https://github.com/ocaml-tracing/ocaml-opentelemetry.git
synced 2026-03-07 18:37:56 -05:00
reduce size of critical section
better to reverse the list without holding the lock, as it allocates and might have to yield to another thread or domain, pause, etc.
This commit is contained in:
parent
76efa381c3
commit
026465f770
1 changed files with 16 additions and 12 deletions
|
|
@ -50,18 +50,22 @@ let ready_to_pop ~force ~now self =
|
|||
self.size > 0 && (force || is_full_ self || timeout_expired_ ~now self)
|
||||
|
||||
let pop_if_ready ?(force = false) ~now (self : _ t) : _ list option =
|
||||
protect self.mutex @@ fun () ->
|
||||
if ready_to_pop ~force ~now self then (
|
||||
assert (self.q <> []);
|
||||
let batch =
|
||||
(* Reverse the list to retrieve the FIFO order. *)
|
||||
List.rev self.q
|
||||
in
|
||||
self.q <- [];
|
||||
self.size <- 0;
|
||||
Some batch
|
||||
) else
|
||||
None
|
||||
let rev_batch_opt =
|
||||
protect_mutex self.mutex @@ fun () ->
|
||||
if ready_to_pop ~force ~now self then (
|
||||
assert (self.q <> []);
|
||||
let batch = self.q in
|
||||
self.q <- [];
|
||||
self.size <- 0;
|
||||
Some batch
|
||||
) else
|
||||
None
|
||||
in
|
||||
match rev_batch_opt with
|
||||
| None -> None
|
||||
| Some batch ->
|
||||
(* Reverse the list to retrieve the FIFO order. *)
|
||||
Some (List.rev batch)
|
||||
|
||||
(* Helper so we can count new elements and prepend them onto the existing [q] in
|
||||
one pass. *)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue