Fix threadpool futexes_linux wait/wake syscall number - #625
Conversation
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
📝 WalkthroughWalkthroughAdds Linux futex syscall portability and introduces threadpool tests for futex waiting and EventCount parking, wakeup, broadcast, cancellation, and high waiter counts. ChangesThreadpool synchronization
Estimated code review effort: 3 (Moderate) | ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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 |
Greptile SummaryThis PR makes Linux futex calls use the target architecture's syscall number and adds tests for waiting behavior. The main changes are:
Confidence Score: 4/5The
constantine/threadpool/primitives/futexes_linux.nim Important Files Changed
Reviews (1): Last reviewed commit: "Fix futexes_linux wait/wake syscall numb..." | Re-trigger Greptile |
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
tests/threadpool/t_backoff.nim (1)
119-157: 📐 Maintainability & Code Quality | 🔵 TrivialDuplicate wakeAll test bodies for 4 vs. 257 waiters.
The two tests are structurally identical (busy-wait for
committedSleep == numWaiters, set condition,wakeAll(), join, assert), differing only in thread count and container type (arrayvsseq). Consider extracting a shared helper parameterized by waiter count to avoid the duplication.
[recommended]♻️ Proposed refactor
+proc runWakeAllTest(numWaiters: int) = + var s: WakeAllState + s.ec.initialize() + + var threads = newSeq[Thread[ptr WakeAllState]](numWaiters) + for t in mitems(threads): + createThread(t, multiParker, addr s) + + while s.ec.getNumWaiters().committedSleep != numWaiters: + discard + + s.condition.store(true, moRelease) + s.ec.wakeAll() + joinThreads(threads) + + check s.woke.load(moAcquire) == numWaiters + check s.ec.getNumWaiters().preSleep == 0 + check s.ec.getNumWaiters().committedSleep == 0 + suite "EventCount": ... test "wakeAll() releases every parked waiter": - const numWaiters = 4 - var s: WakeAllState - s.ec.initialize() - - var threads: array[numWaiters, Thread[ptr WakeAllState]] - for t in mitems(threads): - createThread(t, multiParker, addr s) - - while s.ec.getNumWaiters().committedSleep != numWaiters: - discard - - s.condition.store(true, moRelease) - s.ec.wakeAll() - joinThreads(threads) - - check s.woke.load(moAcquire) == numWaiters - check s.ec.getNumWaiters().preSleep == 0 - check s.ec.getNumWaiters().committedSleep == 0 + runWakeAllTest(4) test "supports more than 256 committed waiters": - const numWaiters = 257 - var s: WakeAllState - s.ec.initialize() - - var threads = newSeq[Thread[ptr WakeAllState]](numWaiters) - for t in mitems(threads): - createThread(t, multiParker, addr s) - - while s.ec.getNumWaiters().committedSleep != numWaiters: - discard - - s.condition.store(true, moRelease) - s.ec.wakeAll() - joinThreads(threads) - - check s.woke.load(moAcquire) == numWaiters - check s.ec.getNumWaiters().preSleep == 0 - check s.ec.getNumWaiters().committedSleep == 0 + runWakeAllTest(257)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/threadpool/t_backoff.nim` around lines 119 - 157, Extract the duplicated wakeAll scenario from the tests into a shared helper parameterized by waiter count, including thread creation, waiter synchronization, condition update, wakeAll invocation, joining, and final assertions. Update both tests to call the helper with 4 and 257, while preserving support for each count and avoiding separate array-versus-seq implementations.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/threadpool/t_futexes.nim`:
- Around line 35-38: Move the s.reachedWait.store readiness update from before
the futex-checking loop into the loop body immediately before s.futex.wait(0),
for both affected wait paths. Keep the existing acquire load, wait call, and
spin counter update unchanged so readiness is only signaled after the worker
reaches the blocking operation.
- Around line 70-79: Replace the mismatched-value test’s use of waiter with a
dedicated thread procedure that unconditionally calls s.futex.wait(0) and then
records completion in the shared WaitState. Start and join that procedure after
storing 1, and retain the completion check to verify wait returns immediately
when the value differs from the expected value.
---
Nitpick comments:
In `@tests/threadpool/t_backoff.nim`:
- Around line 119-157: Extract the duplicated wakeAll scenario from the tests
into a shared helper parameterized by waiter count, including thread creation,
waiter synchronization, condition update, wakeAll invocation, joining, and final
assertions. Update both tests to call the helper with 4 and 257, while
preserving support for each count and avoiding separate array-versus-seq
implementations.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: e3443c83-9847-4d9d-ade2-207c3838865c
📒 Files selected for processing (4)
constantine.nimbleconstantine/threadpool/primitives/futexes_linux.nimtests/threadpool/t_backoff.nimtests/threadpool/t_futexes.nim
Fix #5 ; Ported from [Constantine threadpool](https://github.com/mratsim/constantine/tree/master/constantine/threadpool) Changes: - Implement event count backoff from Constantine - Fallback to generic futexes. [Constantine errors out instead](https://github.com/mratsim/constantine/blob/ea8c268603a5c5f5be479dda9ba27dcbdc51dade/constantine/threadpool/primitives/futexes.nim#L18). - Removed `foreignThreadsParked` redundant logic to match constantine. - Additional fixes: - mratsim/constantine#623 - mratsim/constantine#624 - mratsim/constantine#625 In the fib bench, this is ~10x faster. In the SPC it's ~5x faster when setting task granularity to 1. It's 2x faster in nqueens and heat. It also does not run into the event notifier race conditions reproduced by `tests/stress/test_shutdown.nim` that cause a hang.
The NR_Futex value differs per arch, for example i386 is 240, not 202 (where in this case makes it return immediately instead of wait).
Added regression tests for futexes and backoff.
Summary by CodeRabbit
Bug Fixes
SYS_futexsyscall definition instead of a hardcoded value.Tests
wake()/wakeAll(), and supporting more than 256 committed waiters.wait()(including immediate-return) andwakeAll()(broadcast release verification).