31 static_assert(std::has_unique_object_representations_v<EventRingHeader>);
32 static_assert(std::atomic_ref<std::uint64_t>::is_always_lock_free);
33 static_assert(std::atomic_ref<std::uint32_t>::is_always_lock_free);
34 static_assert(std::atomic_ref<std::uint64_t>::required_alignment <=
alignof(std::uint64_t));
51 constexpr static auto EMPTY = std::numeric_limits<std::uint64_t>::max();
68 constexpr static std::size_t
slotStride(std::uint32_t payloadSize)
noexcept
70 constexpr auto alignment = std::size_t{128};
71 return (
sizeof(
Event) + payloadSize + alignment - 1) & ~(alignment - 1U);
95 static void initialize(
void* memory, std::uint32_t
count, std::uint32_t payloadSize);
113 (
_header->payloadSize != payloadSize))
115 throw std::invalid_argument{
"Invalid event ring header."};
118 for (
auto i = std::size_t{0}; i <
count; ++i)
121 if ((header.version !=
STORAGE_VERSION) || (header.size !=
sizeof header))
123 throw std::invalid_argument{
"Invalid event slot header."};
146 auto sequence = std::atomic_ref{
event.header.sequence};
147 auto const previous = sequence.load(std::memory_order_relaxed);
148 if ((previous !=
EMPTY) && (previous != (
index << 1)) && ((previous >> 1) >=
index))
154 sequence.store(
index << 1, std::memory_order_release);
155 storeBytes(event.header.infoWords, &info,
sizeof info);
157 sequence.store((
index << 1) | 1, std::memory_order_release);
176 auto const expected = (
index << 1) | 1;
177 auto const before =
load(event.header.sequence);
178 if (before != expected)
182 loadBytes(&info, event.header.infoWords,
sizeof info);
188 if (
load(event.header.sequence) != before)
205 static std::uint64_t
load(std::uint64_t
const& value)
noexcept
209 return std::atomic_ref{
const_cast<std::uint64_t&
>(value)}.load(std::memory_order_acquire);
219 if ((
count < 2) || (
count > 65536) || (payloadSize == 0) || (payloadSize > 1048576) ||
222 throw std::invalid_argument{
"Invalid event buffer dimensions."};
243 return reinterpret_cast<std::uint64_t*
>(
reinterpret_cast<std::uint8_t*
>(&event) +
sizeof(
Event));
252 static void storeBytes(std::uint64_t* destination,
void const* source, std::size_t
size)
noexcept
254 auto const bytes =
static_cast<std::uint8_t const*
>(source);
255 for (
auto offset = std::size_t{0}; offset <
size; offset +=
sizeof *destination)
257 auto word = std::uint64_t{};
258 std::memcpy(&word, bytes + offset, std::min(
sizeof word,
size - offset));
261 std::atomic_ref{destination[offset /
sizeof word]}.store(word, std::memory_order_release);
271 static void loadBytes(
void* destination, std::uint64_t
const* source, std::size_t
size)
noexcept
273 auto bytes =
static_cast<std::uint8_t*
>(destination);
274 for (
auto offset = std::size_t{0}; offset <
size; offset +=
sizeof *source)
276 auto const word =
load(source[offset /
sizeof(std::uint64_t)]);
277 std::memcpy(bytes + offset, &word, std::min(
sizeof word,
size - offset));
Internal shared-memory headers for discrete grains and event slots.
static void initialize(void *memory, std::uint32_t count, std::uint32_t payloadSize)
Construct headers, empty sequence tags and payload words in a fresh mapping.
EventRingBuffer(void *memory, std::uint32_t count, std::uint32_t payloadSize)
Attach a view and validate immutable headers across all slots.
EventRingHeader * _header
Borrowed address of the mapping's immutable ring header.
static constexpr auto EMPTY
Tag identifying a slot with no publication.
std::size_t _stride
Aligned distance in bytes between slot headers.
static std::uint64_t * payloadWords(Event &event) noexcept
Locate the atomic payload words immediately after an event slot header.
ReadResult read(std::uint64_t index, mxlEventInfo &info, std::uint8_t *payload) const noexcept
Copy one entry with sequence validation before and after the copy.
static void validateGeometry(std::uint32_t count, std::uint32_t payloadSize)
Validate slot count, payload capacity and mapping-size limits.
static constexpr std::size_t slotStride(std::uint32_t payloadSize) noexcept
Compute the physical stride between adjacent slots.
static std::size_t bufferSize(std::uint32_t count, std::uint32_t payloadSize)
Validate geometry and compute the complete mapping length.
mxlStatus publish(std::uint64_t index, mxlEventInfo const &info, std::uint8_t const *payload) noexcept
Atomically publish metadata and payload under a new sequence tag.
static constexpr auto MAX_INDEX
Largest index whose complete tag does not collide with EMPTY.
static void loadBytes(void *destination, std::uint64_t const *source, std::size_t size) noexcept
Copy shared words into private bytes using acquire loads.
ReadResult
Outcome of a bounded attempt to copy an entry without waiting.
@ Overwritten
The slot advanced past this index or changed while being copied.
@ Pending
The requested entry is not yet published or its index is unrepresentable.
@ Invalid
A stable snapshot contains unsupported or invalid metadata.
@ Ready
Metadata and payload form a validated snapshot of the requested entry.
std::uint32_t _count
Validated physical slot count.
std::uint32_t _payloadSize
Validated per-entry payload capacity in bytes.
static void storeBytes(std::uint64_t *destination, void const *source, std::size_t size) noexcept
Copy private bytes to shared words using release stores.
static constexpr auto STORAGE_VERSION
Version used by immutable ring and slot headers.
Event & slot(std::uint64_t index) const noexcept
Resolve a logical queue index to a physical slot modulo capacity.
static std::uint64_t load(std::uint64_t const &value) noexcept
Load a shared 64-bit word with acquire ordering, including from a read-only mapping.
struct mxlEventInfo_t mxlEventInfo
Metadata for one event queue entry, either a complete event or a fragment.
mxlStatus
MXL SDK Status codes.
@ MXL_ERR_OUT_OF_RANGE_TOO_LATE
constexpr auto EVENT_HEADER_VERSION
Version stored in the public mxlEventInfo metadata of every published entry.
std::size_t size() const noexcept
Returns the total number of writers that are registered in the DomainWatcher.
std::size_t count(uuids::uuid id) const noexcept
Returns the number of writers registered for flow id 'id'.
Fixed part of an event slot; its payload follows immediately in the mapping.
EventHeader header
Sequence, layout and metadata fields for this slot.