1
0
Fork 0
mirror of https://github.com/tailix/libshmemq.git synced 2024-11-27 11:14:51 -05:00

Add semaphores

This commit is contained in:
Alex Kotov 2020-12-15 20:01:30 +05:00
parent f832a65fad
commit 59299ffcba
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
3 changed files with 18 additions and 0 deletions

View file

@ -31,6 +31,7 @@ AC_FUNC_MMAP
AC_CHECK_HEADER_STDBOOL
AC_CHECK_HEADERS([fcntl.h])
AC_CHECK_HEADERS([semaphore.h])
AC_CHECK_HEADERS([stddef.h])
AC_CHECK_HEADERS([stdlib.h])
AC_CHECK_HEADERS([string.h])
@ -52,6 +53,7 @@ AC_CHECK_FUNCS([strcpy])
AC_CHECK_FUNCS([strlen])
AC_CHECK_FUNCS([waitpid])
AC_SEARCH_LIBS([sem_init], [pthread])
AC_SEARCH_LIBS([shm_open], [rt])
AC_SEARCH_LIBS([shm_unlink], [rt])

View file

@ -1,6 +1,7 @@
#ifndef SHMEMQ_INCLUDED
#define SHMEMQ_INCLUDED 1
#include <semaphore.h>
#include <stdbool.h>
#include <stddef.h>
@ -44,6 +45,7 @@ typedef enum ShmemqError {
SHMEMQ_ERROR_FAILED_MUNMAP = 104,
SHMEMQ_ERROR_FAILED_CLOSE = 105,
SHMEMQ_ERROR_FAILED_SHM_UNLINK = 106,
SHMEMQ_ERROR_FAILED_SEM_INIT = 107,
} ShmemqError;
struct ShmemqFrameHeader {
@ -60,6 +62,8 @@ struct ShmemqBufferHeader {
size_t frames_count;
size_t read_frame_index;
size_t write_frame_index;
sem_t read_sem;
sem_t write_sem;
};
struct ShmemqBuffer {

View file

@ -125,6 +125,18 @@ void shmemq_init(
return;
}
if (sem_init(&shmemq->buffer->header.read_sem, 1, 0) != 0) {
shm_unlink(shmemq->name);
if (error_ptr) *error_ptr = SHMEMQ_ERROR_FAILED_SEM_INIT;
return;
}
if (sem_init(&shmemq->buffer->header.write_sem, 1, 0) != 0) {
shm_unlink(shmemq->name);
if (error_ptr) *error_ptr = SHMEMQ_ERROR_FAILED_SEM_INIT;
return;
}
// We don't really need this condition, but it is useful in tests
// for platform sanity check and maybe we can use it to prevent race
// condition.