Im looking for some feedback on if I am doing the following properly. Im working on porting some windows real time code that had heavy use of named mutex's. It took some searching but i came across some stuff saying you can use shared memory as mutexes in linux, using shm open.
I cant include all of the code here, but i put together the key areas that i need some feed back on. My questions are if im setting up the shared memory region, and mutex correctly, and also if my pointers will be set correctly, along with how to utilize it for locking/unlocking.
volatile struct GenQ {
volatile pthread_mutex_t *qMutexId
volatile sem_t qSemId
volatile int nexton
volatile int nextoff
}
typedef struct Node{
void *qid
char shmname[80]
sem_t *semid
pthread_mutex_t *mutexID
struct node *next
struct node *prev
}
void * init (const char *qname)
{
struct GenQ *myq;
char mtxstr[80];
pthread_mutex_t *mutexQueAccess;
int mode = S_IRWXU | S_IRWXG;
int fd = 0;
int status = 0;
mtxstr[0] = "\0";
strcpy(mtxstr,"/");
strcat(mtxstr, qname);
strcat(mtxstr, "_MTX");
fd = shm_open(mtxstr, O_CREATE | O_RDWR | O_TRUNC, mode);
if (fd == -1)
//err handling stuff
status = ftruncate(fd, sizeof(pthread_mutex_t));
if(status==0){
//err handling stuff
mutexQueAccess = (pthread_mutex_t*) mmap(NULL, sizeof(pthread_mutex_t),
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if(mutexQueAccess == MAP_FAILED)
//err handling stuff
pthread_mutexattr_t mutexAttr;
pthread_mutexattr_init(&mutexAttr);
pthread_mutexattr_setpshared(&mutexAttr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(mutexQueAccess, &mutexAttr);
myq->qMutexId = mutexQueAccess;
newNode = (Node*)malloc(sizeof(node));
newNode->mutedID = mutexQueAccess;
//add node to link list
}
void * openQ(*const char *qname)
{
pthread_mutex_t *mutexQueAccess;
int fd = 0;
int status = 0;
char mtxstr[80];
int mode = S_IRWXU | S_IRWXG;
mtxstr[0] = "\0";
strcpy(mtxstr,"/");
strcat(mtxstr, qname);
strcat(mtxstr, "_MTX");
fd = shm_open(mtxstr, O_CREATE | O_RDWR, mode);
//check fd for err
mutexQueAccess = (pthread_mutex_t *)mmap(NULL, sizeof(pthread_mutex_t),
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
//check for err on mmap
newNode = (Node*)malloc(sizeof(node));
newNode->mutedID = mutexQueAccess;
//add node to link list
}
void * enque(const char *qname, char *msg_data)
{
node = //search for node
pthread_mutex_lock(&(node->mutexQueAccess))
}