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;
192 int mbind (
int numa)
const noexcept;
199 int mlock () const noexcept;
209 static constexpr
size_t _deletedReserve = 64;
212 static constexpr
size_t _queueSize = 1024;
215 static constexpr
size_t _maxEvents = 1024;
220 enum class CommandType
230 struct alignas (64) Command
235 std::atomic<bool>* done;
236 std::atomic<int>* errc;
245 int registerHandler (
int fd, EventHandler* handler)
noexcept;
252 int unregisterHandler (
int fd)
noexcept;
259 int writeCommand (
const Command& cmd)
noexcept;
265 void processCommand (
const Command& cmd)
noexcept;
270 void readCommands () noexcept;
276 void dispatchEvent (const epoll_event& event);
288 bool isActive (
int fd) const noexcept;
297 LocalMem::Mpsc::Queue<Command> _commands;
300 std::unordered_map<
int, EventHandler*> _handlers;
303 std::unordered_set<
int> _deleted;
306 std::atomic<
bool> _running{
false};
309 std::atomic<pthread_t> _threadId{0};
354 static pthread_t
handle ();
362 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:316
static int affinity()
get reactor thread affinity.
Definition reactor.cpp:467
static int priority()
get reactor thread priority.
Definition reactor.cpp:485
static pthread_t handle()
get the handle of the reactor thread.
Definition reactor.cpp:494
static Reactor * reactor()
get the global Reactor instance.
Definition reactor.cpp:449
static int mlock()
lock command queue memory in RAM.
Definition reactor.cpp:514
Reactor class.
Definition reactor.hpp:120
Reactor(Reactor &&other)=delete
move constructor.
Reactor()
default constructor.
Definition reactor.cpp:46
bool isReactorThread() const noexcept
check if the calling thread is the reactor thread.
Definition reactor.cpp:259
int mlock() const noexcept
lock command queue memory in RAM.
Definition reactor.cpp:250
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