Skip to content

Change timedWait functions to accept Timeout parameter - #239

Merged
lalinsky merged 1 commit into
mainfrom
feat-timeout-parameter
Jan 26, 2026
Merged

lalinsky merged 1 commit into
mainfrom
feat-timeout-parameter

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

Summary

All timedWait functions now accept the Timeout union type instead of Duration, providing consistent support for:

  • Duration-based timeouts (.{ .duration = .fromMilliseconds(100) })
  • Deadline-based timeouts (.{ .deadline = timestamp })
  • No timeout (.none)

Changes

  • Added Timeout.toDeadline() helper method for converting duration-based timeouts to deadline-based ones
  • Updated Waiter.timedWait to accept Timeout parameter
  • Updated AutoCancel.set to accept Timeout parameter
  • Updated all sync primitives: Condition, Semaphore, Notify, ResetEvent, Signal, and Futex
  • Added basic test for Futex.timedWait (was previously untested)
  • Updated all tests and documentation to use new Timeout syntax

Test plan

  • All 327 tests pass
  • Added new test for Futex.timedWait
  • Existing timedWait tests verify the new API works correctly

All timedWait functions now accept Timeout union instead of Duration,
allowing support for duration-based, deadline-based, or no timeout.

Changes:
- Add Timeout.toDeadline() helper for converting duration to deadline
- Update Waiter.timedWait to accept Timeout parameter
- Update AutoCancel.set to accept Timeout parameter
- Update all sync primitives (Condition, Semaphore, Notify, ResetEvent, Signal, Futex)
- Add basic test for Futex.timedWait which was previously untested
- Update all tests and documentation to use new Timeout syntax
@coderabbitai

coderabbitai Bot commented Jan 26, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR replaces Duration parameters with a new Timeout union type across timeout-related APIs in synchronization primitives and the runtime. Changes include function signature updates, import additions, control flow handling for .none timeouts, and test refactoring to use Timeout struct literals.

Changes

Cohort / File(s) Summary
Type infrastructure
src/time.zig
Added toDeadline() method to Timeout union to convert relative durations to absolute deadlines using monotonic time.
Runtime and core waiter
src/common.zig, src/runtime/autocancel.zig
Updated timedWait() and set() signatures to accept Timeout instead of Duration. Added early-exit paths: if timeout is .none, delegate to untimed operations without creating timers.
Signal layer
src/signal.zig
Imported Timeout type, updated Signal.timedWait() signature, and adjusted internal timer setup and test calls to use new Timeout struct literals.
Synchronization primitives
src/sync/Condition.zig, src/sync/Futex.zig, src/sync/Notify.zig, src/sync/ResetEvent.zig, src/sync/Semaphore.zig
Updated timedWait() signatures across all primitives to accept Timeout instead of Duration. Added public Timeout exports where needed. Semaphore adds deadline-based logic via timeout.toDeadline(). Tests updated to construct timeouts as structured literals.
Documentation
docs/signals.md
Updated example syntax to reflect new Timeout struct literal format for specifying wait duration.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

A timeout union takes the stage, 🕐
.none, .duration, .deadline on the page—
No more Duration everywhere,
Cleaner APIs everywhere! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: updating timedWait functions to accept a Timeout parameter instead of Duration.
Description check ✅ Passed The description directly and comprehensively relates to the changeset, explaining the Timeout union support, specific functions updated, and test coverage details.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat-timeout-parameter

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (5)
src/sync/ResetEvent.zig (1)

150-159: Update timedWait docs to reflect Timeout semantics.
The comment still describes a duration-only timeout.

📝 Suggested doc tweak
-/// Like `wait()`, but returns `error.Timeout` if the event is not set within the
-/// specified duration. The timeout is specified in nanoseconds.
+/// Like `wait()`, but returns `error.Timeout` if the event is not set within the
+/// specified timeout. `.none` waits indefinitely.
src/sync/Notify.zig (1)

137-145: Update timedWait docs to reflect Timeout semantics.
The comment still says “duration in nanoseconds.”

📝 Suggested doc tweak
-/// Like `wait()`, but returns `error.Timeout` if no signal is received within the
-/// specified duration. The timeout is specified in nanoseconds.
+/// Like `wait()`, but returns `error.Timeout` if no signal is received within the
+/// specified timeout. `.none` waits indefinitely.
src/sync/Condition.zig (1)

144-156: Update timedWait docs to match Timeout.
The comment still describes “duration in nanoseconds,” but the API now accepts .duration, .deadline, or .none. That’s misleading for users.

✏️ Proposed doc tweak
-/// Like `wait()`, but will return `error.Timeout` if no signal is received
-/// within the specified duration.
-///
-/// The timeout is specified in nanoseconds. If a signal is received before
-/// the timeout expires, the function returns successfully with the mutex held.
+/// Like `wait()`, but will return `error.Timeout` if no signal is received
+/// before the provided `Timeout` expires.
+///
+/// The timeout can be duration-based (`.{ .duration = ... }`), deadline-based
+/// (`.{ .deadline = ... }`), or `.none` to wait indefinitely. If a signal is
+/// received before the timeout expires, the function returns successfully with
+/// the mutex held.
src/sync/Semaphore.zig (1)

109-125: Docs still describe duration-only timeouts.
The comment says “duration in nanoseconds,” but the API now accepts Timeout union values. Update the docs to match.

✏️ Proposed doc tweak
-/// Like `wait()`, but returns `error.Timeout` if no permit becomes available
-/// within the specified duration. The timeout is specified in nanoseconds.
+/// Like `wait()`, but returns `error.Timeout` if no permit becomes available
+/// before the provided `Timeout` expires.
+///
+/// The timeout can be duration-based (`.{ .duration = ... }`), deadline-based
+/// (`.{ .deadline = ... }`), or `.none` to wait indefinitely.
src/runtime/autocancel.zig (1)

251-262: Rename the test to match the new .none semantics.
The test name and comment still mention Duration.max, but the new API uses .none to disable.

✏️ Proposed rename
-test "AutoCancel: set with Duration.max clears prior timer" {
+test "AutoCancel: set with .none clears prior timer" {
@@
-    // Disable it with .max
+    // Disable it with .none

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant