[PR #2402] vulkan: read the retirement records under their own lock (RT-RETIRE-TICKET-LOCK) #2403

Open
opened 2026-11-26 21:21:07 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2402
Author: @wavehs
Created: 11/26/2026
Status: 🔄 Open

Base: main ← Head: retire-ticket-lock


📝 Commits (1)

  • 35eb7b4 vulkan: read the retirement records under their own lock (RT-RETIRE-TICKET-LOCK)

📊 Changes

4 files changed (+109 additions, -16 deletions)

View changed files

📝 runtime/CMakeLists.txt (+10 -0)
📝 runtime/shims/d3d12_vulkan_device.cpp (+3 -2)
📝 runtime/src/vulkan_backend.h (+31 -14)
➕ runtime/test/retire_ticket_lock_test.cpp (+65 -0)

📄 Description

Claimed in the coordination issue as: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7201897573
(follow-up to DadaCap's finding: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7201692029)

Depends on: none.

What and why

VulkanContext::completed_retire_ticket() and wait_retire_ticket() read retire_tickets_ under submit_mutex_, so a caller that only asks what retired waits behind whoever holds that lock — a queue submission, or a vkDeviceWaitIdle under it. That is the hop DadaCap described: the held-work releaser inside a gate Signal and the progress watch both want the completed ticket and neither needs to submit.

retire_tickets_ now has its own innermost retire_mutex_:

  • record_submit_locked and retire_queue_idle_locked take submit_mutex_ -> retire_mutex_;
  • completed_retire_ticket and wait_retire_ticket take retire_mutex_ alone; the wait_submitted_locked snapshot takes it under the submit_mutex_ its caller already holds;
  • nothing takes retire_mutex_ before submit_mutex_, so the order is unchanged everywhere else;
  • poll_retirement_locked try-locks retire_mutex_ on purpose: a query already in progress watches the same timelines, and waiting for it while holding the submit lock is what this change removes.

Queue teardown is still safe: it marks the queue's entries idle under retire_mutex_ (via retire_queue_idle_locked) before destroying the timeline semaphore, so an in-flight query either finishes first or is skipped because the entry is idle.

Test

New CPU-only ctest retire_ticket_lock (runtime/test/retire_ticket_lock_test.cpp): one thread holds context.submit_mutex() for 500 ms, the main thread calls completed_retire_ticket() and must get ticket 0 in under 200 ms.

=== main 2d43417d4 (only runtime/src/vulkan_backend.h reverted, same build directory)
completed_retire_ticket returned 505ms after a submit lock was held (ticket 0)
line 55: waited < 200
NEG_EXIT=1

=== with the change
completed_retire_ticket returned 0ms after a submit lock was held (ticket 0)
retire_ticket_lock_test: passed
POS_EXIT=0

Tested

macOS 27.2.1 arm64 (Apple clang, CMake/Ninja, RelWithDebInfo), portable runtime build, no GPU and no game files:

  • ctest after a full build: 114/114 passed (63-66 s). retire_tickets and replay_pipeline_compile_unlock are in that run; submit_mutex is Windows-only and is not part of the portable set.
  • python3 -I tests/test_ctest_registration.py -> 6 tests, OK.
  • python3 -I scripts/check_no_game_data.py on the four changed files -> exit 0.
  • python3 -m compileall -q make_game.py tools tests -> OK.

Not built here: the Windows runtime (MSVC) and the title. The change is portable C++17 and the new target links Vulkan::Headers Threads::Threads like its neighbours, but the Windows build should confirm it.

AI-assisted.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2402 **Author:** [@wavehs](https://github.com/wavehs) **Created:** 11/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `retire-ticket-lock` --- ### 📝 Commits (1) - [`35eb7b4`](https://github.com/ubiquitarianism/leonida/commit/35eb7b4a4071897c5337ba67c428918b15c08ed1) vulkan: read the retirement records under their own lock (RT-RETIRE-TICKET-LOCK) ### 📊 Changes **4 files changed** (+109 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+10 -0) 📝 `runtime/shims/d3d12_vulkan_device.cpp` (+3 -2) 📝 `runtime/src/vulkan_backend.h` (+31 -14) ➕ `runtime/test/retire_ticket_lock_test.cpp` (+65 -0) </details> ### 📄 Description Claimed in the coordination issue as: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7201897573 (follow-up to DadaCap's finding: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7201692029) Depends on: none. ### What and why `VulkanContext::completed_retire_ticket()` and `wait_retire_ticket()` read `retire_tickets_` under `submit_mutex_`, so a caller that only asks what retired waits behind whoever holds that lock — a queue submission, or a `vkDeviceWaitIdle` under it. That is the hop DadaCap described: the held-work releaser inside a gate `Signal` and the progress watch both want the completed ticket and neither needs to submit. `retire_tickets_` now has its own innermost `retire_mutex_`: - `record_submit_locked` and `retire_queue_idle_locked` take `submit_mutex_ -> retire_mutex_`; - `completed_retire_ticket` and `wait_retire_ticket` take `retire_mutex_` alone; the `wait_submitted_locked` snapshot takes it under the `submit_mutex_` its caller already holds; - nothing takes `retire_mutex_` before `submit_mutex_`, so the order is unchanged everywhere else; - `poll_retirement_locked` try-locks `retire_mutex_` on purpose: a query already in progress watches the same timelines, and waiting for it while holding the submit lock is what this change removes. Queue teardown is still safe: it marks the queue's entries idle under `retire_mutex_` (via `retire_queue_idle_locked`) before destroying the timeline semaphore, so an in-flight query either finishes first or is skipped because the entry is idle. ### Test New CPU-only ctest `retire_ticket_lock` (`runtime/test/retire_ticket_lock_test.cpp`): one thread holds `context.submit_mutex()` for 500 ms, the main thread calls `completed_retire_ticket()` and must get ticket 0 in under 200 ms. ```text === main 2d43417d4 (only runtime/src/vulkan_backend.h reverted, same build directory) completed_retire_ticket returned 505ms after a submit lock was held (ticket 0) line 55: waited < 200 NEG_EXIT=1 === with the change completed_retire_ticket returned 0ms after a submit lock was held (ticket 0) retire_ticket_lock_test: passed POS_EXIT=0 ``` ### Tested macOS 27.2.1 arm64 (Apple clang, CMake/Ninja, RelWithDebInfo), portable runtime build, no GPU and no game files: - `ctest` after a full build: **114/114 passed** (63-66 s). `retire_tickets` and `replay_pipeline_compile_unlock` are in that run; `submit_mutex` is Windows-only and is not part of the portable set. - `python3 -I tests/test_ctest_registration.py` -> 6 tests, OK. - `python3 -I scripts/check_no_game_data.py` on the four changed files -> exit 0. - `python3 -m compileall -q make_game.py tools tests` -> OK. Not built here: the Windows runtime (MSVC) and the title. The change is portable C++17 and the new target links `Vulkan::Headers Threads::Threads` like its neighbours, but the Windows build should confirm it. AI-assisted. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2403
No description provided.