dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
FdListener.hpp
1// SPDX-FileCopyrightText: 2025-2026 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-utilities/BorrowedFd.hpp>
19#include <dfx-utilities/OwnedFd.hpp>
20
21namespace dfx::Runtime
22{
50{
51public:
53 struct FdInfo
54 {
57
59 FdWatch::EventTriggers events;
60
62 FdWatch::EventInterests eventInterest;
63 };
64
65public:
69 using Callback = std::move_only_function<void (std::vector<FdInfo> fdInfos)>;
70
71public:
77 explicit FdListener(Callback cb);
78
83
85 bool isRunning() const noexcept { return _running.load(std::memory_order::relaxed); }
86
99 void registerFd(Utils::BorrowedFd fd, FdWatch::EventInterests events);
100
108
116 void updateFdEvents(Utils::BorrowedFd fd, FdWatch::EventInterests events);
117
124 void rearmFd(Utils::BorrowedFd fd, FdWatch::EventInterests events);
125
126private:
127 void _start();
128 void _stop() noexcept;
129
130 void _init();
131 void _listen(std::stop_token stopToken) noexcept;
132 void _cleanup() noexcept;
133
134private:
135 Utils::OwnedFd _epfd;
136 Utils::OwnedFd _evtfd;
137 Callback _cb;
138
139private:
140 std::atomic_bool _running{false};
141 std::jthread _listener;
142};
143} // !namespace dfx::Runtime
Event interest and trigger flags for file-descriptor watching (Linux/epoll).
void updateFdEvents(Utils::BorrowedFd fd, FdWatch::EventInterests events)
Update the list of event that this fd is attached to.
void rearmFd(Utils::BorrowedFd fd, FdWatch::EventInterests events)
Rearm a previously registered FD.
~FdListener()
Stop the listener thread and release underlying resources.
bool isRunning() const noexcept
Whether the listener thread is currently running.
Definition FdListener.hpp:85
void registerFd(Utils::BorrowedFd fd, FdWatch::EventInterests events)
Register an FD with a set of interests.
void deregisterFd(Utils::BorrowedFd fd) noexcept
Deregister a previously registered FD.
std::move_only_function< void(std::vector< FdInfo > fdInfos)> Callback
Callback invoked for every observed FD event.
Definition FdListener.hpp:69
FdListener(Callback cb)
Construct and start the listener thread.
Non-owning wrapper around a file descriptor.
Definition BorrowedFd.hpp:33
Definition Node.hpp:48
Definition SystemConfigCommandHandler.hpp:15
STL namespace.
Fd plus any associated information.
Definition FdListener.hpp:54
Utils::BorrowedFd fd
The actual FD in borrowed form.
Definition FdListener.hpp:56
FdWatch::EventInterests eventInterest
Event interest list attached to this FD.
Definition FdListener.hpp:62
FdWatch::EventTriggers events
Trigger event associated with the FD.
Definition FdListener.hpp:59