l3.libnazgul/src/queueInit.c
2004-02-25 11:12:03 +00:00

72 lines
1.6 KiB
C

#include "libnazgul.h"
#include "ids.h"
msgQueue * msgQueueInit(msgSpaceId externId, int queueIdx) {
int queueFd;
msgQueue * queue;
sem_t * semProtectFd;
msgQueueSemId queueSemProtectId;
sem_t * semReadableFd;
msgQueueSemId queueSemReadableId;
msgQueueId queueId;
queue = NULL;
msgQueueProtSemIdIntern(queueSemProtectId,externId,queueIdx);
msgQueueReadSemIdIntern(queueSemReadableId,externId,queueIdx);
// creation du semaphore de lecture
semReadableFd = sem_open(queueSemReadableId,
O_CREAT|O_EXCL, SEM_DEFAULT_MODE, 0);
if(semReadableFd == SEM_FAILED) {
NZG_ERROR("sem_open", queueSemReadableId);
goto ERROR;
}
// creation du semaphore de protection
semProtectFd = sem_open(queueSemProtectId,
O_CREAT|O_EXCL, SEM_DEFAULT_MODE, 0);
if(semProtectFd == SEM_FAILED) {
NZG_ERROR("sem_open", queueSemProtectId);
goto ERROR;
}
/* if(sem_wait(semProtectFd)==-1){
NZG_ERROR("sem_wait",queueSemProtectId);
goto ERROR;
} */
if(msgQueueIdIntern(queueId, externId, queueIdx) < 0) {
return NULL;
}
queueFd = shm_open(queueId, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, MSGSPACE_DEFAULT_MODE);
if(queueFd == -1) {
NZG_ERROR("shm_open : queueInit",queueId);
return NULL;
}
if(ftruncate(queueFd, sizeof(msgQueue)) == -1) {
fprintf( stderr, "Queue resizing failed: %s\n",
strerror( errno ) );
return NULL;
}
queue=msgQueueOpen(queueId);
/* on remplit la structure msgQueue */
queue->elemCounter = 0;
strcpy(queue->id,queueId);
strcpy(queue->headId,queue->id);
strcpy(queue->tailId,queue->id);
/* on ferme tout ce qu'il faut */
close(queueFd);
msgQueueProtUnlock(externId,queueIdx);
return queue;
ERROR:
return NULL;
}