fix(libc): keep the _Cnd_timedwait deadline in the host clock's unit #6580
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6580"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-15T21:14:26Z.
What
libc does not build on macOS:
_Cnd_timedwaitturns its deadline into asystem_clock::time_pointfromseconds + nanoseconds, a nanosecond duration. On macOSsystem_clockcounts microseconds, so the implicit conversion is lossy and AppleClang rejects it:The seconds now stay in the clock's unit and only the nanoseconds are rounded up to it (
std::chrono::ceil), so the wait never ends before the requested time. With a nanosecondsystem_clock(Linux, MinGW) the expression is unchanged.Casting the nanosecond sum instead would compile but is wrong on macOS: the range check above allows deadlines up to the microsecond clock's limit (about 2.9e14 s), while the sum overflows above 2^63 ns (about 9.2e9 s). A deadline of 1e10 s then wraps into the past and the wait times out at once. The new test case waits on exactly that deadline: on a host whose clock holds it, the wait must block until a second thread wakes it; on a nanosecond clock it must throw the overflow error, as for any deadline past the clock's range.
Two commits: the test, then the fix. There are no further real steps for this change.
Tested
macOS 26.7.1, Apple M2, AppleClang 21.0.0, x86-64 build under Rosetta (as the full build selects), Release.
maindoes not build libc on macOS for other reasons as well, which #4627 addresses, so the test ran in a local tree ofmaince991f1ecwith #4627, #4630 and #6124 merged. The branch itself is based one16b2bcceand merges with it cleanly.guest_libc_cnd_timed_wait_testslinks only libc.main'sThreads.cpp: libc fails to compile with the error above.time_point_castof asys_time<nanoseconds>): builds; the new case aborts, 3 runs of 3.guest_libc_cnd_timed_wait_testspasses 5 runs of 5, the existing cases included.tools/check_conventions.py --base origin/mainandgit diff --checkpass.Linux and Windows, run by others on
2854da23a(thanks): jotitadev (Ubuntu 26.04, GCC 15.2) and coltken95 (Linux x86-64, GCC) and a WSL2 Ubuntu 24.04 run (GCC 13.3) passguest_libc_cnd_timed_wait; johnmaia (Windows 11, WinLibs GCC 15.2.0) passes it and the full suite. With a nanosecondsystem_clockthe new case takes the overflow branch, so it also passes withmain'sThreads.cppthere, as expected.Overlap: yabanana's AOT pull requests (#1835 and #3891, #4156, #4396, #4397, #4422) carry the same
ceilconversion inside their Apple ARM64 work. This lands it on its own with a test. @yabanana, if you'd rather keep it in your series, say so and I'll close this one. #6222 converts the nanosecond sum, which overflows as described above. #6044 movesGuestLibcCndTimedWait.cpp; whichever lands second rebases.Checklist
main; no other open PR implements the same functions (see Overlap above)NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T21:26:14Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libc/src/Threads.cppcore/libs/prx/libc/src/Threads.cppcore/libs/tests/GuestLibcCndTimedWait.cppcore/libs/prx/libc/src/Threads.cppImported from boykopovar/AnyPS5. Originally posted by coltken95 on 2026-10-15T21:42:11Z.
Tested on Linux x86-64 (Release build, GCC/libstdc++), with
2854da23amerged into main31cbb55d7:guest_libc_cnd_timed_waitpasses.libc/src/Threads.cppit also passes here, as expected: libstdc++'sstd::chrono::system_clock::durationis nanoseconds, so the addedceil<system_clock::duration>changes nothing on Linux. The bug needs a coarsersystem_clock(microseconds on macOS's libc++), so this run only shows the change is harmless on Linux.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-15T21:48:34Z.
Review: COMMENTED
Linux run, which the description says is still to come.
Ubuntu 26.04, Intel i7-8700, GCC 15.2 (libstdc++, nanosecond
system_clock), Ninja, Release,BUILD_TESTING=ON, full build.main31cbb55d7with head2854da23amerged on top.guest_libc_cnd_timed_waitpasses 3 runs of 3. With a nanosecond clock, 1e10 s is above the clock's limit (about 9.2e9 s), so the new case takes thestd::overflow_errorbranch, as described.Threads.cppreverted tomain, test kept: it also passes. That is expected here:ceilto nanoseconds is the identity, so the change cannot show up on Linux; the red case is the macOS one in the description.ctest -E "^agc|gpu|vulkan|video_out" -j8): 648/650, same asmainon this machine. The 2 failures (file_writer_binary,file_writer_text) also fail onmainhere.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by johnmaia on 2026-10-15T23:53:11Z.
Windows run, since Windows is untested. Windows 11, WinLibs GCC 15.2.0 r7 (MinGW), Release,
BUILD_TESTING=ON, RTX 4080,ctest -j4 --timeout 300. Head2854da23amerged intomain5d9dcc42d, plus #6452 so the MinGW test build works.guest_libc_cnd_timed_waitpasses.guest_kqueue("the vnode watch error lacks "parent""). An elevated session holds the backup privilege, which bypasses the denied-parent case; #6599, #6629 and #6173 address that test, and it is unrelated to this change.Not tested: a negative control on Windows.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-16T03:23:10Z.
Linux and Windows results for
2854da23a. The merge base ise16b2bcce1.system_clockThreads.cppas one16b2bcce1, new test keptguest_libc_cnd_timed_waitpasses 3/3On both hosts the new case takes the overflow branch. The 1e10 s deadline is beyond the clock's 9223372036 s, so the test requires
std::overflow_error, and_Cnd_timedwaitthrows it. I printed the clock values with a small program built by the same compilers, using the test's ownduration_cast<seconds>(system_clock::duration::max()).As expected, the test does not tell the fix from the old code on a nanosecond clock. There the deadline expression is the same before and after, so the with/without difference only shows on macOS. After restoring the branch's
Threads.cpp, the test passes again (1/1 on Linux).AI-assisted: yes