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>
33 #include <boost/interprocess/sync/interprocess_condition.hpp>
39 #include <ArmarXCore/interface/core/SharedMemory.h>
87 template <
class MemoryObject,
class MemoryObjectMetaInfo = MetaInfoSizeBase>
91 class Wrapper :
public MemoryObjectMetaInfo
110 if (info->capacity <= 0)
115 this->memoryName = newMemoryName;
116 auto env_c_str = getenv(
"USER");
117 std::string userName = env_c_str ? env_c_str :
"";
120 if (!userName.empty())
122 this->memoryName += userName;
128 boost::interprocess::shared_memory_object::remove(this->memoryName.c_str());
133 std::size_t shmSize = info->capacity
134 +
sizeof(MemoryObjectMetaInfo)
135 +
sizeof(boost::interprocess::interprocess_mutex)
136 +
sizeof(boost::interprocess::interprocess_condition)
141 sharedMemorySegment =
new boost::interprocess::managed_shared_memory(boost::interprocess::open_or_create,
145 catch (std::exception& e)
151 sharedMemorySegment->destroy<Wrapper>(
"Info");
152 this->info = sharedMemorySegment->find_or_construct<Wrapper>(
"Info")();
162 sharedMemorySegment->destroy<boost::interprocess::interprocess_upgradable_mutex>(
"SharedMutex");
163 memoryMutex = sharedMemorySegment->find_or_construct<boost::interprocess::interprocess_upgradable_mutex>(
"SharedMutex")();
170 sharedMemorySegment->destroy<boost::interprocess::interprocess_condition>(
"CondSizeChanged");
171 condSizeChanged = sharedMemorySegment->find_or_construct<boost::interprocess::interprocess_condition>(
"CondSizeChanged")();
173 if (!condSizeChanged)
179 sharedMemorySegment->destroy<MemoryObject>(
"Data");
180 this->data = sharedMemorySegment->find_or_construct<MemoryObject>(
"Data")[info->capacity]();
198 boost::interprocess::shared_memory_object::remove(memoryName.c_str());
217 void setMetaInfo(
const typename MemoryObjectMetaInfo::PointerType& info,
bool threadSafe =
false)
231 if (info->capacity != this->info->capacity)
236 else if (info->size > this->info->capacity)
241 bool sizeChanged = (this->info->size != info->size);
245 condSizeChanged->notify_all();
272 return info->capacity;
328 memoryMutex->unlock();
335 using pointer_type = std::shared_ptr<SharedMemoryProvider<MemoryObject, MemoryObjectMetaInfo> >;
341 std::string memoryName;
346 boost::interprocess::managed_shared_memory* sharedMemorySegment;
351 mutable boost::interprocess::interprocess_upgradable_mutex* memoryMutex;
356 boost::interprocess::interprocess_condition* condSizeChanged;
363 MemoryObjectMetaInfo* info;