upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 10:39:32 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 10:39:32 -0500
commit8b61d266bba4e5a5d4b553b81e7c8d66c644bfba (patch)
tree3278f334a28108aea1a812aa6055cb07c22ae76d /runtime
parent525edbc9b749e941d523ec4cf9e7a37bd8852126 (diff)
parentb01bed6efdee5823e94b90643075e4560c5ed70b (diff)
Merge pull request #1933 from Emirhan Karaca
Diffstat (limited to 'runtime')
-rw-r--r--runtime/CMakeLists.txt16
-rw-r--r--runtime/src/vulkan_backend.h165
-rw-r--r--runtime/test/vk_fence_dispatcher_sync_test.cpp262
3 files changed, 398 insertions, 45 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt
index c5788c4da..fdfb75e7b 100644
--- a/runtime/CMakeLists.txt
+++ b/runtime/CMakeLists.txt
@@ -805,6 +805,22 @@ if(TARGET Vulkan::Headers)
805 add_test(NAME vk_bc_zero_init COMMAND vk_bc_zero_init_test) 805 add_test(NAME vk_bc_zero_init COMMAND vk_bc_zero_init_test)
806 set_tests_properties(vk_bc_zero_init PROPERTIES SKIP_RETURN_CODE 77) 806 set_tests_properties(vk_bc_zero_init PROPERTIES SKIP_RETURN_CODE 77)
807 endif() 807 endif()
808 # FenceWaitDispatcher concurrency and device-lost regressions (re-entrant callbacks, wakeup on device lost, counter error drain).
809 add_executable(vk_fence_dispatcher_sync_test test/vk_fence_dispatcher_sync_test.cpp)
810 target_link_libraries(vk_fence_dispatcher_sync_test PRIVATE Vulkan::Headers ${CMAKE_DL_LIBS})
811 if(WIN32)
812 target_compile_definitions(vk_fence_dispatcher_sync_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
813 else()
814 find_package(Threads REQUIRED)
815 target_link_libraries(vk_fence_dispatcher_sync_test PRIVATE Threads::Threads)
816 endif()
817 if(MSVC)
818 target_compile_options(vk_fence_dispatcher_sync_test PRIVATE /W4 /permissive- /utf-8)
819 endif()
820 if(BUILD_TESTING)
821 add_test(NAME vk_fence_dispatcher_sync COMMAND vk_fence_dispatcher_sync_test)
822 set_tests_properties(vk_fence_dispatcher_sync PROPERTIES SKIP_RETURN_CODE 77)
823 endif()
808 # CPU-only: the bindless set's array sizes from synthetic device limits. 824 # CPU-only: the bindless set's array sizes from synthetic device limits.
809 add_executable(bindless_sizing_test test/bindless_sizing_test.cpp) 825 add_executable(bindless_sizing_test test/bindless_sizing_test.cpp)
810 target_link_libraries(bindless_sizing_test PRIVATE Vulkan::Headers) 826 target_link_libraries(bindless_sizing_test PRIVATE Vulkan::Headers)
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index db14934c1..93d7e5259 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -500,56 +500,79 @@ public:
500 500
501 void enqueue(VkSemaphore sem, std::uint64_t value, void* event_handle) { 501 void enqueue(VkSemaphore sem, std::uint64_t value, void* event_handle) {
502 if (!sem || !event_handle) return; 502 if (!sem || !event_handle) return;
503 if (device_lost_.load(std::memory_order_acquire)) { 503 SignalEventFn immediate_fn = nullptr;
504 SignalEventFn fn = nullptr;
505 {
506 std::lock_guard lock(mutex_);
507 fn = signal_event_;
508 }
509 if (fn) fn(event_handle);
510 return;
511 }
512 { 504 {
513 std::lock_guard lock(mutex_); 505 std::lock_guard lock(mutex_);
514 pending_.push_back({sem, value, event_handle, {}}); 506 if (stop_ || device_lost_.load(std::memory_order_acquire)) {
515 if (!thread_.joinable() && wakeup_sem_) thread_ = std::thread([this] { run(); }); 507 immediate_fn = signal_event_;
516 signal_wakeup_locked(); 508 } else {
509 pending_.push_back({sem, value, event_handle, {}});
510 if (!thread_.joinable() && wakeup_sem_) thread_ = std::thread([this] { run(); });
511 signal_wakeup_locked();
512 }
513 }
514 if (immediate_fn) {
515 immediate_fn(event_handle);
516 return;
517 } 517 }
518 cv_.notify_one(); 518 cv_.notify_one();
519 } 519 }
520 520
521 void enqueue_callback(VkSemaphore sem, std::uint64_t value, std::function<void()> cb) { 521 void enqueue_callback(VkSemaphore sem, std::uint64_t value, std::function<void()> cb) {
522 if (!sem || !cb) return; 522 if (!sem || !cb) return;
523 if (device_lost_.load(std::memory_order_acquire)) { 523 std::function<void()> immediate_cb;
524 cb(); 524 {
525 std::lock_guard lock(mutex_);
526 if (stop_ || device_lost_.load(std::memory_order_acquire)) {
527 immediate_cb = std::move(cb);
528 } else {
529 pending_.push_back({sem, value, nullptr, std::move(cb)});
530 if (!thread_.joinable() && wakeup_sem_) thread_ = std::thread([this] { run(); });
531 signal_wakeup_locked();
532 }
533 }
534 if (immediate_cb) {
535 immediate_cb();
525 return; 536 return;
526 } 537 }
538 cv_.notify_one();
539 }
540
541 // Wakes run() when another path marks the device lost so pending waits complete and stop()/cancel() never hang.
542 void notify_device_lost() {
527 { 543 {
528 std::lock_guard lock(mutex_); 544 std::lock_guard lock(mutex_);
529 pending_.push_back({sem, value, nullptr, std::move(cb)});
530 if (!thread_.joinable() && wakeup_sem_) thread_ = std::thread([this] { run(); });
531 signal_wakeup_locked(); 545 signal_wakeup_locked();
532 } 546 }
533 cv_.notify_one(); 547 cv_.notify_all();
534 } 548 }
535 549
536 // Completes the pending waits for `sem` (a released D3D12 fence counts as reached, as on the PC runtime) and 550 // Completes the pending waits for `sem` (a released D3D12 fence counts as reached, as on the PC runtime) and
537 // blocks until any in-flight vkWaitSemaphores releases `sem` before the caller destroys the VkSemaphore. 551 // blocks until any in-flight vkWaitSemaphores releases `sem` before the caller destroys the VkSemaphore.
538 void cancel(VkSemaphore sem) { 552 void cancel(VkSemaphore sem) {
539 if (!sem) return; 553 if (!sem) return;
540 std::unique_lock lock(mutex_); 554 std::vector<Entry> cancelled;
541 std::erase_if(pending_, [this, sem](const Entry& e) { 555 SignalEventFn fn = nullptr;
542 if (e.semaphore != sem) return false; 556 {
543 if (e.callback) e.callback(); 557 std::unique_lock lock(mutex_);
544 else if (signal_event_ && e.event_handle) signal_event_(e.event_handle); 558 fn = signal_event_;
545 return true; 559 std::size_t write = 0;
546 }); 560 for (std::size_t i = 0; i < pending_.size(); ++i) {
547 if (!thread_.joinable()) return; 561 if (pending_[i].semaphore == sem) {
548 if (waiting_) { 562 cancelled.push_back(std::move(pending_[i]));
549 const std::uint64_t target_epoch = epoch_ + 1; 563 } else {
550 signal_wakeup_locked(); 564 if (write != i) pending_[write] = std::move(pending_[i]);
551 epoch_cv_.wait(lock, [this, target_epoch] { return !waiting_ || epoch_ >= target_epoch; }); 565 ++write;
566 }
567 }
568 pending_.resize(write);
569 if (thread_.joinable() && waiting_) {
570 const std::uint64_t target_epoch = epoch_ + 1;
571 signal_wakeup_locked();
572 epoch_cv_.wait(lock, [this, target_epoch] { return !waiting_ || epoch_ >= target_epoch; });
573 }
552 } 574 }
575 dispatch_entries(cancelled, fn);
553 } 576 }
554 577
555 void stop() { 578 void stop() {
@@ -564,10 +587,18 @@ public:
564 } 587 }
565 588
566private: 589private:
590 static void dispatch_entries(std::vector<Entry>& entries, SignalEventFn signal_event) {
591 for (Entry& e : entries) {
592 if (e.callback) e.callback();
593 else if (signal_event && e.event_handle) signal_event(e.event_handle);
594 }
595 }
596
567 // Only signals wakeup_sem_ when run() is actively waiting (waiting_ == true) on wakeup_val_ + 1, 597 // Only signals wakeup_sem_ when run() is actively waiting (waiting_ == true) on wakeup_val_ + 1,
568 // eliminating any lost-wakeup race between snapshot construction and vkWaitSemaphores. 598 // eliminating any lost-wakeup race between snapshot construction and vkWaitSemaphores. Keep signaling even if
599 // device_lost_ is already set so a blocked vkWaitSemaphores wakes cleanly for cancel()/stop().
569 void signal_wakeup_locked() { 600 void signal_wakeup_locked() {
570 if (!waiting_ || !wakeup_sem_ || device_lost_.load(std::memory_order_relaxed)) return; 601 if (!waiting_ || !wakeup_sem_) return;
571 const std::uint64_t next = ++wakeup_val_; 602 const std::uint64_t next = ++wakeup_val_;
572 VkSemaphoreSignalInfo si{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO}; 603 VkSemaphoreSignalInfo si{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO};
573 si.semaphore = wakeup_sem_; 604 si.semaphore = wakeup_sem_;
@@ -578,7 +609,15 @@ private:
578 void run() { 609 void run() {
579 std::unique_lock lock(mutex_); 610 std::unique_lock lock(mutex_);
580 while (true) { 611 while (true) {
581 cv_.wait(lock, [this] { return stop_ || !pending_.empty(); }); 612 cv_.wait(lock, [this] { return stop_ || !pending_.empty() || device_lost_.load(std::memory_order_acquire); });
613 if (device_lost_.load(std::memory_order_acquire)) {
614 std::vector<Entry> drained = std::move(pending_);
615 pending_.clear();
616 const SignalEventFn fn = signal_event_;
617 lock.unlock();
618 dispatch_entries(drained, fn);
619 return;
620 }
582 if (stop_) return; 621 if (stop_) return;
583 622
584 semaphores_.clear(); 623 semaphores_.clear();
@@ -605,25 +644,53 @@ private:
605 ++epoch_; 644 ++epoch_;
606 epoch_cv_.notify_all(); 645 epoch_cv_.notify_all();
607 646
608 if (res == VK_ERROR_DEVICE_LOST) { 647 if (res == VK_ERROR_DEVICE_LOST || device_lost_.load(std::memory_order_acquire)) {
609 device_lost_.store(true, std::memory_order_release); 648 device_lost_.store(true, std::memory_order_release);
610 for (const Entry& e : pending_) { 649 std::vector<Entry> drained = std::move(pending_);
611 if (e.callback) e.callback();
612 else if (signal_event_ && e.event_handle) signal_event_(e.event_handle);
613 }
614 pending_.clear(); 650 pending_.clear();
651 const SignalEventFn fn = signal_event_;
652 lock.unlock();
653 dispatch_entries(drained, fn);
615 return; 654 return;
616 } 655 }
617 if (stop_) return; 656 if (stop_) return;
618 657
619 std::erase_if(pending_, [this](const Entry& e) { 658 std::vector<Entry> ready;
659 bool lost = false;
660 std::size_t write = 0;
661 for (std::size_t i = 0; i < pending_.size(); ++i) {
620 std::uint64_t done = 0; 662 std::uint64_t done = 0;
621 if (vk_.vkGetSemaphoreCounterValue(device_, e.semaphore, &done) != VK_SUCCESS) return false; 663 const VkResult cr = vk_.vkGetSemaphoreCounterValue(device_, pending_[i].semaphore, &done);
622 if (done < e.value) return false; 664 if (cr == VK_ERROR_DEVICE_LOST) {
623 if (e.callback) e.callback(); 665 lost = true;
624 else if (signal_event_ && e.event_handle) signal_event_(e.event_handle); 666 for (std::size_t j = i; j < pending_.size(); ++j) ready.push_back(std::move(pending_[j]));
625 return true; 667 break;
626 }); 668 }
669 if (cr == VK_SUCCESS && done >= pending_[i].value) {
670 ready.push_back(std::move(pending_[i]));
671 } else {
672 if (write != i) pending_[write] = std::move(pending_[i]);
673 ++write;
674 }
675 }
676 if (lost) {
677 pending_.resize(write);
678 for (Entry& e : pending_) ready.push_back(std::move(e));
679 pending_.clear();
680 device_lost_.store(true, std::memory_order_release);
681 const SignalEventFn fn = signal_event_;
682 lock.unlock();
683 dispatch_entries(ready, fn);
684 return;
685 }
686 pending_.resize(write);
687 if (!ready.empty()) {
688 const SignalEventFn fn = signal_event_;
689 lock.unlock();
690 dispatch_entries(ready, fn);
691 lock.lock();
692 if (stop_) return;
693 }
627 } 694 }
628 } 695 }
629 696
@@ -1126,7 +1193,15 @@ public:
1126 // Vulkan requires external synchronisation of a VkQueue; every D3D12 queue of the device may share one. 1193 // Vulkan requires external synchronisation of a VkQueue; every D3D12 queue of the device may share one.
1127 std::mutex& submit_mutex() { return submit_mutex_; } 1194 std::mutex& submit_mutex() { return submit_mutex_; }
1128 1195
1129 void mark_device_lost() noexcept { device_lost_.store(true, std::memory_order_release); } 1196 void mark_device_lost() noexcept {
1197 device_lost_.store(true, std::memory_order_release);
1198 if (fence_dispatcher_) {
1199 try {
1200 fence_dispatcher_->notify_device_lost();
1201 } catch (...) {
1202 }
1203 }
1204 }
1130 bool is_device_lost() const noexcept { return device_lost_.load(std::memory_order_acquire); } 1205 bool is_device_lost() const noexcept { return device_lost_.load(std::memory_order_acquire); }
1131 1206
1132 void set_signal_event_fn(FenceWaitDispatcher::SignalEventFn fn) { 1207 void set_signal_event_fn(FenceWaitDispatcher::SignalEventFn fn) {
diff --git a/runtime/test/vk_fence_dispatcher_sync_test.cpp b/runtime/test/vk_fence_dispatcher_sync_test.cpp
new file mode 100644
index 000000000..138fa0f6a
--- /dev/null
+++ b/runtime/test/vk_fence_dispatcher_sync_test.cpp
@@ -0,0 +1,262 @@
1// FenceWaitDispatcher concurrency and device-lost regressions (runtime/src/vulkan_backend.h):
2// 1. Completion callbacks in run() execute outside mutex_, so a callback can re-entrantly call
3// enqueue_fence_callback() and cancel_fence_waits() without deadlocking.
4// 2. Cancelled callbacks in cancel() execute outside mutex_, so a callback can re-entrantly enqueue another wait
5// without deadlocking.
6// 3. mark_device_lost() wakes a blocked vkWaitSemaphores() so pending waits complete and cancel()/stop() never hang.
7// 4. vkGetSemaphoreCounterValue() returning VK_ERROR_DEVICE_LOST drains pending waits and marks device_lost_ instead
8// of spinning forever.
9// Skips with exit code 77 when no Vulkan 1.3 device is available. No game data.
10#include "vk_test_device.h"
11#include "../src/vulkan_backend.h"
12
13#include <atomic>
14#include <chrono>
15#include <condition_variable>
16#include <cstdint>
17#include <cstdio>
18#include <cstdlib>
19#include <mutex>
20#include <string_view>
21#include <thread>
22
23#define EXPECT(condition) do { \
24 if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); std::_Exit(1); } \
25} while (0)
26
27namespace {
28
29namespace driver {
30PFN_vkGetDeviceProcAddr real_get_device = nullptr;
31PFN_vkWaitSemaphores real_wait_semaphores = nullptr;
32PFN_vkGetSemaphoreCounterValue real_get_counter = nullptr;
33
34std::atomic<bool> in_wait_semaphores{false};
35std::atomic<bool> fail_counter_device_lost{false};
36std::atomic<std::uint32_t> counter_calls{0};
37
38VKAPI_ATTR VkResult VKAPI_CALL wait_semaphores(VkDevice device, const VkSemaphoreWaitInfo* info, std::uint64_t timeout) {
39 in_wait_semaphores.store(true, std::memory_order_release);
40 const VkResult res = real_wait_semaphores(device, info, timeout);
41 in_wait_semaphores.store(false, std::memory_order_release);
42 return res;
43}
44
45VKAPI_ATTR VkResult VKAPI_CALL get_semaphore_counter_value(VkDevice device, VkSemaphore sem, std::uint64_t* value) {
46 counter_calls.fetch_add(1, std::memory_order_relaxed);
47 if (fail_counter_device_lost.load(std::memory_order_acquire)) return VK_ERROR_DEVICE_LOST;
48 return real_get_counter(device, sem, value);
49}
50
51VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_device(VkDevice device, const char* name) {
52 const std::string_view n = name;
53 if (n == "vkWaitSemaphores") return reinterpret_cast<PFN_vkVoidFunction>(wait_semaphores);
54 if (n == "vkGetSemaphoreCounterValue") return reinterpret_cast<PFN_vkVoidFunction>(get_semaphore_counter_value);
55 return real_get_device(device, name);
56}
57
58void install(PFN_vkGetDeviceProcAddr device_proc, VkDevice device) {
59 real_get_device = device_proc;
60 real_wait_semaphores = reinterpret_cast<PFN_vkWaitSemaphores>(device_proc(device, "vkWaitSemaphores"));
61 real_get_counter =
62 reinterpret_cast<PFN_vkGetSemaphoreCounterValue>(device_proc(device, "vkGetSemaphoreCounterValue"));
63}
64} // namespace driver
65
66bool wait_flag(std::mutex& mu, std::condition_variable& cv, const bool& flag, int timeout_ms = 2000) {
67 std::unique_lock lk(mu);
68 return cv.wait_for(lk, std::chrono::milliseconds(timeout_ms), [&] { return flag; });
69}
70
71bool wait_until_in_vk_wait(int timeout_ms = 2000) {
72 const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms);
73 while (!driver::in_wait_semaphores.load(std::memory_order_acquire)) {
74 if (std::chrono::steady_clock::now() >= deadline) return false;
75 std::this_thread::sleep_for(std::chrono::milliseconds(1));
76 }
77 return true;
78}
79
80} // namespace
81
82int main() {
83 using namespace leonida;
84
85 vktest::Device d;
86 if (!d.create(false)) {
87 std::fprintf(stderr, "vk_fence_dispatcher_sync_test: no Vulkan 1.3 device, skipped\n");
88 return 77;
89 }
90 driver::install(d.device_proc, d.device);
91
92 // 1. Re-entrant completion callback in run(): callback calls enqueue_fence_callback() and cancel_fence_waits().
93 {
94 VulkanContext context(driver::get_device, d.device, true);
95 EXPECT(context.loaded());
96 VulkanFence fence_a(context, 0);
97 VulkanFence fence_b(context, 0);
98 VulkanFence fence_c(context, 0);
99 EXPECT(fence_a.semaphore() != VK_NULL_HANDLE && fence_b.semaphore() != VK_NULL_HANDLE &&
100 fence_c.semaphore() != VK_NULL_HANDLE);
101
102 std::mutex mu;
103 std::condition_variable cv;
104 bool first_ran = false;
105 bool chained_ran = false;
106 bool c_cancelled = false;
107
108 context.enqueue_fence_callback(fence_c.semaphore(), 99, [&] {
109 std::lock_guard lk(mu);
110 c_cancelled = true;
111 cv.notify_all();
112 });
113 context.enqueue_fence_callback(fence_a.semaphore(), 1, [&] {
114 context.enqueue_fence_callback(fence_b.semaphore(), 1, [&] {
115 std::lock_guard lk(mu);
116 chained_ran = true;
117 cv.notify_all();
118 });
119 context.cancel_fence_waits(fence_c.semaphore());
120 std::lock_guard lk(mu);
121 first_ran = true;
122 cv.notify_all();
123 });
124
125 EXPECT(fence_a.signal(1));
126 EXPECT(wait_flag(mu, cv, first_ran));
127 EXPECT(wait_flag(mu, cv, c_cancelled));
128 EXPECT(fence_b.signal(1));
129 EXPECT(wait_flag(mu, cv, chained_ran));
130
131 context.cancel_fence_waits(fence_a.semaphore());
132 context.cancel_fence_waits(fence_b.semaphore());
133 std::printf("fence dispatcher: re-entrant completion callback in run() ok\n");
134
135 // 2. Re-entrant callback in cancel(): cancelled callback calls enqueue_fence_callback() on another fence.
136 VulkanFence fence_d(context, 0);
137 VulkanFence fence_e(context, 0);
138 EXPECT(fence_d.semaphore() != VK_NULL_HANDLE && fence_e.semaphore() != VK_NULL_HANDLE);
139
140 bool cancel_returned = false;
141 bool e_ran = false;
142 context.enqueue_fence_callback(fence_d.semaphore(), 50, [&] {
143 context.enqueue_fence_callback(fence_e.semaphore(), 1, [&] {
144 std::lock_guard lk(mu);
145 e_ran = true;
146 cv.notify_all();
147 });
148 });
149
150 std::thread cancel_thread([&] {
151 context.cancel_fence_waits(fence_d.semaphore());
152 std::lock_guard lk(mu);
153 cancel_returned = true;
154 cv.notify_all();
155 });
156 const bool ok_cancel = wait_flag(mu, cv, cancel_returned);
157 EXPECT(ok_cancel);
158 cancel_thread.join();
159
160 EXPECT(fence_e.signal(1));
161 EXPECT(wait_flag(mu, cv, e_ran));
162 context.cancel_fence_waits(fence_e.semaphore());
163 std::printf("fence dispatcher: re-entrant callback in cancel() ok\n");
164 }
165
166 // 3. mark_device_lost() wakes blocked vkWaitSemaphores(), completes pending waits, and stop()/cancel() never hang.
167 {
168 driver::in_wait_semaphores.store(false, std::memory_order_release);
169 std::mutex mu;
170 std::condition_variable cv;
171 bool callback_fired = false;
172 bool teardown_done = false;
173
174 std::thread lost_test([&] {
175 VulkanContext lost_ctx(driver::get_device, d.device, true);
176 if (!lost_ctx.loaded()) std::_Exit(1);
177 VulkanFence stuck(lost_ctx, 0);
178 if (stuck.semaphore() == VK_NULL_HANDLE) std::_Exit(1);
179
180 lost_ctx.enqueue_fence_callback(stuck.semaphore(), 100, [&] {
181 std::lock_guard lk(mu);
182 callback_fired = true;
183 cv.notify_all();
184 });
185 if (!wait_until_in_vk_wait()) std::_Exit(1);
186
187 lost_ctx.mark_device_lost();
188 if (!wait_flag(mu, cv, callback_fired)) std::_Exit(1);
189
190 // cancel() and ~VulkanContext() -> stop() must return promptly even after device_lost_ is set.
191 lost_ctx.cancel_fence_waits(stuck.semaphore());
192 });
193
194 std::thread joiner([&] {
195 lost_test.join();
196 std::lock_guard lk(mu);
197 teardown_done = true;
198 cv.notify_all();
199 });
200 const bool ok_lost = wait_flag(mu, cv, teardown_done, 3000);
201 EXPECT(ok_lost);
202 joiner.join();
203 EXPECT(callback_fired);
204 std::printf("fence dispatcher: mark_device_lost() wakes blocked wait and teardown completes ok\n");
205 }
206
207 // 4. vkGetSemaphoreCounterValue() returning VK_ERROR_DEVICE_LOST drains pending waits instead of spinning forever.
208 {
209 driver::in_wait_semaphores.store(false, std::memory_order_release);
210 driver::fail_counter_device_lost.store(false, std::memory_order_release);
211 driver::counter_calls.store(0, std::memory_order_release);
212
213 std::mutex mu;
214 std::condition_variable cv;
215 bool drained = false;
216 bool done = false;
217 bool marked_lost = false;
218 std::uint32_t observed_calls = 0;
219
220 std::thread err_test([&] {
221 VulkanContext err_ctx(driver::get_device, d.device, true);
222 if (!err_ctx.loaded()) std::_Exit(1);
223 VulkanFence sem(err_ctx, 0);
224 if (sem.semaphore() == VK_NULL_HANDLE) std::_Exit(1);
225
226 err_ctx.enqueue_fence_callback(sem.semaphore(), 1, [&] {
227 std::lock_guard lk(mu);
228 drained = true;
229 cv.notify_all();
230 });
231 if (!wait_until_in_vk_wait()) std::_Exit(1);
232
233 driver::fail_counter_device_lost.store(true, std::memory_order_release);
234 if (!sem.signal(1)) std::_Exit(1);
235 if (!wait_flag(mu, cv, drained, 1500)) std::_Exit(1);
236
237 marked_lost = err_ctx.is_device_lost();
238 observed_calls = driver::counter_calls.load(std::memory_order_acquire);
239 err_ctx.cancel_fence_waits(sem.semaphore());
240 });
241
242 std::thread joiner([&] {
243 err_test.join();
244 std::lock_guard lk(mu);
245 done = true;
246 cv.notify_all();
247 });
248 const bool ok_err = wait_flag(mu, cv, done, 3000);
249 driver::fail_counter_device_lost.store(false, std::memory_order_release);
250 EXPECT(ok_err);
251 joiner.join();
252 EXPECT(drained);
253 EXPECT(marked_lost);
254 EXPECT(observed_calls >= 1 && observed_calls <= 4);
255 std::printf("fence dispatcher: vkGetSemaphoreCounterValue DEVICE_LOST drains without spin (%u calls) ok\n",
256 observed_calls);
257 }
258
259 EXPECT(d.validation_errors() == 0);
260 std::printf("vk_fence_dispatcher_sync_test: ok\n");
261 return 0;
262}