56 :
m_cell(std::exchange(other.m_cell,
nullptr))
57 ,
m_value(std::exchange(other.m_value,
nullptr))
65 m_cell = std::exchange(other.m_cell,
nullptr);
66 m_value = std::exchange(other.m_value,
nullptr);
72 [[nodiscard]]
const T*
get() const noexcept {
return m_value; }
78 [[nodiscard]]
explicit operator bool() const noexcept {
return m_value !=
nullptr; }
92 m_cell->m_readers.fetch_sub(1);
107 :
m_head(
std::make_unique<T>(
std::move(initial)).release())
133 const T* value =
m_head.load();
138 return Guard(
this, value);
149 replace(std::make_unique<T>(std::move(value)).release());
171 template <
typename Edit>
172 requires std::invocable<Edit&, T&> && std::default_initializable<T> && std::copy_constructible<T>
175 using Result = std::invoke_result_t<Edit&, T&>;
176 static_assert(std::is_void_v<Result> || std::is_same_v<Result, bool>,
177 "SnapshotCell::update edit must return void or bool");
181 const T* current = pin.
get();
183 auto draft = current ? std::make_unique<T>(*current) : std::make_unique<T>();
185 if constexpr (std::is_void_v<Result>) {
186 std::invoke(edit, *draft);
187 }
else if (!std::invoke(edit, *draft)) {
191 const T* expected = current;
192 if (
m_head.compare_exchange_strong(expected, draft.get())) {
210 m_retired.push(std::unique_ptr<const T>(old));
221 [
this](std::unique_ptr<const T>&) {
return m_readers.load() == 0; },
222 [](std::unique_ptr<const T>& snapshot) { snapshot.reset(); });
225 alignas(64) std::atomic<const T*>
m_head {
nullptr };
226 alignas(64)
mutable std::atomic<uint32_t>
m_readers { 0 };
friend class SnapshotCell
const T * get() const noexcept
The snapshot, or null when the cell was empty.
Guard(Guard &&other) noexcept
Guard & operator=(const Guard &)=delete
Guard(const SnapshotCell *cell, const T *value) noexcept
const SnapshotCell * m_cell
Guard(const Guard &)=delete
const T * operator->() const noexcept
const T & operator*() const noexcept
Guard & operator=(Guard &&other) noexcept
Keeps one snapshot alive and unchanged while it exists.
SnapshotCell(const SnapshotCell &)=delete
SweepList< std::unique_ptr< const T > > m_retired
void replace(const T *next)
Guard read() const noexcept
Pins the current snapshot.
void publish(T value)
Replaces the snapshot with value, whatever it was.
std::atomic< uint32_t > m_readers
~SnapshotCell()
Frees the current snapshot and every retired one.
std::atomic< const T * > m_head
SnapshotCell & operator=(const SnapshotCell &)=delete
SnapshotCell() noexcept=default
Creates an empty cell.
void reset()
Empties the cell.
SnapshotCell & operator=(SnapshotCell &&)=delete
void retire(const T *old)
SnapshotCell(SnapshotCell &&)=delete
bool update(Edit edit)
Edits a copy of the current snapshot and publishes it.
Lock-free, unbounded, multi-producer collection of in-flight items.