dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
FdListener.hpp
1// SPDX-FileCopyrightText: 2025 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <atomic>
13#include <functional>
14#include <thread>
15
16// Project includes
17#include <dfx-fdwatch/BorrowedFd.hpp>
19#include <dfx-fdwatch/OwnedFd.hpp>
20
21namespace dfx::Runtime
22{
50{
51public:
56 using Callback = std::move_only_function<void (FdWatch::BorrowedFd, FdWatch::EventTriggers)>;
57
58public:
65
70
72 bool isRunning() const noexcept { return _running.load(std::memory_order::relaxed); }
73
81 void registerFd(FdWatch::BorrowedFd fd, FdWatch::EventInterests events);
82
90
91private:
92 void _start();
93 void _stop();
94
95 void _init();
96 void _listen(std::stop_token stopToken) noexcept;
97 void _cleanup() noexcept;
98
99private:
100 FdWatch::OwnedFd _epfd;
101 FdWatch::OwnedFd _evtfd;
102 Callback _cb;
103
104private:
105 std::atomic_bool _running{false};
106 std::jthread _listener;
107};
108} // !namespace dfx::Runtime
Event interest and trigger flags for file-descriptor watching (Linux/epoll).
Non-owning wrapper around a file descriptor.
Definition BorrowedFd.hpp:37
void deregisterFd(FdWatch::BorrowedFd fd) noexcept
Deregister a previously registered FD.
void registerFd(FdWatch::BorrowedFd fd, FdWatch::EventInterests events)
Register an FD with a set of interests.
~FdListener()
Stop the listener thread and release underlying resources.
bool isRunning() const noexcept
Whether the listener thread is currently running.
Definition FdListener.hpp:72
FdListener(Callback cb)
Construct and start the listener thread.
std::move_only_function< void(FdWatch::BorrowedFd, FdWatch::EventTriggers)> Callback
Callback invoked for every observed FD event.
Definition FdListener.hpp:56
Definition SocketClient.hpp:23
Definition Node.hpp:47
STL namespace.