Skip to content

Add waiting_io and waiting_sync states to differentiate wait types - #60

Merged
lalinsky merged 1 commit into
mainfrom
waiting-states
Oct 20, 2025
Merged

lalinsky merged 1 commit into
mainfrom
waiting-states

Conversation

@lalinsky

@lalinsky lalinsky commented Oct 20, 2025 •

Copy link
Copy Markdown
Owner

Split the generic .waiting state into two distinct states:

  • .waiting_io: for I/O operations (xev completions, timers)
  • .waiting_sync: for synchronization primitives (mutexes, conditions, channels)

This distinction enables future work stealing for tasks blocked on synchronization while keeping I/O-bound tasks pinned to their executor. It also makes the code more explicit about what type of waiting is happening.

Updated timedWaitForReady to accept desired_state parameter for flexibility.

Summary by CodeRabbit

  • Refactor
    • Enhanced coroutine state machine to distinguish between I/O waits and synchronization waits for more precise scheduling control
    • Updated timed wait APIs with a new required parameter specifying the desired wait state
    • Modified synchronization primitives to use refined wait state transitions for improved accuracy

Split the generic .waiting state into two distinct states:
- .waiting_io: for I/O operations (xev completions, timers)
- .waiting_sync: for synchronization primitives (mutexes, conditions, channels)

This distinction enables future work stealing for tasks blocked on synchronization
while keeping I/O-bound tasks pinned to their executor. It also makes the code
more explicit about what type of waiting is happening.

Updated timedWaitForReady to accept desired_state parameter for flexibility.
@coderabbitai

coderabbitai Bot commented Oct 20, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

The coroutine state machine is refactored to distinguish between I/O and synchronization waits. The generic .waiting state is split into .waiting_io (for I/O completions) and .waiting_sync (for synchronization primitives). New desired_state parameters are added to runtime wait functions, and all synchronization primitives are updated to use the new states.

Changes

Cohort / File(s) Summary
Coroutine state enum
src/coroutines.zig
CoroutineState enum members restructured: .waiting and .dead replaced with .waiting_io = 2, .waiting_sync = 3, and .dead = 4 (shifted numeric values)
Runtime executor and wait/yield logic
src/runtime.zig
Rework of wait state handling to use .waiting_io and .waiting_sync instead of generic .waiting; added desired_state parameter to timedWaitForReadyWithCallback and timedWaitForReady public APIs; updated yield and resume semantics across multiple code paths
Synchronization primitives
src/sync/Condition.zig, src/sync/Mutex.zig, src/sync/ResetEvent.zig
Updated wait and timed wait paths to transition to .waiting_sync instead of .waiting; synchronized with new runtime state semantics

Sequence Diagram

sequenceDiagram
    participant Coroutine
    participant Executor
    participant SyncPrimitive as Sync Primitive<br/>(Mutex/Condition/ResetEvent)

    Note over Coroutine,Executor: Old Flow: Generic Waiting State
    Coroutine->>SyncPrimitive: acquire/wait
    SyncPrimitive->>Executor: yield(waiting)
    Executor->>Coroutine: suspend

    Note over Coroutine,Executor: New Flow: Refined States
    Coroutine->>SyncPrimitive: acquire/wait
    SyncPrimitive->>Executor: yield(waiting_sync, desired_state=waiting_sync)
    Executor->>Coroutine: suspend with refined state
    
    Note over Executor: Separate I/O operations
    Coroutine->>Executor: waitForXevCompletion
    Executor->>Coroutine: yield(waiting_io, desired_state=waiting_io)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

The changes involve coordinated updates to a core state machine enum, multiple affected runtime APIs with new parameters, and consistent (but semantically important) state transitions across synchronization primitives. While the pattern is repetitive, the logic density of state handling and careful coordination between runtime and sync modules demands careful verification.

Possibly related PRs

Poem

🐰 A rabbit hops through states so clear,
Where .waiting_io and .waiting_sync appear—
No more confusion in the queue,
Each pause now knows what waits it through.
From .waiting's blur, two paths ring true! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "Add waiting_io and waiting_sync states to differentiate wait types" directly and clearly summarizes the main change in this changeset. The PR introduces two new coroutine states that split the generic .waiting state into I/O-specific and synchronization-specific variants, which is precisely what the title communicates. The title is concise, avoids unnecessary details or noise, and effectively captures the primary objective that a developer scanning the history would immediately understand. The title accurately reflects both the action (adding states) and the intent (differentiating wait types).
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch waiting-states

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8ad4a00 and 0bc15fd.

📒 Files selected for processing (5)
  • src/coroutines.zig (1 hunks)
  • src/runtime.zig (11 hunks)
  • src/sync/Condition.zig (2 hunks)
  • src/sync/Mutex.zig (1 hunks)
  • src/sync/ResetEvent.zig (2 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-15T19:38:44.659Z
Learnt from: lalinsky
PR: lalinsky/zio#47
File: src/sync/Mutex.zig:237-237
Timestamp: 2025-10-15T19:38:44.659Z
Learning: In the `unlock` method of `src/sync/Mutex.zig`, the `runtime` parameter is intentionally retained (marked with `_ = runtime;`) for API consistency and future-proofing, even though it's not currently used in the implementation.

Applied to files:

  • src/sync/Mutex.zig
🔇 Additional comments (12)
src/coroutines.zig (1)

11-17: LGTM! Clean state machine refactor.

The enum restructuring clearly separates I/O waits from synchronization waits, which aligns well with the PR's goal of enabling future work stealing for sync-blocked tasks.

src/sync/ResetEvent.zig (1)

135-137: LGTM! Correct state classification for synchronization primitive.

ResetEvent appropriately uses .waiting_sync for both regular and timed waits, consistent with its role as a coordination mechanism between tasks.

Also applies to: 195-199

src/sync/Mutex.zig (1)

89-91: LGTM! Proper state for mutex synchronization.

The update to .waiting_sync correctly categorizes mutex acquisition as a synchronization operation, enabling future work stealing optimizations for lock contention scenarios.

src/sync/Condition.zig (1)

99-101: LGTM! Consistent synchronization state usage.

Both wait() and timedWait() correctly use .waiting_sync, properly categorizing condition variable waits as synchronization operations.

Also applies to: 148-152

src/runtime.zig (8)

196-198: LGTM! Appropriate state for task coordination.

Using .waiting_sync for awaitable completion is correct—waiting for another task/future is a synchronization operation, making it eligible for future work stealing.


352-360: LGTM! Correct handling of both wait states.

resumeTask properly checks for both .waiting_io and .waiting_sync before scheduling, ensuring tasks are resumed regardless of which wait type they were in. The updated comment and panic message accurately reflect the new state variants.


1018-1022: Excellent documentation of state semantics.

The updated docstring clearly explains the distinction between .waiting_io (for I/O completions) and .waiting_sync (for synchronization primitives), making it easier for developers to use the correct state.


1130-1136: LGTM! Correct I/O state for timer operations.

sleep appropriately uses .waiting_io since it waits for a timer completion event, keeping I/O-bound tasks pinned to their executor as intended by the PR design.


1348-1356: Note: Breaking public API change.

The addition of the desired_state parameter to timedWaitForReadyWithCallback (and timedWaitForReady at line 1453) is a breaking change to the public API. While necessary for the new wait state semantics, this should be documented in release notes or changelog.

All call sites within the PR have been updated correctly.


1478-1487: LGTM! Textbook example of .waiting_io usage.

waitForXevCompletion correctly uses .waiting_io as it explicitly waits for I/O completion events, ensuring tasks blocked on I/O remain pinned to their executor as designed.


1941-1943: LGTM! Appropriate state for multi-task coordination.

The select function correctly uses .waiting_sync when waiting for the first of multiple tasks to complete, properly categorizing this as a coordination operation eligible for future work stealing.


256-267: State transition pattern is inconsistent but actual safety impact unclear—developer verification needed.

The verification confirms the review comment is accurate. timedWaitForComplete (line 256) is the only place in the codebase that passes .ready directly to timedWaitForReadyWithCallback, whereas all other wait patterns explicitly set .preparing_to_wait first:

  • waitForComplete (line 183): Sets .preparing_to_wait before yielding
  • ResetEvent (line 197): Calls with .preparing_to_wait
  • Condition (line 150): Sets .preparing_to_wait before calling

This inconsistency is real and measurable. However, whether this creates an actual race condition depends on deeper state machine semantics that static analysis cannot fully determine. Recommend manual verification to confirm either:

  1. This different pattern is safe/intentional, or
  2. The call should be updated to match the .preparing_to_wait pattern used elsewhere

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.

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