|
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
|
#include <fastmm/net/reactor.hpp>
Public Types | |
| using | TimerCallback = InplaceCallback |
| Stored inline (no heap): captures up to InplaceCallback::kCapacity bytes. | |
| using | Task = std::function< void()> |
| using | EventHook = void(*)(void *ctx) |
| Called on the reactor thread after the handler of each I/O event, between one socket's event and the next of the same wait: work that must not wait for the whole batch to be handled (the engine's orders behind a burst of market data, live/venue_slot.cpp) goes here. | |
Public Member Functions | |
| Reactor () | |
| Reactor (ReactorBackend backend) | |
| Throws std::runtime_error if the backend (epoll_create1 / io_uring_setup) or the eventfd cannot be created. | |
| ~Reactor () | |
| Reactor (const Reactor &)=delete | |
| Reactor & | operator= (const Reactor &)=delete |
| ReactorBackend | backend () const noexcept |
| bool | add (int fd, IoHandler &handler, IoEvent events) noexcept |
| fd registration (reactor thread only). | |
| bool | modify (int fd, IoHandler &handler, IoEvent events) noexcept |
| bool | remove (int fd) noexcept |
| bool | is_registered (int fd) const noexcept |
| TimerId | add_timer (std::int64_t deadline_ns, TimerCallback cb) |
| Timers (reactor thread only). | |
| TimerId | add_timer_after (std::int64_t delay_ns, TimerCallback cb) |
| bool | cancel_timer (TimerId id) noexcept |
| std::size_t | active_timers () const noexcept |
| void | post (Task task) |
| Thread-safe: queue a task for the reactor thread and wake it. | |
| void | wake () noexcept |
| Thread-safe: interrupt a blocking wait in run_once(). | |
| int | wake_fd () const noexcept |
| The eventfd wake() writes. | |
| int | run_once (int max_wait_ms) |
| One iteration: wait for I/O at most min(next timer, max_wait_ms), dispatch I/O, posted tasks, expired timers. | |
| void | run (const std::atomic< bool > &stop) |
Loop until stop becomes true (checked once per iteration; call wake() after setting it). | |
| void | set_busy_poll (bool enabled) noexcept |
| Busy polling: never block, pause instruction between empty polls. | |
| bool | busy_poll () const noexcept |
| void | set_after_event (EventHook fn, void *ctx) noexcept |
| bool | event_hangup () const noexcept |
| True while a handler runs for an event that also reported a hang-up or an error (RDHUP/HUP/ERR). | |
| int | epoll_fd () const noexcept |
| The epoll descriptor, or -1 with the io_uring backend. | |
Static Public Member Functions | |
| static bool | io_uring_supported () noexcept |
| True when this kernel lets the process create an io_uring with everything the backend uses (multishot poll, poll update, EXT_ARG timeouts). | |
| static ReactorBackend | resolve_backend (ReactorBackend requested) noexcept |
requested, or Epoll when io_uring was requested but is not supported. | |
| static std::int64_t | now_ns () noexcept |
| CLOCK_MONOTONIC. | |
| using fastmm::net::Reactor::TimerCallback = InplaceCallback |
Stored inline (no heap): captures up to InplaceCallback::kCapacity bytes.
| using fastmm::net::Reactor::Task = std::function<void()> |
| using fastmm::net::Reactor::EventHook = void (*)(void* ctx) |
Called on the reactor thread after the handler of each I/O event, between one socket's event and the next of the same wait: work that must not wait for the whole batch to be handled (the engine's orders behind a burst of market data, live/venue_slot.cpp) goes here.
Null: none.
|
inline |
|
explicit |
Throws std::runtime_error if the backend (epoll_create1 / io_uring_setup) or the eventfd cannot be created.
Check io_uring_supported() before asking for IoUring.
| fastmm::net::Reactor::~Reactor | ( | ) |
|
delete |
|
inlinenoexcept |
|
staticnoexcept |
True when this kernel lets the process create an io_uring with everything the backend uses (multishot poll, poll update, EXT_ARG timeouts).
Probed once, then cached. False on ENOSYS, EPERM (kernel.io_uring_disabled, seccomp), ENOMEM and kernels older than 5.13.
|
staticnoexcept |
requested, or Epoll when io_uring was requested but is not supported.
fd registration (reactor thread only).
Registrations always include RDHUP so half-closes surface as on_readable() (read returns EOF). add() fails for an fd that is already registered, a closed fd and (like epoll) regular files and directories.
|
noexcept |
|
noexcept |
| TimerId fastmm::net::Reactor::add_timer | ( | std::int64_t | deadline_ns, |
| TimerCallback | cb | ||
| ) |
Timers (reactor thread only).
Deadlines are on the now_ns() clock. Arming a timer allocates only when more timers are live than ever before (slot and heap growth).
|
inline |
|
noexcept |
|
inlinenoexcept |
| void fastmm::net::Reactor::post | ( | Task | task | ) |
Thread-safe: queue a task for the reactor thread and wake it.
|
noexcept |
Thread-safe: interrupt a blocking wait in run_once().
|
inlinenoexcept |
The eventfd wake() writes.
Another process that holds a duplicate (the gateway passes it to the strategy with SCM_RIGHTS) wakes this reactor by writing 1 to it.
| int fastmm::net::Reactor::run_once | ( | int | max_wait_ms | ) |
One iteration: wait for I/O at most min(next timer, max_wait_ms), dispatch I/O, posted tasks, expired timers.
max_wait_ms < 0 blocks until something happens. Returns the number of I/O events (epoll) or completions (io_uring) dispatched, or -1 on a wait error other than EINTR. epoll rounds the timer bound up to whole milliseconds; io_uring waits in nanoseconds.
| void fastmm::net::Reactor::run | ( | const std::atomic< bool > & | stop | ) |
Loop until stop becomes true (checked once per iteration; call wake() after setting it).
|
inlinenoexcept |
Busy polling: never block, pause instruction between empty polls.
epoll calls epoll_wait with a zero timeout; io_uring reads the completion ring without a system call unless the kernel flags pending work.
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
True while a handler runs for an event that also reported a hang-up or an error (RDHUP/HUP/ERR).
Only when it is false may a handler take a short read as "socket drained" and skip the read that would return EAGAIN: edge-triggered readiness reports data that arrives later with a new event, but an end of stream already reported has no second event.
|
staticnoexcept |
CLOCK_MONOTONIC.
|
inlinenoexcept |
The epoll descriptor, or -1 with the io_uring backend.