Repository navigation
Add support for deadline-based timers - #238
Conversation
Change Timer.delay from Duration to ?Duration to support both duration-based and absolute deadline-based timer initialization. Update setTimer logic to handle optional delay field. Add tests for zero duration timers and explicit deadline timers.
📝 WalkthroughWalkthroughTimer representation changed from a raw delay field to a Timeout union; Timer.init now stores the Timeout directly. Loop APIs and internal timer lifecycle compute deadlines from the Timeout variants. Call sites and tests updated to pass Timeout-shaped literals and added zero-duration and explicit-deadline tests. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@src/ev/loop.zig`:
- Around line 225-238: The current setTimer uses was_active = timer.c.state ==
.running which can race with the firing path that removes the heap entry and
zeroes deadline while leaving state as .running; change the guard to treat a
timer as active only if its deadline sentinel is set (e.g., was_active =
timer.c.state == .running and timer.deadline.ns > 0) so cross-thread rearming
won't call timers.remove() on a timer already removed; ensure you still update
deadline when delay is provided (supporting explicit-deadline timers where delay
is null), keep the std.debug.assert(timer.deadline.ns > 0) check, and then use
that was_active to decide between timers.remove(timer) vs incrementing
self.active before setting timer.c.state = .running and
self.timers.insert(timer).
Change Timer.delay to Timer.timeout to clarify that this field stores the user's timeout specification (none/duration/deadline), while the deadline field is only for internal heap management. Fix setTimer to check timer.deadline.ns > 0 instead of timer.c.state to correctly detect if timer was active. Remove timer from heap before updating deadline, since heap removal depends on the old deadline value.
Summary
Test Plan