Merge branch 'issue/5-immutable-publication'
This commit is contained in:
commit
f8eca20ef5
2 files changed
+442
No files matched your search
@@ -0,0 +1,232 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <compare>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Core
|
||||
{
|
||||
|
||||
/// Identity of a published snapshot: an epoch (session/domain) and a revision
|
||||
/// that increases within that epoch.
|
||||
struct PublicationVersion {
|
||||
std::uint64_t epoch{0};
|
||||
std::uint64_t revision{0};
|
||||
|
||||
[[nodiscard]] auto operator<=>(const PublicationVersion&) const noexcept =
|
||||
default;
|
||||
[[nodiscard]] bool
|
||||
operator==(const PublicationVersion&) const noexcept = default;
|
||||
};
|
||||
|
||||
/// Single-writer, multi-reader publication of immutable values.
|
||||
///
|
||||
/// A producer prepares a complete immutable value and calls `publish` with its
|
||||
/// version; readers call `acquire` and keep the returned `Snapshot` alive for
|
||||
/// as long as they need a coherent view. Publishing swaps one fully built state
|
||||
/// atomically: a reader sees either the previous or the next snapshot, never a
|
||||
/// mixture, and a retained snapshot stays valid across any number of later
|
||||
/// publications.
|
||||
///
|
||||
/// `publish` is compare-and-publish: when `expected_epoch` is supplied it must
|
||||
/// equal the current epoch, and the new version must advance the current one
|
||||
/// (higher epoch, or same epoch with a higher revision). A rejected publish
|
||||
/// leaves the last good publication untouched, so a failed preparation that
|
||||
/// never calls `publish` cannot corrupt readers.
|
||||
///
|
||||
/// The value is released when the publication and the last reader holding it
|
||||
/// drop their snapshots.
|
||||
template <typename T> class Publication
|
||||
{
|
||||
private:
|
||||
|
||||
struct State {
|
||||
std::shared_ptr<const T> value;
|
||||
PublicationVersion version;
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
using Value = T;
|
||||
|
||||
/// Retained, immutable view of one publication.
|
||||
class Snapshot
|
||||
{
|
||||
public:
|
||||
|
||||
Snapshot() noexcept = default;
|
||||
|
||||
[[nodiscard]] explicit operator bool() const noexcept
|
||||
{
|
||||
return state_ != nullptr && state_->value != nullptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] const T& operator*() const noexcept
|
||||
{
|
||||
return *state_->value;
|
||||
}
|
||||
|
||||
[[nodiscard]] const T* operator->() const noexcept
|
||||
{
|
||||
return state_->value.get();
|
||||
}
|
||||
|
||||
[[nodiscard]] const T* get() const noexcept
|
||||
{
|
||||
return state_ != nullptr ? state_->value.get()
|
||||
: nullptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] PublicationVersion version() const noexcept
|
||||
{
|
||||
return state_ != nullptr ? state_->version
|
||||
: PublicationVersion{};
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
friend class Publication;
|
||||
|
||||
explicit Snapshot(std::shared_ptr<const State> state) noexcept
|
||||
: state_(std::move(state))
|
||||
{
|
||||
}
|
||||
|
||||
std::shared_ptr<const State> state_{};
|
||||
};
|
||||
|
||||
enum class PublishStatus : std::uint8_t {
|
||||
published = 0,
|
||||
/// The supplied or expected epoch is obsolete.
|
||||
stale_epoch,
|
||||
/// Same epoch, but the revision does not advance.
|
||||
stale_revision,
|
||||
/// The value is null.
|
||||
invalid_value,
|
||||
};
|
||||
|
||||
struct PublishResult {
|
||||
PublishStatus status{PublishStatus::invalid_value};
|
||||
PublicationVersion version{};
|
||||
|
||||
[[nodiscard]] bool published() const noexcept
|
||||
{
|
||||
return status == PublishStatus::published;
|
||||
}
|
||||
};
|
||||
|
||||
explicit Publication(std::shared_ptr<const T> initial,
|
||||
PublicationVersion version = {})
|
||||
: current_{std::make_shared<State>(
|
||||
State{std::move(initial), version})}
|
||||
{
|
||||
}
|
||||
|
||||
Publication(const Publication&) = delete;
|
||||
Publication& operator=(const Publication&) = delete;
|
||||
|
||||
/// Current snapshot; the returned handle keeps the value alive.
|
||||
[[nodiscard]] Snapshot acquire() const
|
||||
{
|
||||
return Snapshot{
|
||||
current_.load(std::memory_order_acquire)};
|
||||
}
|
||||
|
||||
[[nodiscard]] PublicationVersion version() const
|
||||
{
|
||||
return current_.load(std::memory_order_acquire)->version;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::uint64_t epoch() const
|
||||
{
|
||||
return version().epoch;
|
||||
}
|
||||
|
||||
/// The version that `publish_next` would use: same epoch, next revision.
|
||||
[[nodiscard]] PublicationVersion next_version() const
|
||||
{
|
||||
PublicationVersion next = version();
|
||||
++next.revision;
|
||||
return next;
|
||||
}
|
||||
|
||||
/// Publish `value` as `next`, optionally requiring `expected_epoch`.
|
||||
///
|
||||
/// The whole state (value and version) is swapped with one atomic
|
||||
/// compare-exchange; on rejection nothing changes.
|
||||
[[nodiscard]] PublishResult
|
||||
publish(std::shared_ptr<const T> value,
|
||||
PublicationVersion next,
|
||||
std::optional<std::uint64_t> expected_epoch = std::nullopt)
|
||||
{
|
||||
if (value == nullptr) {
|
||||
return PublishResult{.status = PublishStatus::invalid_value,
|
||||
.version = version()};
|
||||
}
|
||||
|
||||
std::shared_ptr<State> current =
|
||||
current_.load(std::memory_order_acquire);
|
||||
for (;;) {
|
||||
const PublicationVersion& observed = current->version;
|
||||
if (expected_epoch.has_value() &&
|
||||
*expected_epoch != observed.epoch) {
|
||||
return PublishResult{.status = PublishStatus::stale_epoch,
|
||||
.version = observed};
|
||||
}
|
||||
if (next.epoch < observed.epoch) {
|
||||
return PublishResult{.status = PublishStatus::stale_epoch,
|
||||
.version = observed};
|
||||
}
|
||||
if (next.epoch == observed.epoch &&
|
||||
next.revision <= observed.revision) {
|
||||
return PublishResult{
|
||||
.status = PublishStatus::stale_revision,
|
||||
.version = observed};
|
||||
}
|
||||
|
||||
std::shared_ptr<State> desired =
|
||||
std::make_shared<State>(State{value, next});
|
||||
if (current_.compare_exchange_weak(
|
||||
current,
|
||||
std::move(desired),
|
||||
std::memory_order_acq_rel,
|
||||
std::memory_order_acquire)) {
|
||||
return PublishResult{.status = PublishStatus::published,
|
||||
.version = next};
|
||||
}
|
||||
// `current` now holds the state that won the race; retry the
|
||||
// validation and the exchange against it.
|
||||
}
|
||||
}
|
||||
|
||||
/// Convenience for the common case: same epoch, next revision.
|
||||
[[nodiscard]] PublishResult
|
||||
publish_next(std::shared_ptr<const T> value,
|
||||
std::optional<std::uint64_t> expected_epoch =
|
||||
std::nullopt)
|
||||
{
|
||||
return publish(std::move(value), next_version(), expected_epoch);
|
||||
}
|
||||
|
||||
/// Start a new epoch at revision 0, optionally requiring the previous
|
||||
/// epoch.
|
||||
[[nodiscard]] PublishResult
|
||||
publish_epoch(std::shared_ptr<const T> value,
|
||||
std::uint64_t epoch,
|
||||
std::optional<std::uint64_t> expected_epoch =
|
||||
std::nullopt)
|
||||
{
|
||||
return publish(std::move(value),
|
||||
PublicationVersion{.epoch = epoch, .revision = 0},
|
||||
expected_epoch);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::atomic<std::shared_ptr<State>> current_;
|
||||
};
|
||||
|
||||
} // namespace Void::Core
|
||||
Reference in new issue
Block a user