upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/vulkan_backend.h
diff options
context:
space:
mode:
authorsds-228 <329233051+sds-228@users.noreply.github.com>2026-11-23 04:44:51 +0300
committersds-228 <329233051+sds-228@users.noreply.github.com>2026-11-23 04:44:51 +0300
commitdf99c89162d18c30fd255cd47e85ce6499413c8b (patch)
tree4559776c843fd87ed5fad0602fb0d9d53e49f2c2 /runtime/src/vulkan_backend.h
parent03b09a086592a197db64d91655c6e65c30764d96 (diff)
vulkan: make fence cancel wait for in-flight callbacks
run() unlocked mutex_ before dispatching ready entries, while cancel() only waited for an active vkWaitSemaphores. A cancel could return and destroy the VkSemaphore while a callback for it was still running. Record each dispatched entry with its owning thread before unlocking; cancel waits for entries it does not own, so callbacks can still re-entrantly cancel their own semaphore. All four run() dispatch paths use the same helper. Adds a deterministic regression where cancel must block until a blocked callback has used the semaphore; reverting only the wait fails it. AI-assisted (OpenCode)
Diffstat (limited to 'runtime/src/vulkan_backend.h')
-rw-r--r--runtime/src/vulkan_backend.h59
1 files changed, 43 insertions, 16 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index 03ab7ba26..ab194a33c 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -505,6 +505,13 @@ public:
505 std::function<void()> callback; 505 std::function<void()> callback;
506 }; 506 };
507 507
508 // A dispatch runs outside mutex_. Its owner matters: a callback may re-entrantly call cancel() for its own
509 // semaphore, and that inner cancel must not wait for the callback it is already inside of.
510 struct InFlight {
511 VkSemaphore semaphore = VK_NULL_HANDLE;
512 std::thread::id owner;
513 };
514
508 using SignalEventFn = void (*)(void* event_handle); 515 using SignalEventFn = void (*)(void* event_handle);
509 516
510 // Longest single vkWaitSemaphores call in run(): bounds how long the thread can sit in an uninterruptible wait. 517 // Longest single vkWaitSemaphores call in run(): bounds how long the thread can sit in an uninterruptible wait.
@@ -585,7 +592,8 @@ public:
585 } 592 }
586 593
587 // Completes the pending waits for `sem` (a released D3D12 fence counts as reached, as on the PC runtime) and 594 // Completes the pending waits for `sem` (a released D3D12 fence counts as reached, as on the PC runtime) and
588 // blocks until any in-flight vkWaitSemaphores releases `sem` before the caller destroys the VkSemaphore. 595 // blocks until both a vkWaitSemaphores on `sem` and any callback already dispatched for it have finished, before
596 // the caller destroys the VkSemaphore.
589 void cancel(VkSemaphore sem) { 597 void cancel(VkSemaphore sem) {
590 if (!sem) return; 598 if (!sem) return;
591 std::vector<Entry> cancelled; 599 std::vector<Entry> cancelled;
@@ -603,10 +611,12 @@ public:
603 } 611 }
604 } 612 }
605 pending_.resize(write); 613 pending_.resize(write);
606 if (thread_.joinable() && waiting_) { 614 if (thread_.joinable() && (waiting_ || in_flight(sem))) {
607 const std::uint64_t target_epoch = epoch_ + 1; 615 const std::uint64_t target_epoch = epoch_ + 1;
608 signal_wakeup_locked(); 616 signal_wakeup_locked();
609 epoch_cv_.wait(lock, [this, target_epoch] { return !waiting_ || epoch_ >= target_epoch; }); 617 epoch_cv_.wait(lock, [this, sem, target_epoch] {
618 return (!waiting_ || epoch_ >= target_epoch) && !in_flight(sem);
619 });
610 } 620 }
611 } 621 }
612 dispatch_entries(cancelled, fn); 622 dispatch_entries(cancelled, fn);
@@ -631,6 +641,31 @@ private:
631 } 641 }
632 } 642 }
633 643
644 bool in_flight(VkSemaphore sem) const {
645 const std::thread::id me = std::this_thread::get_id();
646 return std::any_of(dispatching_.begin(), dispatching_.end(), [&](const InFlight& d) {
647 return d.semaphore == sem && d.owner != me;
648 });
649 }
650
651 // mutex_ is held on entry and again on return. Recording the entries before unlocking closes the window where
652 // run() has taken them out of pending_ but has not started the callback, yet cancel() can return and let the
653 // caller destroy the semaphore.
654 void dispatch_outside_lock(std::unique_lock<std::mutex>& lock, std::vector<Entry>& entries, SignalEventFn fn) {
655 const std::thread::id me = std::this_thread::get_id();
656 for (const Entry& e : entries) dispatching_.push_back({e.semaphore, me});
657 lock.unlock();
658 dispatch_entries(entries, fn);
659 lock.lock();
660 for (const Entry& e : entries) {
661 const auto it = std::find_if(dispatching_.begin(), dispatching_.end(), [&](const InFlight& d) {
662 return d.semaphore == e.semaphore && d.owner == me;
663 });
664 if (it != dispatching_.end()) dispatching_.erase(it);
665 }
666 epoch_cv_.notify_all();
667 }
668
634 // Only signals wakeup_sem_ when run() is actively waiting (waiting_ == true) on wakeup_val_ + 1, 669 // Only signals wakeup_sem_ when run() is actively waiting (waiting_ == true) on wakeup_val_ + 1,
635 // eliminating any lost-wakeup race between snapshot construction and vkWaitSemaphores. Keep signaling even if 670 // eliminating any lost-wakeup race between snapshot construction and vkWaitSemaphores. Keep signaling even if
636 // device_lost_ is already set so a blocked vkWaitSemaphores wakes cleanly for cancel()/stop(). 671 // device_lost_ is already set so a blocked vkWaitSemaphores wakes cleanly for cancel()/stop().
@@ -650,9 +685,7 @@ private:
650 if (device_lost_.load(std::memory_order_acquire)) { 685 if (device_lost_.load(std::memory_order_acquire)) {
651 std::vector<Entry> drained = std::move(pending_); 686 std::vector<Entry> drained = std::move(pending_);
652 pending_.clear(); 687 pending_.clear();
653 const SignalEventFn fn = signal_event_; 688 dispatch_outside_lock(lock, drained, signal_event_);
654 lock.unlock();
655 dispatch_entries(drained, fn);
656 return; 689 return;
657 } 690 }
658 if (stop_) return; 691 if (stop_) return;
@@ -690,9 +723,7 @@ private:
690 device_lost_.store(true, std::memory_order_release); 723 device_lost_.store(true, std::memory_order_release);
691 std::vector<Entry> drained = std::move(pending_); 724 std::vector<Entry> drained = std::move(pending_);
692 pending_.clear(); 725 pending_.clear();
693 const SignalEventFn fn = signal_event_; 726 dispatch_outside_lock(lock, drained, signal_event_);
694 lock.unlock();
695 dispatch_entries(drained, fn);
696 return; 727 return;
697 } 728 }
698 if (stop_) return; 729 if (stop_) return;
@@ -720,17 +751,12 @@ private:
720 for (Entry& e : pending_) ready.push_back(std::move(e)); 751 for (Entry& e : pending_) ready.push_back(std::move(e));
721 pending_.clear(); 752 pending_.clear();
722 device_lost_.store(true, std::memory_order_release); 753 device_lost_.store(true, std::memory_order_release);
723 const SignalEventFn fn = signal_event_; 754 dispatch_outside_lock(lock, ready, signal_event_);
724 lock.unlock();
725 dispatch_entries(ready, fn);
726 return; 755 return;
727 } 756 }
728 pending_.resize(write); 757 pending_.resize(write);
729 if (!ready.empty()) { 758 if (!ready.empty()) {
730 const SignalEventFn fn = signal_event_; 759 dispatch_outside_lock(lock, ready, signal_event_);
731 lock.unlock();
732 dispatch_entries(ready, fn);
733 lock.lock();
734 if (stop_) return; 760 if (stop_) return;
735 } 761 }
736 } 762 }
@@ -747,6 +773,7 @@ private:
747 std::condition_variable cv_; 773 std::condition_variable cv_;
748 std::condition_variable epoch_cv_; 774 std::condition_variable epoch_cv_;
749 std::vector<Entry> pending_; 775 std::vector<Entry> pending_;
776 std::vector<InFlight> dispatching_; // callbacks running outside mutex_, with the thread running each one
750 std::vector<VkSemaphore> semaphores_; 777 std::vector<VkSemaphore> semaphores_;
751 std::vector<std::uint64_t> values_; 778 std::vector<std::uint64_t> values_;
752 std::uint64_t epoch_ = 0; 779 std::uint64_t epoch_ = 0;