mirror of
https://github.com/tailix/libshmemq.git
synced 2024-11-27 11:14:51 -05:00
Add semaphores
This commit is contained in:
parent
f832a65fad
commit
59299ffcba
3 changed files with 18 additions and 0 deletions
|
@ -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])
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
12
src/main.c
12
src/main.c
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue