Repository navigation
os: implement Futex for NetBSD - #640
Conversation
📝 WalkthroughWalkthroughChangesNetBSD futex support
Estimated code review effort: 4 (Complex) | ~40 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 |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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 `@src/os/netbsd.zig`:
- Around line 8-19: Update the NetBSD futex support around FUTEX_WAIT,
FUTEX_WAKE, and SYS___futex to either require NetBSD 10 or provide a NetBSD 9
fallback with equivalent wait/wake behavior. In src/os/netbsd.zig lines 8-19,
correct the documented minimum version or add the fallback; in src/os/thread.zig
lines 59-67, do not select FutexNetBSD on NetBSD 9 unless that fallback is
available.
In `@src/os/thread.zig`:
- Around line 538-561: Replace the `@intCast`(expected) futex comparison arguments
with `@bitCast`(expected) in both wait and timedWait, preserving the exact 32-bit
word representation for high-bit values. Add a regression test covering expected
= 0x8000_0000 and verify both NetBSD futex call paths accept it without runtime
safety failure.
🪄 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: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 0d6aa343-bb4d-441c-b49a-1faf77702e63
📒 Files selected for processing (2)
src/os/netbsd.zigsrc/os/thread.zig
NetBSD 9 leaves syscall 166 unimplemented; __futex shipped in NetBSD 10. Zig's minimum supported NetBSD is 10.1, so no fallback is needed, but the documented requirement was wrong. The expected value is a raw 32-bit word, and @intcast to the platform calls' c_int rejects values with the high bit set under runtime safety. Use @bitcast in all three implementations that take c_int (NetBSD, OpenBSD, DragonFly - the latter two had the same latent cast), and extend the timedWait test with a 0x8000_0000 word so every futex platform exercises the high-bit path. Reported by review on #640.
NetBSD was the one supported POSIX target whose Futex resolved to void: FreeBSD, OpenBSD, DragonFly and Darwin all have implementations, and anything built on os.Futex (the metrics monitor in #636 being the first) failed to compile there. NetBSD >= 9 provides a Linux-compatible futex through the __futex syscall (FUTEX_WAIT with a relative timeout, FUTEX_WAKE, FUTEX_PRIVATE_FLAG). libc ships no ___futex stub, so the call goes through the generic syscall(2) entry point with SYS___futex (166), which the libc symbol list does provide. Also adds a timedWait test (expiry and already-changed-value paths) so CI exercises the syscall on every futex platform, not just the wake path the existing test covers.
NetBSD 9 leaves syscall 166 unimplemented; __futex shipped in NetBSD 10. Zig's minimum supported NetBSD is 10.1, so no fallback is needed, but the documented requirement was wrong. The expected value is a raw 32-bit word, and @intcast to the platform calls' c_int rejects values with the high bit set under runtime safety. Use @bitcast in all three implementations that take c_int (NetBSD, OpenBSD, DragonFly - the latter two had the same latent cast), and extend the timedWait test with a 0x8000_0000 word so every futex platform exercises the high-bit path. Reported by review on #640.
NotifyNetBSD (lwp_park based) and the MutexNotify selection existed only because NetBSD had no futex. With FutexNetBSD in place, NetBSD uses the same futex-based Notify, Mutex and Condition as the other POSIX targets (Condition already selected by futex availability), so the lwp_park externs and the WaitQueue-based mutex go away, along with the requirement that a Notify be created on its waiting thread.
|
@coderabbitai please check for me if the futex syscall in netbsd is actually meant to be used |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
echo '== PR diff and local declarations =='
git diff -- src/os/netbsd.zig src/os/thread.zig
printf '\n== netbsd.zig outline ==\n'
ast-grep outline src/os/netbsd.zig --items all
printf '\n== relevant declarations ==\n'
rg -n -C 5 '(__futex|futex|SYS___futex|syscall)' src/os/netbsd.zig src/os/thread.zig
echo '== NetBSD upstream code search: syscall and libc wrapper =='
gh api -H 'Accept: application/vnd.github+json' \
'/search/code?q=repo%3ANetBSD%2Fsrc+__futex' \
--jq '.items[] | [.path, .html_url] | `@tsv`' || true
echo '== NetBSD CVS source queries =='
for url in \
'https://raw.githubusercontent.com/NetBSD/src/trunk/sys/kern/sys_futex.c' \
'https://raw.githubusercontent.com/NetBSD/src/trunk/sys/sys/futex.h' \
'https://raw.githubusercontent.com/NetBSD/src/trunk/lib/libc/sys/futex.2' \
'https://raw.githubusercontent.com/NetBSD/src/trunk/lib/libc/sys/Makefile.inc'
do
echo "--- $url"
curl --fail --silent --show-error --location "$url" | sed -n '1,240p' || true
doneLength of output: 44056 🏁 Script executed: #!/bin/bash
set -euo pipefail
base='https://raw.githubusercontent.com/NetBSD/src'
files=(
'trunk/lib/libc/include/futex_private.h'
'trunk/sys/kern/syscalls.master'
'trunk/lib/libc/pthread/pthread__futex.c'
'netbsd-10/sys/sys/futex.h'
'netbsd-10/sys/kern/syscalls.master'
'netbsd-10/sys/kern/sys_futex.c'
)
for file in "${files[@]}"; do
echo "===== ${file} ====="
if content=$(curl --fail --silent --show-error --location "${base}/${file}"); then
printf '%s\n' "$content" |
rg -n -C 8 '(__futex|futex|166|FUTEX_WAIT|FUTEX_WAKE|PRIVATE)' || true
else
echo 'not found'
fi
done
echo '===== libc uses of private futex interface ====='
curl --fail --silent --show-error --location \
'https://api.github.com/search/code?q=repo%3ANetBSD%2Fsrc+path%3Alib%2Flibc+__futex' |
jq -r '.items[]? | [.path, .html_url] | `@tsv`'Length of output: 62719
The NetBSD 10 source defines syscall 166 as a __futex(int *uaddr, int op, int val,
const struct timespec *timeout, int *uaddr2, int val2, int val3)Its public kernel header, NetBSD libc also has an internal wrapper in _syscall(SYS___futex, uaddr, op, val, timeout, uaddr2, val2, val3)This matches the PR wrapper exactly. The NetBSD kernel implementation documents the expected lock use case and implements relative There is no exported libc The PR’s NetBSD 10 guard is also correct. NetBSD 9 leaves syscall 166 unimplemented. You are interacting with an AI system. |
NetBSD was the one supported POSIX target whose
Futexresolved tovoid(FreeBSD, OpenBSD, DragonFly and Darwin all have implementations), so anything built onos.Futexfailed to compile there — first hit by the metrics monitor thread in #636, whose NetBSD CI job fails until this lands.NetBSD >= 10 provides a Linux-compatible futex via the
__futexsyscall:FUTEX_WAITblocks while*uaddr == valwith a relative timeout (ETIMEDOUTon expiry,EAGAINwhen the value already changed),FUTEX_WAKEwakes up tovalwaiters, andFUTEX_PRIVATE_FLAGmatches Linux (128). One wrinkle: libc ships no___futexstub (zig's NetBSD abilists confirm), so the implementation goes through the genericsyscall(2)entry point withSYS___futex(166), which the symbol list does provide.Also adds a
Futex.timedWaittest covering both the expiry path and the already-changed-value path, so CI's NetBSD VM (and every other futex platform) exercises the new syscall for real — the existing test only covered wake.The switch's
else => voidfallback remains for targets genuinely without a futex; #636 will grow a comptime guard for those separately.Follow-up commits
@bitCastin all threec_int-taking implementations (NetBSD, OpenBSD, DragonFly had the same latent@intCast), with a0x8000_0000case added to the timedWait test.NotifyNetBSD(lwp_park based) and theMutexNotifyselection existed only because NetBSD had no futex. NetBSD now uses the same futex-basedNotify/Mutex/Conditionas the other POSIX targets (Conditionalready selected on futex availability), deleting the lwp_park externs and the create-Notify-on-the-waiting-thread constraint.