1#ifndef LINTEL_SHMCONSTRUCTORS_H
2#define LINTEL_SHMCONSTRUCTORS_H
4#include <boost/container/map.hpp>
5#include <boost/container/string.hpp>
6#include <boost/container/vector.hpp>
7#include <boost/date_time/posix_time/posix_time.hpp>
8#include <boost/interprocess/managed_mapped_file.hpp>
9#include <boost/interprocess/sync/named_semaphore.hpp>
51 using SegmentManager = boost::interprocess::managed_mapped_file::segment_manager;
54 template<
class Object>
using SegmentAllocator = boost::interprocess::allocator<Object, SegmentManager>;
60 template<
class Key,
class Value>
64 template<
class Object>
using Vector = boost::container::vector<Object, SegmentAllocator<Object>>;
90 const auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(
timeout).count();
92 boost::posix_time::microsec_clock::universal_time() + boost::posix_time::microseconds(
microseconds);
93 if (!m_semaphore->timed_wait(
deadline)) {
96 m_semaphore =
nullptr;
97 throw std::runtime_error(
98 "timed out acquiring the shared memory segment remap semaphore; "
99 "a previous process may have crashed while holding it");
105 if (m_semaphore !=
nullptr) { m_semaphore->post(); }
113 boost::interprocess::named_semaphore *m_semaphore;
124 template<
class Object, std::
size_t Size>
126 const std::shared_ptr<SharedMemorySegment> &
segment,
127 const std::string &name)
129 std::lock_guard<std::mutex>
lock(
service.segmentsMutexOf());
132 return *(handle.m_managedMappedFile->find_or_construct<std::array<Object, Size>>(name.c_str())());
143 template<
class Key,
class Value>
145 const std::shared_ptr<SharedMemorySegment> &
segment,
146 const std::string &name)
148 using pairType = std::pair<const Key, Value>;
150 std::lock_guard<std::mutex>
lock(
service.segmentsMutexOf());
167 template<
class Object>
169 const std::shared_ptr<SharedMemorySegment> &
segment,
170 const std::string &name)
173 std::lock_guard<std::mutex>
lock(
service.segmentsMutexOf());
187 template<
class Object>
189 const std::shared_ptr<SharedMemorySegment> &
segment,
190 const std::string &name)
192 std::lock_guard<std::mutex>
lock(
service.segmentsMutexOf());
195 return *(handle.m_managedMappedFile->find_or_construct<
Object>(name.c_str())());
210 const std::shared_ptr<SharedMemorySegment> &
segment,
211 const std::string &name,
214 std::lock_guard<std::mutex>
lock(
service.segmentsMutexOf());
217 return *(handle.m_managedMappedFile->find_or_construct<
Object>(name.c_str())(std::forward<Args>(
args)...));
227 const std::shared_ptr<SharedMemorySegment> &
segment,
228 const std::string &name)
230 std::lock_guard<std::mutex>
lock(
service.segmentsMutexOf());
244 const std::shared_ptr<SharedMemorySegment> &
segment)
246 std::lock_guard<std::mutex>
lock(
service.segmentsMutexOf());
248 if (
found ==
service.segmentsOf().end()) {
return nullptr; }
250 return found->second->m_managedMappedFile->get_segment_manager();
#define LOG_TRACE(message,...)
Definition LoggerService.h:128
Definition ShmConstructors.h:33
std::shared_ptr< SharedMemorySegment > m_sharedMemorySegment
Definition ShmConstructors.h:36
std::unique_ptr< boost::interprocess::named_semaphore > m_remapSemaphore
Definition ShmConstructors.h:41
std::shared_ptr< ShmSegmentAccessor::Allocator > m_segmentAllocator
Definition ShmConstructors.h:45
std::shared_ptr< boost::interprocess::managed_mapped_file > m_managedMappedFile
Definition ShmConstructors.h:35
Definition SharedMemoryService.h:37
Definition ShmConstructors.h:76
~SegmentSemaphoreGuard()
Definition ShmConstructors.h:103
SegmentSemaphoreGuard(const SegmentSemaphoreGuard &)=delete
static constexpr std::chrono::seconds kDefaultWaitTimeout
Definition ShmConstructors.h:80
SegmentSemaphoreGuard & operator=(const SegmentSemaphoreGuard &)=delete
SegmentSemaphoreGuard(boost::interprocess::named_semaphore &semaphore, std::chrono::steady_clock::duration timeout=kDefaultWaitTimeout)
Definition ShmConstructors.h:86
boost::container::map< Key, Value, std::less< Key >, SegmentAllocator< std::pair< const Key, Value > > > Map
Definition ShmConstructors.h:61
Object & constructObject(SharedMemoryService &service, const std::shared_ptr< SharedMemorySegment > &segment, const std::string &name)
Definition ShmConstructors.h:188
std::array< Object, Size > & constructArray(SharedMemoryService &service, const std::shared_ptr< SharedMemorySegment > &segment, const std::string &name)
Definition ShmConstructors.h:125
Object & constructObjectWith(SharedMemoryService &service, const std::shared_ptr< SharedMemorySegment > &segment, const std::string &name, Args &&...args)
Definition ShmConstructors.h:209
boost::container::basic_string< char, std::char_traits< char >, SegmentAllocator< char > > String
Definition ShmConstructors.h:57
String constructString(SharedMemoryService &service, const std::shared_ptr< SharedMemorySegment > &segment, const std::string &name)
Definition ShmConstructors.h:226
boost::interprocess::managed_mapped_file::segment_manager SegmentManager
Definition ShmConstructors.h:51
SegmentManager * segmentManagerOf(SharedMemoryService &service, const std::shared_ptr< SharedMemorySegment > &segment)
Definition ShmConstructors.h:243
boost::interprocess::allocator< Object, SegmentManager > SegmentAllocator
Definition ShmConstructors.h:54
Map< Key, Value > & constructMap(SharedMemoryService &service, const std::shared_ptr< SharedMemorySegment > &segment, const std::string &name)
Definition ShmConstructors.h:144
boost::container::vector< Object, SegmentAllocator< Object > > Vector
Definition ShmConstructors.h:64
Vector< Object > & constructVector(SharedMemoryService &service, const std::shared_ptr< SharedMemorySegment > &segment, const std::string &name)
Definition ShmConstructors.h:168
Definition BaseFeature.h:6
@ Object
Definition SharedMemoryRepository.h:19
constexpr std::string_view type_name()
Definition TypeName.h:9