feat(SystemAsync): add async IO ProcessManager/communicate and shared WatcherService with WatchSubscription (M-SYSTEM-03, M-SYSTEM-04)
This commit is contained in:
1 parent
fcdbc063b2
commit
9a3c5d86ef
9 files changed
+2062
No files matched your search
@@ -0,0 +1,111 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Async/Detail/Types.hpp>
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/Task.hpp>
|
||||
#include <Void/IO/Driver.hpp>
|
||||
#include <Void/System/Process.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace Void::SystemAsync
|
||||
{
|
||||
|
||||
struct ProcessManagerOptions {
|
||||
std::size_t max_active_children{64};
|
||||
std::size_t max_pending_spawns{128};
|
||||
std::chrono::milliseconds default_kill_grace{std::chrono::milliseconds{2000}};
|
||||
};
|
||||
|
||||
class ProcessManager;
|
||||
|
||||
class ManagedChild
|
||||
{
|
||||
public:
|
||||
ManagedChild() noexcept = default;
|
||||
~ManagedChild() noexcept;
|
||||
|
||||
ManagedChild(const ManagedChild&) = delete;
|
||||
ManagedChild& operator=(const ManagedChild&) = delete;
|
||||
ManagedChild(ManagedChild&&) noexcept;
|
||||
ManagedChild& operator=(ManagedChild&&) noexcept;
|
||||
|
||||
[[nodiscard]] bool valid() const noexcept;
|
||||
[[nodiscard]] Void::Process::Child& child() noexcept { return child_; }
|
||||
[[nodiscard]] const Void::Process::Child& child() const noexcept
|
||||
{
|
||||
return child_;
|
||||
}
|
||||
|
||||
void release_slot() noexcept;
|
||||
|
||||
private:
|
||||
friend class ProcessManager;
|
||||
struct SlotGuard;
|
||||
|
||||
Void::Process::Child child_{};
|
||||
std::shared_ptr<void> slot_token_{};
|
||||
};
|
||||
|
||||
class ProcessManager
|
||||
{
|
||||
public:
|
||||
struct Impl;
|
||||
|
||||
explicit ProcessManager(
|
||||
Void::IO::Driver& driver,
|
||||
Void::Async::Executor& executor,
|
||||
ProcessManagerOptions options = {},
|
||||
const Void::Time::MonotonicClock& clock =
|
||||
Void::Time::SystemMonotonicClock::instance());
|
||||
|
||||
~ProcessManager() noexcept;
|
||||
|
||||
ProcessManager(const ProcessManager&) = delete;
|
||||
ProcessManager& operator=(const ProcessManager&) = delete;
|
||||
ProcessManager(ProcessManager&&) = delete;
|
||||
ProcessManager& operator=(ProcessManager&&) = delete;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<ManagedChild, Void::Result::Error>
|
||||
spawn(const Void::Process::SpawnOptions& options);
|
||||
|
||||
[[nodiscard]] Void::Coro::Task<
|
||||
Void::Result::Result<Void::Process::CommunicateResult, Void::Result::Error>>
|
||||
communicate(Void::Process::Child& child,
|
||||
std::string stdin_data = {},
|
||||
Void::Process::CaptureLimits limits = {},
|
||||
Void::Async::OperationOptions options = {});
|
||||
|
||||
[[nodiscard]] Void::Coro::Task<
|
||||
Void::Result::Result<Void::Process::CommunicateResult, Void::Result::Error>>
|
||||
communicate(ManagedChild& managed,
|
||||
std::string stdin_data = {},
|
||||
Void::Process::CaptureLimits limits = {},
|
||||
Void::Async::OperationOptions options = {});
|
||||
|
||||
[[nodiscard]] Void::Coro::Task<
|
||||
Void::Result::Result<Void::Process::ExitStatus, Void::Result::Error>>
|
||||
wait(Void::Process::Child& child,
|
||||
Void::Async::OperationOptions options = {});
|
||||
|
||||
[[nodiscard]] Void::Coro::Task<
|
||||
Void::Result::Result<Void::Process::ExitStatus, Void::Result::Error>>
|
||||
wait(ManagedChild& managed,
|
||||
Void::Async::OperationOptions options = {});
|
||||
|
||||
[[nodiscard]] Void::Coro::Task<Void::Result::Result<void, Void::Result::Error>>
|
||||
close_and_join(Void::Async::OperationOptions options = {});
|
||||
|
||||
[[nodiscard]] std::size_t active_children() const noexcept;
|
||||
[[nodiscard]] bool closed() const noexcept;
|
||||
|
||||
private:
|
||||
std::shared_ptr<Impl> impl_;
|
||||
};
|
||||
|
||||
} // namespace Void::SystemAsync
|
||||
@@ -0,0 +1,116 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace Void::SystemAsync
|
||||
{
|
||||
|
||||
enum class WatchEventKind {
|
||||
created,
|
||||
modified,
|
||||
deleted,
|
||||
replaced_inode,
|
||||
symlink_changed,
|
||||
permission_denied,
|
||||
rescan_required,
|
||||
};
|
||||
|
||||
struct WatchEvent {
|
||||
WatchEventKind kind{WatchEventKind::modified};
|
||||
std::filesystem::path path{};
|
||||
bool exists{false};
|
||||
std::uint64_t dev{0};
|
||||
std::uint64_t ino{0};
|
||||
std::uint64_t size{0};
|
||||
int sys_errno{0};
|
||||
};
|
||||
|
||||
struct WatcherLimits {
|
||||
std::size_t max_watched_paths{1024};
|
||||
std::size_t max_queued_events{256};
|
||||
std::chrono::milliseconds poll_interval{std::chrono::milliseconds{250}};
|
||||
int debounce_polls{1};
|
||||
};
|
||||
|
||||
struct WatchPathOptions {
|
||||
std::optional<std::filesystem::path> allowed_root{};
|
||||
bool allow_symlink{false};
|
||||
};
|
||||
|
||||
using WatchCallback = std::function<void(const WatchEvent&)>;
|
||||
using ErrorSink = std::function<void(const Void::Result::Error&)>;
|
||||
|
||||
class WatcherService;
|
||||
|
||||
class WatchSubscription
|
||||
{
|
||||
public:
|
||||
WatchSubscription() noexcept = default;
|
||||
~WatchSubscription() noexcept;
|
||||
|
||||
WatchSubscription(const WatchSubscription&) = delete;
|
||||
WatchSubscription& operator=(const WatchSubscription&) = delete;
|
||||
WatchSubscription(WatchSubscription&&) noexcept;
|
||||
WatchSubscription& operator=(WatchSubscription&&) noexcept;
|
||||
|
||||
[[nodiscard]] bool active() const noexcept;
|
||||
[[nodiscard]] Void::Result::Result<void, Void::Result::Error>
|
||||
close_and_join();
|
||||
|
||||
private:
|
||||
friend class WatcherService;
|
||||
|
||||
struct Token;
|
||||
std::shared_ptr<Token> token_;
|
||||
};
|
||||
|
||||
class WatcherService
|
||||
{
|
||||
public:
|
||||
struct Impl;
|
||||
|
||||
explicit WatcherService(
|
||||
Void::Async::Executor& executor,
|
||||
WatcherLimits limits = {},
|
||||
const Void::Time::MonotonicClock& clock =
|
||||
Void::Time::SystemMonotonicClock::instance(),
|
||||
ErrorSink error_sink = {},
|
||||
bool start_background_thread = false);
|
||||
|
||||
~WatcherService() noexcept;
|
||||
|
||||
WatcherService(const WatcherService&) = delete;
|
||||
WatcherService& operator=(const WatcherService&) = delete;
|
||||
WatcherService(WatcherService&&) = delete;
|
||||
WatcherService& operator=(WatcherService&&) = delete;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<WatchSubscription, Void::Result::Error>
|
||||
watch(const std::filesystem::path& path,
|
||||
WatchCallback callback,
|
||||
WatchPathOptions options = {});
|
||||
|
||||
void poll_once();
|
||||
|
||||
[[nodiscard]] std::size_t watch_count() const noexcept;
|
||||
[[nodiscard]] std::size_t queued_event_count() const noexcept;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void, Void::Result::Error> shutdown();
|
||||
|
||||
private:
|
||||
std::shared_ptr<Impl> impl_;
|
||||
};
|
||||
|
||||
} // namespace Void::SystemAsync
|
||||
Reference in new issue
Block a user