fix(libc): keep the _Cnd_timedwait deadline in the host clock's unit #6580

Merged
DavidSamir merged 2 commits from upstream-pr/6580 into main 2026-10-17 11:34:17 +00:00
DavidSamir commented 2026-10-15 21:14:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-15T21:14:26Z.


What

libc does not build on macOS: _Cnd_timedwait turns its deadline into a system_clock::time_point from seconds + nanoseconds, a nanosecond duration. On macOS system_clock counts microseconds, so the implicit conversion is lossy and AppleClang rejects it:

core/libs/prx/libc/src/Threads.cpp:248:35: error: no matching conversion for functional-style cast from 'typename common_type<duration<long long, std::ratio<1, 1>>, duration<long long, std::ratio<1, 1000000000>>>::type' ...

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 nanosecond system_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.

main does not build libc on macOS for other reasons as well, which #4627 addresses, so the test ran in a local tree of main ce991f1ec with #4627, #4630 and #6124 merged. The branch itself is based on e16b2bcce and merges with it cleanly. guest_libc_cnd_timed_wait_tests links only libc.

  • main's Threads.cpp: libc fails to compile with the error above.
  • Casting the nanosecond sum (time_point_cast of a sys_time<nanoseconds>): builds; the new case aborts, 3 runs of 3.
  • This branch: builds with no new warnings; guest_libc_cnd_timed_wait_tests passes 5 runs of 5, the existing cases included.
  • tools/check_conventions.py --base origin/main and git diff --check pass.

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) pass guest_libc_cnd_timed_wait; johnmaia (Windows 11, WinLibs GCC 15.2.0) passes it and the full suite. With a nanosecond system_clock the new case takes the overflow branch, so it also passes with main's Threads.cpp there, as expected.

Overlap: yabanana's AOT pull requests (#1835 and #3891, #4156, #4396, #4397, #4422) carry the same ceil conversion 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 moves GuestLibcCndTimedWait.cpp; whichever lands second rebases.

Checklist

  • Based on current main; no other open PR implements the same functions (see Overlap above)
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none (every title on macOS needs libc to build)
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4879944393 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6580). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-15T21:14:26Z. --- ### What libc does not build on macOS: `_Cnd_timedwait` turns its deadline into a `system_clock::time_point` from `seconds + nanoseconds`, a nanosecond duration. On macOS `system_clock` counts microseconds, so the implicit conversion is lossy and AppleClang rejects it: ``` core/libs/prx/libc/src/Threads.cpp:248:35: error: no matching conversion for functional-style cast from 'typename common_type<duration<long long, std::ratio<1, 1>>, duration<long long, std::ratio<1, 1000000000>>>::type' ... ``` 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 nanosecond `system_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. `main` does not build libc on macOS for other reasons as well, which #4627 addresses, so the test ran in a local tree of `main` ce991f1ec with #4627, #4630 and #6124 merged. The branch itself is based on e16b2bcce and merges with it cleanly. `guest_libc_cnd_timed_wait_tests` links only libc. - `main`'s `Threads.cpp`: libc fails to compile with the error above. - Casting the nanosecond sum (`time_point_cast` of a `sys_time<nanoseconds>`): builds; the new case aborts, 3 runs of 3. - This branch: builds with no new warnings; `guest_libc_cnd_timed_wait_tests` passes 5 runs of 5, the existing cases included. - `tools/check_conventions.py --base origin/main` and `git diff --check` pass. 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) pass `guest_libc_cnd_timed_wait`; johnmaia (Windows 11, WinLibs GCC 15.2.0) passes it and the full suite. With a nanosecond `system_clock` the new case takes the overflow branch, so it also passes with `main`'s `Threads.cpp` there, as expected. Overlap: yabanana's AOT pull requests (#1835 and #3891, #4156, #4396, #4397, #4422) carry the same `ceil` conversion 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 moves `GuestLibcCndTimedWait.cpp`; whichever lands second rebases. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (see Overlap above) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none (every title on macOS needs libc to build) - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-15 21:26:14 +00:00 (Migrated from github.com)

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.

Pull request Shared files not compared
PR 1835 core/libs/prx/libc/src/Threads.cpp
PR 5021 core/libs/prx/libc/src/Threads.cpp
PR 6044 core/libs/tests/GuestLibcCndTimedWait.cpp
PR 6222 core/libs/prx/libc/src/Threads.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6202621802 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6580#issuecomment-6202621802). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T21:26:14Z. --- <!-- pr-overlap --> 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. | Pull request | Shared files not compared | |---|---| | [PR 1835](https://www.github.com/boykopovar/AnyPS5/pull/1835) | `core/libs/prx/libc/src/Threads.cpp` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libc/src/Threads.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestLibcCndTimedWait.cpp` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/prx/libc/src/Threads.cpp` |
coltken95 commented 2026-10-15 21:42:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by coltken95 on 2026-10-15T21:42:11Z.


Tested on Linux x86-64 (Release build, GCC/libstdc++), with 2854da23a merged into main 31cbb55d7:

  • With the PR: guest_libc_cnd_timed_wait passes.
  • With main's libc/src/Threads.cpp it also passes here, as expected: libstdc++'s std::chrono::system_clock::duration is nanoseconds, so the added ceil<system_clock::duration> changes nothing on Linux. The bug needs a coarser system_clock (microseconds on macOS's libc++), so this run only shows the change is harmless on Linux.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6202881796 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6580#issuecomment-6202881796). Originally posted by [coltken95](https://github.com/coltken95) on 2026-10-15T21:42:11Z. --- Tested on Linux x86-64 (Release build, GCC/libstdc++), with 2854da23a merged into main 31cbb55d7: - With the PR: `guest_libc_cnd_timed_wait` passes. - With main's `libc/src/Threads.cpp` it also passes here, as expected: libstdc++'s `std::chrono::system_clock::duration` is nanoseconds, so the added `ceil<system_clock::duration>` changes nothing on Linux. The bug needs a coarser `system_clock` (microseconds on macOS's libc++), so this run only shows the change is harmless on Linux. AI-assisted: yes (Claude Code)
jotitadev commented 2026-10-15 21:48:34 +00:00 (Migrated from github.com)

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. main 31cbb55d7 with head 2854da23a merged on top.

  • With the PR: libc builds with no new warnings; guest_libc_cnd_timed_wait passes 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 the std::overflow_error branch, as described.
  • Threads.cpp reverted to main, test kept: it also passes. That is expected here: ceil to nanoseconds is the identity, so the change cannot show up on Linux; the red case is the macOS one in the description.
  • Non-GPU suite (ctest -E "^agc|gpu|vulkan|video_out" -j8): 648/650, same as main on this machine. The 2 failures (file_writer_binary, file_writer_text) also fail on main here.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5540354684 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6580#pullrequestreview-5540354684). Originally posted by [jotitadev](https://github.com/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. `main` 31cbb55d7 with head 2854da23a merged on top. - With the PR: libc builds with no new warnings; `guest_libc_cnd_timed_wait` passes 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 the `std::overflow_error` branch, as described. - `Threads.cpp` reverted to `main`, test kept: it also passes. That is expected here: `ceil` to nanoseconds is the identity, so the change cannot show up on Linux; the red case is the macOS one in the description. - Non-GPU suite (`ctest -E "^agc|gpu|vulkan|video_out" -j8`): 648/650, same as `main` on this machine. The 2 failures (`file_writer_binary`, `file_writer_text`) also fail on `main` here. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
johnmaia commented 2026-10-15 23:53:11 +00:00 (Migrated from github.com)

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. Head 2854da23a merged into main 5d9dcc42d, plus #6452 so the MinGW test build works.

  • guest_libc_cnd_timed_wait passes.
  • The full suite passes 1299 of 1300. The only failure, in all three PR runs of this batch, is 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)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6204733685 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6580#issuecomment-6204733685). Originally posted by [johnmaia](https://github.com/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`. Head 2854da23a merged into `main` 5d9dcc42d, plus #6452 so the MinGW test build works. - `guest_libc_cnd_timed_wait` passes. - The full suite passes 1299 of 1300. The only failure, in all three PR runs of this batch, is `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)
DavidSamir commented 2026-10-16 03:23:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-16T03:23:10Z.


Linux and Windows results for 2854da23a. The merge base is e16b2bcce1.

Host Toolchain system_clock With the fix Threads.cpp as on e16b2bcce1, new test kept
Ubuntu 24.04 (WSL2) GCC 13.3.0, Release 1 ns, max 9223372036 s guest_libc_cnd_timed_wait passes 3/3 passes 3/3
Windows 11 WinLibs GCC 15.2.0 r7 (UCRT), Release 1 ns, max 9223372036 s passes 3/3 passes 3/3

On 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_timedwait throws it. I printed the clock values with a small program built by the same compilers, using the test's own duration_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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6207354338 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6580#issuecomment-6207354338). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-16T03:23:10Z. --- Linux and Windows results for 2854da23a. The merge base is e16b2bcce1. | Host | Toolchain | `system_clock` | With the fix | `Threads.cpp` as on e16b2bcce1, new test kept | |---|---|---|---|---| | Ubuntu 24.04 (WSL2) | GCC 13.3.0, Release | 1 ns, max 9223372036 s | `guest_libc_cnd_timed_wait` passes 3/3 | passes 3/3 | | Windows 11 | WinLibs GCC 15.2.0 r7 (UCRT), Release | 1 ns, max 9223372036 s | passes 3/3 | passes 3/3 | On 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_timedwait` throws it. I printed the clock values with a small program built by the same compilers, using the test's own `duration_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
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!6580
No description provided.