fix(unix): properly implement timers(!)

This commit is contained in:
Simon Cruanes 2025-05-02 13:47:57 -04:00
parent 1dcadb3470
commit 03af765e43
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4

View file

@ -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, _ =