Repository navigation
Split futex wait into separate wait() and timedWait() methods - #289
Conversation
📝 WalkthroughWalkthroughSplit wait APIs into explicit indefinite and timeout-aware paths: Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 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 |
- wait() now blocks indefinitely without timeout parameter - timedWait() takes timeout and returns error.Timeout on expiration - Updated all platform implementations (Linux, Windows, Darwin, FreeBSD, OpenBSD, DragonFly, NetBSD) - Updated Event API and all internal callers - Added test for timedWait success case - Updated timeout test to expect error.Timeout
9b692c1 to
cd871c5
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@src/os/thread_wait.zig`:
- Around line 544-558: In timedWait, the timeout is passed to sys.umtx_sleep in
nanoseconds (timeout_ns from timeout.toNanoseconds()), but DragonFly's
umtx_sleep expects microseconds; replace the use of timeout.toNanoseconds() with
timeout.toMicroseconds() (rename timeout_ns accordingly), ensure the value is
int-cast the same way before calling sys.umtx_sleep, and keep the rest of
timedWait and the sys.umtx_sleep call unchanged.
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@src/os/thread_wait.zig`:
- Around line 544-558: timedWait currently does `@intCast`(timeout_us) which will
panic for Duration values > ~2147s; change the conversion of timeout_us before
calling sys.umtx_sleep by clamping/casting to the target C int safely (use
std.math.cast or equivalent with a maxInt fallback like the Darwin path) so the
value passed to sys.umtx_sleep is capped at the platform C int max and avoids a
safety panic; update the conversion for both the timeout_us argument and any
other `@intCast` usages in timedWait to use the safe cast/clamp and keep the rest
of the sys.umtx_sleep call unchanged.
- Around line 544-558: In timedWait, avoid using `@intCast` on timeout_us (u64)
which can panic for large durations; instead convert with std.math.cast to the C
int width and clamp to the C max value on overflow (use a maxInt fallback
similar to the Darwin implementation) and also ensure zero microseconds does not
become an infinite wait by clamping the converted timeout to at least 1 (i.e.,
set timeout_us_converted = max(1, casted_or_max)); update the call site of
sys.umtx_sleep(&ptr.raw, `@intCast`(current), timeout_us_converted) accordingly so
umtx_sleep always receives a safe i32-range, non-zero microsecond value.
Summary
This PR refactors the futex wait API to provide clearer semantics and better error handling by splitting the unified
wait()function into two distinct methods:wait(ptr, current)- Blocks indefinitely without timeouttimedWait(ptr, current, timeout)- Blocks with timeout, returnserror.Timeouton expirationChanges
wait(), making the infinite wait case explicittimedWait()now returnserror.Timeoutinstead of silently returning on timeoutEventFutexandEventNetBSDto expose both methodstimedWait()and updated timeout test to expect errorBenefits