From 5a076cd8f2f8dd20c2166768e1f51b2a912cbfa2 Mon Sep 17 00:00:00 2001 From: Aleksey Selihov <1+faumaray@noreply@void.com> Date: Thu, 3 Dec 2026 07:02:33 +0000 Subject: [PATCH 1/2] feat(Async): bounded admission gated on an exact retained publication (#16) --- include/Void/Async/PublicationGatedQueue.hpp | 137 +++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 include/Void/Async/PublicationGatedQueue.hpp diff --git a/include/Void/Async/PublicationGatedQueue.hpp b/include/Void/Async/PublicationGatedQueue.hpp new file mode 100644 index 0000000..220bf0f --- /dev/null +++ b/include/Void/Async/PublicationGatedQueue.hpp @@ -0,0 +1,137 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace Void::Async +{ + +/// Bounded work queue whose admission is conditioned on an exact retained +/// publication token. +/// +/// A producer validates a publication (and may retain its value elsewhere), +/// then submits work with that token. Comparison against the current token and +/// the capacity reservation happen under one lock, so a replacement published +/// between validation and submission is detected: the work is not moved and +/// the queue is unchanged. +/// +/// Refusal outcomes are distinct: `publication_changed`, `saturated` and +/// `closed` (checked in that order). The payload is moved only when the result +/// is `admitted`. Admitted work is popped exactly once. +/// +/// Thread-safe. +template +class PublicationGatedQueue +{ + public: + + struct Limits { + std::size_t capacity{64}; + }; + + enum class Status : std::uint8_t { + admitted = 0, + /// The current token differs from the expected one. + publication_changed, + /// The queue is at capacity. + saturated, + /// The queue no longer accepts submissions. + closed, + }; + + explicit PublicationGatedQueue(Token initial, + Limits limits = Limits{}) + : token_(std::move(initial)) + , limits_(limits) + { + } + + PublicationGatedQueue(const PublicationGatedQueue&) = delete; + PublicationGatedQueue& operator=(const PublicationGatedQueue&) = + delete; + + /// Publish a replacement token. Returns false once closed. + bool publish(Token next) + { + std::lock_guard lock{mutex_}; + if (closed_) { + return false; + } + token_ = std::move(next); + return true; + } + + [[nodiscard]] Token current() const + { + std::lock_guard lock{mutex_}; + return token_; + } + + /// Admit `work` only if `expected` is still the current publication and + /// capacity is available. `work` is moved only on `admitted`. + [[nodiscard]] Status try_submit(const Token& expected, Work&& work) + { + std::lock_guard lock{mutex_}; + if (closed_) { + return Status::closed; + } + if (!(token_ == expected)) { + return Status::publication_changed; + } + if (queue_.size() >= limits_.capacity) { + return Status::saturated; + } + queue_.push_back(std::move(work)); + return Status::admitted; + } + + /// Remove one admitted item, if any. + [[nodiscard]] std::optional try_pop() + { + std::lock_guard lock{mutex_}; + if (queue_.empty()) { + return std::nullopt; + } + Work work = std::move(queue_.front()); + queue_.pop_front(); + return work; + } + + void close() + { + std::lock_guard lock{mutex_}; + closed_ = true; + } + + [[nodiscard]] bool closed() const + { + std::lock_guard lock{mutex_}; + return closed_; + } + + [[nodiscard]] std::size_t size() const + { + std::lock_guard lock{mutex_}; + return queue_.size(); + } + + [[nodiscard]] Limits limits() const + { + std::lock_guard lock{mutex_}; + return limits_; + } + + private: + + mutable std::mutex mutex_; + Token token_; + Limits limits_{}; + std::deque queue_; + bool closed_{false}; +}; + +} // namespace Void::Async -- 2.54.0 From 60860d167a83160e7f2125dc0b28433b5f50d200 Mon Sep 17 00:00:00 2001 From: Aleksey Selihov <1+faumaray@noreply@void.com> Date: Thu, 3 Dec 2026 07:02:35 +0000 Subject: [PATCH 2/2] feat(Async): bounded admission gated on an exact retained publication (#16) --- .../Async/PublicationGatedQueue.cpp | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 tests/standalone/Async/PublicationGatedQueue.cpp diff --git a/tests/standalone/Async/PublicationGatedQueue.cpp b/tests/standalone/Async/PublicationGatedQueue.cpp new file mode 100644 index 0000000..c72786e --- /dev/null +++ b/tests/standalone/Async/PublicationGatedQueue.cpp @@ -0,0 +1,137 @@ +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace +{ + +struct Token { + std::uint64_t epoch{0}; + std::uint64_t revision{0}; + + [[nodiscard]] bool operator==(const Token&) const noexcept = default; +}; + +[[nodiscard]] Token token(std::uint64_t epoch, std::uint64_t revision) +{ + return Token{.epoch = epoch, .revision = revision}; +} + +using Queue = + Void::Async::PublicationGatedQueue, Token>; +using Status = Queue::Status; + +} // namespace + +Void::Testing::Test gated_queue_rejects_stale_publication_without_moving() +{ + Queue queue{token(1, 1), Queue::Limits{.capacity = 4}}; + Void::Testing::require(queue.publish(token(1, 2))); + + auto payload = std::make_unique(7); + Void::Testing::expect(queue.try_submit(token(1, 1), std::move(payload)) == + Status::publication_changed); + // The payload was not moved and the queue is unchanged. + Void::Testing::require(payload != nullptr); + Void::Testing::expect_eq(*payload, 7); + Void::Testing::expect_eq(queue.size(), std::size_t{0}); + + Void::Testing::expect(queue.try_submit(token(1, 2), std::move(payload)) == + Status::admitted); + Void::Testing::expect(payload == nullptr); + Void::Testing::expect_eq(queue.size(), std::size_t{1}); +} + +Void::Testing::Test gated_queue_saturation_and_capacity_reuse() +{ + Queue queue{token(1, 1), Queue::Limits{.capacity = 1}}; + auto first = std::make_unique(1); + Void::Testing::expect(queue.try_submit(token(1, 1), std::move(first)) == + Status::admitted); + + auto second = std::make_unique(2); + Void::Testing::expect(queue.try_submit(token(1, 1), std::move(second)) == + Status::saturated); + Void::Testing::require(second != nullptr); + + auto popped = queue.try_pop(); + Void::Testing::require(popped.has_value()); + Void::Testing::expect_eq(**popped, 1); + Void::Testing::expect(queue.try_submit(token(1, 1), std::move(second)) == + Status::admitted); +} + +Void::Testing::Test gated_queue_closed() +{ + Queue queue{token(1, 1)}; + auto pending = std::make_unique(3); + Void::Testing::expect(queue.try_submit(token(1, 1), std::move(pending)) == + Status::admitted); + queue.close(); + Void::Testing::expect(!queue.publish(token(1, 2))); + Void::Testing::expect(queue.closed()); + + auto rejected = std::make_unique(4); + Void::Testing::expect(queue.try_submit(token(1, 1), std::move(rejected)) == + Status::closed); + Void::Testing::require(rejected != nullptr); + + // Already admitted work can still be drained. + auto popped = queue.try_pop(); + Void::Testing::require(popped.has_value()); + Void::Testing::expect_eq(**popped, 3); +} + +Void::Testing::Test gated_queue_admitted_work_finishes_once() +{ + Queue queue{token(1, 1)}; + auto payload = std::make_unique(9); + Void::Testing::require(queue.try_submit(token(1, 1), std::move(payload)) == + Status::admitted); + + auto first = queue.try_pop(); + Void::Testing::require(first.has_value()); + Void::Testing::expect_eq(**first, 9); + Void::Testing::expect(!queue.try_pop().has_value()); +} + +Void::Testing::Test gated_queue_stale_submission_after_concurrent_publish() +{ + Queue queue{token(1, 1)}; + std::promise validated; + std::promise published; + std::atomic status{Status::admitted}; + std::unique_ptr retained; + + std::thread producer{[&] { + auto payload = std::make_unique(5); + validated.set_value(); + published.get_future().wait(); + status = queue.try_submit(token(1, 1), std::move(payload)); + retained = std::move(payload); + }}; + + validated.get_future().wait(); + // Same epoch, newer revision: the producer's retained publication is + // obsolete before its submission. + Void::Testing::require(queue.publish(token(1, 2))); + published.set_value(); + producer.join(); + + Void::Testing::expect(status.load() == Status::publication_changed); + Void::Testing::require(retained != nullptr); + Void::Testing::expect_eq(*retained, 5); + Void::Testing::expect_eq(queue.size(), std::size_t{0}); + + // A fresh producer can submit against the new publication. + auto fresh = std::make_unique(6); + Void::Testing::expect(queue.try_submit(token(1, 2), std::move(fresh)) == + Status::admitted); +} -- 2.54.0