This commit is contained in:
2026-03-26 14:35:10 +01:00
parent b35a982c6f
commit cf28fba3f5
14 changed files with 314 additions and 149 deletions

View File

@@ -1,4 +1,5 @@
#include "shm.h"
#include "utils.h"
#include <string.h>
#include <stdio.h>
@@ -306,14 +307,14 @@ class ShmSemApi {
void wait() {
int r = sem_wait(_sem);
if (r != 0) {
fprintf(stderr, "sem_wait error: %d, %s\n", errno, strerror(errno));
ERROR2("sem_wait error: %d, %s\n", errno, strerror(errno));
}
}
bool trywait() {
int r = sem_trywait(reinterpret_cast<sem_t *>(_sem));
if (r != 0 && r != EAGAIN) {
fprintf(stderr, "sem_wait error: %d, %s\n", errno, strerror(errno));
ERROR2("sem_wait error: %d, %s\n", errno, strerror(errno));
}
return (r == 0);
@@ -373,7 +374,7 @@ public:
name);
if (_shm_handle == NULL) {
fprintf(stderr, "Cannot create shared memory with name %s and size %lld\n", name, size);
ERROR2("Cannot create shared memory with name %s and size %lld\n", name, size);
_valid = false;
}
} else {
@@ -381,7 +382,7 @@ public:
FALSE,
name);
if (_shm_handle == NULL) {
fprintf(stderr, "Cannot open shared memory with name %s\n", name);
ERROR1("Cannot open shared memory with name %s\n", name);
_valid = false;
}
}
@@ -393,7 +394,7 @@ public:
0,
size));
if (_mem == NULL) {
fprintf(stderr, "Cannot map shared memory, errorcode = %ld\n", GetLastError());
ERROR1("Cannot map shared memory, errorcode = %ld\n", GetLastError());
_valid = false;
CloseHandle(_shm_handle);
}
@@ -410,7 +411,7 @@ public:
_sem_handle = OpenSemaphoreA(SYNCHRONIZE|SEMAPHORE_MODIFY_STATE, FALSE, _sem_name);
}
if (_sem_handle == NULL) {
fprintf(stderr, "Cannot create or open semaphore with name '%s' (%ld)\n", _sem_name, GetLastError());
ERROR2("Cannot create or open semaphore with name '%s' (%ld)\n", _sem_name, GetLastError());
UnmapViewOfFile(_mem);
CloseHandle(_shm_handle);
_valid = false;
@@ -486,14 +487,14 @@ public:
void wait() {
DWORD r = WaitForSingleObject(_sem, INFINITE);
if (r != WAIT_OBJECT_0) {
fprintf(stderr, "sem_wait error: %ld\n", r);
ERROR1("sem_wait error: %ld\n", r);
}
}
bool trywait() {
DWORD r = WaitForSingleObject(_sem , 0);
if (r != WAIT_OBJECT_0 && r != WAIT_TIMEOUT) {
fprintf(stderr, "sem_wait error: %ld\n", r);
ERROR1("sem_wait error: %ld\n", r);
}
return r == WAIT_OBJECT_0;
}
@@ -512,7 +513,7 @@ public:
}
if (_sem == NULL) {
fprintf(stderr, "Cannot create or open semaphore with name '%s'\n", _name);
ERROR1("Cannot create or open semaphore with name '%s'\n", _name);
}
_owner = owner;
}