14 explicit IpcNamedMutex(boost::interprocess::named_mutex& mx) : _mx(mx) {}
16 template<
typename Duration>
18 bool try_lock_for(
const Duration& dur) {
19 auto abs_time = boost::posix_time::microsec_clock::universal_time();
20 auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
21 auto bmillis = boost::posix_time::milliseconds(millis);
23 return _mx.timed_lock(abs_time);
26 template<
typename Duration>
28 bool try_lock_until(
const Duration& abs_time) {
29 return _mx.timed_lock(abs_time);
32 void lock() { _mx.lock(); }
34 void unlock() { _mx.unlock(); }
37 bool try_lock() {
return _mx.try_lock(); }
40 boost::interprocess::named_mutex& _mx;
45 SharedDummyBackend::SharedMemoryManager::SharedMemoryManager(
46 SharedDummyBackend& sharedDummyBackend_,
const std::string& instanceId,
const std::string& mapFileName)
47 : sharedDummyBackend(sharedDummyBackend_), userHash(std::
to_string(std::hash<std::string>{}(
getUserName()))),
48 mapFileHash(
std::to_string(std::hash<std::string>{}(mapFileName))),
49 instanceIdHash(
std::to_string(std::hash<std::string>{}(instanceId))),
50 name(
"ChimeraTK_SharedDummy_" + instanceIdHash +
"_" + mapFileHash +
"_" + userHash),
51 segment(boost::interprocess::open_or_create, name.c_str(), getRequiredMemoryWithOverhead()),
52 sharedMemoryIntAllocator(segment.get_segment_manager()),
53 interprocessMutex(boost::interprocess::open_or_create, name.c_str()) {
57 IpcNamedMutex proxy(interprocessMutex);
58 std::unique_lock<IpcNamedMutex> lock(proxy, std::defer_lock);
59 auto ok = lock.try_lock_for(std::chrono::milliseconds(2000));
61 std::cerr <<
"SharedDummyBackend: stale lock detected, removing mutex... " << std::endl;
64 boost::interprocess::named_mutex::remove(name.c_str());
65 interprocessMutex.~named_mutex();
66 new(&interprocessMutex) boost::interprocess::named_mutex(boost::interprocess::open_or_create, name.c_str());
71 pidSet = findOrConstructVector(SHARED_MEMORY_PID_SET_NAME, 0);
74 bool reInitRequired = checkPidSetConsistency();
83 requiredVersion = segment.find_or_construct<
unsigned>(SHARED_MEMORY_REQUIRED_VERSION_NAME)(0);
88 std::string errMsg{
"Maximum number of accessing members reached."};
91 InterruptDispatcherInterface::cleanupShm(segment, pidSet);
93 pidSet->emplace_back(
static_cast<int32_t
>(
getOwnPID()));
95 this->intDispatcherIf = boost::movelib::unique_ptr<InterruptDispatcherInterface>(
96 new InterruptDispatcherInterface(sharedDummyBackend, segment, interprocessMutex));
99 SharedDummyBackend::SharedMemoryManager::~SharedMemoryManager() {
101 intDispatcherIf.reset();
109 std::lock_guard<boost::interprocess::named_mutex> lock(interprocessMutex);
112 checkPidSetConsistency();
114 auto ownPid =
static_cast<int32_t
>(
getOwnPID());
115 for(
auto it = pidSet->begin(); it != pidSet->end();) {
117 it = pidSet->erase(it);
123 pidSetSize = pidSet->size();
125 catch(boost::interprocess::interprocess_exception&) {
132 if(pidSetSize == 0) {
133 boost::interprocess::shared_memory_object::remove(name.c_str());
134 boost::interprocess::named_mutex::remove(name.c_str());
140 const std::string& objName,
const size_t size) {
142 segment.find_or_construct<
SharedMemoryVector>(objName.c_str())(size, 0, sharedMemoryIntAllocator);
147 size_t SharedDummyBackend::SharedMemoryManager::getRequiredMemoryWithOverhead() {
150 return SHARED_MEMORY_OVERHEAD_PER_VECTOR * sharedDummyBackend._barSizesInBytes.size() +
151 SHARED_MEMORY_CONST_OVERHEAD + sharedDummyBackend.getTotalRegisterSizeInBytes() +
sizeof(ShmForSems);
154 std::pair<size_t, size_t> SharedDummyBackend::SharedMemoryManager::getInfoOnMemory() {
155 return std::make_pair(segment.get_size(), segment.get_free_memory());
158 bool SharedDummyBackend::SharedMemoryManager::checkPidSetConsistency() {
159 unsigned pidSetSizeBeforeCleanup = pidSet->size();
161 for(
auto it = pidSet->begin(); it != pidSet->end();) {
164 it = pidSet->erase(it);
171 return pidSetSizeBeforeCleanup != 0 && pidSet->empty();
174 void SharedDummyBackend::SharedMemoryManager::reInitMemory() {
175 std::vector<std::string> nameList = listNamedElements();
177 for(
auto& item : nameList) {
178 if(item == SHARED_MEMORY_REQUIRED_VERSION_NAME) {
179 segment.destroy<
unsigned>(item.c_str());
183 else if(item != SHARED_MEMORY_PID_SET_NAME) {
187 InterruptDispatcherInterface::cleanupShm(segment);
190 std::vector<std::string> SharedDummyBackend::SharedMemoryManager::listNamedElements() {
191 std::vector<std::string> list(segment.get_num_named_objects());
193 for(
auto seg = segment.named_begin(); seg != segment.named_end(); ++seg) {
194 list.emplace_back(seg->name());