join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
reactor.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CORE_REACTOR_HPP
26#define JOIN_CORE_REACTOR_HPP
27
28// libjoin.
29#include <join/thread.hpp>
30#include <join/queue.hpp>
31
32// C++.
33#include <unordered_map>
34#include <unordered_set>
35#include <atomic>
36
37// C.
38#include <sys/epoll.h>
39
40namespace join
41{
46 {
47 public:
51 EventHandler () = default;
52
57 EventHandler (const EventHandler& other) = default;
58
64 EventHandler& operator= (const EventHandler& other) = default;
65
70 EventHandler (EventHandler&& other) = default;
71
78
82 virtual ~EventHandler () = default;
83
84 protected:
89 virtual void onReceive ([[maybe_unused]] int fd)
90 {
91 // do nothing.
92 }
93
98 virtual void onClose ([[maybe_unused]] int fd)
99 {
100 // do nothing.
101 }
102
107 virtual void onError ([[maybe_unused]] int fd)
108 {
109 // do nothing.
110 }
111
113 friend class Reactor;
114 };
115
120 {
121 public:
125 Reactor ();
126
131 Reactor (const Reactor& other) = delete;
132
138 Reactor& operator= (const Reactor& other) = delete;
139
144 Reactor (Reactor&& other) = delete;
145
151 Reactor& operator= (Reactor&& other) = delete;
152
156 ~Reactor () noexcept;
157
165 int addHandler (int fd, EventHandler* handler, bool sync = true) noexcept;
166
173 int delHandler (int fd, bool sync = true) noexcept;
174
178 void run ();
179
184 void stop (bool sync = true) noexcept;
185
191 int mbind (int numa) const noexcept;
192
197 int mlock () const noexcept;
198
199 private:
201 static constexpr size_t _deletedReserve = 64;
202
204 static constexpr size_t _queueSize = 1024;
205
207 static constexpr size_t _maxEvents = 1024;
208
212 enum class CommandType
213 {
214 Add,
215 Del,
216 Stop
217 };
218
222 struct alignas (64) Command
223 {
224 CommandType type;
225 int fd;
226 EventHandler* handler;
227 std::atomic<bool>* done;
228 std::atomic<int>* errc;
229 };
230
237 int registerHandler (int fd, EventHandler* handler) noexcept;
238
244 int unregisterHandler (int fd) noexcept;
245
251 int writeCommand (const Command& cmd) noexcept;
252
257 void processCommand (const Command& cmd) noexcept;
258
262 void readCommands () noexcept;
263
268 void dispatchEvent (const epoll_event& event);
269
273 void eventLoop ();
274
280 bool isActive (int fd) const noexcept;
281
283 int _wakeup = -1;
284
286 int _epoll = -1;
287
289 LocalMem::Mpsc::Queue<Command> _commands;
290
292 std::unordered_map<int, EventHandler*> _handlers;
293
295 std::unordered_set<int> _deleted;
296
298 std::atomic<bool> _running{false};
299
301 std::atomic<pthread_t> _threadId{0};
302 };
303
308 {
309 public:
314 static Reactor* reactor ();
315
321 static int affinity (int core);
322
327 static int affinity ();
328
334 static int priority (int prio);
335
340 static int priority ();
341
346 static pthread_t handle ();
347
353 static int mbind (int numa);
354
359 static int mlock ();
360
361 private:
366 static ReactorThread& instance ();
367
371 ReactorThread ();
372
377 ReactorThread (const ReactorThread& other) = delete;
378
384 ReactorThread& operator= (const ReactorThread& other) = delete;
385
390 ReactorThread (ReactorThread&& other) noexcept = delete;
391
397 ReactorThread& operator= (ReactorThread&& other) noexcept = delete;
398
403
405 Reactor _reactor;
406
408 Thread _dispatcher;
409 };
410}
411
412#endif
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
Definition error.hpp:137