MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
mxl::lib::SharedMemoryBase Class Reference

#include <SharedMemory.hpp>

Inheritance diagram for mxl::lib::SharedMemoryBase:

Public Member Functions

constexpr AccessMode accessMode () const noexcept
constexpr bool created () const noexcept
constexpr bool isValid () const noexcept
constexpr std::size_t mappedSize () const noexcept
constexpr operator bool () const noexcept
constexpr void swap (SharedMemoryBase &other) noexcept
void touch ()

Protected Member Functions

constexpr void const * cdata () const noexcept
constexpr void const * data () const noexcept
constexpr void * data () noexcept
constexpr bool isExclusive () const
bool makeExclusive ()
constexpr SharedMemoryBase () noexcept
 SharedMemoryBase (char const *path, AccessMode mode, std::size_t payloadSize, LockMode lockMode)
constexpr SharedMemoryBase (SharedMemoryBase &&other) noexcept
 SharedMemoryBase (SharedMemoryBase const &other)=delete
 ~SharedMemoryBase ()

Private Types

enum class  LockType { None , Exclusive , Shared }

Private Attributes

void * _data
int _fd
LockType _lockType
std::size_t _mappedSize
AccessMode _mode

Detailed Description

Definition at line 29 of file SharedMemory.hpp.

Member Enumeration Documentation

◆ LockType

enum class mxl::lib::SharedMemoryBase::LockType
strongprivate
Enumerator
None 
Exclusive 
Shared 

Definition at line 125 of file SharedMemory.hpp.

Constructor & Destructor Documentation

◆ 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 {
228 swap(other);
229 }
constexpr SharedMemoryBase() noexcept
constexpr void swap(SharedMemoryBase &other) noexcept

◆ SharedMemoryBase() [3/4]

mxl::lib::SharedMemoryBase::SharedMemoryBase ( SharedMemoryBase const & other)
protecteddelete

◆ SharedMemoryBase() [4/4]

MXL_EXPORT mxl::lib::SharedMemoryBase::SharedMemoryBase ( char const * path,
AccessMode mode,
std::size_t payloadSize,
LockMode lockMode )
protected

Create a shared memory mapping with the specified payload size, or alterntively open it for reading and writing

Parameters
pathThe memory mapping path
modeThe memory mapping access mode
payloadSizeThe minimum expected size of the shared memory
Exceptions
Ifopening 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
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 }

◆ ~SharedMemoryBase()

MXL_EXPORT SharedMemoryBase::~SharedMemoryBase ( )
protected

Destructor.

Definition at line 109 of file SharedMemory.cpp.

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 }

Member Function Documentation

◆ 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.

284 {
285 return _data;
286 }

◆ 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.

252 {
254 }

◆ 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.

279 {
280 return _data;
281 }

◆ 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.

274 {
275 return _data;
276 }

◆ 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
Ifthere 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_errorIf 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 // 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 }

◆ 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.

242 {
243 return _mappedSize;
244 }

◆ 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 {
238 return isValid();
239 }
constexpr bool isValid() const noexcept

◆ swap()

void mxl::lib::SharedMemoryBase::swap ( SharedMemoryBase & other)
constexprnoexcept

Definition at line 256 of file SharedMemory.hpp.

257 {
258 // Workaround for std::swap not being declared constexpr in libstdc++ v10
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);
269 cx_swap(_mappedSize, other._mappedSize);
270 cx_swap(_lockType, other._lockType);
271 }

◆ touch()

void SharedMemoryBase::touch ( )

Update the attributes of the file

Definition at line 123 of file SharedMemory.cpp.

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 }
#define MXL_ERROR(...)
Definition Logging.hpp:67
constexpr AccessMode accessMode() const noexcept

Member Data Documentation

◆ _data

void* mxl::lib::SharedMemoryBase::_data
private

Pointer to the mapped region.

Definition at line 142 of file SharedMemory.hpp.

◆ _fd

int mxl::lib::SharedMemoryBase::_fd
private

File descriptor of the shared memory object.

Definition at line 134 of file SharedMemory.hpp.

◆ _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

The size of the mapped region in bytes.

Definition at line 144 of file SharedMemory.hpp.

◆ _mode

AccessMode mxl::lib::SharedMemoryBase::_mode
private

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: