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] 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