151 lines
5.0 KiB
C++
151 lines
5.0 KiB
C++
#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};
|
|
}
|
|
|
|
const auto poll_start = std::chrono::steady_clock::now();
|
|
auto poll_res = driver.poll_once(wait);
|
|
const auto poll_elapsed = std::chrono::steady_clock::now() - poll_start;
|
|
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())};
|
|
}
|
|
std::size_t turn_work = *poll_res;
|
|
|
|
if (options.timers != nullptr) {
|
|
turn_work += options.timers->poll_expired();
|
|
}
|
|
turn_work += manual.drain();
|
|
total_work += turn_work;
|
|
|
|
if (done()) {
|
|
return total_work;
|
|
}
|
|
if (driver.shutdown_complete()) {
|
|
return total_work;
|
|
}
|
|
if (turn_work == 0 && manual.empty() &&
|
|
wait > std::chrono::nanoseconds{0} && poll_elapsed < wait) {
|
|
const auto remain = std::chrono::duration_cast<std::chrono::nanoseconds>(
|
|
wait - poll_elapsed);
|
|
const auto slice = std::min(
|
|
remain,
|
|
std::chrono::nanoseconds{std::chrono::milliseconds{1}});
|
|
wait_stop_aware(slice);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace Void::IO
|