upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/queue_holds.h
diff options
context:
space:
mode:
authorAlxcBTW <187763517+AlxcBTW@users.noreply.github.com>2026-11-22 21:02:33 +0100
committerAlxcBTW <187763517+AlxcBTW@users.noreply.github.com>2026-11-22 21:02:33 +0100
commitf8007dda67f9c45757b63df5c5fd8377647da4ae (patch)
treecdffc5cc7c81dcbbf683af9c0df6077c6587ac0d /runtime/src/queue_holds.h
parent9bebfb107070d84609e303009b0f91b011b9aa2e (diff)
Queue holds: review fixes (Present ordering, pins, timeline monotonicity, fence lifetime, teardown, docs)
- Present is queued behind held work (QueueHolds::after_held) and submitted in order by the releasing thread. - Held batches keep the pipeline states and root signatures of their lists; fences a queue waits on or signals are kept until the submission has completed on the GPU (a D3D12 queue holds them). - A held Signal released below an unheld one on the same fence leaves the Vulkan signal out (claimed under the submit lock). - A queue destroyed with held Signals completes the CPU waits on their fences and reports it. - A failed held batch is reported (take_held_failure, last_execute_result), its waits stay for the next submission, and the Signal behind it still signals. - d3d12_queue_handover_test: pairs, two-thread signalling stress, held Signal of a released fence, shared fence, teardown with a CPU waiter; --two-queues and --shared-queue modes on a caller-owned device. - Tests for each fix in vk_presenter_sync_test, vk_queue_query_sync_test and d3d12_m4_compute_test; docs section 4.4. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/queue_holds.h')
-rw-r--r--runtime/src/queue_holds.h124
1 files changed, 109 insertions, 15 deletions
diff --git a/runtime/src/queue_holds.h b/runtime/src/queue_holds.h
index eafe3ebc8..4a1f990d8 100644
--- a/runtime/src/queue_holds.h
+++ b/runtime/src/queue_holds.h
@@ -2,14 +2,15 @@
2 2
3#include <atomic> 3#include <atomic>
4#include <cstdint> 4#include <cstdint>
5#include <cstdio> 5#include <deque>
6#include <functional>
6#include <mutex> 7#include <mutex>
7#include <unordered_map> 8#include <unordered_map>
8#include <vector> 9#include <vector>
9 10
10namespace leonida { 11namespace leonida {
11 12
12// Cross-queue Wait-before-Signal (docs section 4.4, "Wait before Signal"). 13// Cross-queue Wait-before-Signal (docs section 4.4, "Wait before Signal across queues").
13// 14//
14// A queue replays its ExecuteCommandLists against one device-wide layout tracker, in the order the calls reach the 15// A queue replays its ExecuteCommandLists against one device-wide layout tracker, in the order the calls reach the
15// layer. When queue B calls Wait(fence, v) and the Signal(fence, v) on queue A is only submitted afterwards, B's 16// layer. When queue B calls Wait(fence, v) and the Signal(fence, v) on queue A is only submitted afterwards, B's
@@ -19,23 +20,30 @@ namespace leonida {
19// own timeline semaphores still order the GPU work. 20// own timeline semaphores still order the GPU work.
20// 21//
21// This class is the device-wide half: for every fence, the highest value a Signal has been submitted for (or the 22// This class is the device-wide half: for every fence, the highest value a Signal has been submitted for (or the
22// CPU has set it to), which is not the completed value; and the held queues, which it asks to release whatever 23// CPU has set it to), which is not the completed value; the held queues, which it asks to release whatever became
23// became ready. It owns no Vulkan objects. Semaphores it was never told about count as satisfied, so a queue used 24// ready; and "after held work" callbacks (the presenter), which run once every operation held when they were
24// with a plain VkSemaphore behaves as before. 25// registered has been released. It owns no Vulkan objects. Semaphores it was never told about count as satisfied,
26// so a queue used with a plain VkSemaphore behaves as before.
25// 27//
26// Threads. Nothing here blocks on the GPU. The thread that makes a wait satisfiable (a queue's Signal, a CPU 28// Threads. No GPU wait happens here, but the thread that makes a wait satisfiable (a queue's Signal, a CPU Signal)
27// Signal) releases the ready batches itself, after it has released its own queue's lock: recording a held batch 29// does the release itself, after it has dropped its own queue's state lock: it records and submits the held
28// costs what its own ExecuteCommandLists would have. Only one thread releases at a time; a thread that finds 30// batches (a held ExecuteCommandLists costs what it would have cost when called, which includes whatever
29// another one releasing leaves a note and that one takes another pass, so no caller waits for another's recording. 31// execute_now does: the bindless upload's wait for the device is one) and runs the callbacks. The COM wrapper
32// still holds the mutex of the queue whose call that thread is in, so that queue's other calls wait meanwhile.
33// One thread releases at a time; a thread that finds another one releasing leaves a note and that one takes
34// another pass. detach() and cancel_after() (and remove_fence() from outside a release) wait for a running
35// release.
30template<class Semaphore> 36template<class Semaphore>
31class QueueHolds { 37class QueueHolds {
32public: 38public:
33 // A queue that can hold batches. Both calls take the queue's own lock; neither is called with it held. 39 // A queue that can hold batches. The calls take the queue's own lock; none is made with it held.
34 struct Holder { 40 struct Holder {
35 // Releases the held operations at the front of the queue whose waits are satisfied. True if any was. 41 // Releases the held operations at the front of the queue whose waits are satisfied. True if any was.
36 virtual bool release_ready() = 0; 42 virtual bool release_ready() = 0;
37 // Drops the held operations that wait on `semaphore` (its fence is gone), reporting each. 43 // Drops the held operations that wait on `semaphore` (its fence is gone), reporting each.
38 virtual void discard_waiting_on(Semaphore semaphore) = 0; 44 virtual void discard_waiting_on(Semaphore semaphore) = 0;
45 // Sequence number (next_seq()) of the oldest held operation, 0 when nothing is held.
46 virtual std::uint64_t oldest_held_seq() = 0;
39 47
40 protected: 48 protected:
41 ~Holder() = default; 49 ~Holder() = default;
@@ -46,16 +54,21 @@ public:
46 fences_[semaphore] = initial; 54 fences_[semaphore] = initial;
47 } 55 }
48 56
49 // The fence is released: held batches waiting on it can never run, so they are dropped and reported. 57 // The fence is gone. Operations that still reference it are dropped and reported (the COM layer keeps a fence
58 // alive while a held operation names it, so this is a safety net for direct users of VulkanFence). Not done
59 // from inside a release: the releasing thread already holds the lock, and what it is releasing cannot
60 // reference a fence whose last reference it is dropping, since that reference is the one it holds.
50 void remove_fence(Semaphore semaphore) { 61 void remove_fence(Semaphore semaphore) {
51 { 62 {
52 std::lock_guard lock(mutex_); 63 std::lock_guard lock(mutex_);
53 fences_.erase(semaphore); 64 fences_.erase(semaphore);
54 } 65 }
55 if (held_.load(std::memory_order_acquire) == 0) return; 66 if (held_.load(std::memory_order_acquire) == 0 || in_pump()) return;
56 { 67 {
57 std::lock_guard pump(pump_mutex_); 68 std::lock_guard pump(pump_mutex_);
69 in_pump() = true; // what is dropped may release other fences: they must not wait for this lock
58 for (Holder* holder : snapshot()) holder->discard_waiting_on(semaphore); 70 for (Holder* holder : snapshot()) holder->discard_waiting_on(semaphore);
71 in_pump() = false;
59 } 72 }
60 pump_ready(); 73 pump_ready();
61 } 74 }
@@ -68,6 +81,20 @@ public:
68 if (it != fences_.end() && it->second < value) it->second = value; 81 if (it != fences_.end() && it->second < value) it->second = value;
69 } 82 }
70 83
84 // A queue is about to submit a Signal of `value`; call inside the device submit lock, so the order of the
85 // calls is the order of the vkQueueSubmits. False when a Signal of at least `value` was already submitted: the
86 // timeline cannot go down or stay (VUID-VkSubmitInfo-pSignalSemaphores-03242 and the timeline rules), and a wait
87 // for `value` is already satisfied, so the Vulkan signal is left out. This happens when a held Signal(F, 2) is
88 // released after an unheld Signal(F, 3). Unknown semaphores are always signaled.
89 bool claim_signal(Semaphore semaphore, std::uint64_t value) {
90 std::lock_guard lock(mutex_);
91 const auto it = fences_.find(semaphore);
92 if (it == fences_.end()) return true;
93 if (it->second >= value) return false;
94 it->second = value;
95 return true;
96 }
97
71 // true when no submitted Signal reaches `value` yet. Unknown semaphores are satisfied. 98 // true when no submitted Signal reaches `value` yet. Unknown semaphores are satisfied.
72 bool pending(Semaphore semaphore, std::uint64_t value) const { 99 bool pending(Semaphore semaphore, std::uint64_t value) const {
73 std::lock_guard lock(mutex_); 100 std::lock_guard lock(mutex_);
@@ -94,14 +121,47 @@ public:
94 pump_ready(); 121 pump_ready();
95 } 122 }
96 123
124 // Sequence numbers of held operations, in the order they were held (across queues).
125 std::uint64_t next_seq() { return seq_.fetch_add(1, std::memory_order_acq_rel) + 1; }
126 std::uint64_t current_seq() const { return seq_.load(std::memory_order_acquire); }
127
128 // Runs `fn` once every operation held up to now has been released (or dropped), on the thread that releases the
129 // last of them, or right here if none is held. Callbacks of one owner run in registration order. Used by the
130 // presenter: a Present queued behind held work shows what that work draws.
131 void after_held(const void* owner, std::function<void()> fn) {
132 {
133 std::lock_guard lock(mutex_);
134 barriers_.push_back({owner, current_seq(), std::move(fn)});
135 barrier_count_.fetch_add(1, std::memory_order_acq_rel);
136 }
137 pump_ready();
138 }
139
140 // Drops the callbacks of `owner` that have not run, and waits for one that is running.
141 void cancel_after(const void* owner) {
142 {
143 std::lock_guard pump(pump_mutex_);
144 std::lock_guard lock(mutex_);
145 for (auto it = barriers_.begin(); it != barriers_.end();)
146 if (it->owner == owner) {
147 it = barriers_.erase(it);
148 barrier_count_.fetch_sub(1, std::memory_order_acq_rel);
149 } else {
150 ++it;
151 }
152 }
153 pump_ready();
154 }
155
97 // Held operations over all queues, for the cheap "is there anything to do" test. 156 // Held operations over all queues, for the cheap "is there anything to do" test.
98 void held_changed(std::int64_t delta) { held_.fetch_add(delta, std::memory_order_acq_rel); } 157 void held_changed(std::int64_t delta) { held_.fetch_add(delta, std::memory_order_acq_rel); }
99 bool any_held() const { return held_.load(std::memory_order_acquire) > 0; } 158 bool any_held() const { return held_.load(std::memory_order_acquire) > 0; }
100 159
101 // Releases every held operation that has become ready, in dependency order: a release can submit a Signal 160 // Releases every held operation that has become ready, in dependency order: a release can submit a Signal
102 // that makes another queue's front ready, so passes repeat until one finds nothing new. 161 // that makes another queue's front ready, so passes repeat until one finds nothing new. Then the callbacks
162 // whose held work is all gone.
103 void pump_ready() { 163 void pump_ready() {
104 if (!any_held()) return; 164 if (!any_held() && barrier_count_.load(std::memory_order_acquire) == 0) return;
105 dirty_.store(true, std::memory_order_release); 165 dirty_.store(true, std::memory_order_release);
106 if (in_pump()) return; // a release on this thread submitted a Signal: the running pass will go again 166 if (in_pump()) return; // a release on this thread submitted a Signal: the running pass will go again
107 for (;;) { 167 for (;;) {
@@ -114,6 +174,7 @@ public:
114 progress = false; 174 progress = false;
115 for (Holder* holder : snapshot()) progress |= holder->release_ready(); 175 for (Holder* holder : snapshot()) progress |= holder->release_ready();
116 } 176 }
177 run_barriers();
117 } 178 }
118 in_pump() = false; 179 in_pump() = false;
119 pump.unlock(); 180 pump.unlock();
@@ -122,6 +183,12 @@ public:
122 } 183 }
123 184
124private: 185private:
186 struct Barrier {
187 const void* owner;
188 std::uint64_t seq;
189 std::function<void()> fn;
190 };
191
125 static bool& in_pump() { 192 static bool& in_pump() {
126 static thread_local bool pumping = false; 193 static thread_local bool pumping = false;
127 return pumping; 194 return pumping;
@@ -132,11 +199,38 @@ private:
132 return holders_; 199 return holders_;
133 } 200 }
134 201
135 mutable std::mutex mutex_; // leaf: fences_ and holders_ 202 // Called with pump_mutex_ held. A callback runs when no held operation is older than the moment it was asked.
203 void run_barriers() {
204 for (;;) {
205 Barrier front;
206 {
207 std::lock_guard lock(mutex_);
208 if (barriers_.empty()) return;
209 front = barriers_.front();
210 }
211 for (Holder* holder : snapshot()) {
212 const std::uint64_t oldest = holder->oldest_held_seq();
213 if (oldest && oldest <= front.seq) return;
214 }
215 {
216 std::lock_guard lock(mutex_);
217 if (barriers_.empty() || barriers_.front().seq != front.seq || barriers_.front().owner != front.owner)
218 continue; // cancelled meanwhile
219 barriers_.pop_front();
220 barrier_count_.fetch_sub(1, std::memory_order_acq_rel);
221 }
222 front.fn();
223 }
224 }
225
226 mutable std::mutex mutex_; // leaf: fences_, holders_ and barriers_
136 std::mutex pump_mutex_; // one releasing thread; held across release_ready(), so detach() waits for it 227 std::mutex pump_mutex_; // one releasing thread; held across release_ready(), so detach() waits for it
137 std::unordered_map<Semaphore, std::uint64_t> fences_; 228 std::unordered_map<Semaphore, std::uint64_t> fences_;
138 std::vector<Holder*> holders_; 229 std::vector<Holder*> holders_;
230 std::deque<Barrier> barriers_;
139 std::atomic<std::int64_t> held_{0}; 231 std::atomic<std::int64_t> held_{0};
232 std::atomic<std::int64_t> barrier_count_{0};
233 std::atomic<std::uint64_t> seq_{0};
140 std::atomic<bool> dirty_{false}; 234 std::atomic<bool> dirty_{false};
141}; 235};
142 236