Skip to content

Simplify Futex.wake() by using stack instead of array - #263

Merged
lalinsky merged 1 commit into
mainfrom
simplify-futex-wake
Jan 30, 2026
Merged

lalinsky merged 1 commit into
mainfrom
simplify-futex-wake

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

Summary

Simplifies Futex.wake() by replacing the fixed-size array approach with a simple stack-based approach.

Changes

Before:

  • Used fixed 64-element array to collect waiters
  • Required batching loop to handle more than 64 waiters
  • More complex control flow

After:

  • Build local stack using only next pointers
  • No size limit, no batching needed
  • Single pass through the queue
  • Simpler, more readable code

How it works

  1. Lock mutex
  2. Iterate queue, for each matching waiter:
    • remove() sets prev = null (sentinel)
    • Push to local stack using next pointer
  3. Unlock mutex
  4. Iterate stack and wake all waiters

The prev = null sentinel prevents concurrent remove() operations from manipulating nodes while they're being processed in the local stack.

Benefits

  • Simpler code: Single-pass instead of batching loop
  • No arbitrary limits: Can wake unlimited waiters in one call
  • Consistent pattern: Matches Channel/BroadcastChannel approach
  • Same safety: Sentinel prevents concurrent access race

Testing

All 328 tests pass, including 13 Futex-specific tests.

Replace the fixed-size array with a simple stack approach:
- No array size limit (eliminates 64-waiter batching loop)
- Build local stack using only next pointers
- Wake all nodes from stack outside the lock

The prev = null sentinel from remove() prevents concurrent remove()
operations from manipulating nodes while they're in the local stack.

Benefits:
- Simpler code (single pass instead of batching loop)
- No arbitrary limit on waiters per wake
- Consistent pattern with Channel/BroadcastChannel

All 328 tests pass.
@coderabbitai

coderabbitai Bot commented Jan 30, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The wake function in Futex.zig is refactored to collect matching waiters into a local stack while holding the bucket lock, then release the lock before signaling those waiters. This replaces in-lock batch signaling with post-lock signaling, reducing critical section duration and adding a count-based limit to wake operations.

Changes

Cohort / File(s) Summary
Futex wake implementation
src/sync/Futex.zig
Reorganized wake logic to gather eligible waiters under lock with state validation (null prev/next check), transition them to a local stack, release the bucket lock, then signal all collected waiters outside the critical section. Replaced fixed-size batching with complete post-lock signaling and added max_waiters count limit.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🔐 A lock held tight, then let it fly—
Waiters gathered, signaled dry.
Move the wake outside the gate,
Less contention, lower wait.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and clearly summarizes the main change: replacing a fixed-size array with a stack-based approach in Futex.wake().
Description check ✅ Passed The description clearly explains the changes, implementation details, benefits, and test results—all directly related to the changeset.
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 simplify-futex-wake

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