feat(Async): add OperationOptions, OperationState, ResumeTicket, TaskScope, and bounded executor tickets (M-ASYNC-02, M-ASYNC-03)
This commit is contained in:
1 parent
4369fde867
commit
c75056fe07
6 files changed
+2229
-4
No files matched your search
@@ -1,15 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Async/Detail/Types.hpp>
|
||||
#include <Void/Async/MpscQueue.hpp>
|
||||
#include <Void/Async/Task.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <coroutine>
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <expected>
|
||||
#include <optional>
|
||||
|
||||
namespace Void::Async
|
||||
{
|
||||
|
||||
class Executor;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
struct ActiveExecutorFrame {
|
||||
const Executor* exec{nullptr};
|
||||
const ActiveExecutorFrame* prev{nullptr};
|
||||
|
||||
[[nodiscard]] static inline const ActiveExecutorFrame*& head() noexcept
|
||||
{
|
||||
thread_local const ActiveExecutorFrame* slot = nullptr;
|
||||
return slot;
|
||||
}
|
||||
};
|
||||
|
||||
class ExecutorThreadGuard
|
||||
{
|
||||
public:
|
||||
explicit ExecutorThreadGuard(const Executor* exec) noexcept
|
||||
: frame_{exec, ActiveExecutorFrame::head()}
|
||||
{
|
||||
ActiveExecutorFrame::head() = &frame_;
|
||||
}
|
||||
|
||||
~ExecutorThreadGuard()
|
||||
{
|
||||
ActiveExecutorFrame::head() = frame_.prev;
|
||||
}
|
||||
|
||||
ExecutorThreadGuard(const ExecutorThreadGuard&) = delete;
|
||||
ExecutorThreadGuard& operator=(const ExecutorThreadGuard&) = delete;
|
||||
|
||||
private:
|
||||
ActiveExecutorFrame frame_;
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
// ── Executor ──────────────────────────────────────────────────────────────────
|
||||
// Minimal scheduling interface bridging Coro::Task to a driving thread.
|
||||
// post() must be safe to call from any thread.
|
||||
@@ -21,6 +62,54 @@ public:
|
||||
|
||||
// Schedule a coroutine for resumption on this executor. Thread-safe.
|
||||
virtual void post(std::coroutine_handle<> handle) = 0;
|
||||
|
||||
// Bounded non-blocking admission. Subclasses may override with queue caps.
|
||||
[[nodiscard]] virtual std::expected<void, AdmissionError>
|
||||
try_post(std::coroutine_handle<> handle)
|
||||
{
|
||||
if (!handle) {
|
||||
return std::unexpected{AdmissionError::invalid_argument};
|
||||
}
|
||||
post(handle);
|
||||
return {};
|
||||
}
|
||||
|
||||
// Reserved lane for resuming waiting/joining continuations without normal
|
||||
// work-queue backpressure rejections.
|
||||
virtual void post_completion(std::coroutine_handle<> handle)
|
||||
{
|
||||
if (handle) {
|
||||
post(handle);
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] virtual bool is_executor_thread() const noexcept
|
||||
{
|
||||
for (const auto* f = detail::ActiveExecutorFrame::head();
|
||||
f != nullptr;
|
||||
f = f->prev) {
|
||||
if (f->exec == this) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
[[nodiscard]] virtual bool supports_reservation() const noexcept { return false; }
|
||||
|
||||
[[nodiscard]] virtual std::expected<void, AdmissionError> try_reserve_slot()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
virtual void commit_reserved(std::coroutine_handle<> handle)
|
||||
{
|
||||
post_completion(handle);
|
||||
}
|
||||
|
||||
virtual void release_reserved() noexcept {}
|
||||
|
||||
[[nodiscard]] virtual bool has_background_workers() const noexcept { return true; }
|
||||
};
|
||||
|
||||
// ── ManualExecutor ────────────────────────────────────────────────────────────
|
||||
@@ -31,18 +120,111 @@ public:
|
||||
class ManualExecutor final : public Executor
|
||||
{
|
||||
public:
|
||||
explicit ManualExecutor(std::size_t max_queue = 0) noexcept
|
||||
: max_queue_(max_queue)
|
||||
{
|
||||
}
|
||||
|
||||
void post(std::coroutine_handle<> handle) override
|
||||
{
|
||||
if (!handle) {
|
||||
return;
|
||||
}
|
||||
pending_normal_.fetch_add(1, std::memory_order_acq_rel);
|
||||
queue_.push(handle);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<void, AdmissionError>
|
||||
try_post(std::coroutine_handle<> handle) override
|
||||
{
|
||||
if (!handle) {
|
||||
return std::unexpected{AdmissionError::invalid_argument};
|
||||
}
|
||||
auto reserved = try_reserve_slot();
|
||||
if (!reserved.has_value()) {
|
||||
return reserved;
|
||||
}
|
||||
queue_.push(handle);
|
||||
return {};
|
||||
}
|
||||
|
||||
void post_completion(std::coroutine_handle<> handle) override
|
||||
{
|
||||
if (!handle) {
|
||||
return;
|
||||
}
|
||||
completion_queue_.push(handle);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool supports_reservation() const noexcept override { return true; }
|
||||
|
||||
[[nodiscard]] std::expected<void, AdmissionError> try_reserve_slot() override
|
||||
{
|
||||
std::size_t cur = pending_normal_.load(std::memory_order_acquire);
|
||||
for (;;) {
|
||||
if (!accepting_.load(std::memory_order_acquire)) {
|
||||
return std::unexpected{AdmissionError::stopped};
|
||||
}
|
||||
if (max_queue_ != 0 && cur >= max_queue_) {
|
||||
return std::unexpected{AdmissionError::overloaded};
|
||||
}
|
||||
if (pending_normal_.compare_exchange_weak(
|
||||
cur,
|
||||
cur + 1,
|
||||
std::memory_order_acq_rel,
|
||||
std::memory_order_acquire)) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void commit_reserved(std::coroutine_handle<> handle) override
|
||||
{
|
||||
if (!handle) {
|
||||
pending_normal_.fetch_sub(1, std::memory_order_acq_rel);
|
||||
return;
|
||||
}
|
||||
queue_.push(handle);
|
||||
}
|
||||
|
||||
void release_reserved() noexcept override
|
||||
{
|
||||
pending_normal_.fetch_sub(1, std::memory_order_acq_rel);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool has_background_workers() const noexcept override { return false; }
|
||||
|
||||
void shutdown() noexcept
|
||||
{
|
||||
accepting_.store(false, std::memory_order_release);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool is_accepting() const noexcept
|
||||
{
|
||||
return accepting_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t pending_normal() const noexcept
|
||||
{
|
||||
return pending_normal_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t max_queue() const noexcept { return max_queue_; }
|
||||
|
||||
// Resume one pending coroutine. Returns false if none was pending.
|
||||
bool run_one()
|
||||
{
|
||||
if (auto c = completion_queue_.try_pop(); c.has_value()) {
|
||||
detail::ExecutorThreadGuard guard{this};
|
||||
c->resume();
|
||||
return true;
|
||||
}
|
||||
auto h = queue_.try_pop();
|
||||
if (!h) {
|
||||
return false;
|
||||
}
|
||||
pending_normal_.fetch_sub(1, std::memory_order_acq_rel);
|
||||
detail::ExecutorThreadGuard guard{this};
|
||||
h->resume();
|
||||
return true;
|
||||
}
|
||||
@@ -58,10 +240,17 @@ public:
|
||||
return count;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool empty() const noexcept { return queue_.empty(); }
|
||||
[[nodiscard]] bool empty() const noexcept
|
||||
{
|
||||
return completion_queue_.empty() && queue_.empty();
|
||||
}
|
||||
|
||||
private:
|
||||
MpscQueue<std::coroutine_handle<>> queue_;
|
||||
std::size_t max_queue_{0};
|
||||
std::atomic<std::size_t> pending_normal_{0};
|
||||
std::atomic<bool> accepting_{true};
|
||||
MpscQueue<std::coroutine_handle<>> queue_{};
|
||||
MpscQueue<std::coroutine_handle<>> completion_queue_{};
|
||||
};
|
||||
|
||||
// ── schedule_on ───────────────────────────────────────────────────────────────
|
||||
@@ -112,4 +301,61 @@ inline void spawn(Executor& executor, Coro::Task<void> task)
|
||||
Detail::spawn_impl(executor, std::move(task));
|
||||
}
|
||||
|
||||
struct ScheduleStatusAwaiter {
|
||||
Executor& executor;
|
||||
OperationOptions options{};
|
||||
const Void::Time::MonotonicClock* clock{&Void::Time::SystemMonotonicClock::instance()};
|
||||
Void::Result::Result<void> pre_outcome{};
|
||||
|
||||
[[nodiscard]] bool await_ready() noexcept
|
||||
{
|
||||
if (options.stop.stop_requested()) {
|
||||
pre_outcome = std::unexpected{make_async_error(
|
||||
AsyncErrorCode::cancelled, "schedule_on cancelled before scheduling")};
|
||||
return true;
|
||||
}
|
||||
if (is_deadline_expired(options, *clock)) {
|
||||
pre_outcome = std::unexpected{make_async_error(
|
||||
AsyncErrorCode::timeout, "schedule_on deadline expired before scheduling")};
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool await_suspend(std::coroutine_handle<> handle)
|
||||
{
|
||||
const auto post_res = executor.try_post(handle);
|
||||
if (!post_res.has_value()) {
|
||||
pre_outcome = std::unexpected{make_async_error(
|
||||
post_res.error(), "schedule_on rejected by executor")};
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> await_resume() noexcept
|
||||
{
|
||||
if (!pre_outcome.has_value()) {
|
||||
return pre_outcome;
|
||||
}
|
||||
if (options.stop.stop_requested()) {
|
||||
return std::unexpected{make_async_error(
|
||||
AsyncErrorCode::cancelled, "schedule_on cancelled before execution")};
|
||||
}
|
||||
if (is_deadline_expired(options, *clock)) {
|
||||
return std::unexpected{make_async_error(
|
||||
AsyncErrorCode::timeout, "schedule_on deadline expired before execution")};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
[[nodiscard]] inline ScheduleStatusAwaiter schedule_on(
|
||||
Executor& executor,
|
||||
OperationOptions options,
|
||||
const Void::Time::MonotonicClock& clock = Void::Time::SystemMonotonicClock::instance()) noexcept
|
||||
{
|
||||
return ScheduleStatusAwaiter{executor, std::move(options), &clock};
|
||||
}
|
||||
|
||||
} // namespace Void::Async
|
||||
Reference in new issue
Block a user