170 lines
6.5 KiB
C++
170 lines
6.5 KiB
C++
#pragma once
|
|
|
|
#include <Void/Async/Executor.hpp>
|
|
#include <Void/Async/Operation.hpp>
|
|
#include <Void/Async/Task.hpp>
|
|
#include <Void/IO/File.hpp>
|
|
#include <Void/IO/Types.hpp>
|
|
#include <Void/Time/Clock.hpp>
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <stop_token>
|
|
#include <vector>
|
|
|
|
namespace Void::IO
|
|
{
|
|
|
|
class ByteStream;
|
|
|
|
namespace detail
|
|
{
|
|
|
|
class StreamState;
|
|
class FileState;
|
|
|
|
enum class Direction {
|
|
read,
|
|
write
|
|
};
|
|
|
|
class DriverBackend : public std::enable_shared_from_this<DriverBackend>
|
|
{
|
|
public:
|
|
virtual ~DriverBackend() = default;
|
|
|
|
[[nodiscard]] virtual BackendPreference kind() const noexcept = 0;
|
|
[[nodiscard]] virtual const IoLimits& limits() const noexcept = 0;
|
|
[[nodiscard]] virtual const Void::Time::MonotonicClock& clock() const noexcept = 0;
|
|
[[nodiscard]] virtual Async::Executor& executor() noexcept = 0;
|
|
|
|
[[nodiscard]] virtual Void::Result::Result<std::shared_ptr<StreamState>>
|
|
create_mock_stream() = 0;
|
|
|
|
[[nodiscard]] virtual Void::Result::Result<std::shared_ptr<StreamState>>
|
|
adopt_fd(int fd) = 0;
|
|
|
|
[[nodiscard]] virtual Void::Result::Result<
|
|
std::pair<std::shared_ptr<StreamState>, std::shared_ptr<StreamState>>>
|
|
create_socket_pair() = 0;
|
|
|
|
[[nodiscard]] virtual Void::Result::Result<std::shared_ptr<Async::OperationState<Transfer>>>
|
|
submit_read(const std::shared_ptr<StreamState>& stream,
|
|
std::span<std::byte> buf,
|
|
Async::OperationOptions options) = 0;
|
|
|
|
[[nodiscard]] virtual Void::Result::Result<std::shared_ptr<Async::OperationState<Transfer>>>
|
|
submit_write(const std::shared_ptr<StreamState>& stream,
|
|
std::span<const std::byte> buf,
|
|
Async::OperationOptions options) = 0;
|
|
|
|
[[nodiscard]] virtual Void::Result::Result<std::shared_ptr<Async::OperationState<void>>>
|
|
submit_close(const std::shared_ptr<StreamState>& stream,
|
|
Async::OperationOptions options) = 0;
|
|
|
|
virtual void on_stream_destroyed(const DescriptorId& id) noexcept = 0;
|
|
|
|
[[nodiscard]] virtual Void::Result::Result<std::size_t>
|
|
poll_once(std::optional<std::chrono::nanoseconds> max_wait) = 0;
|
|
|
|
virtual void wake() noexcept = 0;
|
|
virtual void request_stop() noexcept = 0;
|
|
[[nodiscard]] virtual Void::Result::Result<void> shutdown_and_join() = 0;
|
|
[[nodiscard]] virtual DriverTelemetry telemetry() const noexcept = 0;
|
|
|
|
// ── Positional files (bounded worker pool shared by every backend) ──────
|
|
[[nodiscard]] virtual Void::Result::Result<std::shared_ptr<FileState>>
|
|
open_file(const std::filesystem::path& path, const FileOpenOptions& options) = 0;
|
|
[[nodiscard]] virtual Void::Result::Result<std::shared_ptr<FileState>>
|
|
adopt_file_fd(int fd) = 0;
|
|
[[nodiscard]] virtual Void::Result::Result<void> start_file_workers() = 0;
|
|
[[nodiscard]] virtual Void::Result::Result<std::size_t>
|
|
run_file_work(std::size_t max_requests) = 0;
|
|
|
|
[[nodiscard]] virtual Void::Result::Result<std::shared_ptr<Async::OperationState<Transfer>>>
|
|
submit_file_read(const std::shared_ptr<FileState>& file,
|
|
std::uint64_t offset,
|
|
std::span<std::byte> buf,
|
|
Async::OperationOptions options) = 0;
|
|
[[nodiscard]] virtual Void::Result::Result<std::shared_ptr<Async::OperationState<Transfer>>>
|
|
submit_file_write(const std::shared_ptr<FileState>& file,
|
|
std::uint64_t offset,
|
|
std::span<const std::byte> buf,
|
|
Async::OperationOptions options) = 0;
|
|
[[nodiscard]] virtual Void::Result::Result<std::shared_ptr<Async::OperationState<void>>>
|
|
submit_file_sync(const std::shared_ptr<FileState>& file,
|
|
SyncMode mode,
|
|
Async::OperationOptions options) = 0;
|
|
[[nodiscard]] virtual Void::Result::Result<std::shared_ptr<Async::OperationState<void>>>
|
|
submit_file_close(const std::shared_ptr<FileState>& file,
|
|
Async::OperationOptions options) = 0;
|
|
|
|
// ── Bounded shutdown core ────────────────────────────────────────────────
|
|
// Idempotent: closes admission, cancels queued requests and records stop
|
|
// intent for active ones, then wakes the poller. Never completes,
|
|
// dispatches or frees anything itself.
|
|
virtual void begin_shutdown() noexcept = 0;
|
|
// Drives shutdown until complete, the injected-clock deadline passes or
|
|
// `stop` fires; the latter two return the current report with
|
|
// complete == false (not an error). Returns
|
|
// Async::make_async_error(AdmissionError::self_join) on one of this
|
|
// backend's file workers or inside one of its poll turns.
|
|
[[nodiscard]] virtual Void::Result::Result<DriverShutdownReport> advance_shutdown(
|
|
std::optional<Void::Time::MonotonicClock::time_point> deadline,
|
|
std::stop_token stop) = 0;
|
|
[[nodiscard]] virtual bool shutdown_complete() const noexcept = 0;
|
|
// No owned operations, file requests or in-flight syscalls remain.
|
|
[[nodiscard]] virtual bool quiescent() const noexcept = 0;
|
|
// Driver::~Driver only: begin + advance to completion (blocking).
|
|
virtual void teardown_blocking() noexcept = 0;
|
|
|
|
// Mock inspection / test-only control hooks (returns nullptr on non-mock backends).
|
|
[[nodiscard]] virtual class MockBackend* as_mock() noexcept { return nullptr; }
|
|
};
|
|
|
|
class StreamState
|
|
{
|
|
public:
|
|
StreamState(std::weak_ptr<DriverBackend> backend, DescriptorId id) noexcept
|
|
: backend_(std::move(backend))
|
|
, id_(id)
|
|
{
|
|
}
|
|
|
|
~StreamState()
|
|
{
|
|
if (!explicitly_closed_.load(std::memory_order_acquire)) {
|
|
if (auto b = backend_.lock()) {
|
|
b->on_stream_destroyed(id_);
|
|
}
|
|
}
|
|
}
|
|
|
|
StreamState(const StreamState&) = delete;
|
|
StreamState& operator=(const StreamState&) = delete;
|
|
|
|
[[nodiscard]] DescriptorId id() const noexcept { return id_; }
|
|
[[nodiscard]] std::shared_ptr<DriverBackend> lock_backend() const noexcept
|
|
{
|
|
return backend_.lock();
|
|
}
|
|
|
|
void mark_explicit_closed() noexcept
|
|
{
|
|
explicitly_closed_.store(true, std::memory_order_release);
|
|
}
|
|
|
|
private:
|
|
std::weak_ptr<DriverBackend> backend_{};
|
|
DescriptorId id_{};
|
|
std::atomic<bool> explicitly_closed_{false};
|
|
};
|
|
|
|
} // namespace detail
|
|
} // namespace Void::IO
|