MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
PosixEventFlowReader.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 <ctime>
11#include <array>
12#include <atomic>
13#include <filesystem>
14#include <stdexcept>
15#include <fcntl.h>
16#include <unistd.h>
17#include <uuid.h>
18#include <sys/stat.h>
19#include <mxl/flow.h>
20#include <mxl/mxl.h>
21#include <mxl/time.h>
22#include "mxl-internal/Flow.hpp"
26#include "mxl-internal/Sync.hpp"
27
28namespace mxl::lib
29{
30 namespace
31 {
37 bool updateFileAccessTime(int fd) noexcept
38 {
39 auto const times = std::array<timespec, 2>{
40 {{.tv_sec = 0, .tv_nsec = UTIME_NOW}, {.tv_sec = 0, .tv_nsec = UTIME_OMIT}}
41 };
42 return (::futimens(fd, times.data()) == 0);
43 }
44 }
45
46 PosixEventFlowReader::PosixEventFlowReader(FlowManager const& manager, uuids::uuid const& flowId, std::unique_ptr<EventFlowData>&& data)
47 : EventFlowReader{flowId, manager.getDomain()}
48 , _flowData{std::move(data)}
49 , _accessFileFd{-1}
50 , _nextIndex{0}
51 , _snapshot(_flowData->flowInfo()->config.event.eventPayloadSize)
52 {
53 _nextIndex = _flowData->oldestIndex();
54 auto const accessFile = makeFlowAccessFilePath(manager.getDomain(), to_string(flowId));
55 _accessFileFd = ::open(accessFile.string().c_str(), O_RDWR | O_CLOEXEC);
56
57 // Opening the access file may fail if the domain is in a read only volume.
58 // we can still execute properly but the 'lastReadTime' will never be updated.
59 // Ignore failures.
60 }
61
63 {
64 if (_accessFileFd != -1)
65 {
66 if (::close(_accessFileFd) != 0)
67 {
68 auto const error = errno;
69 MXL_ERROR("Failed to close access file fd: {}", std::strerror(error));
70 }
71 _accessFileFd = -1;
72 }
73 }
74
76 {
77 if (_flowData)
78 {
79 return *_flowData;
80 }
81 throw std::runtime_error{"No open flow."};
82 }
83
88
93
98
99 mxlStatus PosixEventFlowReader::getEvent(Timepoint deadline, mxlEventInfo* info, std::uint8_t** payload)
100 {
101 auto flow = _flowData->flow();
102 auto sync = std::atomic_ref{flow->state.syncCounter};
103 while (true)
104 {
105 auto const previous = sync.load(std::memory_order_acquire);
106 auto const oldest = _flowData->oldestIndex();
107 if (_nextIndex < oldest)
108 {
109 _nextIndex = oldest;
111 }
112 auto received = mxlEventInfo{};
113 auto const result = _flowData->ring().read(_nextIndex, received, _snapshot.data());
115 {
116 ++_nextIndex;
117 *info = received;
118 *payload = _snapshot.data();
119 (void)updateFileAccessTime(_accessFileFd);
120 return MXL_STATUS_OK;
121 }
123 {
124 _nextIndex = std::max(_nextIndex + 1, _flowData->oldestIndex());
126 }
128 {
130 }
131 if (!isFlowValidImpl())
132 {
134 }
135 if ((currentTime(Clock::Realtime) >= deadline) || !waitUntilChanged(&flow->state.syncCounter, previous, deadline))
136 {
138 }
139 }
140 }
141
143 {
144 return _flowData && isFlowValidImpl();
145 }
146
148 {
149 auto const flowState = _flowData->flowState();
150 auto const flowDataPath = makeFlowDataFilePath(getDomain(), to_string(getId()));
151
152 using FileStatus = struct stat;
153 auto st = FileStatus{};
154 if (::stat(flowDataPath.string().c_str(), &st) != 0)
155 {
156 return false;
157 }
158 return (st.st_ino == flowState->inode);
159 }
160}
Internal shared-memory headers for discrete grains and event slots.
#define MXL_ERROR(...)
Definition Logging.hpp:67
Paths and filenames utilities used by the MXL SDK.
POSIX event reader declaration and reader-owned snapshot state.
Backend contract for reading complete event entries or fragments in queue order. Each reader has one ...
@ Overwritten
The slot advanced past this index or changed while being copied.
@ Invalid
A stable snapshot contains unsupported or invalid metadata.
@ Ready
Metadata and payload form a validated snapshot of the requested entry.
constexpr mxlFlowInfo * flowInfo() noexcept
Definition FlowData.hpp:92
std::filesystem::path const & getDomain() const
uuids::uuid const & getId() const
std::filesystem::path const & getDomain() const
virtual mxlFlowInfo getFlowInfo() const override
virtual FlowData const & getFlowData() const override
std::vector< std::uint8_t > _snapshot
Payload storage reused on the next read attempt.
virtual ~PosixEventFlowReader() override
Close the access descriptor and release the snapshot and shared mappings.
int _accessFileFd
Access notification descriptor, or -1 if unavailable.
std::unique_ptr< EventFlowData > _flowData
Owned common metadata and read-only event mappings.
PosixEventFlowReader(FlowManager const &manager, uuids::uuid const &flowId, std::unique_ptr< EventFlowData > &&data)
Attach a reader at the oldest retained entry.
virtual mxlFlowConfigInfo getFlowConfigInfo() const override
virtual mxlStatus getEvent(Timepoint deadline, mxlEventInfo *info, std::uint8_t **payload) override
Copy the next available entry and advance this reader's cursor.
virtual bool isFlowValid() const override
virtual mxlFlowRuntimeInfo getFlowRuntimeInfo() const override
std::uint64_t _nextIndex
Next queue entry for this reader's owning thread.
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.
struct mxlFlowRuntimeInfo_t mxlFlowRuntimeInfo
struct mxlFlowConfigInfo_t mxlFlowConfigInfo
struct mxlFlowInfo_t mxlFlowInfo
mxlStatus
MXL SDK Status codes.
Definition mxl.h:22
@ MXL_ERR_FLOW_INVALID
Definition mxl.h:41
@ MXL_ERR_OUT_OF_RANGE_TOO_EARLY
Definition mxl.h:31
@ MXL_STATUS_OK
Definition mxl.h:23
@ MXL_ERR_OUT_OF_RANGE_TOO_LATE
Definition mxl.h:30
std::filesystem::path makeFlowAccessFilePath(std::filesystem::path const &flowDirectory)
Definition PathUtils.cpp:29
std::filesystem::path makeFlowDataFilePath(std::filesystem::path const &flowDirectory)
Definition PathUtils.cpp:17
bool waitUntilChanged(T const *in_addr, T in_expected, Timepoint in_deadline)
Definition Sync.cpp:68
mxlFlowRuntimeInfo runtime
Definition flowinfo.h:229
mxlFlowConfigInfo config
Definition flowinfo.h:227
auto const flowId
auto const currentTime
Definition test_time.cpp:58