Skip to content

fix(libc): end _Mtx_timedlock on an owned plain mutex at its deadline on Windows - #22380

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
LeandroLP23:fix/libc-owned-timedlock-precise-sleep
Nov 27, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
LeandroLP23:fix/libc-owned-timedlock-precise-sleep

Conversation

@LeandroLP23

@LeandroLP23 LeandroLP23 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What

Part of #1471. _Mtx_timedlock on a plain mutex the caller already holds sleeps until the deadline and times out. It slept with std::this_thread::sleep_until, which on Windows goes through winpthreads nanosleep: whole milliseconds, ending only once GetTickCount64 has advanced, in 15.6 ms steps. A deadline 2 ms away returned after about 15.6 ms, and one under 1 ms away spun without sleeping. It now sleeps with PreciseSleepUntil from PreciseWait.hpp, which _Thrd_sleep in the same file already uses on Windows. Other paths of _Mtx_timedlock and _Cnd_timedwait already wait on native Windows primitives.

guest_libc_threads now also makes 20 such _Mtx_timedlock calls with a deadline 2 ms ahead, which must each time out and take 38 to 200 ms in total. 2 ms rather than 1 ms, because with main a remaining time under 1 ms is a busy spin that ends on time, so a 1 ms deadline does not show the late return reliably.

Tested

Windows 11 10.0.26300, AMD Ryzen AI 7 350, WinLibs GCC 15.2, Release, main 9f94e4a2ce.

  • guest_libc_threads: passes 3 of 3 on this head. With main's Threads.cpp put back and the new test kept, it aborts 2 of 2 at the new check.
  • Durations of the 20 waits, from a local copy of the test that prints them: 40.8, 40.3 and 40.7 ms on this head; 307.7, 312.1 and 311.9 ms with main's Threads.cpp.
  • Linux not run by me. The first head (a99bdd7cda) did not build there because PreciseWait.hpp was included only on Windows; 416a76e580 includes it on every platform, which leaves the Windows build unchanged. On Linux the sleep was already precise, and PreciseSleepUntil uses the same timer sleep with a short spin tail as its other callers.
  • check_conventions.py --base origin/main passes. pr_overlap.py --branch could not compare feat(aot): discuss native ARM64 compiler architecture and integration #1835 and feat(libc): add the std::__sce_v2 thread start, timed wait, clock and processor count calls #9447, which conflict with main; their diffs do not touch _Mtx_timedlock or TimedMutex.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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 (Claude Code)

… on Windows

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against a99bdd7cda:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libc_threads_tests failed to build.
    core/libs/prx/libc/src/Threads.cpp:281:13: error: ‘PreciseSleepUntil’ was not declared in this scope
    

Requesting changes for the points above.

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 416a76e580:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libc_threads_tests built cleanly.
  • CTest (guest_libc_threads):
    1/1 Test #1197: guest_libc_threads ...............   Passed    0.33 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/Threads.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1197: guest_libc_threads ...............   Passed    0.33 sec
    100% tests passed, 0 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified that PreciseWait.hpp is now unconditionally included in core/libs/prx/libc/src/Threads.cpp (fixing the prior Linux build failure where PreciseSleepUntil was undeclared when #ifdef _WIN32 gated the header).
    • Verified that PreciseSleepUntil(deadline) in _Mtx_timedlock_nid_postfix correctly handles the deadline parameter typed as std::chrono::system_clock::time_point, matching XtimePoint(*target, ...) conversion.
    • Verified that the caller-owned plain mutex deadlock prevention path correctly retains ownership state, exits with threadTimedout, and releases no locks.
    • Verified the timing assertions in core/libs/tests/GuestLibcThreads.cpp: 20 iterations of 2 ms waits correctly expect a cumulative elapsed duration in [38 ms, 200 ms), properly testing sub-15.6ms sleep precision on Windows without being flaky.
    • Verified AGENTS.md compliance: no comments added, conventional commit title, no new files outside allowed locations, and conventions tool passed.

Looks Good To Me

@LeandroLP23

Copy link
Copy Markdown
Contributor Author

Thanks for the Linux run, and thanks oneandonlydean for 416a76e: I had gated the include on _WIN32 and missed that the call is unconditional.

On the negative control: on Linux the old code was already precise, so with main's sources the test passes there, as the pasted output shows. The regression only shows on Windows, where winpthreads ends timed waits in 15.6 ms steps; the Windows negative control and the before/after timings are in the Tested section.

AI-assisted: yes (Claude Code)

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

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

@Kal0s-design

Copy link
Copy Markdown

Windows run on a second machine, mainly to check the new timing bound for flakiness, since the description has 3 runs. Windows 11, WinLibs GCC 15 Release, head 416a76e580 merged onto main 4fa81a86fb (clean merge).

  • Negative control — main 4fa81a86fb with only this PR's GuestLibcThreads.cpp: guest_libc_threads aborts in 20 of 20 runs.
  • With the change: guest_libc_threads passes 50 of 50 runs, so the 38 to 200 ms window held every time here.
  • ctest -R '^guest_(libc_threads|cnd|mtx|thrd|tss|call_once)' with the change: 4 of 4 pass.

Not covered: Linux and macOS, a loaded machine (the runs were sequential on an otherwise idle system, so this says nothing about the upper bound under a parallel ctest), and I did not print the individual durations.

AI-assisted: yes

@1tsethan

Copy link
Copy Markdown
Contributor

Native Windows mutex deadline check. I applied the 416a76e580 change alone onto 732c68c9fd and built guest_libc_threads_tests with MSYS2 GCC 16.2.0. The full guest_libc_threads CTest passed five consecutive runs, including the new 20-call self-owned-mutex timeout assertion.

AI-assisted with OpenAI Codex.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Nothing blocks this change, and the inspected code supports merging at the reviewed head.

What I ran

Reviewed head 416a76e5808b990c50eadb2d695caba19eb0b69a. I inspected the prepared diff and targeted source ranges using repo_read.py; no build or tests were run.

Results

Check Result Note
Diff and targeted source review passed Both changed files read
Build and tests not run No commands executed

Findings

Blocking

None.

Non-blocking

None.

Not verified

No build or test execution was performed. Windows and Linux runtime behavior was not independently confirmed; the platform test results in the PR description are author-reported. Both files listed in manifest.json under reviewed were read: core/libs/prx/libc/src/Threads.cpp and core/libs/tests/GuestLibcThreads.cpp. No files were excluded in the available manifest.

To get this to a good state

Nothing is required.

@kiwe147

kiwe147 commented Nov 20, 2026

Copy link
Copy Markdown

Tested head 416a76e580, applied with git apply on main fd4aeed483 together with the other sixteen PRs of one batch (#24596, #24594, #24595, #24599, #24600, #24602, #24593, #24659, #24651, #24626, #24625, #24624, #24456, #24656, #24502, #24351). All seventeen applied without conflicts. The tree built except libSceAjm.prx: #24659 breaks it with duplicate patched export name: -qLsfDAywIY (checked alone on main, reported there). No other target failed to build.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
guest_libc_threads (1 test) all pass outside the iGPU subset

Whole batch: the full ctest -j 12 (6524 tests) on the RTX 3080 has 4 failures, and all 4 also fail on main fd4aeed483 (agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10). The 2125 agc_/video tests on the iGPU fail agc_driver_stencil_plane_memory (also on main), agc_driver_store_over_untracked (also fails alone on main on this iGPU, 6 of 6) and agc_driver_image_table_packed_guards (passes 5 of 5 alone on this tree, known flaky). None of this batch touches AGC.

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

@oneandonlydean
oneandonlydean merged commit 8689e05 into boykopovar:main Nov 27, 2026
22 of 24 checks passed
@LeandroLP23
LeandroLP23 deleted the fix/libc-owned-timedlock-precise-sleep branch November 27, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants