eiger: locking for local link access in shared memory

This commit is contained in:
2020-09-15 11:08:05 +02:00
parent a7e24717a9
commit 092b3fa8b9
6 changed files with 82 additions and 42 deletions

View File

@@ -8,8 +8,8 @@ int sharedMemory_open(int port);
int sharedMemory_attach();
int sharedMemory_detach();
int sharedMemory_remove();
void sharedMemory_lock();
void sharedMemory_unlock();
void sharedMemory_lockStatus();
void sharedMemory_unlockStatus();
#ifdef VIRTUAL
void sharedMemory_setStatus(enum runStatus s);
enum runStatus sharedMemory_getStatus();
@@ -19,4 +19,8 @@ int sharedMemory_getStop();
void sharedMemory_setScanStatus(enum runStatus s);
enum runStatus sharedMemory_getScanStatus();
void sharedMemory_setScanStop(int s);
int sharedMemory_getScanStop();
int sharedMemory_getScanStop();
#ifdef EIGERD
void sharedMemory_lockLocalLink();
void sharedMemory_unlockLocalLink();
#endif

View File

@@ -8,14 +8,21 @@
#include <sys/shm.h>
#include <unistd.h>
#define SHM_NAME "sls_server_shared_memory"
#define SHM_NAME "sls_server_shared_memory"
#ifdef EIGERD
#define SHM_VERSION 0x200915
#else
#define SHM_VERSION 0x200625
#define SHM_KEY 5678
#define MEM_SIZE 128
#endif
#define SHM_KEY 5678
#define MEM_SIZE 128
typedef struct Memory {
int version;
pthread_mutex_t lock;
pthread_mutex_t lockStatus;
#ifdef EIGERD
pthread_mutex_t lockLocalLink;
#endif
enum runStatus scanStatus; // idle, running or error
int scanStop;
#ifdef VIRTUAL
@@ -80,11 +87,18 @@ int sharedMemory_create(int port) {
int sharedMemory_initialize() {
shm->version = SHM_VERSION;
if (pthread_mutex_init(&(shm->lock), NULL) != 0) {
LOG(logERROR,
("Failed to initialize pthread lock for shared memory\n"));
if (pthread_mutex_init(&(shm->lockStatus), NULL) != 0) {
LOG(logERROR, ("Failed to initialize pthread_mutex_t lockStatus for "
"shared memory\n"));
return FAIL;
}
#ifdef EIGERD
if (pthread_mutex_init(&(shm->lockLocalLink), NULL) != 0) {
LOG(logERROR, ("Failed to initialize pthread_mutex_t lockLocalLink for "
"shared memory\n"));
return FAIL;
}
#endif
shm->scanStatus = IDLE;
shm->scanStop = 0;
#ifdef VIRTUAL
@@ -141,64 +155,72 @@ int sharedMemory_remove() {
return OK;
}
void sharedMemory_lock() { pthread_mutex_lock(&(shm->lock)); }
void sharedMemory_lockStatus() { pthread_mutex_lock(&(shm->lockStatus)); }
void sharedMemory_unlock() { pthread_mutex_unlock(&(shm->lock)); }
void sharedMemory_unlockStatus() { pthread_mutex_unlock(&(shm->lockStatus)); }
#ifdef VIRTUAL
void sharedMemory_setStatus(enum runStatus s) {
sharedMemory_lock();
sharedMemory_lockStatus();
shm->status = s;
sharedMemory_unlock();
sharedMemory_unlockStatus();
}
enum runStatus sharedMemory_getStatus() {
enum runStatus s = 0;
sharedMemory_lock();
sharedMemory_lockStatus();
s = shm->status;
sharedMemory_unlock();
sharedMemory_unlockStatus();
return s;
}
void sharedMemory_setStop(int s) {
sharedMemory_lock();
sharedMemory_lockStatus();
shm->stop = s;
sharedMemory_unlock();
sharedMemory_unlockStatus();
}
int sharedMemory_getStop() {
int s = 0;
sharedMemory_lock();
sharedMemory_lockStatus();
s = shm->stop;
sharedMemory_unlock();
sharedMemory_unlockStatus();
return s;
}
#endif
void sharedMemory_setScanStatus(enum runStatus s) {
sharedMemory_lock();
sharedMemory_lockStatus();
shm->scanStatus = s;
sharedMemory_unlock();
sharedMemory_unlockStatus();
}
enum runStatus sharedMemory_getScanStatus() {
enum runStatus s = IDLE;
sharedMemory_lock();
sharedMemory_lockStatus();
s = shm->scanStatus;
sharedMemory_unlock();
sharedMemory_unlockStatus();
return s;
}
void sharedMemory_setScanStop(int s) {
sharedMemory_lock();
sharedMemory_lockStatus();
shm->scanStop = s;
sharedMemory_unlock();
sharedMemory_unlockStatus();
}
int sharedMemory_getScanStop() {
int s = 0;
sharedMemory_lock();
sharedMemory_lockStatus();
s = shm->scanStop;
sharedMemory_unlock();
sharedMemory_unlockStatus();
return s;
}
}
#ifdef EIGERD
void sharedMemory_lockLocalLink() { pthread_mutex_lock(&(shm->lockLocalLink)); }
void sharedMemory_unlockLocalLink() {
pthread_mutex_unlock(&(shm->lockLocalLink));
}
#endif