upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/submit_frame_test.cpp
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-29 21:05:26 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-29 21:05:26 -0500
commit1aefed0dacd492a040909c9a423f270ebdab84f4 (patch)
treeb0f89e2eb8857e2b927e5876fb073cebea24ba0b /runtime/test/submit_frame_test.cpp
parent22e1349d788cbd1af1cbecfa84180f40c428617d (diff)
runtime: fix present_mutex AB-BA deadlock, cross-queue relay cycle, and held-queue stalls
Diffstat (limited to 'runtime/test/submit_frame_test.cpp')
-rw-r--r--runtime/test/submit_frame_test.cpp157
1 files changed, 157 insertions, 0 deletions
diff --git a/runtime/test/submit_frame_test.cpp b/runtime/test/submit_frame_test.cpp
index 0d456617d..77b8481a0 100644
--- a/runtime/test/submit_frame_test.cpp
+++ b/runtime/test/submit_frame_test.cpp
@@ -2800,6 +2800,163 @@ int queue_sync_and_async() {
2800 std::printf("queue r10 ws3 + r10b a4/a5: signal_gpu_relay, relay_wait, signal-only vis_seq transitivity ok\n"); 2800 std::printf("queue r10 ws3 + r10b a4/a5: signal_gpu_relay, relay_wait, signal-only vis_seq transitivity ok\n");
2801 } 2801 }
2802 2802
2803 // 10. Regression for VulkanPresenter::submit() <-> QueueHolds::barrier_worker AB-BA deadlock on real VulkanQueue:
2804 // Frame 0 has a held non-empty CommandList + deferred Present 0 (barrier_worker callback taking present_mutex_).
2805 // Frame 1 queues a post-barrier non-empty CommandList whose release calls wait_for_preceding_barriers() ->
2806 // wait_barriers_drained(). Using register_after_held() under present_mutex_ and pump_ready() after unlocking
2807 // present_mutex_ completes without deadlocking and preserves strict Frame 0 -> Present 0 -> Frame 1 -> Present 1
2808 // order.
2809 {
2810 VulkanContext context(d.device_proc, d.device, true);
2811 EXPECT(context.loaded());
2812 VulkanQueue direct(context, d.queue, d.family, VulkanQueue::QueueKind::direct);
2813 EXPECT(direct.ok());
2814
2815 void* buf_map = nullptr;
2816 const VkBuffer buf = d.buffer(64, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, &buf_map);
2817 EXPECT(buf != VK_NULL_HANDLE);
2818 ResourceBinding rb{};
2819 rb.buffer = buf;
2820 rb.size = 64;
2821 context.add_resource(901, rb);
2822
2823 CommandList copy_list;
2824 EXPECT(copy_list.copy_buffer(CopyBuffer{901, 32, 901, 0, 32}) && copy_list.close());
2825 auto copy_snap = std::make_shared<const CommandList>(copy_list);
2826
2827 VulkanFence gate(context, 0), done1(context, 0);
2828 EXPECT(gate.semaphore() && done1.semaphore());
2829
2830 std::recursive_mutex present_mutex;
2831 std::deque<int> deferred;
2832 std::mutex order_m;
2833 std::vector<int> order;
2834 auto record = [&](int v) {
2835 std::lock_guard lk(order_m);
2836 order.push_back(v);
2837 };
2838 std::atomic<int> exec_no{0};
2839 context.set_before_execute([&] { record(exec_no.fetch_add(1, std::memory_order_acq_rel) + 1); });
2840
2841 std::atomic<bool> p0_entered{false}, release_p0{false};
2842 int pres_owner = 0;
2843 auto run_deferred = [&] {
2844 if (!p0_entered.exchange(true, std::memory_order_acq_rel)) {
2845 while (!release_p0.load(std::memory_order_acquire)) std::this_thread::yield();
2846 }
2847 std::lock_guard lk(present_mutex);
2848 if (deferred.empty()) return;
2849 const int p = deferred.front();
2850 deferred.pop_front();
2851 record(p);
2852 };
2853
2854 // Frame 0: held behind gate >= 1, then Present 0 (100) queued.
2855 direct.wait(gate.semaphore(), 1);
2856 EXPECT(direct.execute({&copy_snap, 1}) == ReplayResult::success);
2857 {
2858 submit_phases::TitleCall present_call(submit_phases::part_present);
2859 std::lock_guard lk(present_mutex);
2860 deferred.push_back(100);
2861 context.holds().register_after_held(&pres_owner, run_deferred);
2862 }
2863 context.holds().pump_ready();
2864
2865 // Frame 1: held behind Frame 0's gate (also behind Present 0 barrier), signaling done1.
2866 EXPECT(direct.execute({&copy_snap, 1}) == ReplayResult::success);
2867 EXPECT(direct.signal(done1.semaphore(), 1));
2868
2869 // Signal gate on a worker thread: Frame 0 executes, stops at Present 0 barrier, moves Present 0 to
2870 // barrier_worker (which pauses before locking present_mutex_), then Frame 1 enters wait_barriers_drained().
2871 std::atomic<bool> sig_ok{false};
2872 std::thread signaller([&] { sig_ok.store(gate.signal(1), std::memory_order_release); });
2873 while (!p0_entered.load(std::memory_order_acquire)) std::this_thread::yield();
2874
2875 // Frame 1 Present 1 (101) arrives while Present 0 is paused right before locking present_mutex_.
2876 {
2877 submit_phases::TitleCall present_call(submit_phases::part_present);
2878 std::lock_guard lk(present_mutex);
2879 deferred.push_back(101);
2880 context.holds().register_after_held(&pres_owner, run_deferred);
2881 release_p0.store(true, std::memory_order_release);
2882 }
2883 context.holds().pump_ready();
2884 signaller.join();
2885 EXPECT(sig_ok.load(std::memory_order_acquire));
2886
2887 EXPECT(done1.wait(1, 5'000'000'000ull));
2888 context.holds().wait_barriers_drained(UINT64_MAX);
2889 EXPECT(direct.idle());
2890 EXPECT(deferred.empty());
2891 EXPECT((order == std::vector<int>{1, 100, 2, 101}));
2892 std::printf("queue register_after_held presenter mutex AB-BA regression ok\n");
2893 }
2894
2895 // 11. VulkanQueue::is_held() distinguishes a queue genuinely blocked behind an unsatisfied fence wait (in inbox_,
2896 // waits_, or held_) from an unheld Signal merely queued in async_worker's inbox_ under LEONIDA_SUBMIT_ASYNC>0.
2897 {
2898 VulkanContext context(d.device_proc, d.device, true);
2899 EXPECT(context.loaded());
2900 VulkanQueue async_q(context, d.queue, d.family, VulkanQueue::QueueKind::direct);
2901 EXPECT(async_q.ok());
2902 async_q.set_async_depth(4);
2903
2904 auto empty_list = std::make_shared<CommandList>();
2905 EXPECT(empty_list->uav_barrier({}) && empty_list->close());
2906 std::shared_ptr<const CommandList> snap = empty_list;
2907
2908 std::atomic<bool> worker_paused{false}, worker_release{false};
2909 context.set_before_execute([&] {
2910 if (!worker_paused.exchange(true, std::memory_order_acq_rel)) {
2911 while (!worker_release.load(std::memory_order_acquire)) std::this_thread::yield();
2912 }
2913 });
2914
2915 VulkanFence written(context, 0), title_f(context, 0);
2916 EXPECT(written.semaphore() && title_f.semaphore());
2917 EXPECT(!async_q.is_held());
2918
2919 // Pause async_worker inside batch 1 so the subsequent Signal stays queued in inbox_ (pending in QueueHolds).
2920 EXPECT(async_q.execute({&snap, 1}) == ReplayResult::success);
2921 while (!worker_paused.load(std::memory_order_acquire)) std::this_thread::yield();
2922 EXPECT(async_q.signal(written.semaphore(), 1));
2923 EXPECT(context.holds().pending(written.semaphore(), 1));
2924 EXPECT(!async_q.is_held()); // unheld Signal sitting in inbox_ MUST NOT read as held!
2925
2926 // Now enqueue an unsatisfied Wait + Signal behind it in inbox_: queue MUST read as held immediately.
2927 async_q.wait(title_f.semaphore(), 1);
2928 EXPECT(async_q.signal(written.semaphore(), 2));
2929 EXPECT(async_q.is_held());
2930
2931 // Unpause async_worker: batch 1 + written=1 complete; Wait(title_f, 1) + Signal(written, 2) move to waits_/held_.
2932 worker_release.store(true, std::memory_order_release);
2933 EXPECT(written.wait(1, 5'000'000'000ull));
2934 // Wait until async_worker moves the held Wait/Signal from inbox_ to waits_/held_ (still held!).
2935 for (int i = 0; i < 2000 && !context.holds().any_held(); ++i)
2936 std::this_thread::sleep_for(std::chrono::milliseconds(1));
2937 EXPECT(async_q.is_held());
2938
2939 // Signal title_f=1: queue unblocks, written reaches 2, and is_held() returns false.
2940 EXPECT(title_f.signal(1));
2941 EXPECT(written.wait(2, 5'000'000'000ull));
2942 EXPECT(async_q.idle());
2943 EXPECT(!async_q.is_held());
2944
2945 // Sync queue check (LEONIDA_SUBMIT_ASYNC=0): unheld vs held behind unsatisfied Wait.
2946 VulkanQueue sync_q(context, d.queue, d.family, VulkanQueue::QueueKind::compute);
2947 EXPECT(sync_q.ok());
2948 sync_q.set_async_depth(0);
2949 EXPECT(!sync_q.is_held());
2950 sync_q.wait(title_f.semaphore(), 2);
2951 EXPECT(sync_q.is_held());
2952 EXPECT(sync_q.signal(written.semaphore(), 3));
2953 EXPECT(sync_q.is_held());
2954 EXPECT(title_f.signal(2));
2955 EXPECT(written.wait(3, 5'000'000'000ull));
2956 EXPECT(!sync_q.is_held());
2957 std::printf("queue is_held async vs sync regression ok\n");
2958 }
2959
2803 return 0; 2960 return 0;
2804} 2961}
2805 2962