25#ifndef JOIN_CORE_REACTOR_HPP
26#define JOIN_CORE_REACTOR_HPP
35#include <unordered_map>
36#include <unordered_set>
92 virtual void onEvent (
int fd, uint32_t revents)
106 else if (revents & EPOLLOUT)
134 virtual void onClose ([[maybe_unused]]
int fd)
143 virtual void onError ([[maybe_unused]]
int fd)
207 bool sync = true) noexcept;
215 int delHandler (
int fd,
bool sync = true) noexcept;
234 void stop (
bool sync = true) noexcept;
247 int mbind (
int numa)
const noexcept;
254 int mlock () const noexcept;
270 static constexpr
size_t _deletedReserve = 64;
273 static constexpr
size_t _queueSize = 1024;
276 static constexpr
size_t _maxEvents = 1024;
281 enum class CommandType
292 struct alignas (64) Command
298 std::atomic<bool>* done;
299 std::error_code* errc;
310 int registerHandler (
int fd, EventHandler* handler, uint32_t events)
noexcept;
317 int unregisterHandler (
int fd)
noexcept;
331 int writeCommand (
const Command& cmd)
noexcept;
337 void processCommand (
const Command& cmd)
noexcept;
342 void readCommands () noexcept;
348 void dispatchEvent (const epoll_event& event);
360 bool isActive (
int fd) const noexcept;
369 alignas (64)
std::atomic<
bool> _notified{
false};
375 std::unordered_map<int, EventHandler*> _handlers;
378 std::unordered_set<int> _deleted;
381 std::atomic<bool> _running{
false};
384 static constexpr pthread_t _invalidThreadId =
static_cast<pthread_t
> (-1);
387 std::atomic<pthread_t> _threadId{_invalidThreadId};
432 static pthread_t
handle ();
440 static int mbind (
int numa);
Event handler interface class.
Definition reactor.hpp:48
EventHandler(const EventHandler &other)=default
copy constructor.
virtual void onClose(int fd)
method called when handle was closed by the peer.
Definition reactor.hpp:134
virtual ~EventHandler()=default
destroy instance.
virtual void onError(int fd)
method called when an error occurred on handle.
Definition reactor.hpp:143
EventHandler()=default
create instance.
virtual void onReadable(int fd)
method called when data are ready to be read on handle.
Definition reactor.hpp:116
virtual void onEvent(int fd, uint32_t revents)
method called when events are reported on handle.
Definition reactor.hpp:92
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:125
fixed-capacity move-only allocation-free alternative to std::function.
Definition function.hpp:44
Convenience class that owns a Reactor running on a dedicated background thread.
Definition reactor.hpp:394
static int affinity()
get reactor thread affinity.
Definition reactor.cpp:602
static int priority()
get reactor thread priority.
Definition reactor.cpp:620
static pthread_t handle()
get the handle of the reactor thread.
Definition reactor.cpp:629
static Reactor & reactor()
get the global Reactor instance.
Definition reactor.cpp:584
static int mlock()
lock command queue memory in RAM.
Definition reactor.cpp:649
Reactor class.
Definition reactor.hpp:156
bool isRunning() const noexcept
check if the event loop is running.
Definition reactor.cpp:314
Reactor(Reactor &&other)=delete
move constructor.
Reactor()
default constructor.
Definition reactor.cpp:46
Function< void(), 64 > InvokeHandler
function invoked on the reactor thread.
Definition reactor.hpp:159
void waitStopped() const noexcept
wait for the event loop thread to terminate.
Definition reactor.cpp:280
bool isReactorThread() const noexcept
check if the calling thread is the reactor thread.
Definition reactor.cpp:323
int mlock() const noexcept
lock command queue memory in RAM.
Definition reactor.cpp:305
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:241
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, ignored if no event loop is running.
Definition reactor.cpp:255
int addHandler(int fd, EventHandler *handler, bool wantRead=true, bool wantWrite=false, bool sync=true) noexcept
add handler to reactor.
Definition reactor.cpp:100
int invoke(InvokeHandler *fn, bool sync=true) noexcept
invoke a function from the reactor thread.
Definition reactor.cpp:198
thread class.
Definition thread.hpp:147
Definition acceptor.hpp:32
BasicQueue< Type, Backend, SyncPolicy< Type, Backend > > Queue
queue type alias combining backend and policy.
Definition queue.hpp:1054
#define JOIN_LIKELY(x)
Definition utils.hpp:45
#define JOIN_UNLIKELY(x)
Definition utils.hpp:46