MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
SharedMemory.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 <cerrno>
6#include <cstring>
7#include <array>
8#include <stdexcept>
9#include <system_error>
10#include <fcntl.h>
11#include <unistd.h>
12#include <utime.h>
13#include <sys/file.h>
14#include <sys/mman.h>
15#include <sys/stat.h>
16#include <sys/types.h>
17#include <mxl/platform.h>
19
20namespace mxl::lib
21{
23 SharedMemoryBase::SharedMemoryBase(char const* path, AccessMode mode, std::size_t payloadSize, LockMode lockMode)
25 {
26 constexpr auto const OMODE_CREATE = O_EXCL | O_CREAT | O_RDWR | O_CLOEXEC;
27 constexpr auto const OMODE_RO = O_RDONLY | O_CLOEXEC;
28 constexpr auto const OMODE_RW = O_RDWR | O_CLOEXEC;
29
30 if ((mode == AccessMode::CREATE_READ_WRITE) && ((_fd = ::open(path, OMODE_CREATE, 0664)) != -1))
31 {
32#ifdef __linux__
33 // Ensure the file is large enough to hold the shared data
34 if (auto error = ::posix_fallocate(_fd, 0, payloadSize); error != 0)
35 {
36#elif defined __APPLE__
37 if (::ftruncate(_fd, payloadSize) < 0)
38 {
39 auto const error = errno;
40#endif
41 // No need to close _fd, as the destructor *will* be called,
42 // because we delegated to the default constructor.
43 throw std::system_error(error, std::generic_category(), "Could not resize shared memory segment.");
44 }
46 }
47 else
48 {
49 if ((_fd = ::open(path, (mode == AccessMode::READ_ONLY) ? OMODE_RO : OMODE_RW)) == -1)
50 {
51 throw std::system_error(errno, std::generic_category(), "Could not open shared memory segment.");
52 }
54 }
55
56 if (lockMode != LockMode::None && mode != AccessMode::READ_ONLY)
57 {
58 auto lockFlags = LOCK_NB;
59
60 if (lockMode == LockMode::Exclusive)
61 {
62 lockFlags |= LOCK_EX;
64 }
65 else
66 {
67 lockFlags |= LOCK_SH;
69 }
70
71 if (::flock(_fd, lockFlags) == -1)
72 {
73 throw std::system_error{errno, std::system_category(), fmt::format("Failed to lock shared memory segment.")};
74 }
75 }
76 else
77 {
79 }
80
81 if (struct stat statBuf; ::fstat(_fd, &statBuf) != -1)
82 {
83 if (static_cast<std::size_t>(statBuf.st_size) >= payloadSize)
84 {
85 auto const shared_data_buffer = ::mmap(
86 nullptr, statBuf.st_size, PROT_READ | ((mode != AccessMode::READ_ONLY) ? PROT_WRITE : 0), MAP_FILE | MAP_SHARED, _fd, 0);
87 if (shared_data_buffer != MAP_FAILED)
88 {
89 _data = shared_data_buffer;
90 _mappedSize = statBuf.st_size;
91 }
92 else
93 {
94 throw std::system_error(errno, std::generic_category(), "Could not map shared memory segment.");
95 }
96 }
97 else
98 {
99 throw std::system_error(ENOMEM, std::generic_category(), "Shared memory segment does not meet the minimum size requirements.");
100 }
101 }
102 else
103 {
104 throw std::system_error(errno, std::generic_category(), "Could not determine size of shared memory segment.");
105 }
106 }
107
110 {
111 if (_data != nullptr)
112 {
113 (void)::munmap(_data, _mappedSize);
114 }
115
116 if (_fd != -1)
117 {
118 (void)::flock(_fd, LOCK_UN);
119 (void)::close(_fd);
120 }
121 }
124 {
125 // Update the file times
126 auto const times = std::array<std::timespec, 2>{
127 {{0, UTIME_NOW}, {0, (accessMode() == AccessMode::READ_ONLY) ? UTIME_OMIT : UTIME_NOW}}
128 };
129
130 if (::futimens(_fd, times.data()) == -1)
131 {
132 MXL_ERROR("Failed to update file times");
133 }
134 }
135
137 {
139 {
140 // This is a no-op
141 return true;
142 }
143
144 if (::flock(_fd, LOCK_EX | LOCK_NB) < 0)
145 {
146 auto const error = errno;
147 switch (error)
148 {
149 case EWOULDBLOCK:
150 // Another process (or this process on another fd) is still holding a shared lock on the file
151 return false;
152 case EBADF: throw std::runtime_error("Tried to upgrade a lock of an invalid shared memory segment");
153 default: throw std::system_error(error, std::system_category(), fmt::format("Failed to upgrade lock of shared memory file"));
154 }
155 }
156
158 return true;
159 }
160}
#define MXL_ERROR(...)
Definition Logging.hpp:67
constexpr AccessMode accessMode() const noexcept
constexpr SharedMemoryBase() noexcept
#define MXL_EXPORT
Definition platform.h:9