feat(Async): bounded admission gated on an exact retained publication (#16)
This commit is contained in:
1 parent
7b0fea6095
commit
5a076cd8f2
1 file changed
+137
@@ -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
|
||||
Reference in new issue
Block a user