45 lines
1.3 KiB
C++
45 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
|
|
#include <chrono>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <vector>
|
|
|
|
namespace Void::Process
|
|
{
|
|
/// Converts process signals into synchronous, deterministic waits.
|
|
/// Construct this before worker threads so the selected signals inherit a
|
|
/// blocked mask. A watcher must be destroyed on the thread that created it.
|
|
class SignalWatcher
|
|
{
|
|
public:
|
|
|
|
/// An empty signal list watches SIGINT and SIGTERM.
|
|
[[nodiscard]] static Void::Result::Result<SignalWatcher>
|
|
create(std::vector<int> signals = {});
|
|
|
|
~SignalWatcher();
|
|
SignalWatcher(SignalWatcher&&) noexcept;
|
|
SignalWatcher& operator=(SignalWatcher&&) noexcept;
|
|
SignalWatcher(const SignalWatcher&) = delete;
|
|
SignalWatcher& operator=(const SignalWatcher&) = delete;
|
|
|
|
/// Wait for a configured signal. A zero timeout performs a poll;
|
|
/// milliseconds::max() waits indefinitely.
|
|
[[nodiscard]] Void::Result::Result<std::optional<int>>
|
|
wait(std::chrono::milliseconds timeout =
|
|
std::chrono::milliseconds::max());
|
|
|
|
[[nodiscard]] Void::Result::Result<std::optional<int>> poll();
|
|
|
|
private:
|
|
|
|
struct Impl;
|
|
std::unique_ptr<Impl> m_impl;
|
|
explicit SignalWatcher(std::unique_ptr<Impl> impl) noexcept;
|
|
};
|
|
} // namespace Void::Process
|