feat(io): add Driver::run, embedded pump_until, and shutdown conformance tests (S-IO-04)
This commit is contained in:
1 parent
ef85ce07b7
commit
50029e1f7b
5 files changed
+1248
No files matched your search
@@ -7,12 +7,15 @@
|
||||
#include <Void/IO/Types.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <memory_resource>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <stop_token>
|
||||
@@ -148,6 +151,69 @@ public:
|
||||
return backend_->shutdown_complete();
|
||||
}
|
||||
|
||||
/// Runs the poll loop until `stop` is requested, `shutdown_complete()` becomes
|
||||
/// true, or an unrecoverable error occurs. Retries `IoErrorCode::busy` and
|
||||
/// idle mock turns with short stop-aware waits so the loop never busy-spins.
|
||||
[[nodiscard]] Void::Result::Result<std::size_t> run(
|
||||
std::stop_token stop,
|
||||
std::chrono::nanoseconds max_wait = std::chrono::milliseconds{5})
|
||||
{
|
||||
if (max_wait < std::chrono::nanoseconds{0}) {
|
||||
max_wait = std::chrono::nanoseconds{0};
|
||||
}
|
||||
auto wait_stop_aware = [&](std::chrono::nanoseconds budget) {
|
||||
if (budget <= std::chrono::nanoseconds{0} ||
|
||||
stop.stop_requested() || shutdown_complete()) {
|
||||
return;
|
||||
}
|
||||
std::mutex mu;
|
||||
std::condition_variable cv;
|
||||
std::stop_callback cb(stop, [&]() noexcept {
|
||||
std::lock_guard lk{mu};
|
||||
cv.notify_all();
|
||||
});
|
||||
std::unique_lock lk{mu};
|
||||
auto remaining = budget;
|
||||
constexpr auto kSlice =
|
||||
std::chrono::nanoseconds{std::chrono::milliseconds{1}};
|
||||
while (remaining > std::chrono::nanoseconds{0} &&
|
||||
!stop.stop_requested() && !shutdown_complete()) {
|
||||
const auto step = std::min(remaining, kSlice);
|
||||
cv.wait_for(lk, step, [&]() {
|
||||
return stop.stop_requested() || shutdown_complete();
|
||||
});
|
||||
remaining -= step;
|
||||
}
|
||||
};
|
||||
|
||||
std::size_t total = 0;
|
||||
while (!stop.stop_requested() && !shutdown_complete()) {
|
||||
auto res = poll_once(max_wait);
|
||||
if (!res.has_value()) {
|
||||
if (res.error().code().domain == Void::Result::kIoErrorDomain &&
|
||||
res.error().code().value ==
|
||||
static_cast<std::int32_t>(IoErrorCode::busy)) {
|
||||
const auto busy_slice =
|
||||
max_wait > std::chrono::nanoseconds{0}
|
||||
? std::min(max_wait,
|
||||
std::chrono::nanoseconds{
|
||||
std::chrono::milliseconds{1}})
|
||||
: std::chrono::nanoseconds{
|
||||
std::chrono::microseconds{200}};
|
||||
wait_stop_aware(busy_slice);
|
||||
continue;
|
||||
}
|
||||
return std::unexpected{std::move(res.error())};
|
||||
}
|
||||
total += *res;
|
||||
if (*res == 0 && active_backend() == BackendPreference::mock &&
|
||||
max_wait > std::chrono::nanoseconds{0}) {
|
||||
wait_stop_aware(max_wait);
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
private:
|
||||
Driver(std::shared_ptr<detail::DriverBackend> backend,
|
||||
std::shared_ptr<detail::OwnedBufferTracker> buffer_tracker) noexcept;
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Async/Detail/Types.hpp>
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/Timer.hpp>
|
||||
#include <Void/IO/Driver.hpp>
|
||||
#include <Void/IO/Types.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <concepts>
|
||||
#include <condition_variable>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
#include <stop_token>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::IO
|
||||
{
|
||||
|
||||
struct PumpOptions {
|
||||
std::chrono::nanoseconds max_wait{0};
|
||||
std::size_t max_turns{1u << 20};
|
||||
Async::OperationOptions operation{};
|
||||
Async::TimerQueue* timers{nullptr};
|
||||
};
|
||||
|
||||
/// Drives `driver.poll_once`, `options.timers->poll_expired()`, and
|
||||
/// `manual.drain()` until `done()` returns true, `driver.shutdown_complete()`
|
||||
/// becomes true, stop/deadline fires, or `max_turns` is exceeded.
|
||||
template <class Done>
|
||||
requires std::predicate<Done&>
|
||||
[[nodiscard]] Void::Result::Result<std::size_t> pump_until(
|
||||
Driver& driver,
|
||||
Async::ManualExecutor& manual,
|
||||
Done&& done,
|
||||
PumpOptions options = {})
|
||||
{
|
||||
const auto wait_stop_aware = [&](std::chrono::nanoseconds slice) {
|
||||
if (slice <= std::chrono::nanoseconds{0} ||
|
||||
options.operation.stop.stop_requested() ||
|
||||
driver.shutdown_complete()) {
|
||||
return;
|
||||
}
|
||||
std::mutex mu;
|
||||
std::condition_variable cv;
|
||||
std::stop_callback cb(options.operation.stop, [&]() noexcept {
|
||||
std::lock_guard lk{mu};
|
||||
cv.notify_all();
|
||||
});
|
||||
std::unique_lock lk{mu};
|
||||
cv.wait_for(lk, slice, [&]() {
|
||||
return options.operation.stop.stop_requested() ||
|
||||
driver.shutdown_complete();
|
||||
});
|
||||
};
|
||||
|
||||
const auto base_wait =
|
||||
options.max_wait < std::chrono::nanoseconds{0}
|
||||
? std::chrono::nanoseconds{0}
|
||||
: options.max_wait;
|
||||
|
||||
std::size_t total_work = 0;
|
||||
std::size_t turns = 0;
|
||||
|
||||
for (;;) {
|
||||
if (done()) {
|
||||
return total_work;
|
||||
}
|
||||
if (options.operation.stop.stop_requested()) {
|
||||
return std::unexpected{make_io_error(
|
||||
IoErrorCode::cancelled, "pump_until stop requested")};
|
||||
}
|
||||
if (Async::is_deadline_expired(options.operation, driver.clock())) {
|
||||
return std::unexpected{make_io_error(
|
||||
IoErrorCode::timeout, "pump_until deadline expired")};
|
||||
}
|
||||
if (turns >= options.max_turns) {
|
||||
return std::unexpected{make_io_error(
|
||||
IoErrorCode::exhausted, "pump_until max_turns exceeded")};
|
||||
}
|
||||
++turns;
|
||||
|
||||
std::chrono::nanoseconds wait = base_wait;
|
||||
if (!manual.empty()) {
|
||||
wait = std::chrono::nanoseconds{0};
|
||||
} else if (options.timers != nullptr) {
|
||||
wait = options.timers->wait_duration(base_wait);
|
||||
}
|
||||
if (options.operation.deadline.has_value()) {
|
||||
const auto rem = Void::Time::remaining_until(
|
||||
driver.clock(), *options.operation.deadline);
|
||||
if (rem <= std::chrono::nanoseconds{0}) {
|
||||
return std::unexpected{make_io_error(
|
||||
IoErrorCode::timeout, "pump_until deadline expired")};
|
||||
}
|
||||
wait = std::min(wait, rem);
|
||||
}
|
||||
if (wait < std::chrono::nanoseconds{0}) {
|
||||
wait = std::chrono::nanoseconds{0};
|
||||
}
|
||||
|
||||
auto poll_res = driver.poll_once(wait);
|
||||
if (!poll_res.has_value()) {
|
||||
if (poll_res.error().code().domain == Void::Result::kIoErrorDomain &&
|
||||
poll_res.error().code().value ==
|
||||
static_cast<std::int32_t>(IoErrorCode::busy)) {
|
||||
const auto slice =
|
||||
wait > std::chrono::nanoseconds{0}
|
||||
? std::min(wait,
|
||||
std::chrono::nanoseconds{
|
||||
std::chrono::milliseconds{1}})
|
||||
: std::chrono::nanoseconds{
|
||||
std::chrono::microseconds{200}};
|
||||
wait_stop_aware(slice);
|
||||
continue;
|
||||
}
|
||||
return std::unexpected{std::move(poll_res.error())};
|
||||
}
|
||||
total_work += *poll_res;
|
||||
|
||||
if (options.timers != nullptr) {
|
||||
total_work += options.timers->poll_expired();
|
||||
}
|
||||
total_work += manual.drain();
|
||||
|
||||
if (done()) {
|
||||
return total_work;
|
||||
}
|
||||
if (driver.shutdown_complete()) {
|
||||
return total_work;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Void::IO
|
||||
Reference in new issue
Block a user