90 : _shared_memory_name{
"Local\\s_" + unique_identifier}
92 : _shared_memory_name{
"/s_" + unique_identifier}
94 , _mutex{(std::string{
"m_"} + unique_identifier).c_str()}
96 using namespace std::string_literals;
98 std::lock_guard<cbeam::concurrency::named_recursive_mutex> lock(_mutex);
101 _shared_memory = CreateFileMappingA(
102 INVALID_HANDLE_VALUE,
105 static_cast<DWORD
>(0xFFFFFFFF & (size >> 32)),
106 static_cast<DWORD
>(0xFFFFFFFF & size),
107 _shared_memory_name.c_str());
109 if (_shared_memory == NULL)
111 if (GetLastError() == ERROR_ALREADY_EXISTS)
113 _shared_memory = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, _shared_memory_name.c_str());
114 if (_shared_memory == NULL)
116 throw cbeam::error::runtime_error(
"cbeam::memory::interprocess_shared_memory: Failed to open existing shared memory for '" + unique_identifier +
"': " +
platform::get_last_windows_error_message());
125 _region = MapViewOfFile(
134 CloseHandle(_shared_memory);
138 CBEAM_LOG_DEBUG(
"cbeam::memory::interprocess_shared_memory: Created or opened shared memory " + unique_identifier);
140 int shared_memory = shm_open(_shared_memory_name.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
141 if (shared_memory == -1)
143 throw cbeam::error::runtime_error(
"cbeam::memory::interprocess_shared_memory: Failed to create/open shared memory: " + unique_identifier);
146 if (ftruncate(shared_memory, size) == -1)
148 close(shared_memory);
149 throw cbeam::error::runtime_error(
"cbeam::memory::interprocess_shared_memory: Failed to set size of shared memory: " + unique_identifier);
152 void* addr = mmap(
nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, shared_memory, 0);
153 if (addr == MAP_FAILED)
155 close(shared_memory);
156 throw cbeam::error::runtime_error(
"cbeam::memory::interprocess_shared_memory: Failed to map shared memory: " + unique_identifier);
159 close(shared_memory);
160 _region = std::make_unique<posix_mapped_region>(addr, size);
168 std::lock_guard<cbeam::concurrency::named_recursive_mutex> lock(_mutex);
171 UnmapViewOfFile(_region);
172 CloseHandle(_shared_memory);
175 shm_unlink(_shared_memory_name.c_str());
178 catch (
const std::system_error& ex)
180 CBEAM_LOG(std::string{
"cbeam::memory::interprocess_shared_memory::~interprocess_shared_memory: Could not lock the mutex. This indicates a serious (unexpected) bug that must be fixed during development phase: "} + ex.what());
194 return _region->get_address();
205 MEMORY_BASIC_INFORMATION info;
206 VirtualQuery(_region, &info,
sizeof(info));
207 return info.RegionSize;
209 return _region->get_size();
229 HANDLE _shared_memory;
232 class posix_mapped_region
235 posix_mapped_region(
void* addr,
size_t size)
239 ~posix_mapped_region()
noexcept
241 munmap(_addr, _size);
244 void* get_address()
const noexcept {
return _addr; }
245 size_t get_size()
const noexcept {
return _size; }
252 std::unique_ptr<posix_mapped_region> _region;
254 const std::string _shared_memory_name;
Provides a cross-platform interface for recursive named mutexes, enabling interprocess synchronizatio...
Definition named_recursive_mutex.hpp:85
Definition interprocess_shared_memory.hpp:64
~lock_guard() noexcept
Definition interprocess_shared_memory.hpp:74
lock_guard(const lock_guard &)=default
lock_guard & operator=(const lock_guard &)=delete
lock_guard(cbeam::concurrency::named_recursive_mutex &mutex)
Definition interprocess_shared_memory.hpp:68
lock_guard get_lock_guard() const
Acquires a lock_guard for mutex synchronization.
Definition interprocess_shared_memory.hpp:217
size_t capacity() const noexcept
Returns the size of the shared memory region.
Definition interprocess_shared_memory.hpp:202
interprocess_shared_memory & operator=(interprocess_shared_memory &&)=delete
void * data() const
Retrieves the starting address of the shared memory region.
Definition interprocess_shared_memory.hpp:189
interprocess_shared_memory(const interprocess_shared_memory &)=delete
interprocess_shared_memory(const std::string &unique_identifier, std::size_t size)
Constructor that initializes the shared memory segment.
Definition interprocess_shared_memory.hpp:88
interprocess_shared_memory & operator=(const interprocess_shared_memory &)=delete
virtual ~interprocess_shared_memory() noexcept
Definition interprocess_shared_memory.hpp:164
interprocess_shared_memory(interprocess_shared_memory &&)=delete
#define CBEAM_LOG(s)
Logs a message using cbeam::logging::log_manager.
Definition log_manager.hpp:124
#define CBEAM_LOG_DEBUG(s)
Logs a debug message if CBEAM_DEBUG_LOGGING is enabled.
Definition log_manager.hpp:138
Houses abstractions for shared-memory and interprocess data exchange. This includes interprocess_shar...
Definition interprocess_shared_memory.hpp:49
Header file to manage inclusion of windows.h with specific settings.