From e31f5f6aba3884e614e79a06dc4e0d4e8b1ee494 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 19 Nov 2025 12:20:13 -0500 Subject: [PATCH] refactor: move the Mutex.protect backport into `Util_mutex` --- src/client-ocurl/b_queue.ml | 15 ++------------- src/client/batch.ml | 19 ++++--------------- src/core/lock.mli | 2 ++ src/core/opentelemetry.ml | 4 ++++ src/core/util_mutex.ml | 12 ++++++++++++ src/core/util_mutex.mli | 1 + 6 files changed, 25 insertions(+), 28 deletions(-) create mode 100644 src/core/util_mutex.ml create mode 100644 src/core/util_mutex.mli diff --git a/src/client-ocurl/b_queue.ml b/src/client-ocurl/b_queue.ml index 897ff89e..98f43876 100644 --- a/src/client-ocurl/b_queue.ml +++ b/src/client-ocurl/b_queue.ml @@ -1,3 +1,5 @@ +open Opentelemetry.Util_mutex + type 'a t = { mutex: Mutex.t; cond: Condition.t; @@ -7,19 +9,6 @@ type 'a t = { exception Closed -(* Mutex.protect was added in OCaml 5.1, but we want support back to 4.08 *) -(* cannot inline, otherwise flambda might move code around. (as per Stdlib) *) -let[@inline never] protect m f = - Mutex.lock m; - match f () with - | x -> - Mutex.unlock m; - x - | exception e -> - (* NOTE: [unlock] does not poll for asynchronous exceptions *) - Mutex.unlock m; - Printexc.raise_with_backtrace e (Printexc.get_raw_backtrace ()) - let create () : _ t = { mutex = Mutex.create (); diff --git a/src/client/batch.ml b/src/client/batch.ml index d017355e..e508c09f 100644 --- a/src/client/batch.ml +++ b/src/client/batch.ml @@ -1,3 +1,5 @@ +module Otel = Opentelemetry + type 'a t = { mutable size: int; mutable q: 'a list; @@ -9,19 +11,6 @@ type 'a t = { mutex: Mutex.t; } -(* Mutex.protect was added in OCaml 5.1, but we want support back to 4.08 *) -(* cannot inline, otherwise flambda might move code around. (as per Stdlib) *) -let[@inline never] protect_mutex m f = - Mutex.lock m; - match f () with - | x -> - Mutex.unlock m; - x - | exception e -> - (* NOTE: [unlock] does not poll for asynchronous exceptions *) - Mutex.unlock m; - Printexc.raise_with_backtrace e (Printexc.get_raw_backtrace ()) - let default_high_watermark batch_size = if batch_size = 1 then 100 @@ -58,7 +47,7 @@ let ready_to_pop ~force ~now self = let pop_if_ready ?(force = false) ~now (self : _ t) : _ list option = let rev_batch_opt = - protect_mutex self.mutex @@ fun () -> + Otel.Util_mutex.protect self.mutex @@ fun () -> if ready_to_pop ~force ~now self then ( assert (self.q <> []); let batch = self.q in @@ -83,7 +72,7 @@ let rec push_unprotected (self : _ t) ~(elems : _ list) : unit = push_unprotected self ~elems:xs let push (self : _ t) elems : [ `Dropped | `Ok ] = - protect_mutex self.mutex @@ fun () -> + Otel.Util_mutex.protect self.mutex @@ fun () -> if self.size >= self.high_watermark then (* drop this to prevent queue from growing too fast *) `Dropped diff --git a/src/core/lock.mli b/src/core/lock.mli index 6ef1946e..2040bd1b 100644 --- a/src/core/lock.mli +++ b/src/core/lock.mli @@ -1,3 +1,5 @@ +(** A global lock, modifiable by the user *) + val set_mutex : lock:(unit -> unit) -> unlock:(unit -> unit) -> unit (** Set a pair of lock/unlock functions that are used to protect access to global state, if needed. By default these do nothing. *) diff --git a/src/core/opentelemetry.ml b/src/core/opentelemetry.ml index 326c900f..ffac42b7 100644 --- a/src/core/opentelemetry.ml +++ b/src/core/opentelemetry.ml @@ -17,6 +17,10 @@ module AList = AList (** Atomic list, for internal usage @since 0.7 *) +module Util_mutex = Util_mutex +(** Utilities for internal usage. + @since NEXT_RELEASE *) + (** {2 Wire format} *) module Proto = Opentelemetry_proto diff --git a/src/core/util_mutex.ml b/src/core/util_mutex.ml new file mode 100644 index 00000000..ab7e48a7 --- /dev/null +++ b/src/core/util_mutex.ml @@ -0,0 +1,12 @@ +(* Mutex.protect was added in OCaml 5.1, but we want support back to 4.08 *) +(* cannot inline, otherwise flambda might move code around. (as per Stdlib) *) +let[@inline never] protect m f = + Mutex.lock m; + match f () with + | x -> + Mutex.unlock m; + x + | exception e -> + (* NOTE: [unlock] does not poll for asynchronous exceptions *) + Mutex.unlock m; + Printexc.raise_with_backtrace e (Printexc.get_raw_backtrace ()) diff --git a/src/core/util_mutex.mli b/src/core/util_mutex.mli new file mode 100644 index 00000000..feccf59f --- /dev/null +++ b/src/core/util_mutex.mli @@ -0,0 +1 @@ +val protect : Mutex.t -> (unit -> 'a) -> 'a