30#include <boost/interprocess/managed_shared_memory.hpp>
31#include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp>
32#include <boost/interprocess/sync/sharable_lock.hpp>
39#include <ArmarXCore/interface/core/SharedMemory.h>
44 boost::interprocess::sharable_lock<boost::interprocess::interprocess_upgradable_mutex>;
54 template <
class MemoryObject,
class MemoryObjectMetaInfo = MetaInfoSizeBase>
58 class Wrapper :
public MemoryObjectMetaInfo
65 Wrapper(
const MemoryObjectMetaInfo& source) : MemoryObjectMetaInfo(source)
80 this->memoryName = newMemoryName;
81 auto env_c_str = getenv(
"USER");
82 std::string userName = env_c_str ? env_c_str :
"";
84 if (!userName.empty())
86 this->memoryName += userName;
92 sharedMemorySegment =
new boost::interprocess::managed_shared_memory(
93 boost::interprocess::open_only, memoryName.c_str());
95 catch (
const boost::interprocess::interprocess_exception& e)
97 std::string reason =
"Error opening shared memory segment for reading: ";
102 info = sharedMemorySegment->find<Wrapper>(
"Info").first;
107 memoryName,
"Error opening memory size for reading");
113 ->find<boost::interprocess::interprocess_upgradable_mutex>(
"SharedMutex")
119 memoryName,
"Error opening shared mutex for reading");
123 data = sharedMemorySegment->find<MemoryObject>(
"Data").first;
128 memoryName,
"Error opening shared data for reading");
133 ->find<boost::interprocess::interprocess_condition>(
"CondSizeChanged")
139 memoryName,
"Error opening condition variable for reading");
164 memoryName,
"Error opening shared mutex for reading");
170 typename MemoryObjectMetaInfo::PointerType
180 return new Wrapper(*info);
225 memoryMutex->lock_sharable();
237 memoryMutex->unlock_sharable();
245 std::shared_ptr<SharedMemoryConsumer<MemoryObject, MemoryObjectMetaInfo>>;
251 std::string memoryName;
256 boost::interprocess::managed_shared_memory* sharedMemorySegment;
261 mutable boost::interprocess::interprocess_upgradable_mutex* memoryMutex;
266 boost::interprocess::interprocess_condition* condSizeChanged;
273 MemoryObjectMetaInfo* info;
Wrapper(const MemoryObjectMetaInfo &source)
MemoryObjectMetaInfo::PointerType getMetaInfo(bool threadSafe=true) const
SharedMemoryScopedReadLockPtr getScopedReadLock() const
Retrieve scoped lock to the shared memory for reading.
MemoryObject * getMemory()
Retrieve pointer to shared memory.
int getSize() const
Retrieve size of usable shared memory.
void unlock()
Unlock shared memory after reading.
void lock()
Lock shared memory for reading.
std::shared_ptr< SharedMemoryConsumer< MemoryObject, MemoryObjectMetaInfo > > pointer_type
~SharedMemoryConsumer()
Destructs the shared memory consumer.
SharedMemoryConsumer(std::string newMemoryName)
Creates a shared memory consumer.
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< SharedMemoryScopedReadLock > SharedMemoryScopedReadLockPtr
boost::interprocess::sharable_lock< boost::interprocess::interprocess_upgradable_mutex > SharedMemoryScopedReadLock