16#include "fastmm/core/msg_ring.hpp"
17#include "fastmm/core/shm_ring.hpp"
35 using DrainFn = void (*)(
void* ctx)
noexcept;
39 : ring_(
ring), policy_(
policy), spin_limit_(spin_limit) {}
45 spin_limit_ = spin_limit;
52 spin_limit_ = spin_limit;
61 drain_on_commit_ = fn !=
nullptr && on_commit;
63 [[nodiscard]]
bool attached() const noexcept {
return ring_ !=
nullptr || shm_ !=
nullptr; }
64 [[nodiscard]] MsgRing*
ring() const noexcept {
return ring_; }
65 [[nodiscard]] ShmRing*
shm_ring() const noexcept {
return shm_; }
69 template <MessageLike M>
70 [[nodiscard]] M*
reserve(std::uint32_t len =
static_cast<std::uint32_t
>(
sizeof(M)))
noexcept {
73 [[nodiscard]] std::byte*
reserve_bytes(std::uint32_t len)
noexcept {
75 std::byte* p = try_reserve(len);
76 if (FASTMM_LIKELY(p !=
nullptr))
return p;
77 if (drain_fn_ !=
nullptr) {
79 drain_fn_(drain_ctx_);
81 if (p !=
nullptr)
return p;
83 for (std::uint32_t i = 0; i < spin_limit_; ++i) {
86 if (p !=
nullptr)
return p;
93 if (FASTMM_LIKELY(ring_ !=
nullptr)) {
99 if (drain_on_commit_) drain_fn_(drain_ctx_);
103 [[nodiscard]]
bool push(
const EventHeader& m)
noexcept {
105 if (p ==
nullptr)
return false;
106 std::memcpy(p, &m, m.len);
111 [[nodiscard]] std::uint64_t
pushed() const noexcept {
return pushed_; }
112 [[nodiscard]] std::uint64_t
overflows() const noexcept {
return overflows_; }
116 [[nodiscard]] FASTMM_FORCE_INLINE std::byte* try_reserve(std::uint32_t len)
noexcept {
117 return FASTMM_LIKELY(ring_ !=
nullptr) ? ring_->try_reserve(len) : shm_->try_reserve(len);
119 void on_overflow() noexcept {
121 if (overflow_fn_ !=
nullptr) overflow_fn_(overflow_ctx_, *
this);
124 MsgRing* ring_ =
nullptr;
125 ShmRing* shm_ =
nullptr;
127 std::uint32_t spin_limit_ = 0;
128 std::uint64_t pushed_ = 0;
129 std::uint64_t overflows_ = 0;
131 void* overflow_ctx_ =
nullptr;
133 void* drain_ctx_ =
nullptr;
134 bool drain_on_commit_ =
false;
Definition event_sink.hpp:34
void commit() noexcept
Definition event_sink.hpp:94
void attach(MsgRing *ring, SinkPolicy policy, std::uint32_t spin_limit=1 '000 '000) noexcept
Definition event_sink.hpp:43
MsgRing * ring() const noexcept
Definition event_sink.hpp:66
ShmRing * shm_ring() const noexcept
Definition event_sink.hpp:67
M * reserve(std::uint32_t len=static_cast< std::uint32_t >(sizeof(M))) noexcept
In-place construction: reserve, fill, commit.
Definition event_sink.hpp:72
bool push(const EventHeader &m) noexcept
Copy-in: m.len bytes starting at the header.
Definition event_sink.hpp:105
std::uint64_t overflows() const noexcept
Definition event_sink.hpp:114
void set_overflow_callback(OverflowFn fn, void *ctx) noexcept
Definition event_sink.hpp:56
bool attached() const noexcept
Definition event_sink.hpp:65
void set_drain_hook(DrainFn fn, void *ctx, bool on_commit) noexcept
Definition event_sink.hpp:60
void(*)(void *ctx) noexcept DrainFn
Definition event_sink.hpp:37
SinkPolicy policy() const noexcept
Definition event_sink.hpp:115
EventSink() noexcept=default
void(*)(void *ctx, const EventSink &sink) noexcept OverflowFn
Definition event_sink.hpp:36
std::uint64_t pushed() const noexcept
Definition event_sink.hpp:113
std::byte * reserve_bytes(std::uint32_t len) noexcept
Definition event_sink.hpp:75
Event bus message structs (5.5).
SinkPolicy
Definition event_sink.hpp:29
@ Drop
market data: return false, count, notify
@ Spin
order events: spin up to spin_limit before giving up