MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
mxl::lib::SharedMemoryInstance< T > Struct Template Reference

#include <SharedMemory.hpp>

Inheritance diagram for mxl::lib::SharedMemoryInstance< T >:
Collaboration diagram for mxl::lib::SharedMemoryInstance< T >:

Public Member Functions

constexpr T const * get () const noexcept
constexpr T * get () noexcept
constexpr bool isExclusive () const
bool makeExclusive ()
SharedMemoryInstance & operator= (SharedMemoryInstance other) noexcept
constexpr SharedMemoryInstance () noexcept
 SharedMemoryInstance (char const *path, AccessMode mode, std::size_t payloadSize, LockMode lockMode)
constexpr SharedMemoryInstance (SharedMemoryInstance &&other)
 SharedMemoryInstance (SharedMemoryInstance const &other)=delete
constexpr void swap (SharedMemoryInstance &other) noexcept
Public Member Functions inherited from mxl::lib::SharedMemoryBase
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 ()

Additional Inherited Members

Protected Member Functions inherited from mxl::lib::SharedMemoryBase
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 ()

Detailed Description

template<typename T>
struct mxl::lib::SharedMemoryInstance< T >

Utility class that allocates and places a structure in shared memory

Definition at line 170 of file SharedMemory.hpp.

Constructor & Destructor Documentation

◆ SharedMemoryInstance() [1/4]

template<typename T>
mxl::lib::SharedMemoryInstance< T >::SharedMemoryInstance ( )
constexprnoexcept

Definition at line 322 of file SharedMemory.hpp.

324 {}
constexpr SharedMemoryBase() noexcept

◆ SharedMemoryInstance() [2/4]

template<typename T>
mxl::lib::SharedMemoryInstance< T >::SharedMemoryInstance ( SharedMemoryInstance< T > && other)
constexpr

◆ SharedMemoryInstance() [3/4]

template<typename T>
mxl::lib::SharedMemoryInstance< T >::SharedMemoryInstance ( SharedMemoryInstance< T > const & other)
delete

◆ SharedMemoryInstance() [4/4]

template<typename T>
mxl::lib::SharedMemoryInstance< T >::SharedMemoryInstance ( char const * path,
AccessMode mode,
std::size_t payloadSize,
LockMode lockMode )
inline

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
extraSizeAdd this size to the shared memory in addition to sizeof(T)
Returns
true on success. false on conflicts or failure to create resources on disk.

Definition at line 332 of file SharedMemory.hpp.

334 {
335 if (created())
336 {
337 if (this->mappedSize() < (payloadSize + sizeof(T)))
338 {
339 throw std::runtime_error("Cannot initialize shared memory instance because not enough memory was mapped.");
340 }
341
342 new (data()) T{};
343 }
344 }
constexpr void * data() noexcept
constexpr bool created() const noexcept
constexpr std::size_t mappedSize() const noexcept

Member Function Documentation

◆ get() [1/2]

template<typename T>
T const * mxl::lib::SharedMemoryInstance< T >::get ( ) const
constexprnoexcept

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 367 of file SharedMemory.hpp.

368 {
369 return static_cast<T const*>(data());
370 }

◆ get() [2/2]

template<typename T>
T * mxl::lib::SharedMemoryInstance< T >::get ( )
constexprnoexcept

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 360 of file SharedMemory.hpp.

361 {
362 return static_cast<T*>(data());
363 ;
364 }

◆ isExclusive()

template<typename T>
bool mxl::lib::SharedMemoryBase::isExclusive ( ) const
nodiscardconstexpr

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 114 of file SharedMemory.hpp.

◆ makeExclusive()

template<typename T>
bool SharedMemoryBase::makeExclusive ( )

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 122 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 }

◆ operator=()

template<typename T>
auto mxl::lib::SharedMemoryInstance< T >::operator= ( SharedMemoryInstance< T > other)
inlinenoexcept

Definition at line 347 of file SharedMemory.hpp.

348 {
349 swap(other);
350 return *this;
351 }
constexpr void swap(SharedMemoryInstance &other) noexcept

◆ swap()

template<typename T>
void mxl::lib::SharedMemoryInstance< T >::swap ( SharedMemoryInstance< T > & other)
constexprnoexcept

Definition at line 354 of file SharedMemory.hpp.

355 {
356 static_cast<SharedMemoryBase*>(this)->swap(other);
357 }

The documentation for this struct was generated from the following file: