61class CaptureSlotRegistry :
public std::enable_shared_from_this<Producer>
64 using SlotType = Slot;
66 CaptureSlotRegistry(std::size_t slotCount, std::size_t minFreeSlots)
67 : m_slotCount(slotCount), m_minFreeSlots(minFreeSlots)
69 Q_ASSERT(slotCount <= Capacity);
71 static_assert(CaptureSlotProducer<Producer, Slot>);
74 std::generate_n(std::back_inserter(m_freeSlotIndices), slotCount,
75 [n = slotCount]()
mutable {
83 Lease(
const Lease &) =
delete;
84 Lease(Lease &&other)
noexcept =
default;
86 Lease &operator=(
const Lease &) =
delete;
87 Lease &operator=(Lease &&other)
noexcept
91 m_producer = std::exchange(other.m_producer, {});
92 m_index = other.m_index;
97 ~Lease() { release(); }
100 friend class CaptureSlotRegistry;
102 Lease(std::shared_ptr<Producer> producer, std::size_t index)
103 : m_producer(std::move(producer)), m_index(index)
110 std::exchange(m_producer, {})->releaseEntry(m_index);
113 std::shared_ptr<Producer> m_producer;
114 std::size_t m_index = 0;
119 std::optional<Lease> tryReserveSlot(Slot &slot)
121 std::lock_guard lock(m_mutex);
122 if (m_stopped || m_outstandingCount + m_minFreeSlots >= m_slotCount) {
123 qCDebug(qLcCaptureSlotsTrace) <<
"refusing slot: stopped:" << m_stopped
124 <<
"outstanding:" << m_outstandingCount <<
"of"
125 << m_slotCount <<
"keeping" << m_minFreeSlots <<
"free";
129 ++m_outstandingCount;
130 const std::size_t index = allocateEntry(Slot(std::move(slot)));
131 qCDebug(qLcCaptureSlotsTrace)
132 <<
"reserved entry" << index <<
"outstanding:" << m_outstandingCount;
134 this->shared_from_this(),
139 std::optional<Lease> tryReserveSlot(Slot &&slot) {
return tryReserveSlot(slot); }
141 std::size_t outstandingSlotCount()
const
143 std::lock_guard lock(m_mutex);
144 return m_outstandingCount;
151 qCDebug(qLcCaptureSlots) <<
"stopping with" << outstandingSlotCount()
152 <<
"slots outstanding";
154 std::unique_lock lock(m_mutex);
156 while (m_releasesInFlight != 0)
157 m_releaseFinished.wait(&m_mutex);
161 ~CaptureSlotRegistry() =
default;
168 constexpr bool isFree()
const {
return !slot.has_value(); }
169 std::optional<Slot> slot;
172 Producer &producer() {
return static_cast<Producer &>(*
this); }
174 std::size_t allocateEntry(Slot slot)
176 Q_ASSERT(!m_freeSlotIndices.empty());
177 const std::size_t index = m_freeSlotIndices.back();
178 m_freeSlotIndices.pop_back();
180 Q_ASSERT(m_entries[index].isFree());
181 qCDebug(qLcCaptureSlotsTrace) <<
"using entry" << index;
182 m_entries[index] = Entry{ std::move(slot) };
186 void releaseEntry(std::size_t index)
188 std::optional<Slot> slot;
190 std::lock_guard lock(m_mutex);
191 Entry &entry = m_entries[index];
192 Q_ASSERT(entry.slot &&
"a lease releases its entry once");
194 slot = std::move(entry.slot);
196 m_freeSlotIndices.push_back(index);
197 --m_outstandingCount;
200 qCDebug(qLcCaptureSlotsTrace) <<
"entry" << index <<
"dropped, recycling stopped";
203 ++m_releasesInFlight;
206 qCDebug(qLcCaptureSlotsTrace) <<
"recycling entry" << index;
207 producer().recycleSlot(std::move(*slot));
209 std::lock_guard lock(m_mutex);
210 if (--m_releasesInFlight == 0)
211 m_releaseFinished.wakeAll();
214 const std::size_t m_slotCount;
215 const std::size_t m_minFreeSlots;
217 mutable QMutex m_mutex;
218 QWaitCondition m_releaseFinished;
219 std::array<Entry, Capacity> m_entries;
220 QVarLengthArray<std::size_t, Capacity> m_freeSlotIndices;
221 std::size_t m_outstandingCount = 0;
222 std::size_t m_releasesInFlight = 0;
223 bool m_stopped =
false;