25#ifndef JOIN_CORE_REACTOR_HPP
26#define JOIN_CORE_REACTOR_HPP
33#include <unordered_map>
34#include <unordered_set>
98 virtual void onClose ([[maybe_unused]]
int fd)
107 virtual void onError ([[maybe_unused]]
int fd)
173 int delHandler (
int fd,
bool sync = true) noexcept;
184 void stop (
bool sync = true) noexcept;
191 int mbind (
int numa) const noexcept;
197 int mlock () const noexcept;
201 static constexpr
size_t _deletedReserve = 64;
204 static constexpr
size_t _queueSize = 1024;
207 static constexpr
size_t _maxEvents = 1024;
212 enum class CommandType
222 struct alignas (64) Command
227 std::atomic<bool>* done;
228 std::atomic<int>* errc;
237 int registerHandler (
int fd, EventHandler* handler)
noexcept;
244 int unregisterHandler (
int fd)
noexcept;
251 int writeCommand (
const Command& cmd)
noexcept;
257 void processCommand (
const Command& cmd)
noexcept;
262 void readCommands () noexcept;
268 void dispatchEvent (const epoll_event& event);
280 bool isActive (
int fd) const noexcept;
289 LocalMem::Mpsc::Queue<Command> _commands;
292 std::unordered_map<
int, EventHandler*> _handlers;
295 std::unordered_set<
int> _deleted;
298 std::atomic<
bool> _running{
false};
301 std::atomic<pthread_t> _threadId{0};
346 static pthread_t
handle ();
353 static int mbind (
int numa);
Event handler interface class.
Definition reactor.hpp:46
EventHandler(const EventHandler &other)=default
copy constructor.
virtual void onClose(int fd)
method called when handle is closed.
Definition reactor.hpp:98
virtual void onReceive(int fd)
method called when data are ready to be read on handle.
Definition reactor.hpp:89
virtual ~EventHandler()=default
destroy instance.
virtual void onError(int fd)
method called when an error occurred on handle.
Definition reactor.hpp:107
EventHandler()=default
create instance.
EventHandler & operator=(const EventHandler &other)=default
copy assignment operator.
EventHandler(EventHandler &&other)=default
move constructor.
Convenience class that owns a Reactor running on a dedicated background thread.
Definition reactor.hpp:308
static int mbind(int numa)
bind command queue memory to a NUMA node.
Definition reactor.cpp:492
static int affinity()
get reactor thread affinity.
Definition reactor.cpp:456
static int priority()
get reactor thread priority.
Definition reactor.cpp:474
static pthread_t handle()
get the handle of the reactor thread.
Definition reactor.cpp:483
static Reactor * reactor()
get the global Reactor instance.
Definition reactor.cpp:438
static int mlock()
lock command queue memory in RAM.
Definition reactor.cpp:501
Reactor class.
Definition reactor.hpp:120
Reactor(Reactor &&other)=delete
move constructor.
Reactor()
default constructor.
Definition reactor.cpp:46
int mlock() const noexcept
lock command queue memory in RAM.
Definition reactor.cpp:248
int mbind(int numa) const noexcept
bind command queue memory to a NUMA node.
Definition reactor.cpp:239
int delHandler(int fd, bool sync=true) noexcept
delete handler from reactor.
Definition reactor.cpp:150
int addHandler(int fd, EventHandler *handler, bool sync=true) noexcept
add handler to reactor.
Definition reactor.cpp:94
Reactor & operator=(const Reactor &other)=delete
copy assignment operator.
void run()
run the event loop (blocking).
Definition reactor.cpp:200
Reactor(const Reactor &other)=delete
copy constructor.
~Reactor() noexcept
destroy instance.
Definition reactor.cpp:82
void stop(bool sync=true) noexcept
stop the event loop.
Definition reactor.cpp:214
thread class.
Definition thread.hpp:148
Definition acceptor.hpp:32