11inline constexpr size_t INVALID_SLOT =
static_cast<size_t>(-1);
47 size_t capacity()
const {
return m_data.size(); }
52 size_t live()
const {
return m_live.load(std::memory_order_relaxed); }
55 void set_live(
size_t n) { m_live.store(n, std::memory_order_relaxed); }
58 void resize(
size_t n) { m_data.resize(n); }
60 void shrink_to_fit() { m_data.shrink_to_fit(); }
77 bool refused()
const {
return m_refused.load(std::memory_order_relaxed); }
78 void clear_refused() { m_refused.store(
false, std::memory_order_relaxed); }
90 T& operator[](
size_t i) {
return m_data[i]; }
91 const T& operator[](
size_t i)
const {
return m_data[i]; }
100 auto begin() {
return m_data.begin(); }
101 auto end() {
return m_data.end(); }
102 auto begin()
const {
return m_data.begin(); }
103 auto end()
const {
return m_data.end(); }
118 size_t first = m_live.load(std::memory_order_relaxed);
122 const size_t cap = m_data.size();
126 if (first > cap || n > cap - first) {
127 m_refused.store(
true, std::memory_order_relaxed);
130 }
while (!m_live.compare_exchange_weak(
133 std::memory_order_acq_rel,
134 std::memory_order_relaxed));
139 for (
size_t i = first; i < first + n; ++i) {
147 std::vector<T> m_data;
148 std::atomic<size_t> m_live{0};
149 static inline thread_local size_t t_taken = 0;
150 std::atomic<bool> m_refused{
false};
Preallocated storage plus the atomic counter that hands slots out of it.
Definition SlotPool.hpp:38
static size_t taken_by_this_thread()
Slots this thread has been handed by pools of this type, over its lifetime.
Definition SlotPool.hpp:88
void set_live(size_t n)
Set the live count directly. For init and consolidate, which fill [0, n) themselves.
Definition SlotPool.hpp:55
size_t capacity() const
Slots the storage can hold: the end of the spare region.
Definition SlotPool.hpp:47
void clear()
Drop the storage and the live count together, leaving an empty pool.
Definition SlotPool.hpp:63
void resize(size_t n)
Resize the storage. Does not touch the live count – the caller decides that.
Definition SlotPool.hpp:58
size_t request(size_t n)
Atomically reserve n contiguous fresh slots.
Definition SlotPool.hpp:116
size_t live() const
Definition SlotPool.hpp:52