dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
EPollPoller.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 <queue>
13#include <vector>
14
15// Project includes
16#include "Poller.hpp"
18#include <dfx-utilities/OwnedFd.hpp>
19
20namespace dfx::FdWatch
21{
44class EPollPoller : public Poller
45{
46 struct FdData
47 {
49 FdCallback cb;
50 };
51
52 using FdDatas = std::vector<FdData>;
53
54public:
60
66
67public:
69 void deregisterFd(Utils::BorrowedFd fd) noexcept override;
71 void deferCall(Callback cb) override;
72
73public:
81 void exec();
82
89 void exec(std::stop_token stopToken);
90
95 void requestStop() noexcept;
96
105 void stop();
106
111 void wake();
112
114 bool isRunning() const noexcept { return _isRunnning; }
115
116private:
117 void _execImpl(std::stop_token stopToken);
118
119 FdDatas::iterator _getFdData(Utils::BorrowedFd fd) noexcept;
120 FdDatas::const_iterator _getFdData(Utils::BorrowedFd fd) const noexcept;
121
122private:
123 std::stop_source _stopSource;
124 bool _isRunnning = false;
125
126private:
127 Utils::OwnedFd _epfd;
128 Utils::OwnedFd _eventFd;
129
130 std::queue<Callback> _deferedCb;
131 FdDatas _fdData;
132};
133} // !namespace dfx::FdWatch
Convenience macros to explicitly control copy and move semantics.
void updateFdEvents(Utils::BorrowedFd fd, EventInterests events) override
Update the list of event that this fd is attached to.
void registerFd(Utils::BorrowedFd fd, EventInterests events, FdCallback cb) override
Registers a file descriptor with the poller.
DFX_DISABLE_COPY_AND_MOVE(EPollPoller)
Non-copyable, non-movable.
void exec()
Runs the event loop until stopping is requested.
void deregisterFd(Utils::BorrowedFd fd) noexcept override
Deregisters a file descriptor from the poller.
EPollPoller()
Constructs the poller and initializes underlying kernel resources.
void wake()
Wakes the event loop. Useful when a stop has been requested while the loop is blocked in epoll_wait()...
void deferCall(Callback cb) override
Defer the call to the callback to the next time the event loop is reached.
bool isRunning() const noexcept
Returns whether the event loop is currently running.
Definition EPollPoller.hpp:114
void requestStop() noexcept
Requests the event loop to stop (non-blocking).
void exec(std::stop_token stopToken)
Runs the event loop until stopToken.stop_requested() returns true.
void stop()
Stops the event loop.
Bitset wrapper for dfx::FdWatch::EventInterest values.
Abstract interface for FD-based event polling.
Definition Poller.hpp:37
Non-owning wrapper around a file descriptor.
Definition BorrowedFd.hpp:33
Owning RAII wrapper around a file descriptor.
Definition OwnedFd.hpp:36
Definition SocketClient.hpp:23
std::move_only_function< void()> Callback
Generic move-only callback with no arguments.
Definition Callback.hpp:31
std::function< void(Utils::BorrowedFd, EventTriggers)> FdCallback
Invoked when events occur on a watched file descriptor.
Definition Callback.hpp:28