From a3e1fcc36299c1fdc629a99a2d7ee27b7c5cc09e Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 17 Dec 2025 11:07:12 -0500 Subject: [PATCH] add clock --- src/core/clock.ml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/core/clock.ml diff --git a/src/core/clock.ml b/src/core/clock.ml new file mode 100644 index 00000000..a9282522 --- /dev/null +++ b/src/core/clock.ml @@ -0,0 +1,26 @@ +open Opentelemetry_atomic + +type t = { now: unit -> Timestamp_ns.t } [@@unboxed] +(** A clock: can get the current timestamp, with nanoseconds precision *) + +let[@inline] now (self : t) : Timestamp_ns.t = self.now () + +(** Clock using {!Unix.gettimeofday} *) +let unix : t = + { now = (fun () -> Int64.of_float (Unix.gettimeofday () *. 1e9)) } + +module Main = struct + open struct + let main : t Atomic.t = Atomic.make unix + end + + let[@inline] get () = Atomic.get main + + let set t : unit = Util_atomic.update_cas main (fun _ -> (), t) + + (** Clock that always defers to the current main clock *) + let dynamic_main : t = { now = (fun () -> now (get ())) } +end + +(** Timestamp using the main clock *) +let[@inline] now_main () = now (Main.get ())