fix(libc): use precise Windows thread sleep #8129

Merged
Th3EvilGod merged 1 commit from upstream-pr/8129 into main 2026-10-20 05:31:11 +00:00
Th3EvilGod commented 2026-10-19 23:23:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Th3EvilGod on 2026-10-19T23:23:41Z.


What

_Thrd_sleep uses winpthreads nanosleep on Windows, which truncates sub-millisecond requests and can finish millisecond waits on 15.6 ms clock steps. Use the existing high-resolution waitable-timer helper for the bounded relative wait, rounding up to 100 ns, then recheck the absolute realtime deadline.

The existing xtime normalization, one-day chunk bound, past-deadline behavior and POSIX nanosleep path remain intact. Add guest-API checks for 100 us and 2 ms deadlines and a broad 12 ms median bound over 31 samples. Keep the existing cancellation/console-contract gaps explicit in libc technical debt.

Related to #1471; this addresses only _Thrd_sleep. #7735 changes condition-variable waits in the same source file, independently of this function.

Tested

Windows 11 Pro (26300), AMD Ryzen 7 9800X3D, WinLibs MinGW-w64 GCC 15.2.0 r7, Release build. No title or console verification.

  • With the new regression test on main 7c2334d20835a4126ec8c8ba4af32735c4535eed, the 2 ms guest sleep median was 15,527,800 ns and guest_ampr_threads failed the 12 ms bound; the other three guest thread/import tests passed.
  • With the fix, all five focused tests passed: precise_wait, guest_libc_threads, guest_libc_static_imports, guest_libc_cnd_timed_wait, and guest_ampr_threads. The 2 ms guest sleep median was 2,005,500 ns. Existing past, normalized-positive/negative xtime and null-target checks passed. Every sampled short sleep was checked against its absolute realtime deadline.
  • Rebuilt and reran the same five tests after rebasing onto main 606ebafd203c016ec18b8501a1f50a76ad52bf02: 5/5 passed.
  • python tools/check_conventions.py --base origin/main: passed.
  • python tools/pr_overlap.py --branch codex/libc-precise-thread-sleep --repo boykopovar/AnyPS5: completed with no confirmed overlaps. Manually inspected the shared files flagged for stale/conflicting PRs #1835, #5021, #5303, #6044, #6226, #6469, #6819, #6879, #7081, #7201, #7332, #7386, #7473 and #7715: the Threads.cpp changes affect _Cnd_timedwait, the debt changes affect other entries, and #6044 moves GuestAmprThreads.cpp unchanged into tests/memory. No other PR implements this sleep fix. #7735 also merges independently.

Checklist

  • Based on current main; no other open PR implements the same functions
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4928734297 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8129). Originally posted by [Th3EvilGod](https://github.com/Th3EvilGod) on 2026-10-19T23:23:41Z. --- ### What `_Thrd_sleep` uses winpthreads `nanosleep` on Windows, which truncates sub-millisecond requests and can finish millisecond waits on 15.6 ms clock steps. Use the existing high-resolution waitable-timer helper for the bounded relative wait, rounding up to 100 ns, then recheck the absolute realtime deadline. The existing xtime normalization, one-day chunk bound, past-deadline behavior and POSIX `nanosleep` path remain intact. Add guest-API checks for 100 us and 2 ms deadlines and a broad 12 ms median bound over 31 samples. Keep the existing cancellation/console-contract gaps explicit in libc technical debt. Related to #1471; this addresses only `_Thrd_sleep`. #7735 changes condition-variable waits in the same source file, independently of this function. ### Tested Windows 11 Pro (26300), AMD Ryzen 7 9800X3D, WinLibs MinGW-w64 GCC 15.2.0 r7, Release build. No title or console verification. - With the new regression test on main `7c2334d20835a4126ec8c8ba4af32735c4535eed`, the 2 ms guest sleep median was 15,527,800 ns and `guest_ampr_threads` failed the 12 ms bound; the other three guest thread/import tests passed. - With the fix, all five focused tests passed: `precise_wait`, `guest_libc_threads`, `guest_libc_static_imports`, `guest_libc_cnd_timed_wait`, and `guest_ampr_threads`. The 2 ms guest sleep median was 2,005,500 ns. Existing past, normalized-positive/negative xtime and null-target checks passed. Every sampled short sleep was checked against its absolute realtime deadline. - Rebuilt and reran the same five tests after rebasing onto main `606ebafd203c016ec18b8501a1f50a76ad52bf02`: 5/5 passed. - `python tools/check_conventions.py --base origin/main`: passed. - `python tools/pr_overlap.py --branch codex/libc-precise-thread-sleep --repo boykopovar/AnyPS5`: completed with no confirmed overlaps. Manually inspected the shared files flagged for stale/conflicting PRs #1835, #5021, #5303, #6044, #6226, #6469, #6819, #6879, #7081, #7201, #7332, #7386, #7473 and #7715: the Threads.cpp changes affect `_Cnd_timedwait`, the debt changes affect other entries, and #6044 moves GuestAmprThreads.cpp unchanged into tests/memory. No other PR implements this sleep fix. #7735 also merges independently. <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
github-actions[bot] commented 2026-10-19 23:32:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T23:32:15Z.


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
docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 6044 core/libs/tests/GuestAmprThreads.cpp
PR 6226 docs/dev/debt/libc.md
PR 6469 docs/dev/debt/libc.md
PR 6819 docs/dev/debt/libc.md
PR 6879 core/libs/prx/libc/src/Threads.cpp
PR 7081 docs/dev/debt/libc.md
PR 7201 core/libs/prx/libc/src/Threads.cpp
PR 7332 docs/dev/debt/libc.md
PR 7386 docs/dev/debt/libc.md
PR 7447 docs/dev/debt/libc.md
PR 7473 docs/dev/debt/libc.md
PR 7715 docs/dev/debt/libc.md
PR 8037 docs/dev/debt/libc.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6279669267 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8129#issuecomment-6279669267). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-19T23:32:15Z. --- <!-- 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`<br>`docs/dev/debt/libc.md` | | [PR 5303](https://www.github.com/boykopovar/AnyPS5/pull/5303) | `docs/dev/debt/libc.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestAmprThreads.cpp` | | [PR 6226](https://www.github.com/boykopovar/AnyPS5/pull/6226) | `docs/dev/debt/libc.md` | | [PR 6469](https://www.github.com/boykopovar/AnyPS5/pull/6469) | `docs/dev/debt/libc.md` | | [PR 6819](https://www.github.com/boykopovar/AnyPS5/pull/6819) | `docs/dev/debt/libc.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libc/src/Threads.cpp` | | [PR 7081](https://www.github.com/boykopovar/AnyPS5/pull/7081) | `docs/dev/debt/libc.md` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libc/src/Threads.cpp` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `docs/dev/debt/libc.md` | | [PR 7386](https://www.github.com/boykopovar/AnyPS5/pull/7386) | `docs/dev/debt/libc.md` | | [PR 7447](https://www.github.com/boykopovar/AnyPS5/pull/7447) | `docs/dev/debt/libc.md` | | [PR 7473](https://www.github.com/boykopovar/AnyPS5/pull/7473) | `docs/dev/debt/libc.md` | | [PR 7715](https://www.github.com/boykopovar/AnyPS5/pull/7715) | `docs/dev/debt/libc.md` | | [PR 8037](https://www.github.com/boykopovar/AnyPS5/pull/8037) | `docs/dev/debt/libc.md` |
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!8129
No description provided.