MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
PosixEventFlowWriter.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
7
9#include <cstdint>
10#include <atomic>
11#include <stdexcept>
12#include <uuid.h>
13#include <mxl/flow.h>
14#include <mxl/mxl.h>
15#include "mxl-internal/Flow.hpp"
17#include "mxl-internal/Sync.hpp"
19
20namespace mxl::lib
21{
22 PosixEventFlowWriter::PosixEventFlowWriter(FlowManager const& manager, uuids::uuid const& flowId, std::unique_ptr<EventFlowData>&& data,
23 std::shared_ptr<DomainWatcher> const& watcher)
24 : EventFlowWriter{flowId, manager.getDomain()}
25 , _flowData{std::move(data)}
26 , _open{false}
28 , _payload(_flowData->flowInfo()->config.event.eventPayloadSize)
29 , _watcher{watcher}
30 {
31 // The caller guarantees sole writer ownership, keeping the committed head
32 // stable while we restore the timestamp from an existing flow.
33 auto flow = _flowData->flow();
34 auto& runtime = flow->info.runtime;
35 auto head = EventRingBuffer::load(runtime.headIndex);
36 if (head != MXL_UNDEFINED_INDEX)
37 {
38 auto info = mxlEventInfo{};
39 if (_flowData->ring().read(head, info, _payload.data()) != EventRingBuffer::ReadResult::Ready)
40 {
41 throw std::runtime_error{"Invalid last committed event."};
42 }
43 _lastTimestamp = info.timestamp;
44 }
45 // A crashed writer may have published the next slot without advancing the head.
46 auto const next = (head == MXL_UNDEFINED_INDEX) ? 0 : head + 1;
47 auto info = mxlEventInfo{};
48 auto const result = _flowData->ring().read(next, info, _payload.data());
50 {
51 if (info.timestamp < _lastTimestamp)
52 {
53 throw std::runtime_error{"Invalid recovered event timestamp."};
54 }
55 _lastTimestamp = info.timestamp;
56 std::atomic_ref{runtime.lastWriteTime}.store(currentTime(Clock::TAI).value, std::memory_order_release);
57 std::atomic_ref{runtime.headIndex}.store(next, std::memory_order_release);
58 head = next;
59 }
60 else if (result != EventRingBuffer::ReadResult::Pending)
61 {
62 throw std::runtime_error{"Invalid next event during writer recovery."};
63 }
64 _watcher->addFlow(this, flowId);
65 if (head != MXL_UNDEFINED_INDEX)
66 {
67 // Also cover a crash after head advancement but before reader notification.
68 std::atomic_ref{flow->state.syncCounter}.fetch_add(1, std::memory_order_release);
69 wakeAll(&flow->state.syncCounter);
70 }
71 }
72
74 {
75 try
76 {
77 _watcher->removeFlow(this, _flowData->flowInfo()->config.common.id);
78 }
79 catch (...)
80 {
81 MXL_ERROR("Bug: exception while removing flow writer from watcher in destructor");
82 }
83 }
84
86 {
87 if (_flowData)
88 {
89 return *_flowData;
90 }
91 throw std::runtime_error{"No open flow."};
92 }
93
95 {
96 if (_flowData)
97 {
98 return *_flowData;
99 }
100 throw std::runtime_error{"No open flow."};
101 }
102
107
112
117
119 {
120 if (_open)
121 {
122 return MXL_ERR_INVALID_ARG;
123 }
124 *info = mxlEventInfo{
125 .version = EVENT_HEADER_VERSION,
126 .size = sizeof(mxlEventInfo),
127 .timestamp = 0,
128 .flags = 0,
129 .registryType = MXL_EVENT_REGISTRY_TYPE_MXL,
130 .dataItemType = {},
131 .eventSize = 0,
132 .offset = 0,
133 .complete = 1,
134 .reserved = {},
135 .index = MXL_UNDEFINED_INDEX,
136 };
137 *payload = _payload.data();
138 _open = true;
139 return MXL_STATUS_OK;
140 }
141
143 {
144 if (!_open)
145 {
146 return MXL_ERR_INVALID_ARG;
147 }
148 _open = false;
149 return MXL_STATUS_OK;
150 }
151
153 {
154 if (!_flowData)
155 {
156 return false;
157 }
158
159 return _flowData->isExclusive();
160 }
161
163 {
164 if (!_flowData)
165 {
166 return false;
167 }
168
169 return _flowData->makeExclusive();
170 }
171
173 {
174 if (!_open || info.version != EVENT_HEADER_VERSION || info.size != sizeof info || info.flags != 0 || info.timestamp < _lastTimestamp ||
175 info.eventSize > _flowData->flowInfo()->config.event.eventPayloadSize)
176 {
177 return MXL_ERR_INVALID_ARG;
178 }
179 auto flow = _flowData->flow();
180 auto& runtime = flow->info.runtime;
181 auto const head = EventRingBuffer::load(runtime.headIndex);
182 auto const index = head == MXL_UNDEFINED_INDEX ? 0 : head + 1;
183 auto const status = _flowData->ring().publish(index, info, _payload.data());
184 if (status != MXL_STATUS_OK)
185 {
186 return status;
187 }
188 std::atomic_ref{runtime.lastWriteTime}.store(currentTime(Clock::TAI).value, std::memory_order_release);
189 std::atomic_ref{runtime.headIndex}.store(index, std::memory_order_release);
191 _open = false;
192 std::atomic_ref{flow->state.syncCounter}.fetch_add(1, std::memory_order_release);
193 wakeAll(&flow->state.syncCounter);
194 return MXL_STATUS_OK;
195 }
196}
Internal shared-memory headers for discrete grains and event slots.
#define MXL_ERROR(...)
Definition Logging.hpp:67
POSIX event writer declaration and private publication state.
Backend contract for a single event producer with private write staging. Callers guarantee one writer...
@ Pending
The requested entry is not yet published or its index is unrepresentable.
@ Ready
Metadata and payload form a validated snapshot of the requested entry.
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.
constexpr mxlFlowInfo * flowInfo() noexcept
Definition FlowData.hpp:92
virtual bool isExclusive() const override
std::vector< std::uint8_t > _payload
Private staging buffer; publication copies only eventSize bytes.
virtual mxlFlowRuntimeInfo getFlowRuntimeInfo() const override
std::uint64_t _lastTimestamp
Last committed timestamp, restored from the ring on attachment.
virtual ~PosixEventFlowWriter() override
Unregister from the watcher and release mappings and the common flow lock.
virtual mxlStatus cancel() override
Discard an open write without advancing the queue or timestamp.
virtual FlowData const & getFlowData() const override
std::shared_ptr< DomainWatcher > _watcher
Watcher kept alive while the writer is registered.
virtual mxlStatus commit(mxlEventInfo const &info) override
Publish the staged bytes as one complete event or fragment.
std::unique_ptr< EventFlowData > _flowData
Owns common metadata, ring mappings and the common flow lock.
bool _open
True while one caller-owned write transaction is open.
virtual mxlStatus openEvent(mxlEventInfo *info, std::uint8_t **payload) override
Begin a write and reset metadata to the event defaults.
virtual mxlFlowInfo getFlowInfo() const override
PosixEventFlowWriter(FlowManager const &manager, uuids::uuid const &flowId, std::unique_ptr< EventFlowData > &&data, std::shared_ptr< DomainWatcher > const &watcher)
Attach the sole writer, recover an interrupted publication and restore timestamp ordering.
virtual mxlFlowConfigInfo getFlowConfigInfo() const override
Public flow reader, writer, payload and synchronization APIs.
struct mxlEventInfo_t mxlEventInfo
Metadata for one event queue entry, either a complete event or a fragment.
@ MXL_EVENT_REGISTRY_TYPE_MXL
Namespaced application DIT strings, such as x-mxl:example.
Definition flowinfo.h:130
struct mxlFlowRuntimeInfo_t mxlFlowRuntimeInfo
struct mxlFlowConfigInfo_t mxlFlowConfigInfo
struct mxlFlowInfo_t mxlFlowInfo
mxlStatus
MXL SDK Status codes.
Definition mxl.h:22
@ MXL_ERR_INVALID_ARG
Definition mxl.h:35
@ MXL_STATUS_OK
Definition mxl.h:23
void wakeAll(T const *in_addr)
Definition Sync.cpp:131
constexpr auto EVENT_HEADER_VERSION
Version stored in the public mxlEventInfo metadata of every published entry.
Definition Flow.hpp:61
#define MXL_UNDEFINED_INDEX
Definition rational.h:17
uint32_t version
Structure version, currently 1.
Definition flow.h:160
uint32_t size
Size of this structure.
Definition flow.h:161
uint32_t eventSize
Number of payload bytes published in this entry, possibly zero. For fragments this is the fragment si...
Definition flow.h:179
uint32_t flags
Reserved; must be zero.
Definition flow.h:163
uint64_t timestamp
Application supplied TAI timestamp in nanoseconds since the epoch; must not decrease within a flow.
Definition flow.h:162
mxlFlowRuntimeInfo runtime
Definition flowinfo.h:229
mxlFlowConfigInfo config
Definition flowinfo.h:227
auto const flowId
uint64_t index
gInfo flags
Mark the grain as invalid.
auto const timestamp
Definition test_time.cpp:61
auto const currentTime
Definition test_time.cpp:58