MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
PosixDiscreteFlowWriter.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
5#include <cstdint>
6#include <algorithm>
7#include <stdexcept>
8#include <fcntl.h>
9#include <uuid.h>
10#include <sys/stat.h>
11#include <mxl/flow.h>
12#include <mxl/mxl.h>
13#include <mxl/time.h>
14#include "mxl-internal/Flow.hpp"
16#include "mxl-internal/Sync.hpp"
18
19namespace mxl::lib
20{
21 PosixDiscreteFlowWriter::PosixDiscreteFlowWriter(FlowManager const& manager, uuids::uuid const& flowId, std::unique_ptr<DiscreteFlowData>&& data,
22 std::shared_ptr<DomainWatcher> const& watcher)
23 : DiscreteFlowWriter{flowId, manager.getDomain()}
24 , _flowData{std::move(data)}
27 , _watcher(watcher)
28 {
29 _watcher->addFlow(this, flowId);
30 }
31
33 {
34 try
35 {
36 _watcher->removeFlow(this, _flowData->flowInfo()->config.common.id);
37 }
38 catch (...)
39 {
40 MXL_ERROR("Bug: exception while removing flow writer from watcher in destructor");
41 }
42 }
43
45 {
46 if (_flowData)
47 {
48 return *_flowData;
49 }
50 throw std::runtime_error("No open flow.");
51 }
52
54 {
55 if (_flowData)
56 {
57 return *_flowData;
58 }
59 throw std::runtime_error("No open flow.");
60 }
61
66
71
76
78 {
79 auto& flowData = static_cast<DiscreteFlowData const&>(getFlowData());
80 auto const offset = in_index % flowData.flowInfo()->config.discrete.grainCount;
81 return flowData.grainAt(offset)->header.info;
82 }
83
84 mxlStatus PosixDiscreteFlowWriter::openGrain(std::uint64_t in_index, mxlGrainInfo* out_grainInfo, std::uint8_t** out_payload)
85 {
86 if (!_flowData)
87 {
88 return MXL_ERR_UNKNOWN;
89 }
90
92 {
94 }
95
96 if ((_lastCommittedIndex != MXL_UNDEFINED_INDEX) && (in_index > (_lastCommittedIndex + 1U)))
97 {
98 // We have to invalidate any grain that would've been written to if the write was continuous. This is to ensure that the reader doesn't
99 // read stale data from a previous write. At most grainCount - 1 grains are invalidated, because grain in_index - grainCount shares its
100 // ring buffer entry with in_index. That entry can already hold the grain being opened: a fabrics initiator wrote it into a target
101 // flow, or an earlier commit of this incomplete grain did.
102 auto const grainCount = _flowData->flowInfo()->config.discrete.grainCount;
103 auto const skippedCount = in_index - _lastCommittedIndex - 1U;
104 auto const invalidatedCount = std::min<std::uint64_t>(skippedCount, grainCount - 1U);
105 auto const firstInvalidatedIndex = in_index - invalidatedCount;
106
107 for (auto missingIndex = firstInvalidatedIndex; missingIndex < in_index; ++missingIndex)
108 {
109 auto const missingOffset = missingIndex % grainCount;
110 auto const missingGrain = _flowData->grainAt(missingOffset);
111 missingGrain->header.info.index = missingIndex;
112 missingGrain->header.info.validSlices = 0;
113 missingGrain->header.info.flags |= MXL_GRAIN_FLAG_INVALID;
114 }
115 }
116
117 auto offset = in_index % _flowData->flowInfo()->config.discrete.grainCount;
118 auto const grain = _flowData->grainAt(offset);
119
120 // If the ring buffer entry already holds this grain index, keep its header. This is the case when an incomplete grain is reopened,
121 // and on a fabrics target, where the remote initiator writes the grain header and the target library sets validSlices before the
122 // application opens the grain to commit it. Otherwise the entry still holds an older grain and its state is reset.
123 if (grain->header.info.index != in_index)
124 {
125 grain->header.info.index = in_index;
126 grain->header.info.flags &= ~MXL_GRAIN_FLAG_INVALID;
127 grain->header.info.validSlices = 0;
128 }
129 *out_grainInfo = grain->header.info;
130 *out_payload = reinterpret_cast<std::uint8_t*>(&grain->header + 1);
131 _currentIndex = in_index;
132 return MXL_STATUS_OK;
133 }
134
140
142 {
143 if (!_flowData)
144 {
145 return false;
146 }
147
148 return _flowData->isExclusive();
149 }
150
152 {
153 if (!_flowData)
154 {
155 return false;
156 }
157
158 return _flowData->makeExclusive();
159 }
160
162 {
163 if (_flowData)
164 {
165 if (mxlGrainInfo.index != _currentIndex)
166 {
167 return MXL_ERR_INVALID_ARG;
168 }
169
170 auto const flow = _flowData->flow();
171 auto const offset = _currentIndex % flow->info.config.discrete.grainCount;
172
173 // Readers reject a header with these values, so refuse to publish one.
174 if ((mxlGrainInfo.grainSize > _flowData->grainPayloadCapacityAt(offset)) || (mxlGrainInfo.validSlices > mxlGrainInfo.totalSlices))
175 {
176 return MXL_ERR_INVALID_ARG;
177 }
178
179 flow->info.runtime.headIndex = _currentIndex;
180 *_flowData->grainInfoAt(offset) = mxlGrainInfo;
181 flow->info.runtime.lastWriteTime = currentTime(mxl::lib::Clock::TAI).value;
182
183 // If the grain is complete, reset the current index of the flow writer.
184 if (mxlGrainInfo.validSlices == mxlGrainInfo.totalSlices)
185 {
188 }
189
190 // Let readers know that the head has moved or that new data is available in a partial grain
191 flow->state.syncCounter++;
192 wakeAll(&flow->state.syncCounter);
193
194 return MXL_STATUS_OK;
195 }
196 return MXL_ERR_UNKNOWN;
197 }
198}
Internal shared-memory headers for discrete grains and event slots.
#define MXL_ERROR(...)
Definition Logging.hpp:67
constexpr mxlFlowInfo * flowInfo() noexcept
Definition FlowData.hpp:92
virtual mxlFlowConfigInfo getFlowConfigInfo() const override
virtual mxlFlowInfo getFlowInfo() const override
std::unique_ptr< DiscreteFlowData > _flowData
virtual mxlStatus openGrain(std::uint64_t in_index, mxlGrainInfo *out_grainInfo, std::uint8_t **out_payload) override
virtual bool isExclusive() const override
PosixDiscreteFlowWriter(FlowManager const &manager, uuids::uuid const &flowId, std::unique_ptr< DiscreteFlowData > &&data, DomainWatcher::ptr const &watcher)
virtual mxlFlowRuntimeInfo getFlowRuntimeInfo() const override
virtual mxlStatus commit(mxlGrainInfo const &mxlGrainInfo) override
virtual mxlGrainInfo getGrainInfo(std::uint64_t in_index) const override
virtual FlowData & getFlowData() override
Public flow reader, writer, payload and synchronization APIs.
struct mxlGrainInfo_t mxlGrainInfo
#define MXL_GRAIN_FLAG_INVALID
Definition flow.h:33
struct mxlFlowRuntimeInfo_t mxlFlowRuntimeInfo
struct mxlFlowConfigInfo_t mxlFlowConfigInfo
struct mxlFlowInfo_t mxlFlowInfo
mxlStatus
MXL SDK Status codes.
Definition mxl.h:22
@ MXL_ERR_UNKNOWN
Definition mxl.h:27
@ 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
#define MXL_UNDEFINED_INDEX
Definition rational.h:17
mxlDiscreteFlowConfigInfo discrete
Definition flowinfo.h:183
mxlFlowRuntimeInfo runtime
Definition flowinfo.h:229
mxlFlowConfigInfo config
Definition flowinfo.h:227
auto const flowId
auto const currentTime
Definition test_time.cpp:58