diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 23:15:09 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 23:15:09 -0500 |
| commit | 65a30b42e798854a7013fda631209e0f7ef035d4 (patch) | |
| tree | 0552dce5cf104652fb4729295bd14a2f67647e74 /runtime/src/vulkan_backend.h | |
| parent | 0a7c207b5337b590e687ec7cddffb45784f9316b (diff) | |
| parent | 047b789d7be5dc84f43282c065613156db24b903 (diff) | |
Merge pull request #4486 from sds-228
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/vulkan_backend.h')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 104 |
1 files changed, 88 insertions, 16 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 5bc75950c..217f1e3d5 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -529,6 +529,20 @@ public: | |||
| 529 | std::function<void()> callback; | 529 | std::function<void()> callback; |
| 530 | }; | 530 | }; |
| 531 | 531 | ||
| 532 | // A dispatch runs outside mutex_. Each entry in a ready batch gets its own slot: a callback may re-entrantly | ||
| 533 | // call cancel() for a later entry in the same batch, which marks that entry cancelled so it is skipped before | ||
| 534 | // it can use the semaphore. A callback running on another thread must still be waited for. | ||
| 535 | // A cancelled entry still in pending_ is dispatched by cancel() itself (callback and bookkeeping both run); | ||
| 536 | // one already in a ready batch is skipped and only its event is signalled. The held Signal op's count is | ||
| 537 | // therefore not decremented for a skipped batch entry. That is safe because only ~Fence cancels a wait, but | ||
| 538 | // keep it in mind if cancel() gains other callers. | ||
| 539 | struct DispatchSlot { | ||
| 540 | VkSemaphore semaphore = VK_NULL_HANDLE; | ||
| 541 | std::thread::id owner; | ||
| 542 | bool active = false; // guarded by mutex_ | ||
| 543 | bool cancelled = false; // guarded by mutex_ | ||
| 544 | }; | ||
| 545 | |||
| 532 | using SignalEventFn = void (*)(void* event_handle); | 546 | using SignalEventFn = void (*)(void* event_handle); |
| 533 | 547 | ||
| 534 | // Longest single vkWaitSemaphores call in run(): bounds how long the thread can sit in an uninterruptible wait. | 548 | // Longest single vkWaitSemaphores call in run(): bounds how long the thread can sit in an uninterruptible wait. |
| @@ -609,7 +623,8 @@ public: | |||
| 609 | } | 623 | } |
| 610 | 624 | ||
| 611 | // Completes the pending waits for `sem` (a released D3D12 fence counts as reached, as on the PC runtime) and | 625 | // Completes the pending waits for `sem` (a released D3D12 fence counts as reached, as on the PC runtime) and |
| 612 | // blocks until any in-flight vkWaitSemaphores releases `sem` before the caller destroys the VkSemaphore. | 626 | // blocks until both a vkWaitSemaphores on `sem` and any callback already dispatched for it have finished, before |
| 627 | // the caller destroys the VkSemaphore. | ||
| 613 | void cancel(VkSemaphore sem) { | 628 | void cancel(VkSemaphore sem) { |
| 614 | if (!sem) return; | 629 | if (!sem) return; |
| 615 | std::vector<Entry> cancelled; | 630 | std::vector<Entry> cancelled; |
| @@ -627,10 +642,18 @@ public: | |||
| 627 | } | 642 | } |
| 628 | } | 643 | } |
| 629 | pending_.resize(write); | 644 | pending_.resize(write); |
| 630 | if (thread_.joinable() && waiting_) { | 645 | // Entries already taken out of pending_ but not yet started belong to a live ready batch. Mark them |
| 646 | // cancelled so dispatch skips them; this is how a re-entrant cancel() from another callback in the | ||
| 647 | // same batch is completed without waiting on the callback that is already on this thread's stack. | ||
| 648 | for (const auto& slot : dispatching_) { | ||
| 649 | if (slot->semaphore == sem && !slot->active) slot->cancelled = true; | ||
| 650 | } | ||
| 651 | if (thread_.joinable() && (waiting_ || callback_on_other_thread(sem))) { | ||
| 631 | const std::uint64_t target_epoch = epoch_ + 1; | 652 | const std::uint64_t target_epoch = epoch_ + 1; |
| 632 | signal_wakeup_locked(); | 653 | signal_wakeup_locked(); |
| 633 | epoch_cv_.wait(lock, [this, target_epoch] { return !waiting_ || epoch_ >= target_epoch; }); | 654 | epoch_cv_.wait(lock, [this, sem, target_epoch] { |
| 655 | return (!waiting_ || epoch_ >= target_epoch) && !callback_on_other_thread(sem); | ||
| 656 | }); | ||
| 634 | } | 657 | } |
| 635 | } | 658 | } |
| 636 | dispatch_entries(cancelled, fn); | 659 | dispatch_entries(cancelled, fn); |
| @@ -655,6 +678,63 @@ private: | |||
| 655 | } | 678 | } |
| 656 | } | 679 | } |
| 657 | 680 | ||
| 681 | // True only for a callback already running on another thread. A slot that has not started yet is cancelled by | ||
| 682 | // cancel() and skipped, so it never uses a destroyed semaphore and must not hold up the cancelling caller. | ||
| 683 | bool callback_on_other_thread(VkSemaphore sem) const { | ||
| 684 | const std::thread::id me = std::this_thread::get_id(); | ||
| 685 | return std::any_of(dispatching_.begin(), dispatching_.end(), [&](const auto& d) { | ||
| 686 | return d->semaphore == sem && d->active && d->owner != me; | ||
| 687 | }); | ||
| 688 | } | ||
| 689 | |||
| 690 | // mutex_ is held on entry and again on return. Recording every entry before unlocking closes the window where | ||
| 691 | // run() has taken them out of pending_ but has not started the callback, yet cancel() can return and let the | ||
| 692 | // caller destroy the semaphore. Each entry has its own slot so a cancel() for a later entry in the same batch | ||
| 693 | // skips that entry instead of being mistaken for the callback already on the cancelling thread's stack. | ||
| 694 | void dispatch_outside_lock(std::unique_lock<std::mutex>& lock, std::vector<Entry>& entries, SignalEventFn fn) { | ||
| 695 | const std::thread::id me = std::this_thread::get_id(); | ||
| 696 | std::vector<std::shared_ptr<DispatchSlot>> slots; | ||
| 697 | slots.reserve(entries.size()); | ||
| 698 | for (const Entry& e : entries) { | ||
| 699 | auto slot = std::make_shared<DispatchSlot>(); | ||
| 700 | slot->semaphore = e.semaphore; | ||
| 701 | slot->owner = me; | ||
| 702 | slots.push_back(slot); | ||
| 703 | dispatching_.push_back(slot); | ||
| 704 | } | ||
| 705 | lock.unlock(); | ||
| 706 | for (std::size_t i = 0; i < entries.size(); ++i) { | ||
| 707 | Entry& e = entries[i]; | ||
| 708 | DispatchSlot& slot = *slots[i]; | ||
| 709 | bool skip = false; | ||
| 710 | { | ||
| 711 | std::lock_guard guard(mutex_); | ||
| 712 | skip = slot.cancelled; | ||
| 713 | if (!skip) slot.active = true; | ||
| 714 | } | ||
| 715 | if (skip) { | ||
| 716 | // A re-entrant cancel() removed this wait; still complete its event so no waiter is left behind. | ||
| 717 | if (fn && e.event_handle) fn(e.event_handle); | ||
| 718 | continue; | ||
| 719 | } | ||
| 720 | if (e.callback) e.callback(); | ||
| 721 | else if (fn && e.event_handle) fn(e.event_handle); | ||
| 722 | { | ||
| 723 | std::lock_guard guard(mutex_); | ||
| 724 | slot.active = false; | ||
| 725 | const auto it = std::find(dispatching_.begin(), dispatching_.end(), slots[i]); | ||
| 726 | if (it != dispatching_.end()) dispatching_.erase(it); | ||
| 727 | } | ||
| 728 | epoch_cv_.notify_all(); | ||
| 729 | } | ||
| 730 | lock.lock(); | ||
| 731 | for (const auto& slot : slots) { | ||
| 732 | const auto it = std::find(dispatching_.begin(), dispatching_.end(), slot); | ||
| 733 | if (it != dispatching_.end()) dispatching_.erase(it); | ||
| 734 | } | ||
| 735 | epoch_cv_.notify_all(); | ||
| 736 | } | ||
| 737 | |||
| 658 | // Only signals wakeup_sem_ when run() is actively waiting (waiting_ == true) on wakeup_val_ + 1, | 738 | // Only signals wakeup_sem_ when run() is actively waiting (waiting_ == true) on wakeup_val_ + 1, |
| 659 | // eliminating any lost-wakeup race between snapshot construction and vkWaitSemaphores. Keep signaling even if | 739 | // eliminating any lost-wakeup race between snapshot construction and vkWaitSemaphores. Keep signaling even if |
| 660 | // device_lost_ is already set so a blocked vkWaitSemaphores wakes cleanly for cancel()/stop(). | 740 | // device_lost_ is already set so a blocked vkWaitSemaphores wakes cleanly for cancel()/stop(). |
| @@ -674,9 +754,7 @@ private: | |||
| 674 | if (device_lost_.load(std::memory_order_acquire)) { | 754 | if (device_lost_.load(std::memory_order_acquire)) { |
| 675 | std::vector<Entry> drained = std::move(pending_); | 755 | std::vector<Entry> drained = std::move(pending_); |
| 676 | pending_.clear(); | 756 | pending_.clear(); |
| 677 | const SignalEventFn fn = signal_event_; | 757 | dispatch_outside_lock(lock, drained, signal_event_); |
| 678 | lock.unlock(); | ||
| 679 | dispatch_entries(drained, fn); | ||
| 680 | return; | 758 | return; |
| 681 | } | 759 | } |
| 682 | if (stop_) return; | 760 | if (stop_) return; |
| @@ -714,9 +792,7 @@ private: | |||
| 714 | device_lost_.store(true, std::memory_order_release); | 792 | device_lost_.store(true, std::memory_order_release); |
| 715 | std::vector<Entry> drained = std::move(pending_); | 793 | std::vector<Entry> drained = std::move(pending_); |
| 716 | pending_.clear(); | 794 | pending_.clear(); |
| 717 | const SignalEventFn fn = signal_event_; | 795 | dispatch_outside_lock(lock, drained, signal_event_); |
| 718 | lock.unlock(); | ||
| 719 | dispatch_entries(drained, fn); | ||
| 720 | return; | 796 | return; |
| 721 | } | 797 | } |
| 722 | if (stop_) return; | 798 | if (stop_) return; |
| @@ -744,17 +820,12 @@ private: | |||
| 744 | for (Entry& e : pending_) ready.push_back(std::move(e)); | 820 | for (Entry& e : pending_) ready.push_back(std::move(e)); |
| 745 | pending_.clear(); | 821 | pending_.clear(); |
| 746 | device_lost_.store(true, std::memory_order_release); | 822 | device_lost_.store(true, std::memory_order_release); |
| 747 | const SignalEventFn fn = signal_event_; | 823 | dispatch_outside_lock(lock, ready, signal_event_); |
| 748 | lock.unlock(); | ||
| 749 | dispatch_entries(ready, fn); | ||
| 750 | return; | 824 | return; |
| 751 | } | 825 | } |
| 752 | pending_.resize(write); | 826 | pending_.resize(write); |
| 753 | if (!ready.empty()) { | 827 | if (!ready.empty()) { |
| 754 | const SignalEventFn fn = signal_event_; | 828 | dispatch_outside_lock(lock, ready, signal_event_); |
| 755 | lock.unlock(); | ||
| 756 | dispatch_entries(ready, fn); | ||
| 757 | lock.lock(); | ||
| 758 | if (stop_) return; | 829 | if (stop_) return; |
| 759 | } | 830 | } |
| 760 | } | 831 | } |
| @@ -771,6 +842,7 @@ private: | |||
| 771 | std::condition_variable cv_; | 842 | std::condition_variable cv_; |
| 772 | std::condition_variable epoch_cv_; | 843 | std::condition_variable epoch_cv_; |
| 773 | std::vector<Entry> pending_; | 844 | std::vector<Entry> pending_; |
| 845 | std::vector<std::shared_ptr<DispatchSlot>> dispatching_; // ready-batch callbacks outside mutex_ | ||
| 774 | std::vector<VkSemaphore> semaphores_; | 846 | std::vector<VkSemaphore> semaphores_; |
| 775 | std::vector<std::uint64_t> values_; | 847 | std::vector<std::uint64_t> values_; |
| 776 | std::uint64_t epoch_ = 0; | 848 | std::uint64_t epoch_ = 0; |