l3.libnazgul/src/queueInit.c

72 lines
1.7 KiB
C
Raw Normal View History

2004-02-23 21:30:30 +00:00
#include "libnazgul.h"
#include "ids.h"
2020-03-03 23:06:43 +00:00
msgQueue *msgQueueInit(msgSpaceId externId, int queueIdx)
{
2004-02-23 21:30:30 +00:00
int queueFd;
2020-03-03 23:06:43 +00:00
msgQueue *queue;
sem_t *semProtectFd;
2004-02-24 09:22:26 +00:00
msgQueueSemId queueSemProtectId;
2020-03-03 23:06:43 +00:00
sem_t *semReadableFd;
2004-02-24 09:22:26 +00:00
msgQueueSemId queueSemReadableId;
2004-02-23 21:30:30 +00:00
msgQueueId queueId;
queue = NULL;
2004-02-24 09:22:26 +00:00
2020-03-03 23:06:43 +00:00
msgQueueProtSemIdIntern(queueSemProtectId, externId, queueIdx);
msgQueueReadSemIdIntern(queueSemReadableId, externId, queueIdx);
2004-02-25 10:32:41 +00:00
2004-02-24 09:22:26 +00:00
// creation du semaphore de lecture
2020-03-03 23:06:43 +00:00
semReadableFd = sem_open(queueSemReadableId,
O_CREAT | O_EXCL, SEM_DEFAULT_MODE, 0);
if (semReadableFd == SEM_FAILED) {
NZG_ERROR("sem_open", queueSemReadableId);
goto ERROR;
2004-02-24 09:22:26 +00:00
}
// creation du semaphore de protection
2020-03-03 23:06:43 +00:00
semProtectFd = sem_open(queueSemProtectId,
O_CREAT | O_EXCL, SEM_DEFAULT_MODE, 0);
if (semProtectFd == SEM_FAILED) {
NZG_ERROR("sem_open", queueSemProtectId);
goto ERROR;
2004-02-24 09:22:26 +00:00
}
2004-02-25 11:12:03 +00:00
/* if(sem_wait(semProtectFd)==-1){
2004-02-24 09:22:26 +00:00
NZG_ERROR("sem_wait",queueSemProtectId);
goto ERROR;
2004-02-25 11:12:03 +00:00
} */
2004-02-24 09:22:26 +00:00
2020-03-03 23:06:43 +00:00
if (msgQueueIdIntern(queueId, externId, queueIdx) < 0) {
2004-02-23 21:30:30 +00:00
return NULL;
}
2020-03-03 23:06:43 +00:00
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;
2004-02-23 21:30:30 +00:00
}
2020-03-03 23:06:43 +00:00
if (ftruncate(queueFd, sizeof(msgQueue)) == -1) {
fprintf(stderr, "Queue resizing failed: %s\n", strerror(errno));
return NULL;
2004-02-25 11:12:03 +00:00
}
2004-02-23 21:30:30 +00:00
2020-03-03 23:06:43 +00:00
queue = msgQueueOpen(queueId);
2004-02-23 21:30:30 +00:00
/* on remplit la structure msgQueue */
queue->elemCounter = 0;
2020-03-03 23:06:43 +00:00
strcpy(queue->id, queueId);
strcpy(queue->headId, queue->id);
strcpy(queue->tailId, queue->id);
2004-02-23 21:30:30 +00:00
/* on ferme tout ce qu'il faut */
close(queueFd);
2020-03-03 23:06:43 +00:00
msgQueueProtUnlock(externId, queueIdx);
2004-02-24 09:22:26 +00:00
2004-02-23 21:30:30 +00:00
return queue;
2020-03-03 23:06:43 +00:00
ERROR:
2004-02-23 21:30:30 +00:00
return NULL;
2004-02-24 09:22:26 +00:00
2004-02-23 21:30:30 +00:00
}