Bounded admission gated on an exact retained publication (#16) #32

Merged
faumaray merged 2 commits from issue/16-publication-gated-queue into main 2026-12-03 08:08:37 +00:00
2 changed files with 274 additions and 0 deletions

No files matched your search

@@ -0,0 +1,137 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <deque>
#include <mutex>
#include <optional>
#include <utility>
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 <typename Work, typename Token = std::uint64_t>
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<Work> 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<Work> queue_;
bool closed_{false};
};
} // namespace Void::Async
@@ -0,0 +1,137 @@
#include <Void/Async/PublicationGatedQueue.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <atomic>
#include <cstdint>
#include <future>
#include <memory>
#include <thread>
#include <utility>
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<std::unique_ptr<int>, 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<int>(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<int>(1);
Void::Testing::expect(queue.try_submit(token(1, 1), std::move(first)) ==
Status::admitted);
auto second = std::make_unique<int>(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<int>(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<int>(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<int>(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<void> validated;
std::promise<void> published;
std::atomic<Status> status{Status::admitted};
std::unique_ptr<int> retained;
std::thread producer{[&] {
auto payload = std::make_unique<int>(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<int>(6);
Void::Testing::expect(queue.try_submit(token(1, 2), std::move(fresh)) ==
Status::admitted);
}