#include <SharedMemory.hpp>
Definition at line 29 of file SharedMemory.hpp.
◆ LockType
◆ SharedMemoryBase() [1/4]
| mxl::lib::SharedMemoryBase::SharedMemoryBase |
( |
| ) |
|
|
constexprprotectednoexcept |
◆ SharedMemoryBase() [2/4]
| mxl::lib::SharedMemoryBase::SharedMemoryBase |
( |
SharedMemoryBase && | other | ) |
|
|
constexprprotectednoexcept |
Definition at line 225 of file SharedMemory.hpp.
227 {
229 }
constexpr SharedMemoryBase() noexcept
constexpr void swap(SharedMemoryBase &other) noexcept
◆ SharedMemoryBase() [3/4]
| mxl::lib::SharedMemoryBase::SharedMemoryBase |
( |
SharedMemoryBase const & | other | ) |
|
|
protecteddelete |
◆ SharedMemoryBase() [4/4]
Create a shared memory mapping with the specified payload size, or alterntively open it for reading and writing
- Parameters
-
| path | The memory mapping path |
| mode | The memory mapping access mode |
| payloadSize | The minimum expected size of the shared memory |
- Exceptions
-
| If | opening or creating the shared memory segment fails. |
Definition at line 23 of file SharedMemory.cpp.
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
31 {
32#ifdef __linux__
33
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
42
43 throw std::system_error(error, std::generic_category(), "Could not resize shared memory segment.");
44 }
46 }
◆ ~SharedMemoryBase()
| MXL_EXPORT SharedMemoryBase::~SharedMemoryBase |
( |
| ) |
|
|
protected |
Destructor.
Definition at line 109 of file SharedMemory.cpp.
110 {
111 if (
_data !=
nullptr)
112 {
114 }
115
117 {
118 (void)::flock(
_fd, LOCK_UN);
120 }
121 }
◆ accessMode()
| AccessMode mxl::lib::SharedMemoryBase::accessMode |
( |
| ) |
const |
|
constexprnoexcept |
Return the (normalized) access mode the segment is operating in.
- Note
- Please note that the returned value is undefined for segments that don't currently represent a valid mapping.
Definition at line 246 of file SharedMemory.hpp.
◆ cdata()
| void const * mxl::lib::SharedMemoryBase::cdata |
( |
| ) |
const |
|
constexprprotectednoexcept |
Accessor for the mapped data
- Returns
- the mapped data, or the null pointer if this instance does not currently represent a mapped segment.
Definition at line 283 of file SharedMemory.hpp.
◆ created()
| bool mxl::lib::SharedMemoryBase::created |
( |
| ) |
const |
|
constexprnoexcept |
Return whether or the underlying file was created by this instance.
Definition at line 251 of file SharedMemory.hpp.
◆ data() [1/2]
| void const * mxl::lib::SharedMemoryBase::data |
( |
| ) |
const |
|
constexprprotectednoexcept |
Accessor for the mapped data
- Returns
- the mapped data, or the null pointer if this instance does not currently represent a mapped segment.
Definition at line 278 of file SharedMemory.hpp.
◆ data() [2/2]
| void * mxl::lib::SharedMemoryBase::data |
( |
| ) |
|
|
constexprprotectednoexcept |
Accessor for the mapped data
- Returns
- the mapped data, or the null pointer if this instance does not currently represent a mapped segment.
Definition at line 273 of file SharedMemory.hpp.
◆ isExclusive()
| bool mxl::lib::SharedMemoryBase::isExclusive |
( |
| ) |
const |
|
nodiscardconstexprprotected |
Check if the advisory lock for the shared memory file mapped by this objects is exclusive or shared.
- Exceptions
-
| If | there is no lock at all, because either this is a read-only mapping of the memory, or because the object is in an invalid state. |
Definition at line 288 of file SharedMemory.hpp.
◆ isValid()
| bool mxl::lib::SharedMemoryBase::isValid |
( |
| ) |
const |
|
constexprnoexcept |
Return whether or not this instance currently represents a valid mapping.
Definition at line 231 of file SharedMemory.hpp.
232 {
233 return (
_data !=
nullptr);
234 }
◆ makeExclusive()
| bool SharedMemoryBase::makeExclusive |
( |
| ) |
|
|
protected |
Upgrade the lock held by the process for the shared memory file that is mapped by this object to an exclusive lock.
- Returns
- Returns true if the upgrade was successful. Returns false if another process also holds a shared lock and acquiring an exclusive lock would be a blocking operation.
- Exceptions
-
| std::runtime_error | If this is a read-only mapping of the memory or if the object is in an invalid state. |
Definition at line 136 of file SharedMemory.cpp.
137 {
139 {
140
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
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 }
◆ mappedSize()
| std::size_t mxl::lib::SharedMemoryBase::mappedSize |
( |
| ) |
const |
|
constexprnoexcept |
Return the size of the memory mapping in bytes.
Definition at line 241 of file SharedMemory.hpp.
◆ operator bool()
| mxl::lib::SharedMemoryBase::operator bool |
( |
| ) |
const |
|
explicitconstexprnoexcept |
Return whether or not this instance currently represents a valid mapping.
- See also
- isValid()
Definition at line 236 of file SharedMemory.hpp.
237 {
239 }
constexpr bool isValid() const noexcept
◆ swap()
Definition at line 256 of file SharedMemory.hpp.
257 {
258
259 constexpr auto const cx_swap = [](auto& lhs, auto& rhs) constexpr noexcept
260 {
261 auto temp = lhs;
262 lhs = rhs;
263 rhs = temp;
264 };
265
266 cx_swap(
_fd, other._fd);
267 cx_swap(
_mode, other._mode);
268 cx_swap(
_data, other._data);
271 }
◆ touch()
| void SharedMemoryBase::touch |
( |
| ) |
|
Update the attributes of the file
Definition at line 123 of file SharedMemory.cpp.
124 {
125
126 auto const times = std::array<std::timespec, 2>{
128 };
129
130 if (::futimens(
_fd, times.data()) == -1)
131 {
132 MXL_ERROR(
"Failed to update file times");
133 }
134 }
constexpr AccessMode accessMode() const noexcept
◆ _data
| void* mxl::lib::SharedMemoryBase::_data |
|
private |
◆ _fd
| int mxl::lib::SharedMemoryBase::_fd |
|
private |
◆ _lockType
| LockType mxl::lib::SharedMemoryBase::_lockType |
|
private |
The type of advisory lock that is held for the mapped file
Definition at line 146 of file SharedMemory.hpp.
◆ _mappedSize
| std::size_t mxl::lib::SharedMemoryBase::_mappedSize |
|
private |
◆ _mode
A derivation of the mode used to open the segemnt that is used to indicate the access mode the segement is operating in, as well as whether or not it was created when it was last opened.
Definition at line 140 of file SharedMemory.hpp.
The documentation for this class was generated from the following files: