Repository navigation
Support sync primitives in non-async contexts - #271
Conversation
Refactor Waiter to support both async task and blocking thread contexts. When no task is present, uses std.Thread.Futex for blocking operations. - Change Waiter.task from *AnyTask to ?*AnyTask - Split wait/timedWait into separate task and futex implementations - Update Runtime.sleep() to use Waiter instead of timer-based I/O - Add getCurrentTaskOrNull() and getCurrentExecutorOrNull() helpers - Add tests for cross-thread signaling with ResetEvent
📝 WalkthroughWalkthroughThis change makes Waiter dual-mode (task or futex) by making its Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 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: 2
🤖 Fix all issues with AI agents
In `@src/common.zig`:
- Around line 116-159: timedWaitFutex returns error.Timeout on timeout but
timedWaitTask never does, causing inconsistent semantics; make timedWaitTask
mirror timedWaitFutex by detecting when the ev.Timer fires and returning
error.Timeout. Specifically, after creating var timer: ev.Timer = .init(timeout)
(the local named timer) and installing callback, have the callback set a small
flag/state on the Waiter (or call a helper on Waiter) to mark "timed_out", then
after the call to self.waitTask(...) check that flag (or query timer state) and
if it indicates the timer fired return error.Timeout; keep the existing defer
that clears the timer to avoid races. Update timedWait (and any callers
expecting error.Timeout) accordingly so both futex and task paths return
error.Timeout on timeout.
In `@src/time.zig`:
- Around line 429-434: durationFromNow() computes time until a Timeout.deadline
using Timestamp.now(.monotonic), which is incorrect if callers construct
Timeout.deadline from a realtime Timestamp; update the code and/or API contract:
either document that Timeout.deadline must be a monotonic timestamp (add a
comment on the Timeout type and on durationFromNow stating “deadline is
monotonic-only”), or provide a clock-aware variant (e.g.,
durationFromClock(nowClock: Clock) or durationFromRealtimeNow that accepts a
realtime Timestamp or Clock) and keep durationFromNow delegating to the
monotonic variant; reference Timeout.deadline, durationFromNow, and the use of
Timestamp.now(.monotonic) when making the change.
| pub fn durationFromNow(self: Timeout) Duration { | ||
| return switch (self) { | ||
| .none => .max, | ||
| .duration => |d| d, | ||
| .deadline => |ts| Timestamp.now(.monotonic).durationTo(ts), | ||
| }; |
There was a problem hiding this comment.
Clarify clock expectations for Timeout.deadline.
durationFromNow() always uses a monotonic “now.” If callers construct a .deadline from a realtime Timestamp, the computed duration will be wrong. Consider documenting “deadline is monotonic-only” or providing a clock-aware variant.
🤖 Prompt for AI Agents
In `@src/time.zig` around lines 429 - 434, durationFromNow() computes time until a
Timeout.deadline using Timestamp.now(.monotonic), which is incorrect if callers
construct Timeout.deadline from a realtime Timestamp; update the code and/or API
contract: either document that Timeout.deadline must be a monotonic timestamp
(add a comment on the Timeout type and on durationFromNow stating “deadline is
monotonic-only”), or provide a clock-aware variant (e.g.,
durationFromClock(nowClock: Clock) or durationFromRealtimeNow that accepts a
realtime Timestamp or Clock) and keep durationFromNow delegating to the
monotonic variant; reference Timeout.deadline, durationFromNow, and the use of
Timestamp.now(.monotonic) when making the change.
Waiter.timedWait should return the same error set as wait() - only cancellation errors when cancel_mode allows. The caller determines if timeout won by checking their condition (e.g., still in wait queue). - Change timedWaitFutex to return void (not Timeoutable!void) - Change timedWait return type to be conditional on cancel_mode - Simplify Runtime.sleep() to directly call timedWait - Update test to not expect error.Timeout
Summary
Waiterto support both async task and blocking thread contextsstd.Thread.Futexfor blocking operationsChanges
Waiter.taskfrom*AnyTaskto?*AnyTaskwait()andtimedWait()into separate task and futex implementationsRuntime.sleep()to useWaiter.timedWait()instead of timer-based I/ORuntime.yield()to callstd.Thread.yield()when not in executor contextgetCurrentTaskOrNull()andgetCurrentExecutorOrNull()helper methodsResetEventTest plan