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:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 23:06:28 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 23:06:28 -0500
commit95b2e2c8bd7d20019231eda0007628fa2f2b5350 (patch)
tree588294b0fe61c6a9d21ada1f56277d000a4d47d6 /runtime/src/queue_holds.h
parent1af1f9e39344ca73bfec9a0e3536fa4254a00b13 (diff)
parent003baa94c1fcba61e324dbeb947e8bd401f4f837 (diff)
Merge pull request #2951 from AlxcBTW
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.h122
1 files changed, 107 insertions, 15 deletions
diff --git a/runtime/src/queue_holds.h b/runtime/src/queue_holds.h
index eafe3ebc8..2839d4210 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,18 @@ 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, and record the value with
85 // signaled() only once the vkQueueSubmit succeeded (a failed submit signals nothing: waiters must stay held and a
86 // retry must go through). False when a Signal of at least `value` was already submitted: the timeline cannot go
87 // down or stay (VUID-VkSubmitInfo-pSignalSemaphores-03242 and the timeline rules), and a wait for `value` is
88 // already satisfied, so the Vulkan signal is left out. This happens when a held Signal(F, 2) is released after an
89 // unheld Signal(F, 3). Unknown semaphores are always signaled.
90 bool would_signal(Semaphore semaphore, std::uint64_t value) const {
91 std::lock_guard lock(mutex_);
92 const auto it = fences_.find(semaphore);
93 return it == fences_.end() || it->second < value;
94 }
95
71 // true when no submitted Signal reaches `value` yet. Unknown semaphores are satisfied. 96 // true when no submitted Signal reaches `value` yet. Unknown semaphores are satisfied.
72 bool pending(Semaphore semaphore, std::uint64_t value) const { 97 bool pending(Semaphore semaphore, std::uint64_t value) const {
73 std::lock_guard lock(mutex_); 98 std::lock_guard lock(mutex_);
@@ -94,14 +119,47 @@ public:
94 pump_ready(); 119 pump_ready();
95 } 120 }
96 121
122 // Sequence numbers of held operations, in the order they were held (across queues).
123 std::uint64_t next_seq() { return seq_.fetch_add(1, std::memory_order_acq_rel) + 1; }
124 std::uint64_t current_seq() const { return seq_.load(std::memory_order_acquire); }
125
126 // Runs `fn` once every operation held up to now has been released (or dropped), on the thread that releases the
127 // last of them, or right here if none is held. Callbacks of one owner run in registration order. Used by the
128 // presenter: a Present queued behind held work shows what that work draws.
129 void after_held(const void* owner, std::function<void()> fn) {
130 {
131 std::lock_guard lock(mutex_);
132 barriers_.push_back({owner, current_seq(), std::move(fn)});
133 barrier_count_.fetch_add(1, std::memory_order_acq_rel);
134 }
135 pump_ready();
136 }
137
138 // Drops the callbacks of `owner` that have not run, and waits for one that is running.
139 void cancel_after(const void* owner) {
140 {
141 std::lock_guard pump(pump_mutex_);
142 std::lock_guard lock(mutex_);
143 for (auto it = barriers_.begin(); it != barriers_.end();)
144 if (it->owner == owner) {
145 it = barriers_.erase(it);
146 barrier_count_.fetch_sub(1, std::memory_order_acq_rel);
147 } else {
148 ++it;
149 }
150 }
151 pump_ready();
152 }
153
97 // Held operations over all queues, for the cheap "is there anything to do" test. 154 // 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); } 155 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; } 156 bool any_held() const { return held_.load(std::memory_order_acquire) > 0; }
100 157
101 // Releases every held operation that has become ready, in dependency order: a release can submit a Signal 158 // 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. 159 // that makes another queue's front ready, so passes repeat until one finds nothing new. Then the callbacks
160 // whose held work is all gone.
103 void pump_ready() { 161 void pump_ready() {
104 if (!any_held()) return; 162 if (!any_held() && barrier_count_.load(std::memory_order_acquire) == 0) return;
105 dirty_.store(true, std::memory_order_release); 163 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 164 if (in_pump()) return; // a release on this thread submitted a Signal: the running pass will go again
107 for (;;) { 165 for (;;) {
@@ -114,6 +172,7 @@ public:
114 progress = false; 172 progress = false;
115 for (Holder* holder : snapshot()) progress |= holder->release_ready(); 173 for (Holder* holder : snapshot()) progress |= holder->release_ready();
116 } 174 }
175 run_barriers();
117 } 176 }
118 in_pump() = false; 177 in_pump() = false;
119 pump.unlock(); 178 pump.unlock();
@@ -122,6 +181,12 @@ public:
122 } 181 }
123 182
124private: 183private:
184 struct Barrier {
185 const void* owner;
186 std::uint64_t seq;
187 std::function<void()> fn;
188 };
189
125 static bool& in_pump() { 190 static bool& in_pump() {
126 static thread_local bool pumping = false; 191 static thread_local bool pumping = false;
127 return pumping; 192 return pumping;
@@ -132,11 +197,38 @@ private:
132 return holders_; 197 return holders_;
133 } 198 }
134 199
135 mutable std::mutex mutex_; // leaf: fences_ and holders_ 200 // Called with pump_mutex_ held. A callback runs when no held operation is older than the moment it was asked.
201 void run_barriers() {
202 for (;;) {
203 Barrier front;
204 {
205 std::lock_guard lock(mutex_);
206 if (barriers_.empty()) return;
207 front = barriers_.front();
208 }
209 for (Holder* holder : snapshot()) {
210 const std::uint64_t oldest = holder->oldest_held_seq();
211 if (oldest && oldest <= front.seq) return;
212 }
213 {
214 std::lock_guard lock(mutex_);
215 if (barriers_.empty() || barriers_.front().seq != front.seq || barriers_.front().owner != front.owner)
216 continue; // cancelled meanwhile
217 barriers_.pop_front();
218 barrier_count_.fetch_sub(1, std::memory_order_acq_rel);
219 }
220 front.fn();
221 }
222 }
223
224 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 225 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_; 226 std::unordered_map<Semaphore, std::uint64_t> fences_;
138 std::vector<Holder*> holders_; 227 std::vector<Holder*> holders_;
228 std::deque<Barrier> barriers_;
139 std::atomic<std::int64_t> held_{0}; 229 std::atomic<std::int64_t> held_{0};
230 std::atomic<std::int64_t> barrier_count_{0};
231 std::atomic<std::uint64_t> seq_{0};
140 std::atomic<bool> dirty_{false}; 232 std::atomic<bool> dirty_{false};
141}; 233};
142 234