7 #ifndef PROJECT_SHAREDMEMORY_H 8 #define PROJECT_SHAREDMEMORY_H 11 #include <semaphore.h> 21 #define DEVELOPMENT_SIMULATOR_SHARED_MEMORY_NAME "development-simulator" 39 void init(
unsigned int value) {
41 if (sem_init(&
_sem, 1, value)) {
42 printf(
"[ERROR] Failed to initialize shared memory semaphore: %s\n",
74 clock_gettime(CLOCK_REALTIME, &ts);
75 ts.tv_nsec += nanoseconds;
77 ts.tv_sec += ts.tv_nsec / 1000000000;
78 ts.tv_nsec %= 1000000000;
79 return (sem_timedwait(&
_sem, &ts) == 0);
111 template <
typename T>
125 bool createNew(
const std::string& name,
bool allowOverwrite =
false) {
126 bool hadToDelete =
false;
130 printf(
"[Shared Memory] open new %s, size %ld bytes\n", name.c_str(),
133 _fd = shm_open(name.c_str(), O_RDWR | O_CREAT,
134 S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IROTH);
136 printf(
"[ERROR] SharedMemoryObject shm_open failed: %s\n",
138 throw std::runtime_error(
"Failed to create shared memory!");
143 if (fstat(_fd, &s)) {
144 printf(
"[ERROR] SharedMemoryObject::createNew(%s) stat: %s\n",
145 name.c_str(), strerror(errno));
146 throw std::runtime_error(
"Failed to create shared memory!");
152 "[Shared Memory] SharedMemoryObject::createNew(%s) on something that " 153 "wasn't new (size is %ld bytes)\n",
154 _name.c_str(), s.st_size);
157 throw std::runtime_error(
158 "Failed to create shared memory - it already exists.");
159 printf(
"\tusing existing shared memory!\n");
163 if (ftruncate(_fd, _size)) {
164 printf(
"[ERROR] SharedMemoryObject::createNew(%s) ftruncate(%ld): %s\n",
165 name.c_str(), _size, strerror(errno));
166 throw std::runtime_error(
"Failed to create shared memory!");
171 mmap(
nullptr, _size, PROT_READ | PROT_WRITE, MAP_SHARED, _fd, 0);
172 if (mem == MAP_FAILED) {
173 printf(
"[ERROR] SharedMemory::createNew(%s) mmap fail: %s\n",
174 _name.c_str(), strerror(errno));
175 throw std::runtime_error(
"Failed to create shared memory!");
182 memset(mem, 0, _size);
195 printf(
"[Shared Memory] open existing %s size %ld bytes\n", name.c_str(),
197 _fd = shm_open(name.c_str(), O_RDWR,
198 S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IROTH);
200 printf(
"[ERROR] SharedMemoryObject::attach shm_open(%s) failed: %s\n",
201 _name.c_str(), strerror(errno));
202 throw std::runtime_error(
"Failed to create shared memory!");
207 if (fstat(_fd, &s)) {
208 printf(
"[ERROR] SharedMemoryObject::attach(%s) stat: %s\n", name.c_str(),
210 throw std::runtime_error(
"Failed to create shared memory!");
214 if ((
size_t)s.st_size != _size) {
216 "[ERROR] SharedMemoryObject::attach(%s) on something that was " 218 "sized (size is %ld bytes, should be %ld)\n",
219 _name.c_str(), s.st_size, _size);
220 throw std::runtime_error(
"Failed to create shared memory!");
225 mmap(
nullptr, _size, PROT_READ | PROT_WRITE, MAP_SHARED, _fd, 0);
226 if (mem == MAP_FAILED) {
227 printf(
"[ERROR] SharedMemory::attach(%s) mmap fail: %s\n", _name.c_str(),
229 throw std::runtime_error(
"Failed to create shared memory!");
244 if (munmap((
void*)_data, _size)) {
245 printf(
"[ERROR] SharedMemoryObject::closeNew (%s) munmap %s\n",
246 _name.c_str(), strerror(errno));
247 throw std::runtime_error(
"Failed to create shared memory!");
253 if (shm_unlink(_name.c_str())) {
254 printf(
"[ERROR] SharedMemoryObject::closeNew (%s) shm_unlink %s\n",
255 _name.c_str(), strerror(errno));
256 throw std::runtime_error(
"Failed to create shared memory!");
262 printf(
"[ERROR] SharedMemoryObject::closeNew (%s) close %s\n",
263 _name.c_str(), strerror(errno));
264 throw std::runtime_error(
"Failed to create shared memory!");
280 if (munmap((
void*)_data, _size)) {
281 printf(
"[ERROR] SharedMemoryObject::detach (%s) munmap %s\n",
282 _name.c_str(), strerror(errno));
283 throw std::runtime_error(
"Failed to create shared memory!");
291 printf(
"[ERROR] SharedMemoryObject::detach (%s) close %s\n",
292 _name.c_str(), strerror(errno));
293 throw std::runtime_error(
"Failed to create shared memory!");
323 #endif // PROJECT_SHAREDMEMORY_H Common types that are only valid in C++.
void attach(const std::string &name)
void init(unsigned int value)
bool createNew(const std::string &name, bool allowOverwrite=false)
bool decrementTimeout(u64 seconds, u64 nanoseconds)