From 03af765e43ef84db13b7eb89a896ec859e4447be Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 2 May 2025 13:47:57 -0400 Subject: [PATCH] fix(unix): properly implement timers(!) --- src/unix/nanoev_unix.ml | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/unix/nanoev_unix.ml b/src/unix/nanoev_unix.ml index b74ea06..17b2088 100644 --- a/src/unix/nanoev_unix.ml +++ b/src/unix/nanoev_unix.ml @@ -174,17 +174,35 @@ let next_deadline_ (self : st) : float option = let step (self : st) : unit = let@ _sp = Trace_.with_span ~__FILE__ ~__LINE__ "nanoev.unix.step" in (* gather the subscriptions and timeout *) + let now = now_ () in let timeout, sub_r, sub_w = let@ self = with_lock_ self in recompute_if_needed self; let timeout = match next_deadline_ self with | None -> 30. - | Some d -> max 0. (d -. now_ ()) + | Some d -> max 0. (d -. now) in timeout, self.sub_r, self.sub_w in + (* run timers *) + while + if Heap.is_empty self.timer then + false + else ( + let (Timer t) = Heap.peek_min_exn self.timer in + if t.deadline <= now then ( + ignore (Heap.pop_min_exn self.timer : timer_ev); + t.f t.x t.y; + true + ) else + false + ) + do + () + done; + (* enter [select] *) Atomic.set self.in_select true; let r_reads, r_writes, _ =