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
|
||||
@@ -0,0 +1,210 @@
|
||||
#include <Void/Core/Publication.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct Payload {
|
||||
std::uint64_t revision{0};
|
||||
std::string text;
|
||||
};
|
||||
|
||||
struct Tracked {
|
||||
Tracked() { ++live; }
|
||||
~Tracked() { --live; }
|
||||
Tracked(const Tracked&) = delete;
|
||||
Tracked& operator=(const Tracked&) = delete;
|
||||
|
||||
static inline std::atomic<int> live{0};
|
||||
};
|
||||
|
||||
[[nodiscard]] std::shared_ptr<const Payload>
|
||||
payload(std::uint64_t revision, std::string text = {})
|
||||
{
|
||||
return std::make_shared<const Payload>(
|
||||
Payload{.revision = revision, .text = std::move(text)});
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test publication_retains_previous_snapshot()
|
||||
{
|
||||
Void::Core::Publication<Payload> publication{
|
||||
payload(1, "world"), {.epoch = 1, .revision = 1}};
|
||||
|
||||
auto held = publication.acquire();
|
||||
Void::Testing::require(held.get() != nullptr);
|
||||
|
||||
auto result = publication.publish(
|
||||
payload(2, "new world"), {.epoch = 1, .revision = 2});
|
||||
Void::Testing::expect(result.published());
|
||||
|
||||
// The retained handle still sees the old, complete snapshot.
|
||||
Void::Testing::expect_eq(held->revision, std::uint64_t{1});
|
||||
Void::Testing::expect_eq(held->text, std::string{"world"});
|
||||
Void::Testing::expect_eq(held.version().revision, std::uint64_t{1});
|
||||
|
||||
auto current = publication.acquire();
|
||||
Void::Testing::expect_eq(current->revision, std::uint64_t{2});
|
||||
Void::Testing::expect_eq(current->text, std::string{"new world"});
|
||||
Void::Testing::expect_eq(current.version().revision, std::uint64_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test publication_compare_and_publish_rejects_obsolete_epoch()
|
||||
{
|
||||
Void::Core::Publication<Payload> publication{
|
||||
payload(1, "epoch one"), {.epoch = 1, .revision = 5}};
|
||||
|
||||
Void::Testing::require(
|
||||
publication
|
||||
.publish(payload(2, "epoch two"), {.epoch = 2, .revision = 0})
|
||||
.published());
|
||||
|
||||
// A producer that validated against the old epoch must be rejected.
|
||||
auto stale = publication.publish(payload(3, "late"),
|
||||
{.epoch = 2, .revision = 1},
|
||||
/*expected_epoch=*/1);
|
||||
Void::Testing::expect(stale.status ==
|
||||
Void::Core::Publication<Payload>::PublishStatus::
|
||||
stale_epoch);
|
||||
Void::Testing::expect_eq(publication.version().epoch, std::uint64_t{2});
|
||||
Void::Testing::expect_eq(publication.acquire()->revision,
|
||||
std::uint64_t{2});
|
||||
|
||||
// A lower epoch is rejected too.
|
||||
auto older = publication.publish(payload(4, "older"),
|
||||
{.epoch = 1, .revision = 99});
|
||||
Void::Testing::expect(older.status ==
|
||||
Void::Core::Publication<Payload>::PublishStatus::
|
||||
stale_epoch);
|
||||
Void::Testing::expect_eq(publication.acquire()->revision,
|
||||
std::uint64_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test publication_rejects_non_advancing_revision()
|
||||
{
|
||||
Void::Core::Publication<Payload> publication{
|
||||
payload(1), {.epoch = 1, .revision = 3}};
|
||||
|
||||
auto same = publication.publish(payload(2), {.epoch = 1, .revision = 3});
|
||||
Void::Testing::expect(same.status ==
|
||||
Void::Core::Publication<Payload>::PublishStatus::
|
||||
stale_revision);
|
||||
auto lower = publication.publish(payload(3), {.epoch = 1, .revision = 2});
|
||||
Void::Testing::expect(lower.status ==
|
||||
Void::Core::Publication<Payload>::PublishStatus::
|
||||
stale_revision);
|
||||
Void::Testing::expect_eq(publication.acquire()->revision,
|
||||
std::uint64_t{1});
|
||||
}
|
||||
|
||||
Void::Testing::Test publication_new_epoch_resets_revision()
|
||||
{
|
||||
Void::Core::Publication<Payload> publication{
|
||||
payload(1), {.epoch = 1, .revision = 100}};
|
||||
auto result = publication.publish_epoch(payload(2), 2);
|
||||
Void::Testing::expect(result.published());
|
||||
Void::Testing::expect_eq(publication.version().epoch, std::uint64_t{2});
|
||||
Void::Testing::expect_eq(publication.version().revision,
|
||||
std::uint64_t{0});
|
||||
}
|
||||
|
||||
Void::Testing::Test publication_releases_value_after_last_reader()
|
||||
{
|
||||
std::weak_ptr<const Tracked> old;
|
||||
{
|
||||
auto initial = std::make_shared<const Tracked>();
|
||||
old = initial;
|
||||
Void::Core::Publication<Tracked> publication{
|
||||
std::move(initial), {.epoch = 1, .revision = 1}};
|
||||
auto held = publication.acquire();
|
||||
Void::Testing::expect(!old.expired());
|
||||
|
||||
Void::Testing::require(
|
||||
publication
|
||||
.publish(std::make_shared<const Tracked>(),
|
||||
{.epoch = 1, .revision = 2})
|
||||
.published());
|
||||
|
||||
// The old value is still referenced by the retained reader.
|
||||
Void::Testing::expect(!old.expired());
|
||||
held = publication.acquire();
|
||||
// Replacing the handle drops the last reference to the old value.
|
||||
Void::Testing::expect(old.expired());
|
||||
}
|
||||
Void::Testing::expect(old.expired());
|
||||
}
|
||||
|
||||
Void::Testing::Test publication_failed_preparation_preserves_last_good()
|
||||
{
|
||||
Void::Core::Publication<Payload> publication{
|
||||
payload(7, "good"), {.epoch = 1, .revision = 7}};
|
||||
|
||||
try {
|
||||
// A preparation that throws never reaches publish.
|
||||
throw std::runtime_error{"preparation failed"};
|
||||
} catch (const std::runtime_error&) {
|
||||
}
|
||||
|
||||
Void::Testing::expect_eq(publication.version().revision,
|
||||
std::uint64_t{7});
|
||||
Void::Testing::expect_eq(publication.acquire()->text, std::string{"good"});
|
||||
}
|
||||
|
||||
Void::Testing::Test publication_concurrent_readers_see_coherent_snapshots()
|
||||
{
|
||||
Void::Core::Publication<Payload> publication{
|
||||
payload(0, "0"), {.epoch = 1, .revision = 0}};
|
||||
|
||||
constexpr std::uint64_t kPublishes = 200;
|
||||
std::atomic<bool> done{false};
|
||||
std::atomic<int> inconsistent{0};
|
||||
std::atomic<std::uint64_t> observed_max{0};
|
||||
|
||||
std::vector<std::thread> readers;
|
||||
for (int reader = 0; reader < 3; ++reader) {
|
||||
readers.emplace_back([&] {
|
||||
while (!done.load(std::memory_order_acquire)) {
|
||||
auto snapshot = publication.acquire();
|
||||
if (snapshot->revision != snapshot.version().revision) {
|
||||
++inconsistent;
|
||||
}
|
||||
if (snapshot->text !=
|
||||
std::to_string(snapshot->revision)) {
|
||||
++inconsistent;
|
||||
}
|
||||
const auto revision = snapshot->revision;
|
||||
auto previous = observed_max.load();
|
||||
while (previous < revision &&
|
||||
!observed_max.compare_exchange_weak(previous,
|
||||
revision)) {
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for (std::uint64_t revision = 1; revision <= kPublishes; ++revision) {
|
||||
const auto result = publication.publish(
|
||||
payload(revision, std::to_string(revision)),
|
||||
{.epoch = 1, .revision = revision});
|
||||
Void::Testing::expect(result.published());
|
||||
}
|
||||
done.store(true, std::memory_order_release);
|
||||
for (auto& reader : readers) {
|
||||
reader.join();
|
||||
}
|
||||
|
||||
Void::Testing::expect_eq(inconsistent.load(), 0);
|
||||
Void::Testing::expect_eq(publication.version().revision, kPublishes);
|
||||
Void::Testing::expect_eq(publication.acquire()->text,
|
||||
std::to_string(kPublishes));
|
||||
}
|
||||
Reference in new issue
Block a user