MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
mxl::lib::PosixContinuousFlowReader Class Referencefinal

#include <PosixContinuousFlowReader.hpp>

Inheritance diagram for mxl::lib::PosixContinuousFlowReader:
Collaboration diagram for mxl::lib::PosixContinuousFlowReader:

Public Member Functions

virtual mxlFlowConfigInfo getFlowConfigInfo () const override
virtual FlowData const & getFlowData () const override
virtual mxlFlowInfo getFlowInfo () const override
virtual mxlFlowRuntimeInfo getFlowRuntimeInfo () const override
virtual std::size_t getMaxReadLength () const override
virtual mxlStatus getSamples (std::uint64_t index, std::size_t count, mxlWrappedMultiBufferSlice &payloadBuffersSlices) override
virtual mxlStatus getSamples (std::uint64_t index, std::size_t count, Timepoint deadline, mxlWrappedMultiBufferSlice &payloadBuffersSlices) override
 PosixContinuousFlowReader (FlowManager const &manager, uuids::uuid const &flowId, std::unique_ptr< ContinuousFlowData > &&data)
virtual mxlStatus waitForSamples (std::uint64_t index, Timepoint deadline) const override
Public Member Functions inherited from mxl::lib::FlowReader
std::filesystem::path const & getDomain () const
uuids::uuid const & getId () const
virtual ~FlowReader ()

Protected Member Functions

virtual bool isFlowValid () const override
Protected Member Functions inherited from mxl::lib::ContinuousFlowReader
 FlowReader (uuids::uuid &&flowId, std::filesystem::path const &domain)
 FlowReader (uuids::uuid const &flowId, std::filesystem::path const &domain)
Protected Member Functions inherited from mxl::lib::FlowReader
bool checkPermissions () const
 FlowReader (uuids::uuid &&flowId, std::filesystem::path const &domain)
 FlowReader (uuids::uuid const &flowId, std::filesystem::path const &domain)

Private Member Functions

mxlStatus getSamplesImpl (std::uint64_t index, std::size_t count, mxlWrappedMultiBufferSlice *payloadBuffersSlices) const
mxlStatus getSamplesImpl (std::uint64_t index, std::size_t count, Timepoint deadline, mxlWrappedMultiBufferSlice *payloadBuffersSlices) const
bool isFlowValidImpl () const

Private Attributes

std::size_t _bufferLength
std::size_t _channelCount
std::unique_ptr< ContinuousFlowData > _flowData

Detailed Description

Implementation of a continuous flow reader based on POSIX shared memory.

Definition at line 22 of file PosixContinuousFlowReader.hpp.

Constructor & Destructor Documentation

◆ PosixContinuousFlowReader()

mxl::lib::PosixContinuousFlowReader::PosixContinuousFlowReader ( FlowManager const & manager,
uuids::uuid const & flowId,
std::unique_ptr< ContinuousFlowData > && data )
Parameters
[in]managerA referene to the flow manager used to obtain additional information about the flows context.

Definition at line 14 of file PosixContinuousFlowReader.cpp.

16 : ContinuousFlowReader{flowId, manager.getDomain()}
17 , _flowData{std::move(data)}
18 , _channelCount{_flowData->channelCount()}
19 , _bufferLength{_flowData->channelBufferLength()}
20 {
21 if (!checkPermissions())
22 {
23 throw std::filesystem::filesystem_error{"Flow is not accessible due to insufficient permissions.",
24 makeFlowDescriptorFilePath(getDomain(), uuids::to_string(getId())),
25 std::make_error_code(std::errc::permission_denied)};
26 }
27 }
bool checkPermissions() const
uuids::uuid const & getId() const
std::filesystem::path const & getDomain() const
std::unique_ptr< ContinuousFlowData > _flowData
std::filesystem::path makeFlowDescriptorFilePath(std::filesystem::path const &flowDirectory)
Definition PathUtils.cpp:23
auto const flowId

Member Function Documentation

◆ getFlowConfigInfo()

mxlFlowConfigInfo mxl::lib::PosixContinuousFlowReader::getFlowConfigInfo ( ) const
nodiscardoverridevirtual
See also
FlowReader::getFlowConfigInfo

Implements mxl::lib::FlowReader.

Definition at line 43 of file PosixContinuousFlowReader.cpp.

44 {
45 return getFlowData().flowInfo()->config;
46 }
constexpr mxlFlowInfo * flowInfo() noexcept
Definition FlowData.hpp:92
virtual FlowData const & getFlowData() const override
mxlFlowConfigInfo config
Definition flowinfo.h:227

◆ getFlowData()

FlowData const & mxl::lib::PosixContinuousFlowReader::getFlowData ( ) const
nodiscardoverridevirtual
See also
FlowReader::getFlowData

Implements mxl::lib::FlowReader.

Definition at line 29 of file PosixContinuousFlowReader.cpp.

30 {
31 if (_flowData)
32 {
33 return *_flowData;
34 }
35 throw std::runtime_error("No open flow.");
36 }

◆ getFlowInfo()

mxlFlowInfo mxl::lib::PosixContinuousFlowReader::getFlowInfo ( ) const
nodiscardoverridevirtual
See also
FlowReader::getFlowInfo

Implements mxl::lib::FlowReader.

Definition at line 38 of file PosixContinuousFlowReader.cpp.

39 {
40 return *getFlowData().flowInfo();
41 }

◆ getFlowRuntimeInfo()

mxlFlowRuntimeInfo mxl::lib::PosixContinuousFlowReader::getFlowRuntimeInfo ( ) const
nodiscardoverridevirtual
See also
FlowReader::getFlowRuntimeInfo

Implements mxl::lib::FlowReader.

Definition at line 48 of file PosixContinuousFlowReader.cpp.

49 {
50 return getFlowData().flowInfo()->runtime;
51 }
mxlFlowRuntimeInfo runtime
Definition flowinfo.h:229

◆ getMaxReadLength()

std::size_t mxl::lib::PosixContinuousFlowReader::getMaxReadLength ( ) const
nodiscardoverridevirtual
See also
ContinuousFlowReader::getMaxReadLength

Implements mxl::lib::ContinuousFlowReader.

Definition at line 53 of file PosixContinuousFlowReader.cpp.

54 {
55 return _bufferLength / 2U;
56 }

◆ getSamples() [1/2]

mxlStatus mxl::lib::PosixContinuousFlowReader::getSamples ( std::uint64_t index,
std::size_t count,
mxlWrappedMultiBufferSlice & payloadBuffersSlices )
overridevirtual
See also
ContinuousFlowReader::getSamples

Implements mxl::lib::ContinuousFlowReader.

Definition at line 91 of file PosixContinuousFlowReader.cpp.

92 {
93 if (_flowData)
94 {
95 auto const result = getSamplesImpl(index, count, &payloadBuffersSlices);
96
97 // If we were too early it could very well be that we're operating
98 // on a stale flow, so we use the opportunity to check whether it's
99 // valid.
100 return ((result != MXL_ERR_OUT_OF_RANGE_TOO_EARLY) || isFlowValidImpl()) ? result : MXL_ERR_FLOW_INVALID;
101 }
102
103 return MXL_ERR_UNKNOWN;
104 }
mxlStatus getSamplesImpl(std::uint64_t index, std::size_t count, mxlWrappedMultiBufferSlice *payloadBuffersSlices) const
@ MXL_ERR_FLOW_INVALID
Definition mxl.h:41
@ MXL_ERR_UNKNOWN
Definition mxl.h:27
@ MXL_ERR_OUT_OF_RANGE_TOO_EARLY
Definition mxl.h:31
std::size_t count(uuids::uuid id) const noexcept
Returns the number of writers registered for flow id 'id'.
uint64_t index

◆ getSamples() [2/2]

mxlStatus mxl::lib::PosixContinuousFlowReader::getSamples ( std::uint64_t index,
std::size_t count,
Timepoint deadline,
mxlWrappedMultiBufferSlice & payloadBuffersSlices )
overridevirtual
See also
ContinuousFlowReader::getSamples

Implements mxl::lib::ContinuousFlowReader.

Definition at line 74 of file PosixContinuousFlowReader.cpp.

76 {
77 if (_flowData)
78 {
79 auto const result = getSamplesImpl(index, count, deadline, &payloadBuffersSlices);
80
81 // If we were ultimately too early, even with blocking for a
82 // certain amount of time it could very well be that we're
83 // operating on a stale flow, so we use the opportunity to check
84 // whether it's valid.
85 return ((result != MXL_ERR_OUT_OF_RANGE_TOO_EARLY) || isFlowValidImpl()) ? result : MXL_ERR_FLOW_INVALID;
86 }
87
88 return MXL_ERR_UNKNOWN;
89 }

◆ getSamplesImpl() [1/2]

mxlStatus mxl::lib::PosixContinuousFlowReader::getSamplesImpl ( std::uint64_t index,
std::size_t count,
mxlWrappedMultiBufferSlice * payloadBuffersSlices ) const
private

Implementation of the various forms of getSamples() that can also be used by other methods that have previously asserted that we're operating on a valid flow (i.e. that _flowData is a valid pointer).

Definition at line 124 of file PosixContinuousFlowReader.cpp.

126 {
127 if (auto const headIndex = _flowData->flowInfo()->runtime.headIndex; index <= headIndex)
128 {
129 auto const minIndex = (headIndex >= (_bufferLength / 2U)) ? (headIndex - (_bufferLength / 2U)) : std::uint64_t{0};
130
131 if ((index >= minIndex) && ((index - minIndex) >= count))
132 {
133 if (payloadBuffersSlices != nullptr)
134 {
135 auto const startOffset = (index + _bufferLength - count) % _bufferLength;
136 auto const endOffset = (index % _bufferLength);
137
138 auto const firstLength = (startOffset < endOffset) ? count : _bufferLength - startOffset;
139 auto const secondLength = count - firstLength;
140
141 auto const baseBufferPtr = static_cast<std::uint8_t const*>(_flowData->channelData());
142 auto const sampleWordSize = _flowData->sampleWordSize();
143
144 payloadBuffersSlices->base.fragments[0].pointer = baseBufferPtr + sampleWordSize * startOffset;
145 payloadBuffersSlices->base.fragments[0].size = sampleWordSize * firstLength;
146
147 payloadBuffersSlices->base.fragments[1].pointer = baseBufferPtr;
148 payloadBuffersSlices->base.fragments[1].size = sampleWordSize * secondLength;
149
150 payloadBuffersSlices->stride = sampleWordSize * _bufferLength;
151 payloadBuffersSlices->count = _channelCount;
152 }
153
154 return MXL_STATUS_OK;
155 }
156
158 }
159
161 }
@ MXL_STATUS_OK
Definition mxl.h:23
@ MXL_ERR_OUT_OF_RANGE_TOO_LATE
Definition mxl.h:30
size_t size
Definition flow.h:55
void const * pointer
Definition flow.h:53
mxlBufferSlice fragments[2]
Definition flow.h:77
mxlWrappedBufferSlice base
Definition flow.h:97

◆ getSamplesImpl() [2/2]

mxlStatus mxl::lib::PosixContinuousFlowReader::getSamplesImpl ( std::uint64_t index,
std::size_t count,
Timepoint deadline,
mxlWrappedMultiBufferSlice * payloadBuffersSlices ) const
private

Implementation of the blocking form of getSamples() and waitForSamples() that can also be used by other methods that have previously asserted that we're operating on a valid flow (i.e. that _flowData is a valid pointer).

Definition at line 163 of file PosixContinuousFlowReader.cpp.

165 {
166 auto const flow = _flowData->flow();
167 auto const syncObject = std::atomic_ref{flow->state.syncCounter};
168 while (true)
169 {
170 auto const previousSyncCounter = syncObject.load(std::memory_order_acquire);
171 auto const result = getSamplesImpl(index, count, payloadBuffersSlices);
172 // NOTE: Before C++26 there is no way to access the address of the object wrapped
173 // by an atomic_ref. If there were it would be much more appropriate to pass
174 // syncObject by reference here and only unwrap the underlying integer in the
175 // implementation of waitUntilChanged.
176 if ((result != MXL_ERR_OUT_OF_RANGE_TOO_EARLY) || !waitUntilChanged(&flow->state.syncCounter, previousSyncCounter, deadline))
177 {
178 return result;
179 }
180 }
181 }
bool waitUntilChanged(T const *in_addr, T in_expected, Timepoint in_deadline)
Definition Sync.cpp:68

◆ isFlowValid()

bool mxl::lib::PosixContinuousFlowReader::isFlowValid ( ) const
nodiscardoverrideprotectedvirtual
See also
FlowReader::isFlowValid

Implements mxl::lib::FlowReader.

Definition at line 106 of file PosixContinuousFlowReader.cpp.

107 {
108 return _flowData && isFlowValidImpl();
109 }

◆ isFlowValidImpl()

bool mxl::lib::PosixContinuousFlowReader::isFlowValidImpl ( ) const
nodiscardprivate

Implementation of isFlowValid() that can be used by other methods that have previously asserted that we're operating on a valid flow (i.e. that _flowData is a valid pointer).

Definition at line 111 of file PosixContinuousFlowReader.cpp.

112 {
113 auto const flowState = _flowData->flowState();
114 auto const flowDataPath = makeFlowDataFilePath(getDomain(), to_string(getId()));
115
116 struct stat st;
117 if (::stat(flowDataPath.string().c_str(), &st) != 0)
118 {
119 return false;
120 }
121 return (st.st_ino == flowState->inode);
122 }
std::filesystem::path makeFlowDataFilePath(std::filesystem::path const &flowDirectory)
Definition PathUtils.cpp:17

◆ waitForSamples()

mxlStatus mxl::lib::PosixContinuousFlowReader::waitForSamples ( std::uint64_t index,
Timepoint deadline ) const
overridevirtual
See also
ContinuousFlowReader::waitForSamples

Implements mxl::lib::ContinuousFlowReader.

Definition at line 58 of file PosixContinuousFlowReader.cpp.

59 {
60 if (_flowData)
61 {
62 auto const result = getSamplesImpl(index, 0U, deadline, nullptr);
63
64 // If we were ultimately too early, even with blocking for a
65 // certain amount of time it could very well be that we're
66 // operating on a stale flow, so we use the opportunity to check
67 // whether it's valid.
68 return ((result != MXL_ERR_OUT_OF_RANGE_TOO_EARLY) || isFlowValidImpl()) ? result : MXL_ERR_FLOW_INVALID;
69 }
70
71 return MXL_ERR_UNKNOWN;
72 }

Member Data Documentation

◆ _bufferLength

std::size_t mxl::lib::PosixContinuousFlowReader::_bufferLength
private

Cached copy of the length of the per channel buffers from mxlFlowInfo.

Definition at line 96 of file PosixContinuousFlowReader.hpp.

◆ _channelCount

std::size_t mxl::lib::PosixContinuousFlowReader::_channelCount
private

Cached copy of the numer of channels from mxlFlowInfo.

Definition at line 94 of file PosixContinuousFlowReader.hpp.

◆ _flowData

std::unique_ptr<ContinuousFlowData> mxl::lib::PosixContinuousFlowReader::_flowData
private

Definition at line 92 of file PosixContinuousFlowReader.hpp.


The documentation for this class was generated from the following files: