dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
EPollPoller.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 <queue>
13#include <vector>
14
15// Project includes
16#include "OwnedFd.hpp"
17#include "Poller.hpp"
19
20namespace dfx::FdWatch
21{
40class EPollPoller : public Poller
41{
42 struct FdData
43 {
44 BorrowedFd fd;
45 FdCallback cb;
46 };
47
48 using FdDatas = std::vector<FdData>;
49
50public:
56
62
66
67public:
68 void registerFd(BorrowedFd fd, EventInterests events, FdCallback cb) override;
69 void deregisterFd(BorrowedFd fd) noexcept override;
70 void deferCall(Callback cb) override;
71
72public:
80 void exec();
81
83 void requestStop() noexcept;
84
88 void stop();
89
92 void wake();
93
95 bool isRunning() const noexcept { return _isRunnning.load(std::memory_order::acquire); }
96
97private:
98 FdDatas::iterator _getFdData(BorrowedFd fd) noexcept;
99 FdDatas::const_iterator _getFdData(BorrowedFd fd) const noexcept;
100
101private:
102 std::atomic_bool _shouldContinue{false};
103 std::atomic_bool _isRunnning{false};
104
105private:
106 OwnedFd _epfd;
107 OwnedFd _eventFd;
108
109 std::queue<Callback> _deferedCb;
110 FdDatas _fdData;
111};
112} // !namespace dfx::FdWatch
Convenience macros to explicitly control copy and move semantics.
Non-owning wrapper around a file descriptor.
Definition BorrowedFd.hpp:37
~EPollPoller()
Destroys the poller and releases kernel resources. The poller must not be running while being destroy...
void exec()
Runs the event loop until stopping is requested.
EPollPoller()
Constructs the poller and initializes underlying kernel resources.
DISABLE_COPY_AND_MOVE(EPollPoller)
Non-copyable, non-movable.
void wake()
Wakes the event loop. Useful when a stop has been requested while the loop is blocked in epoll_wait()...
void deregisterFd(BorrowedFd fd) noexcept override
Deregisters a file descriptor from the poller.
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:95
void registerFd(BorrowedFd fd, EventInterests events, FdCallback cb) override
Registers a file descriptor with the poller.
void requestStop() noexcept
Requests the event loop to stop (non-blocking).
void stop()
Stops the event loop.
Bitset wrapper for dfx::FdWatch::EventInterest values.
Abstract interface for FD-based event polling.
Definition Poller.hpp:37
Definition SocketClient.hpp:23
std::function< void(BorrowedFd, EventTriggers)> FdCallback
Invoked when events occur on a watched file descriptor.
Definition Callback.hpp:28
std::move_only_function< void()> Callback
Generic move-only callback with no arguments.
Definition Callback.hpp:31