25#ifndef JOIN_CORE_REACTOR_HPP
26#define JOIN_CORE_REACTOR_HPP
33#include <unordered_map>
34#include <unordered_set>
107 virtual void onClose ([[maybe_unused]]
int fd)
116 virtual void onError ([[maybe_unused]]
int fd)
177 bool sync = true) noexcept;
185 int delHandler (
int fd,
bool sync = true) noexcept;
196 void stop (
bool sync = true) noexcept;
204 int mbind (
int numa)
const noexcept;
211 int mlock () const noexcept;
227 static constexpr
size_t _deletedReserve = 64;
230 static constexpr
size_t _queueSize = 1024;
233 static constexpr
size_t _maxEvents = 1024;
238 enum class CommandType
248 struct alignas (64) Command
254 std::atomic<bool>* done;
255 std::error_code* errc;
265 int registerHandler (
int fd, EventHandler* handler, uint32_t events)
noexcept;
272 int unregisterHandler (
int fd)
noexcept;
279 int writeCommand (
const Command& cmd)
noexcept;
285 void processCommand (
const Command& cmd)
noexcept;
290 void readCommands () noexcept;
296 void dispatchEvent (const epoll_event& event);
308 bool isActive (
int fd) const noexcept;
317 LocalMem::Mpsc::Queue<Command> _commands;
320 std::unordered_map<
int, EventHandler*> _handlers;
323 std::unordered_set<
int> _deleted;
326 std::atomic<
bool> _running{
false};
329 static constexpr pthread_t _invalidThreadId =
static_cast<pthread_t
> (-1);
332 std::atomic<pthread_t> _threadId{_invalidThreadId};
377 static pthread_t
handle ();
385 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 was closed by the peer.
Definition reactor.hpp:107
virtual ~EventHandler()=default
destroy instance.
virtual void onError(int fd)
method called when an error occurred on handle.
Definition reactor.hpp:116
EventHandler()=default
create instance.
virtual void onReadable(int fd)
method called when data are ready to be read on handle.
Definition reactor.hpp:89
EventHandler & operator=(const EventHandler &other)=default
copy assignment operator.
EventHandler(EventHandler &&other)=default
move constructor.
virtual void onWriteable(int fd)
method called when data are ready to be written on handle.
Definition reactor.hpp:98
Convenience class that owns a Reactor running on a dedicated background thread.
Definition reactor.hpp:339
static int affinity()
get reactor thread affinity.
Definition reactor.cpp:532
static int priority()
get reactor thread priority.
Definition reactor.cpp:550
static pthread_t handle()
get the handle of the reactor thread.
Definition reactor.cpp:559
static Reactor & reactor()
get the global Reactor instance.
Definition reactor.cpp:514
static int mlock()
lock command queue memory in RAM.
Definition reactor.cpp:579
Reactor class.
Definition reactor.hpp:129
bool isRunning() const noexcept
check if the event loop is running.
Definition reactor.cpp:256
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:265
int mlock() const noexcept
lock command queue memory in RAM.
Definition reactor.cpp:247
int delHandler(int fd, bool sync=true) noexcept
delete handler from reactor.
Definition reactor.cpp:155
Reactor & operator=(const Reactor &other)=delete
copy assignment operator.
void run()
run the event loop (blocking).
Definition reactor.cpp:198
Reactor(const Reactor &other)=delete
copy constructor.
~Reactor() noexcept
destroy instance.
Definition reactor.cpp:88
void stop(bool sync=true) noexcept
stop the event loop.
Definition reactor.cpp:212
int addHandler(int fd, EventHandler *handler, bool wantRead=true, bool wantWrite=false, bool sync=true) noexcept
add handler to reactor.
Definition reactor.cpp:100
thread class.
Definition thread.hpp:148
Definition acceptor.hpp:32